forked from scala-cli/scala-js-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.sc
366 lines (327 loc) · 11.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
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.1`
import $ivy.`io.github.alexarchambault.mill::mill-native-image::0.1.29`
import $ivy.`io.github.alexarchambault.mill::mill-native-image-upload:0.1.26`
import $ivy.`io.get-coursier::coursier-launcher:2.1.9`
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.15"
def scalaJsVersion = "1.17.0"
object cli extends Cli
trait Cli extends ScalaModule with ScalaJsCliPublishModule {
def scalaVersion = scala213
def artifactName = "scalajs" + super.artifactName()
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scala-js::scalajs-linker:$scalaJsVersion",
ivy"com.github.scopt::scopt:4.1.0",
ivy"com.lihaoyi::os-lib:0.11.3",
ivy"com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-core:2.33.0", // This is the java8 version of jsoniter, according to scala-cli build
ivy"com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-macros:2.33.0", // This is the java8 version of jsoniter, according to scala-cli build
ivy"com.armanbilge::scalajs-importmap:0.1.1"
)
def mainClass = Some("org.scalajs.cli.Scalajsld")
def transitiveJars: T[Seq[PathRef]] = T {
T.traverse(transitiveModuleDeps)(_.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.{
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)
}
}
trait ScalaJsCliNativeImage extends ScalaModule with NativeImage {
def scalaVersion = scala213
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.3"
def nativeImageGraalVmJvmId = s"graalvm-java17:$graalVmVersion"
def nativeImageName = "scala-js-ld"
def moduleDeps = Seq(
cli
)
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",
compress = true,
suffix = nameSuffix
)
}
}
object native extends ScalaJsCliNativeImage
def native0 = native
def csDockerVersion = "2.1.9"
trait ScalaJsCliStaticNativeImage extends ScalaJsCliNativeImage {
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 ScalaJsCliStaticNativeImage
trait ScalaJsCliMostlyStaticNativeImage extends ScalaJsCliNativeImage {
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 ScalaJsCliMostlyStaticNativeImage
object tests extends ScalaModule {
def scalaVersion = scala213
object test extends ScalaTests with TestModule.Munit {
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scalameta::munit:1.0.3",
ivy"com.lihaoyi::os-lib:0.11.3",
ivy"com.lihaoyi::pprint:0.9.0"
)
def testHelper(
launcherTask: Task[PathRef],
args: Seq[String]
): Task[(String, Seq[mill.testrunner.TestResult])] = {
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=$scalaJsVersion"
)
args ++ extraArgs
}
testTask(argsTask, T.task(Seq.empty[String]))
}
override def test(args: String*) = jvm(args: _*)
def jvm(args: String*): Command[(String, Seq[mill.testrunner.TestResult])] = T.command {
testHelper(cli.standaloneLauncher, args)
}
def native(args: String*) = T.command {
testHelper(native0.nativeImage, args)
}
def nativeStatic(args: String*) = T.command {
testHelper(`native-static`.nativeImage, args)
}
def nativeMostlyStatic(args: String*) = T.command {
testHelper(`native-mostly-static`.nativeImage, args)
}
}
}
def ghOrg = "virtuslab"
def ghName = "scala-js-cli"
trait ScalaJsCliPublishModule extends PublishModule {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = "org.virtuslab.scala-cli",
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()
}
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")
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://oss.sonatype.org/service/local",
snapshotUri = "https://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 -> 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(ghOrg, ghName, ghToken, tag, dryRun = false, overwrite = overwriteAssets)(launchers: _*)
if(version != scalaJsVersion && !version.endsWith("-SNAPSHOT")) // when we release `0.13.0.1` we should also update native launchers in tag `0.13.0`
Upload.upload(ghOrg, ghName, ghToken, s"v$scalaJsVersion", dryRun = false, overwrite = true)(launchers: _*)
}
}
private def bash =
if (isWin) Seq("bash.exe")
else Nil