-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.sc
404 lines (359 loc) · 12.5 KB
/
build.sc
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
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.3.0`
import $ivy.`io.github.alexarchambault.mill::mill-native-image::0.1.23`
import $ivy.`io.github.alexarchambault.mill::mill-native-image-upload:0.1.19`
import $ivy.`io.get-coursier::coursier-launcher:2.1.0-M2`
import de.tobiasroeser.mill.vcs.version._
import io.github.alexarchambault.millnativeimage.NativeImage
import io.github.alexarchambault.millnativeimage.upload.Upload
import mill._
import mill.scalalib._
import coursier.core.Version
import java.io.File
import scala.concurrent.duration._
import scala.util.Properties.isWin
def scalaJsCliVersion = "1.1.1-sc5"
def scala213 = "2.13.10"
def latestScalaJsVersion = "1.13.1"
def scalaJsVersions = Seq("1.9.0", "1.10.0", "1.10.1", "1.11.0", "1.12.0", latestScalaJsVersion)
object cli extends Cross[Cli](scalaJsVersions: _*)
class Cli(val scalaJsVersion0: String) extends ScalaModule with ScalaJsCliPublishModule {
def scalaVersion = scala213
def artifactName = "scalajs" + super.artifactName()
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scala-js::scalajs-linker:$scalaJsVersion0",
ivy"com.github.scopt::scopt:4.1.0"
)
def millSourcePath = super.millSourcePath / os.up
def mainClass = Some("org.scalajs.cli.Scalajsld")
def sources = T.sources {
val extra =
if (Version(scalaJsVersion0) == Version("1.9.0")) millSourcePath / "scala-js-1.9"
else millSourcePath / "scala-js-1.10+"
super.sources() ++ Seq(PathRef(extra))
}
def transitiveJars: T[Agg[PathRef]] = {
def allModuleDeps(todo: List[JavaModule]): List[JavaModule] = {
todo match {
case Nil => Nil
case h :: t =>
h :: allModuleDeps(h.moduleDeps.toList ::: t)
}
}
T {
mill.define.Target.traverse(allModuleDeps(this :: Nil).distinct)(m => T.task(m.jar()))()
}
}
def jarClassPath = T {
val cp = runClasspath() ++ transitiveJars()
cp.filter(ref => os.exists(ref.path) && !os.isDir(ref.path))
}
def standaloneLauncher = T {
val cachePath = os.Path(coursier.cache.FileCache().location, os.pwd)
def urlOf(path: os.Path): Option[String] =
if (path.startsWith(cachePath)) {
val segments = path.relativeTo(cachePath).segments
val url = segments.head + "://" + segments.tail.mkString("/")
Some(url)
}
else None
import coursier.launcher.{
AssemblyGenerator,
BootstrapGenerator,
ClassPathEntry,
Parameters,
Preamble
}
val cp = jarClassPath().map(_.path)
val mainClass0 = mainClass().getOrElse(sys.error("No main class"))
val dest = T.ctx().dest / (if (isWin) "launcher.bat" else "launcher")
val preamble = Preamble()
.withOsKind(isWin)
.callsItself(isWin)
val entries = cp.map { path =>
urlOf(path) match {
case None =>
val content = os.read.bytes(path)
val name = path.last
ClassPathEntry.Resource(name, os.mtime(path), content)
case Some(url) => ClassPathEntry.Url(url)
}
}
val loaderContent = coursier.launcher.ClassLoaderContent(entries)
val params = Parameters.Bootstrap(Seq(loaderContent), mainClass0)
.withDeterministic(true)
.withPreamble(preamble)
BootstrapGenerator.generate(params, dest.toNIO)
PathRef(dest)
}
}
class ScalaJsCliNativeImage(val scalaJsVersion0: String) extends ScalaModule with NativeImage {
def scalaVersion = scala213
def scalaJsVersion = scalaJsVersion0
def nativeImageClassPath = T{
runClasspath()
}
def nativeImageOptions = T{
super.nativeImageOptions() ++ Seq(
"--no-fallback",
"-H:IncludeResources=org/scalajs/linker/backend/emitter/.*.sjsir",
"-H:IncludeResources=com/google/javascript/jscomp/js/polyfills.txt",
"-H:IncludeResourceBundles=com.google.javascript.jscomp.parsing.ParserConfig",
)
}
def nativeImagePersist = System.getenv("CI") != null
def graalVmVersion = "22.3.1"
def nativeImageGraalVmJvmId = s"graalvm-java17:$graalVmVersion"
def nativeImageName = "scala-js-ld"
def moduleDeps() = Seq(
cli(scalaJsVersion0)
)
def compileIvyDeps = super.compileIvyDeps() ++ Seq(
ivy"org.graalvm.nativeimage:svm:$graalVmVersion"
)
def nativeImageMainClass = "org.scalajs.cli.Scalajsld"
def nameSuffix = ""
def copyToArtifacts(directory: String = "artifacts/") = T.command {
val _ = Upload.copyLauncher(
nativeImage().path,
directory,
s"scala-js-ld-$scalaJsVersion",
compress = true,
suffix = nameSuffix
)
}
}
object native extends Cross[ScalaJsCliNativeImage](scalaJsVersions: _*)
def native0 = native
def csDockerVersion = "2.1.0-M5-18-gfebf9838c"
class ScalaJsCliStaticNativeImage(scalaJsVersion0: String) extends ScalaJsCliNativeImage(scalaJsVersion0) {
def nameSuffix = "-static"
def buildHelperImage = T {
os.proc("docker", "build", "-t", "scala-cli-base-musl:latest", ".")
.call(cwd = os.pwd / "musl-image", stdout = os.Inherit)
()
}
def nativeImageDockerParams = T{
buildHelperImage()
Some(
NativeImage.linuxStaticParams(
"scala-cli-base-musl:latest",
s"https://github.com/coursier/coursier/releases/download/v$csDockerVersion/cs-x86_64-pc-linux.gz"
)
)
}
def writeNativeImageScript(scriptDest: String, imageDest: String = "") = T.command {
buildHelperImage()
super.writeNativeImageScript(scriptDest, imageDest)()
}
}
object `native-static` extends Cross[ScalaJsCliStaticNativeImage](scalaJsVersions: _*)
class ScalaJsCliMostlyStaticNativeImage(scalaJsVersion0: String) extends ScalaJsCliNativeImage(scalaJsVersion0) {
def nameSuffix = "-mostly-static"
def nativeImageDockerParams = Some(
NativeImage.linuxMostlyStaticParams(
"ubuntu:18.04", // TODO Pin that?
s"https://github.com/coursier/coursier/releases/download/v$csDockerVersion/cs-x86_64-pc-linux.gz"
)
)
}
object `native-mostly-static` extends Cross[ScalaJsCliMostlyStaticNativeImage](scalaJsVersions: _*)
object tests extends Cross[Tests](scalaJsVersions: _*)
class Tests(val scalaJsVersion0: String) extends ScalaModule {
def scalaVersion = scala213
object test extends Tests {
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scalameta::munit:0.7.29",
ivy"com.lihaoyi::os-lib:0.9.0",
ivy"com.lihaoyi::pprint:0.8.1"
)
def testFramework = "munit.Framework"
private final class TestHelper(
launcherTask: T[PathRef]
) {
def test(args: String*) = {
val argsTask = T.task {
val launcher = launcherTask().path
val extraArgs = Seq(
s"-Dtest.scala-js-cli.path=$launcher",
s"-Dtest.scala-js-cli.scala-js-version=$scalaJsVersion0"
)
args ++ extraArgs
}
T.command {
testTask(argsTask, T.task(Seq.empty[String]))()
}
}
}
def test(args: String*) =
jvm(args: _*)
def jvm(args: String*) =
new TestHelper(cli(scalaJsVersion0).standaloneLauncher).test(args: _*)
def native(args: String*) =
new TestHelper(native0(scalaJsVersion0).nativeImage).test(args: _*)
def nativeStatic(args: String*) =
new TestHelper(`native-static`(scalaJsVersion0).nativeImage).test(args: _*)
def nativeMostlyStatic(args: String*) =
new TestHelper(`native-mostly-static`(scalaJsVersion0).nativeImage).test(args: _*)
private def updateRef(ref: PathRef): PathRef = {
val rawPath = ref.path.toString.replace(
File.separator + scalaJsVersion0 + File.separator,
File.separator
)
PathRef(os.Path(rawPath))
}
def sources = T.sources {
super.sources().flatMap { ref =>
Seq(updateRef(ref), ref)
}
}
def resources = T.sources {
super.resources().flatMap { ref =>
Seq(updateRef(ref), ref)
}
}
}
}
def ghOrg = "scala-cli"
def ghName = "scala-js-cli"
trait ScalaJsCliPublishModule extends PublishModule {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = "io.github.alexarchambault.tmp",
url = s"https://github.com/$ghOrg/$ghName",
licenses = Seq(License.`BSD-3-Clause`),
versionControl = VersionControl.github(ghOrg, ghName),
developers = Seq(
Developer("alexarchambault", "Alex Archambault", "https://github.com/alexarchambault"),
Developer("sjrd", "Sébastien Doeraene", "https://github.com/sjrd"),
Developer("gzm0", "Tobias Schlatter", "https://github.com/gzm0"),
Developer("nicolasstucki", "Nicolas Stucki", "https://github.com/nicolasstucki"),
)
)
def publishVersion =
finalPublishVersion()
}
private def computePublishVersion(state: VcsState, simple: Boolean): String =
if (state.commitsSinceLastTag > 0)
if (simple) {
val versionOrEmpty = state.lastTag
.filter(_ != "latest")
.filter(_ != "nightly")
.map(_.stripPrefix("v"))
.map(_.takeWhile(c => c == '.' || c.isDigit))
.flatMap { tag =>
if (simple) {
val idx = tag.lastIndexOf(".")
if (idx >= 0)
Some(tag.take(idx + 1) + (tag.drop(idx + 1).toInt + 1).toString + "-SNAPSHOT")
else
None
} else {
val idx = tag.indexOf("-")
if (idx >= 0) Some(tag.take(idx) + "+" + tag.drop(idx + 1) + "-SNAPSHOT")
else None
}
}
.getOrElse("0.0.1-SNAPSHOT")
Some(versionOrEmpty)
.filter(_.nonEmpty)
.getOrElse(state.format())
} else {
val rawVersion = os
.proc("git", "describe", "--tags")
.call()
.out
.text()
.trim
.stripPrefix("v")
.replace("latest", "0.0.0")
.replace("nightly", "0.0.0")
val idx = rawVersion.indexOf("-")
if (idx >= 0) rawVersion.take(idx) + "-" + rawVersion.drop(idx + 1) + "-SNAPSHOT"
else rawVersion
}
else
state.lastTag
.getOrElse(state.format())
.stripPrefix("v")
private def finalPublishVersion = {
val isCI = System.getenv("CI") != null
if (isCI)
T.persistent {
val state = VcsVersion.vcsState()
computePublishVersion(state, simple = false)
}
else
T {
val state = VcsVersion.vcsState()
computePublishVersion(state, simple = true)
}
}
object ci extends Module {
def publishSonatype(tasks: mill.main.Tasks[PublishModule.PublishData]) = T.command {
publishSonatype0(
data = define.Target.sequence(tasks.value)(),
log = T.ctx().log
)
}
private def publishSonatype0(
data: Seq[PublishModule.PublishData],
log: mill.api.Logger
): Unit = {
val credentials = sys.env("SONATYPE_USERNAME") + ":" + sys.env("SONATYPE_PASSWORD")
val pgpPassword = sys.env("PGP_PASSPHRASE")
val timeout = 10.minutes
val artifacts = data.map { case PublishModule.PublishData(a, s) =>
(s.map { case (p, f) => (p.path, f) }, a)
}
val isRelease = {
val versions = artifacts.map(_._2.version).toSet
val set = versions.map(!_.endsWith("-SNAPSHOT"))
assert(
set.size == 1,
s"Found both snapshot and non-snapshot versions: ${versions.toVector.sorted.mkString(", ")}"
)
set.head
}
val publisher = new scalalib.publish.SonatypePublisher(
uri = "https://s01.oss.sonatype.org/service/local",
snapshotUri = "https://s01.oss.sonatype.org/content/repositories/snapshots",
credentials = credentials,
signed = true,
// format: off
gpgArgs = Seq(
"--detach-sign",
"--batch=true",
"--yes",
"--pinentry-mode", "loopback",
"--passphrase", pgpPassword,
"--armor",
"--use-agent"
),
// format: on
readTimeout = timeout.toMillis.toInt,
connectTimeout = timeout.toMillis.toInt,
log = log,
awaitTimeout = timeout.toMillis.toInt,
stagingRelease = isRelease
)
publisher.publishAll(isRelease, artifacts: _*)
}
def upload(directory: String = "artifacts/") = T.command {
val version = finalPublishVersion()
val path = os.Path(directory, os.pwd)
val launchers = os.list(path).filter(os.isFile(_)).map { path =>
path.toNIO -> path.last
}
val ghToken = Option(System.getenv("UPLOAD_GH_TOKEN")).getOrElse {
sys.error("UPLOAD_GH_TOKEN not set")
}
val (tag, overwriteAssets) =
if (version.endsWith("-SNAPSHOT")) ("launchers", true)
else ("v" + version, false)
Upload.upload("scala-cli", "scala-js-cli", ghToken, tag, dryRun = false, overwrite = overwriteAssets)(launchers: _*)
}
}
private def bash =
if (isWin) Seq("bash.exe")
else Nil