forked from scala-cli/java-class-name
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.mill.scala
More file actions
375 lines (325 loc) · 12 KB
/
Copy pathbuild.mill.scala
File metadata and controls
375 lines (325 loc) · 12 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
//| mvnDeps:
//| - io.github.alexarchambault.mill::mill-native-image::0.2.3
//| - io.github.alexarchambault.mill::mill-native-image-upload:0.2.3
//| - com.goyeau::mill-scalafix::0.6.0
//| - com.lumidion::sonatype-central-client-requests:0.6.0
package build
import io.github.alexarchambault.millnativeimage.NativeImage
import io.github.alexarchambault.millnativeimage.upload.Upload
import coursier.core.{Dependency, DependencyManagement}
import coursier.version.VersionConstraint
import java.io.File
import scala.annotation.unused
import scala.concurrent.duration.DurationInt
import com.goyeau.mill.scalafix.ScalafixModule
import com.lumidion.sonatype.central.client.core.{PublishingType, SonatypeCredentials}
import mill.*
import mill.scalalib.*
import mill.api.{BuildCtx, Task}
import mill.scalalib.publish.{Developer, License, PomSettings, VersionControl}
import mill.util.{Tasks, VcsVersion}
object Versions {
def scala = "3.3.8"
def scalaCli = "1.9.1"
def graalVmVersion = "22.3.1"
def coursier = "2.1.25-M19"
def osLib = "0.11.8"
def uTest = "0.9.5"
def jline = "3.25.0"
def ubuntu = "24.04"
}
trait JavaMainClassNativeImage extends NativeImage {
def nativeImageOptions: T[Seq[String]] = Task {
super.nativeImageOptions() ++ Seq(
"--no-fallback"
)
}
def nativeImagePersist: Boolean = System.getenv("CI") != null
def nativeImageGraalVmJvmId = s"graalvm-java17:${Versions.graalVmVersion}"
def nativeImageName = "java-class-name"
def nativeImageMainClass = "scala.cli.javaclassname.JavaClassName"
def nameSuffix = ""
@unused
def copyToArtifacts(directory: String = "artifacts/"): Command[Unit] = Task.Command {
val _ = Upload.copyLauncher0(
nativeLauncher = nativeImage().path,
directory = directory,
name = "java-class-name",
compress = true,
workspace = BuildCtx.workspaceRoot,
suffix = nameSuffix
)
}
}
trait JavaClassNameModule extends ScalaModule with ScalafixModule {
override def scalacOptions: T[Seq[String]] =
super.scalacOptions() ++ Seq("-Wunused:all")
override def scalaVersion: T[String] = Versions.scala
private def jlineOrg = "org.jline"
def jlineDeps: Seq[Dep] = Seq(
mvn"$jlineOrg:jline-reader:${Versions.jline}",
mvn"$jlineOrg:jline-terminal:${Versions.jline}",
mvn"$jlineOrg:jline-terminal-jna:${Versions.jline}",
mvn"$jlineOrg:jline-terminal-jni:${Versions.jline}",
mvn"$jlineOrg:jline-native:${Versions.jline}"
)
override def coursierDependency: Dependency =
super.coursierDependency
.addOverrides(
jlineDeps.map(jd =>
DependencyManagement.Key.from(jd.toDependency(jd.version, jd.version, "")) ->
DependencyManagement.Values.empty.withVersionConstraint(
VersionConstraint.Lazy(Versions.jline)
)
)
)
protected def downgradeJline(deps: Seq[Dep]): Seq[Dep] =
deps.map(_.exclude(jlineDeps.map(d => d.organization -> d.name)*)) ++ jlineDeps
override def allMvnDeps: T[Seq[Dep]] = Task {
downgradeJline(super.allMvnDeps())
}
override def mvnDeps: T[Seq[Dep]] = downgradeJline(super.mvnDeps())
}
object `scala3-graal-processor` extends JavaClassNameModule {
override def mainClass: T[Option[String]] = Some("scala.cli.graal.CoursierCacheProcessor")
override def mvnDeps: T[Seq[Dep]] = downgradeJline {
super.mvnDeps() ++ Seq(mvn"org.virtuslab.scala-cli::scala3-graal:${Versions.scalaCli}")
}
}
object `java-class-name` extends JavaClassNameModule with JavaMainClassNativeImage
with JavaClassNamePublishModule {
def nativeImageClassPath: T[Seq[PathRef]] = Task {
// adapted from https://github.com/VirtusLab/scala-cli/blob/b19086697401827a6f8185040ceb248d8865bf21/build.sc#L732-L744
val classpath = runClasspath().map(_.path).mkString(File.pathSeparator)
val cache = Task.dest / "native-cp"
// `scala3-graal-processor`.run() do not give me output and I cannot pass dynamically computed values like classpath
System.err.println("Calling scala3 graal processor on")
for (f <- classpath.split(File.pathSeparator))
System.err.println(s" $f")
val res = mill.util.Jvm.callProcess(
mainClass = `scala3-graal-processor`.finalMainClass(),
classPath = `scala3-graal-processor`.runClasspath().map(_.path),
mainArgs = Seq(cache.toNIO.toString, classpath)
)
val cp = res.out.trim()
if cp.isBlank then System.err.println("class path can't be empty!")
assert(cp.nonEmpty)
System.err.println("Processed class path:")
for (f <- cp.split(File.pathSeparator))
System.err.println(s" $f")
cp.split(File.pathSeparator).toSeq.map(p => mill.PathRef(os.Path(p)))
}
override def mvnDeps: T[Seq[Dep]] = downgradeJline {
super.mvnDeps() ++ Seq(mvn"org.scala-lang::scala3-compiler:${Versions.scala}")
}
override def compileMvnDeps: T[Seq[Dep]] = super.compileMvnDeps() ++ Seq(
mvn"org.graalvm.nativeimage:svm:${Versions.graalVmVersion}"
)
object static extends JavaMainClassNativeImage {
def nameSuffix = "-static"
def nativeImageClassPath: T[Seq[PathRef]] = Task {
`java-class-name`.nativeImageClassPath()
}
def buildHelperImage: T[Unit] = Task {
os.proc("docker", "build", "-t", "scala-cli-base-musl:latest", ".")
.call(cwd = BuildCtx.workspaceRoot / "musl-image", stdout = os.Inherit)
()
}
def nativeImageDockerParams: T[Option[NativeImage.DockerParams]] = Task {
buildHelperImage()
Some(
NativeImage.linuxStaticParams(
"scala-cli-base-musl:latest",
s"https://github.com/coursier/coursier/releases/download/v${Versions.coursier}/cs-x86_64-pc-linux.gz"
)
)
}
def writeNativeImageScript(scriptDest: String, imageDest: String = ""): Command[Unit] =
Task.Command {
buildHelperImage()
super.writeNativeImageScript(scriptDest, imageDest)()
}
}
object `mostly-static` extends JavaMainClassNativeImage {
def nameSuffix = "-mostly-static"
def nativeImageClassPath: T[Seq[PathRef]] = Task {
`java-class-name`.nativeImageClassPath()
}
def nativeImageDockerParams: T[Option[NativeImage.DockerParams]] = Some(
NativeImage.linuxMostlyStaticParams(
s"ubuntu:${Versions.ubuntu}",
s"https://github.com/coursier/coursier/releases/download/v${Versions.coursier}/cs-x86_64-pc-linux.gz"
)
)
}
}
trait Tests(jlineDeps: Seq[Dep]) extends ScalaModule with TestModule.Utest {
def launcher: T[PathRef]
def mvnDeps: T[Seq[Dep]] = super.mvnDeps() ++ jlineDeps ++ Seq(
mvn"com.lihaoyi::os-lib:${Versions.osLib}",
mvn"com.lihaoyi::utest:${Versions.uTest}"
)
def testFramework = "utest.runner.Framework"
def forkEnv: T[Map[String, String]] = super.forkEnv() ++ Seq(
"JAVA_CLASS_NAME_CLI" -> launcher().path.toString
)
}
object `java-class-name-tests` extends JavaClassNameModule with SbtModule {
object test extends Tests(jlineDeps = jlineDeps) with super.SbtTests {
def launcher: T[PathRef] = `java-class-name`.nativeImage()
}
}
object `java-class-name-static-tests` extends JavaClassNameModule with SbtModule {
object test extends Tests(jlineDeps = jlineDeps) with super.SbtTests {
def sources: T[Seq[PathRef]] = `java-class-name-tests`.test.sources()
def launcher: T[PathRef] = `java-class-name`.static.nativeImage()
}
}
object `java-class-name-mostly-static-tests` extends JavaClassNameModule with SbtModule {
object test extends Tests(jlineDeps = jlineDeps) with super.SbtTests {
def sources: T[Seq[PathRef]] = `java-class-name-tests`.test.sources()
def launcher: T[PathRef] = `java-class-name`.`mostly-static`.nativeImage()
}
}
def publishVersion0: T[String] = Task {
val state = VcsVersion.vcsState()
if state.commitsSinceLastTag > 0 then {
val versionOrEmpty = state.lastTag
.filter(_ != "latest")
.map(_.stripPrefix("v"))
.flatMap { tag =>
val idx = tag.lastIndexOf(".")
if idx >= 0 then
Some(tag.take(idx + 1) + (tag.drop(idx + 1).toInt + 1).toString + "-SNAPSHOT")
else None
}
.getOrElse("0.0.1-SNAPSHOT")
Some(versionOrEmpty)
.filter(_.nonEmpty)
.getOrElse(state.format())
}
else
state
.lastTag
.getOrElse(state.format())
.stripPrefix("v")
}
def ghOrg = "VirtusLab"
def ghName = "java-class-name"
def publishOrg = "org.virtuslab.scala-cli.java-class-name"
trait JavaClassNamePublishModule extends SonatypeCentralPublishModule {
def pomSettings: T[PomSettings] = PomSettings(
description = artifactName(),
organization = publishOrg,
url = s"https://github.com/$ghOrg/$ghName",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github(ghOrg, ghName),
developers = Seq(
Developer(
"Gedochao",
"Piotr Chabelski",
"https://github.com/Gedochao",
None
),
Developer(
"alexarchambault",
"Alex Archambault",
"https://github.com/alexarchambault",
None
)
)
)
def publishVersion: T[String] = publishVersion0()
}
@unused
object ci extends Module {
@unused
def publishSonatype(tasks: Tasks[PublishModule.PublishData]): Command[Unit] =
Task.Command {
val publishVersion = publishVersion0()
System.err.println(s"Publish version: $publishVersion")
val bundleName = s"$publishOrg-$ghName-$publishVersion"
System.err.println(s"Publishing bundle: $bundleName")
publishSonatype0(
data = Task.sequence(tasks.value)(),
log = Task.ctx().log,
workspace = BuildCtx.workspaceRoot,
env = Task.env,
bundleName = bundleName
)
}
private def publishSonatype0(
data: Seq[PublishModule.PublishData],
log: mill.api.Logger,
workspace: os.Path,
env: Map[String, String],
bundleName: String
): Unit = {
val credentials = SonatypeCredentials(
username = sys.env("SONATYPE_USERNAME"),
password = sys.env("SONATYPE_PASSWORD")
)
val pgpPassword = sys.env("PGP_PASSWORD")
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 SonatypeCentralPublisher(
credentials = credentials,
gpgArgs = Seq(
"--detach-sign",
"--batch=true",
"--yes",
"--pinentry-mode",
"loopback",
"--passphrase",
pgpPassword,
"--armor",
"--use-agent"
),
readTimeout = timeout.toMillis.toInt,
connectTimeout = timeout.toMillis.toInt,
log = log,
workspace = workspace,
env = env,
awaitTimeout = timeout.toMillis.toInt
)
val publishingType = if isRelease then PublishingType.AUTOMATIC else PublishingType.USER_MANAGED
val finalBundleName = if bundleName.nonEmpty then Some(bundleName) else None
publisher.publishAll(
publishingType = publishingType,
singleBundleName = finalBundleName,
artifacts = artifacts*
)
}
@unused
def upload(directory: String = "artifacts/"): Command[Unit] = Task.Command {
val version: String = publishVersion0()
val path = os.Path(directory, BuildCtx.workspaceRoot)
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") then ("nightly", true) else ("v" + version, false)
Upload.upload(
ghOrg,
ghName,
ghToken,
tag,
dryRun = false,
overwrite = overwriteAssets
)(launchers*)
}
}