-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbuild.mill
287 lines (250 loc) · 8.99 KB
/
build.mill
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
package build
import mill._, scalalib._, scalajslib._, publish._
import mill.scalalib.api.ZincWorkerUtil
import $packages._
import $file.ci.upload
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0`
import $ivy.`com.github.lolgab::mill-mima::0.0.23`
import de.tobiasroeser.mill.vcs.version.VcsVersion
val scala213 = "2.13.15"
val scala212 = "2.12.20"
val scala3 = "3.3.4"
val scalaJS = "1.17.0"
val communityBuildDottyVersion = sys.props.get("dottyVersion").toList
val scalaVersions = List(scala212, scala213, scala3) ++ communityBuildDottyVersion
trait CaskModule extends CrossScalaModule with PublishModule{
def isScala3 = ZincWorkerUtil.isScala3(crossScalaVersion)
def publishVersion = VcsVersion.vcsState().format()
def pomSettings = PomSettings(
description = artifactName(),
organization = "com.lihaoyi",
url = "https://github.com/com-lihaoyi/cask",
licenses = Seq(License.MIT),
versionControl = VersionControl.github("com-lihaoyi", "cask"),
developers = Seq(
Developer("lihaoyi", "Li Haoyi","https://github.com/lihaoyi")
)
)
}
trait CaskMainModule extends CaskModule {
def ivyDeps = T{
Agg(
ivy"io.undertow:undertow-core:2.3.18.Final",
ivy"com.lihaoyi::upickle:4.0.2"
) ++
Agg.when(!isScala3)(ivy"org.scala-lang:scala-reflect:$crossScalaVersion")
}
def compileIvyDeps = Agg.when(!isScala3)(ivy"com.lihaoyi:::acyclic:0.3.15")
def scalacOptions = Agg.when(!isScala3)("-P:acyclic:force").toSeq
def scalacPluginIvyDeps = Agg.when(!isScala3)(ivy"com.lihaoyi:::acyclic:0.3.15")
object test extends ScalaTests with TestModule.Utest{
def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.8.4",
ivy"com.lihaoyi::requests::0.9.0"
)
}
def moduleDeps = Seq(cask.util.jvm(crossScalaVersion))
}
object cask extends Cross[CaskMainModule](scalaVersions) {
object util extends Module {
trait UtilModule extends CaskModule with PlatformScalaModule{
def ivyDeps = Agg(
ivy"com.lihaoyi::sourcecode:0.4.2",
ivy"com.lihaoyi::pprint:0.9.0",
ivy"com.lihaoyi::geny:1.1.1"
)
}
object jvm extends Cross[UtilJvmModule](scalaVersions)
trait UtilJvmModule extends UtilModule {
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"com.lihaoyi::castor::0.3.0",
ivy"org.java-websocket:Java-WebSocket:1.5.3"
)
}
object js extends Cross[UtilJsModule](scala213)
trait UtilJsModule extends UtilModule with ScalaJSModule {
def scalaJSVersion = scalaJS
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"com.lihaoyi::castor::0.3.0",
ivy"org.scala-js::scalajs-dom::2.4.0"
)
}
}
}
trait BenchmarkModule extends CrossScalaModule {
def moduleDeps = Seq(cask(crossScalaVersion))
def ivyDeps = Agg[Dep](
)
}
object benchmark extends Cross[BenchmarkModule](build.scalaVersions) with RunModule {
def waitForServer(url: String, maxAttempts: Int = 120): Boolean = {
(1 to maxAttempts).exists { attempt =>
try {
Thread.sleep(3000)
println("Checking server... Attempt " + attempt)
os.proc("curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", url)
.call(check = false)
.exitCode == 0
} catch {
case _: Throwable =>
Thread.sleep(3000)
false
}
}
}
def runBenchMark(projectRoot: os.Path, example: String, vt: Boolean) = {
def runMillBackground(example: String, vt: Boolean) = {
println(s"Running $example with vt: $vt")
println("projectRoot: " + projectRoot)
os.proc(
"mill",
s"example.$example.app[$scala213].run")
.spawn(
cwd = projectRoot,
env = Map("CASK_VIRTUAL_THREAD" -> vt.toString),
stdout = os.Inherit,
stderr = os.Inherit)
}
val duration = "30s"
val threads = "4"
val connections = "100"
val url = "http://localhost:8080/"
val serverApp = runMillBackground(example, vt)
println(s"Waiting for server to start..., vt:$vt")
if (!waitForServer(url)) {
serverApp.destroy()
println("Failed to start server")
sys.exit(1)
}
val results = os.proc("wrk",
"-t", threads,
"-c", connections,
"-d", duration,
url
).call(stderr = os.Pipe)
serverApp.destroyForcibly()
Thread.sleep(1000)
println(s"""\n$example result with ${if (vt) "(virtual threads)" else "(platform threads)"}:""")
println(results.out.text())
}
def runBenchmarks() = T.command {
val projectRoot = T.workspace
if (os.proc("which", "wrk").call(check = false).exitCode != 0) {
println("Error: wrk is not installed. Please install wrk first.")
sys.exit(1)
}
for (example <- Seq(
"staticFilesWithLoom",
"todoDbWithLoom",
"minimalApplicationWithLoom")) {
println(s"target server started, starting run benchmark with wrk for :$example with VT:false")
runBenchMark(projectRoot, example, vt = false)
println(s"target server started, starting run benchmark with wrk for :$example with VT:true")
runBenchMark(projectRoot, example, vt = true)
}
}
}
trait LocalModule extends CrossScalaModule{
override def millSourcePath = super.millSourcePath / "app"
def moduleDeps = Seq(cask(crossScalaVersion))
}
def zippedExamples = T {
val vcsState = VcsVersion.vcsState()
val releaseTag = vcsState.lastTag.getOrElse("")
val label = vcsState.format()
val examples = Seq(
build.example.compress.millSourcePath,
build.example.compress2.millSourcePath,
build.example.compress3.millSourcePath,
build.example.cookies.millSourcePath,
build.example.decorated.millSourcePath,
build.example.decorated2.millSourcePath,
build.example.decoratedContext.millSourcePath,
build.example.endpoints.millSourcePath,
build.example.formJsonPost.millSourcePath,
build.example.httpMethods.millSourcePath,
build.example.minimalApplication.millSourcePath,
build.example.minimalApplication2.millSourcePath,
build.example.minimalApplicationWithLoom.millSourcePath,
build.example.redirectAbort.millSourcePath,
build.example.scalatags.millSourcePath,
build.example.staticFiles.millSourcePath,
build.example.staticFilesWithLoom.millSourcePath,
build.example.staticFiles2.millSourcePath,
build.example.todo.millSourcePath,
build.example.todoApi.millSourcePath,
build.example.todoDb.millSourcePath,
build.example.todoDbWithLoom.millSourcePath,
build.example.twirl.millSourcePath,
build.example.variableRoutes.millSourcePath,
build.example.queryParams.millSourcePath,
build.example.websockets.millSourcePath,
build.example.websockets2.millSourcePath,
build.example.websockets3.millSourcePath,
build.example.websockets4.millSourcePath,
build.example.multipartFormSubmission.millSourcePath,
)
for (example <- examples) yield {
val f = T.ctx().dest
val last = example.last + "-" + label
os.copy(example, f / last)
os.copy(T.workspace / ".mill-version", f / last / ".mill-version")
os.write.over(f / last / "mill", os.read(T.workspace / "mill"))
os.proc("chmod", "+x", f / last / "mill").call(f / last)
os.move(f / last / "package.mill", f / last / "build.mill")
os.write.over(
f / last / "build.mill",
os.read(f / last / "build.mill")
.replaceAll("package build.*", "package build")
.replaceAll("def moduleDeps =.*", "")
.replaceAll("app =>", "")
.replaceFirst(
"object app extends.*\ntrait AppModule extends CrossScalaModule(.*)\\{",
s"object app extends ScalaModule $$1\\{\n def scalaVersion = \"${scala213}\"")
.replaceAll("build.scala3", s"\"${scala3}\"")
.replaceFirst(
"def ivyDeps = Agg\\[Dep\\]\\(",
"def ivyDeps = Agg(\n ivy\"com.lihaoyi::cask:" + releaseTag + "\","
)
)
os.zip(f / s"$last.zip", Seq(f / last))
PathRef(f / s"$last.zip")
}
}
def testExamples() = T.command{
for(example <- zippedExamples()){
println("Testing " + example.path.last)
val base = T.dest / example.path.baseName
os.unzip(example.path, base)
os.perms.set(base / "mill", "rwxrwxrwx")
os.write.over(
base / "build.mill",
os.read(base / "build.mill").replaceAll(
"ivy\"com.lihaoyi::cask:.*\"",
s"""ivy"com.lihaoyi::cask:${VcsVersion.vcsState().format()}""""
)
)
os.proc("./mill", "app.test").call(cwd = base, stdout = os.Inherit)
}
}
def uploadToGithub() = T.command{
val vcsState = VcsVersion.vcsState()
val authKey = T.env.apply("AMMONITE_BOT_AUTH_TOKEN")
val releaseTag = vcsState.lastTag.getOrElse("")
val label = vcsState.format()
if (releaseTag == label) {
requests.post(
"https://api.github.com/repos/com-lihaoyi/cask/releases",
data = ujson.write(
ujson.Obj(
"tag_name" -> releaseTag,
"name" -> releaseTag
)
),
headers = Seq("Authorization" -> s"token $authKey")
)
}
for(example <- zippedExamples()){
upload.apply(example.path, releaseTag, example.path.last, authKey)
}
}