-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbtnew
executable file
·292 lines (241 loc) · 7.35 KB
/
sbtnew
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
ScalaVersion=3.0.0
SbtVersion=1.5.2
SbtBloopVersion=1.4.8
SbtRevolverVersion=0.9.1
SbtNativePackagerVersion=1.8.1
SbtNativeImageVersion=0.3.0
directoryName=""
function init() {
if [ -n "$1" ]; then
directoryName=$1
else
read -r -p "Directory/Project Name (MyFirstProject): " directoryName
fi
directoryName=${directoryName:-MyFirstProject}
}
function setup() {
echo ""
echo "-----------------------------------------------"
echo "Directory/Project Name: $directoryName"
echo "-----------------------------------------------"
mkdir -p "${directoryName}/src/main/java"
mkdir -p "${directoryName}/src/main/resources"
mkdir -p "${directoryName}/src/main/scala"
mkdir -p "${directoryName}/src/test/scala"
mkdir "${directoryName}/lib" "${directoryName}/project" "${directoryName}/target"
}
# create an initial build.sbt file
#---------------------------------
function createSbt() {
echo "ThisBuild / scalaVersion := \"${ScalaVersion}\"
ThisBuild / version := \"0.1.0-SNAPSHOT\"
ThisBuild / organization := \"com.example\"
ThisBuild / organizationName := \"example\"
val munitVersion = \"0.7.25\"
lazy val root = project
.in(file(\".\"))
.enablePlugins(GraalVMNativeImagePlugin, NativeImagePlugin)
.settings(
name := \"$directoryName\",
description := \"Example sbt project that compiles using Scala\",
//version := \"0.1.0\",
//scalaVersion := \"${ScalaVersion}\",
libraryDependencies ++= Seq(
// (\"io.d11\" %% \"zhttp\" % \"1.0.0.0-RC15\").cross(CrossVersion.for3Use2_13),
// \"com.novocode\" % \"junit-interface\" % \"0.11\" % Test,
\"org.scalameta\" %% \"munit\" % munitVersion % Test
//,\"org.springframework.boot\" % \"spring-boot-starter\" % springBootVersion
),
Compile / mainClass := Some(\"Main\"),
resolvers += (\"p\" at \"https://maven.aliyun.com/repository/public\").withAllowInsecureProtocol(true),
graalVMNativeImageCommand := graalVMNativeImageCommandStr,
graalVMNativeImageOptions ++= projectNativeImageOptions,
nativeImageOptions ++= projectNativeImageOptions
)
lazy val graalVMNativeImageCommandStr = {
val windows = System.getProperty(\"os.name\").toLowerCase.contains(\"windows\")
val nativeImageCmd = \"native-image\" + (if (windows) \".cmd\" else \"\")
import scala.util.chaining._
s\"\${System.getenv(\"GRAALVM_HOME\")}/bin/\$nativeImageCmd\".tap(cmd => {
println(s\"[info] native image cmd: \${cmd}\")
})
}
lazy val projectNativeImageOptions = Seq(
\"--no-fallback\",
\"--allow-incomplete-classpath\",
\"--initialize-at-build-time=org.eclipse.jgit.ignore.internal.PathMatcher\",
\"-H:ReflectionConfigurationFiles=../../src/main/resources/reflection-config.json\",
\"--report-unsupported-elements-at-runtime\",
\"-H:+ReportExceptionStackTraces\",
\"--verbose\",
//\"--initialize-at-build-time\",
\"-H:-CheckToolchain\",
//\"-H:PrintFlags=Debug\",
//\"-H:+TraceNativeToolUsage\"
)" >"${directoryName}/build.sbt" #---------------------------------
}
#-----------------
# init plugins.sbt
#-----------------
function createPlugins() {
echo "addSbtPlugin(\"ch.epfl.scala\" % \"sbt-bloop\" % \"${SbtBloopVersion}\")
addSbtPlugin(\"io.spray\" % \"sbt-revolver\" % \"${SbtRevolverVersion}\")
addSbtPlugin(\"com.typesafe.sbt\" % \"sbt-native-packager\" % \"${SbtNativePackagerVersion}\")
addSbtPlugin(\"org.scalameta\" % \"sbt-native-image\" % \"${SbtNativeImageVersion}\")" >"${directoryName}/project/plugins.sbt"
}
#-----------------
# init build.properties
#-----------------
function createBuildProperties() {
echo "sbt.version=${SbtVersion}" >"${directoryName}/project/build.properties"
}
#----------------------------------
# init src
#----------------------------------
function createSources() {
echo "object utils:
import java.text.SimpleDateFormat
import java.util.Date
extension (d: Date)
def format(pattern: String = \"yyyyMMdd\"): String = new SimpleDateFormat(pattern).format(d)
def time[T](block: => T): T =
val start = System.currentTimeMillis()
val ret = block
val end = System.currentTimeMillis()
println(s\">>Cost time: \${end - start}ms\")
ret
extension[T] (v: T)
def orElse(pred: Boolean)(elseAction: (T) => T): T = if (pred) elseAction(v) else v
def orElse(pred: (T) => Boolean)(elseAction: (T) => T): T = orElse(pred(v))(elseAction)
" >"${directoryName}/src/main/scala/util.scala"
echo "import utils._
import java.util.Date
import scala.io.Source
import scala.jdk.CollectionConverters._
import scala.util.Using
import scala.util.chaining._
object Main:
def main(args: Array[String]): Unit = time {
println(s\"args: \${args.mkString(\" \")}\")
System.getenv.asScala.filter(it => it._1.startsWith(\"ENV\")).foreach(println)
Using(Source.fromFile(\"build.sbt\")) { it => it.getLines().filter(_.contains(\"version\")).foreach(println) }
\"Hello, World\"
.tap(println)
.pipe(_ + \", \" + new Date().format(\"yyyy-MM-dd\"))
.pipe(println)
}
" >"${directoryName}/src/main/scala/main.scala"
}
function createTestSources() {
echo "class Test2 extends munit.FunSuite {
test(\"test1\") {
val a = 2
val b = 2
assertEquals(a, b)
}
}
" >"${directoryName}/src/test/scala/test1.scala"
}
#------------------------------
# create .gitignore, if desired
#------------------------------
function createGitignore() {
echo "bin/
project/project/
target/
lib/
.cache
.classpath
.project
.settings
.svn
.idea/
*.iml
.metals
" >"${directoryName}/.gitignore"
}
function createRakefile() {
echo 'task default: :usage
task :usage do
sh "rake -T"
end
desc "run"
task :run do
system("sbt run")
end
desc "dev"
task :dev do
system("sbt \"~reStart\"")
end
desc "native-build"
task :native_build do
sh "sbt \"show graalvm-native-image:packageBin\""
end
desc "nativeImage"
task :nativeimage do
sh "sbt nativeImage"
end' >"${directoryName}/Rakefile"
}
function createReadme() {
#-----------------------------
# create README.md, if desired
#-----------------------------
echo "# ${directoryName}" >"${directoryName}/README.md"
}
function createReflectionConfigfile() {
echo '[
{"name": "org.apache.log4j.Category"},
{"name": "org.apache.log4j.Logger"},
{"name": "org.apache.log4j.helpers.Loader"},
{
"name":"java.lang.Thread",
"allPublicMethods":true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "giter8.G8$",
"allDeclaredMethods" : true,
"allPublicMethods" : true
},
{
"name": "giter8.G8$$anon$2",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredClasses" : true,
"allPublicClasses" : true
},
{
"name": "java.util.Properties",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredClasses" : true,
"allPublicClasses" : true
}
]' >"$directoryName/src/main/resources/reflection-config.json"
}
function showProjectInfo() {
echo ""
echo "Project created."
echo "cd $directoryName"
}
function main() {
echo "This script creates an SBT project directory beneath the current directory."
init "$1"
setup
createSbt
createBuildProperties
createPlugins
createSources
createReflectionConfigfile
createTestSources
createRakefile
createReadme
showProjectInfo
}
main "$@"