Repository navigation
Expand file tree
/
Copy pathbuild.mill
More file actions
432 lines (402 loc) · 14.3 KB
/
Copy pathbuild.mill
File metadata and controls
432 lines (402 loc) · 14.3 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//| mvnDeps:
//| - com.goyeau::mill-scalafix::0.6.2
//| - com.github.lolgab::mill-crossplatform::0.3.0
//| - com.lihaoyi::mill-contrib-buildinfo:$MILL_VERSION
//| - com.github.lolgab::mill-mima::0.2.2
//| - io.eleven19.mill-github-dependency-graph::mill-github-dependency-graph::0.3.1
package build
import mill._, mill.scalalib._, mill.scalanativelib._, mill.scalanativelib.api._
import mill.scalalib.publish._
import mill.util.VcsVersion
import com.goyeau.mill.scalafix.ScalafixModule
import mill.contrib.buildinfo.BuildInfo
import com.github.lolgab.mill.mima._
import com.github.lolgab.mill.crossplatform._
import java.util.concurrent.atomic.AtomicBoolean
object Versions {
val scalaNative = "0.5.12"
val upickle = "4.4.3"
val undertow = "2.4.1.Final"
val scala212 = "2.12.19"
val scala3 = "3.9.0"
val tapir = "1.14.0"
val endpoints4s = "1.14.0"
val cask = "0.11.3"
val catsEffect = "3.7.1"
val otel4s = "0.19.4"
val http4s023 = "0.23.38"
val http4s1 = "1.0.0-M47"
val utest = "0.9.5"
val osLib = "0.11.8"
val sttp = "3.11.0"
val sttpShared = "1.6.0"
val fs2 = "3.14.0"
val pprint = "0.9.6"
val castor = "0.3.0"
val scalaJavaTime = "2.8.0"
}
def freeUnitLinkingOptions = Task.Input {
unitd.freeUnitDir.toSeq.flatMap(d => Seq(s"-L$d", (d / "libunit.a").toString))
}
object unitd {
private val dest = os.home / ".cache" / "snunit"
val control = dest / "control.sock"
private val pid = dest / "unit.pid"
/** Directory with the `unitd` and `libunit.a` of the prebuilt FreeUnit that the snunit CLI also downloads (the version
* pinned in `.github/freeunit-ref`, cached in `~/.cache/snunit`), unless `unitd` is already on the PATH.
* `SNUNIT_FREEUNIT_DIR` overrides both.
*/
lazy val freeUnitDir: Option[os.Path] =
sys.env.get("SNUNIT_FREEUNIT_DIR").map(os.Path(_, mill.api.BuildCtx.workspaceRoot)).orElse {
if (os.proc("which", "unitd").call(check = false).exitCode == 0) None
else {
val ref = os.read(mill.api.BuildCtx.workspaceRoot / ".github" / "freeunit-ref").trim
val osName = if (System.getProperty("os.name").toLowerCase.contains("mac")) "macos" else "linux"
val arch = os.proc("uname", "-m").call().out.trim() match {
case "arm64" | "aarch64" => "aarch64"
case _ => "x86_64"
}
val name = s"freeunit-$ref-$osName-$arch"
val dir = dest / name
if (!os.exists(dir / "unitd")) {
val url = s"https://github.com/lolgab/snunit/releases/download/freeunit-$ref/$name.tar.gz"
System.err.println(s"Downloading $url")
os.makeDir.all(dest)
val tarball = dest / s"$name.tar.gz.tmp"
try {
os.proc("curl", "-fL", "--progress-bar", "-o", tarball, url).call(stdout = os.Inherit, stderr = os.Inherit)
os.proc("tar", "xzf", tarball, "-C", dest).call()
} finally os.remove(tarball, checkExists = false)
}
Some(dir)
}
}
def executable: String = freeUnitDir.fold("unitd")(d => (d / "unitd").toString)
private var optProc: Option[os.SubProcess] = None
private def closeUnitd(): Unit = {
optProc.foreach { proc =>
proc.close()
Thread.sleep(100)
optProc = None
}
if (os.exists(pid)) {
os.proc("kill", os.read(pid).trim).call(check = false)
}
}
def runBackground(config: ujson.Obj): Unit = {
closeUnitd()
val statedir = dest / "statedir"
os.makeDir.all(statedir)
os.write.over(statedir / "conf.json", config)
os.remove(control)
val started = new AtomicBoolean(false)
optProc = Some(
os.proc(
executable,
"--no-daemon",
"--log",
dest / "log.txt",
"--statedir",
statedir,
"--control",
s"unix:$control",
"--pid",
pid
).spawn(
stdout = os.Inherit,
stderr = os.ProcessOutput.Readlines(line => {
line match {
case s"$_ unit $_ started" =>
Thread.sleep(100)
started.set(true)
case _ =>
}
System.err.println(line)
})
)
)
while (!started.get()) {
println("Waiting for unit to start...")
Thread.sleep(100)
}
}
}
val scalaVersions = Seq(Versions.scala3)
val http4sVersions = Seq(Versions.http4s023, Versions.http4s1)
val http4sAndScalaVersions = for {
scalaV <- scalaVersions
http4sV <- http4sVersions
} yield (scalaV, http4sV)
val osLib = mvn"com.lihaoyi::os-lib:${Versions.osLib}"
val upickle = mvn"com.lihaoyi::upickle::${Versions.upickle}"
val undertow = mvn"io.undertow:undertow-core:${Versions.undertow}"
val utest = mvn"com.lihaoyi::utest::${Versions.utest}"
val testServerPort = 8081
object Common {
trait Shared extends ScalaModule with ScalafixModule {
def organization = "com.github.lolgab"
def scalacOptions = super.scalacOptions() ++
Seq("-deprecation")
}
trait Jvm extends Shared {
def scalaVersion = Versions.scala3
}
trait Native extends Jvm with ScalaNativeModule {
def scalaNativeVersion = Versions.scalaNative
// Link the libunit matching the unitd used by the tests, so that their versions agree.
def nativeLinkingOptions = Task {
super.nativeLinkingOptions() ++ freeUnitLinkingOptions()
}
def baseTestConfig(binary: os.Path) = {
val appName = "app"
ujson.Obj(
"applications" -> ujson.Obj(
appName -> ujson.Obj(
"type" -> "external",
"executable" -> binary.toString
)
),
"listeners" -> ujson.Obj(
s"*:$testServerPort" -> ujson.Obj(
"pass" -> s"applications/$appName"
)
)
)
}
def deployTestApp() = Task.Command {
val binary = nativeLink()
unitd.runBackground(baseTestConfig(binary.path))
}
}
}
trait Publish extends PublishModule with Mima {
def publishVersion = Task { VcsVersion.vcsState().format() }
def pomSettings =
PomSettings(
description = "Scala Native server using FreeUnit",
organization = "com.github.lolgab",
url = "https://github.com/lolgab/snunit",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github(owner = "lolgab", repo = "snunit"),
developers = Seq(
Developer("lolgab", "Lorenzo Gabriele", "https://github.com/lolgab")
)
)
def mimaPreviousVersions = Seq("0.9.0")
// Remove after first release with Scala Native 0.5
def mimaPreviousArtifacts = Task { Seq.empty }
}
object snunit extends Common.Native with Publish {
object test extends ScalaNativeTests with TestModule.Utest {
def mvnDeps = super.mvnDeps() ++ Seq(utest)
}
}
object `snunit-async-cats-effect` extends Common.Native with Publish {
def moduleDeps = Seq(snunit)
def mvnDeps =
Task {
super.mvnDeps() ++ Seq(
mvn"org.typelevel::cats-effect::${Versions.catsEffect}",
// Only linked into the applications that use it (snunit.Http4sOtel4s, snunit.TapirOtel4s)
mvn"org.typelevel::otel4s-sdk::${Versions.otel4s}",
mvn"org.typelevel::otel4s-sdk-exporter::${Versions.otel4s}"
)
}
}
object `snunit-undertow` extends Common.Native with Publish {
def moduleDeps = Seq(snunit)
def mvnDeps = super.mvnDeps() ++ Seq(undertow)
// Keep default jar behavior for Mill compatibility.
// Sometimes it gives problems compiling since our internal API is different
// than the java API in the class files.
def zincIncrementalCompilation = false
}
object `snunit-tapir` extends Common.Native with Publish {
def moduleDeps = Seq(snunit)
def mvnDeps = super.mvnDeps() ++ Seq(mvn"com.softwaremill.sttp.tapir::tapir-server::${Versions.tapir}")
}
object `snunit-tapir-cats-effect` extends Common.Native with Publish {
def moduleDeps = Seq(
`snunit-tapir`,
`snunit-async-cats-effect`
)
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"com.softwaremill.sttp.tapir::tapir-cats-effect::${Versions.tapir}",
mvn"com.softwaremill.sttp.shared::fs2::${Versions.sttpShared}",
mvn"co.fs2::fs2-core::${Versions.fs2}"
)
}
object `snunit-endpoints4s` extends Common.Native with Publish {
def moduleDeps = Seq(snunit)
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.endpoints4s::algebra::${Versions.endpoints4s}",
mvn"com.lihaoyi::ujson::${Versions.upickle}"
)
}
object `snunit-http4s` extends Cross[SNUnitHttp4s](http4sVersions)
trait SNUnitHttp4s extends Common.Native with Cross.Module[String] with Publish {
val http4sVersion = crossValue
def moduleDeps = Seq(
snunit,
`snunit-async-cats-effect`
)
val http4sBinaryVersion = http4sVersion match {
case s"0.23.$_" => "0.23"
case s"1.$_" => "1"
}
def artifactName = s"snunit-http4s$http4sBinaryVersion"
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-server::$http4sVersion"
)
def http4sSourcesDir = Task.Source(moduleDir / s"http4s-$http4sBinaryVersion" / "src")
override def generatedSources = Task {
super.generatedSources() ++ Seq(PathRef(http4sSourcesDir().path))
}
}
def caskPatch = Task.Source(moduleDir / "cask.patch")
def caskSources = Task {
val dest = Task.dest
os.proc("git", "clone", "--branch", Versions.cask, "--depth", "1", "https://github.com/com-lihaoyi/cask", dest).call()
os.proc("git", "apply", caskPatch().path).call(cwd = dest / "cask")
PathRef(dest)
}
def castorSources = Task {
val dest = Task.dest
os.proc("git", "clone", "--branch", Versions.castor, "--depth", "1", "https://github.com/com-lihaoyi/castor", dest)
.call()
PathRef(dest)
}
object `snunit-cask` extends Common.Native with Publish {
override def generatedSources = Task {
val cask = caskSources().path / "cask"
val castor = castorSources().path / "castor"
Seq(cask / "src", cask / "util" / "src", cask / "src-3", castor / "src", castor / "src-js-native").map(PathRef(_))
}
def moduleDeps = Seq(`snunit-undertow`)
// Cask sources aren't compatible with the latest Scala 3 defaults
def scalacOptions = super.scalacOptions() ++ Seq("-source:3.3")
def mvnDeps = super.mvnDeps() ++ Seq(
upickle,
mvn"io.github.cquiroz::scala-java-time::${Versions.scalaJavaTime}",
mvn"com.lihaoyi::pprint::${Versions.pprint}"
)
}
object integration extends ScalaModule {
object tests extends Module {
object `hello-world` extends Common.Native {
def moduleDeps = Seq(snunit)
}
object `websocket-echo` extends Common.Native {
def moduleDeps = Seq(snunit)
}
object `multiple-handlers` extends Common.Native {
def moduleDeps = Seq(snunit)
}
object `gc-background-thread` extends Common.Native {
def moduleDeps = Seq(snunit)
}
object `undertow-helloworld` extends CrossPlatform {
object native extends CrossPlatformScalaModule with Common.Native {
def moduleDeps = Seq(`snunit-undertow`)
}
object jvm extends CrossPlatformScalaModule with Common.Jvm {
def mvnDeps = super.mvnDeps() ++ Seq(undertow)
}
}
object `cask-helloworld` extends CrossPlatform {
object jvm extends CrossPlatformScalaModule with Common.Jvm {
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"com.lihaoyi::cask:${Versions.cask}"
)
}
object native extends CrossPlatformScalaModule with Common.Native {
def moduleDeps = Seq(`snunit-cask`)
}
}
object `tapir-helloworld` extends Common.Native {
override def moduleDeps = Seq(`snunit-tapir`)
}
object `endpoints4s-helloworld` extends Common.Native {
override def moduleDeps = Seq(`snunit-endpoints4s`)
}
object `tapir-websocket-sync` extends Common.Native {
override def moduleDeps = Seq(`snunit-tapir`)
}
object `tapir-websocket` extends Common.Native {
override def moduleDeps = Seq(`snunit-tapir-cats-effect`)
}
object `tapir-app` extends Common.Native {
override def moduleDeps = Seq(`snunit-tapir-cats-effect`)
}
object `http4s-helloworld` extends Cross[Http4sHelloWorldModule](http4sVersions)
trait Http4sHelloWorldModule extends Common.Native with Cross.Module[String] {
def http4sVersion = crossValue
def moduleDeps = Seq(
`snunit-http4s`(http4sVersion)
)
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-dsl::$http4sVersion"
)
}
object `http4s-websocket` extends Cross[Http4sWebsocketModule](http4sVersions)
trait Http4sWebsocketModule extends Common.Native with Cross.Module[String] {
def http4sVersion = crossValue
def moduleDeps = Seq(
`snunit-http4s`(http4sVersion)
)
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-dsl::$http4sVersion"
)
}
object `http4s-concurrency-limit` extends Common.Native {
def moduleDeps = Seq(`snunit-http4s`(Versions.http4s023))
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-dsl::${Versions.http4s023}"
)
}
object `http4s-app` extends Common.Native {
val http4sVersion = Versions.http4s023
def moduleDeps = Seq(
`snunit-http4s`(http4sVersion)
)
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-dsl::$http4sVersion"
)
}
object `tapir-otel4s` extends Common.Native {
def moduleDeps = Seq(`snunit-tapir-cats-effect`)
}
object `http4s-otel4s` extends Common.Native {
def moduleDeps = Seq(`snunit-http4s`(Versions.http4s023))
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.http4s::http4s-dsl::${Versions.http4s023}"
)
}
object `tapir-helloworld-cats-effect` extends Common.Native {
def moduleDeps = Seq(
`snunit-tapir-cats-effect`
)
}
}
def scalaVersion = Versions.scala3
object test extends ScalaTests with TestModule.Utest with BuildInfo {
// All tests share the same unitd instance and port
def testParallelism = false
override def resources = Task {
super[ScalaTests].resources() ++ super[BuildInfo].resources()
}
def buildInfoMembers = Seq(
BuildInfo.Value("port", testServerPort.toString),
BuildInfo.Value("scalaVersions", scalaVersions.mkString(":")),
BuildInfo.Value("http4sVersions", http4sVersions.mkString(":")),
BuildInfo.Value("unitControl", unitd.control.toString)
)
def buildInfoPackageName = "snunit.test"
def mvnDeps =
Seq(
utest,
osLib,
mvn"com.softwaremill.sttp.client3::core:${Versions.sttp}"
)
}
}