forked from scala-cli/scalafmt-native-image
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.sc
190 lines (172 loc) · 5.89 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
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0`
import $ivy.`io.github.alexarchambault.mill::mill-native-image::0.1.26`
import $ivy.`io.github.alexarchambault.mill::mill-native-image-upload:0.1.26`
import de.tobiasroeser.mill.vcs.version._
import io.github.alexarchambault.millnativeimage.NativeImage
import io.github.alexarchambault.millnativeimage.upload.Upload
import mill._
import mill.scalalib._
def scalafmtVersion = "3.8.3"
trait ScalafmtNativeImage extends ScalaModule with NativeImage {
def scalaVersion = "2.13.13"
def nativeImageClassPath = T{
val origCp = runClasspath()
val tmpDir = T.dest / "jars"
os.makeDir.all(tmpDir)
origCp.map { p =>
val path = p.path
val name = path.last
// stripping the "--verbose" option from the native-image.properties in the scalafmt-cli JAR,
// as GraalVM 22.2 doesn't accept this option in properties files anymore
if (name.startsWith("scalafmt-cli_2.13-") && name.endsWith(".jar")) {
import java.io.{ByteArrayOutputStream, FileOutputStream, InputStream}
import java.util.zip.{ZipEntry, ZipFile, ZipOutputStream}
import scala.jdk.CollectionConverters._
val dest = tmpDir / name
var zf: ZipFile = null
var fos: FileOutputStream = null
var zos: ZipOutputStream = null
try {
zf = new ZipFile(path.toIO)
fos = new FileOutputStream(dest.toIO)
zos = new ZipOutputStream(fos)
val buf = Array.ofDim[Byte](64*1024)
for (ent <- zf.entries.asScala) {
val postProcess = ent.getName == "META-INF/native-image/org.scalafmt/scalafmt-cli/native-image.properties"
var is: InputStream = null
try {
is = zf.getInputStream(ent)
if (postProcess) {
val ent0 = new ZipEntry(ent.getName)
zos.putNextEntry(ent0)
val baos = new ByteArrayOutputStream
var read = -1
while ({
read = is.read(buf)
read >= 0
}) {
if (read > 0)
baos.write(buf, 0, read)
}
val content = new String(baos.toByteArray, "UTF-8")
val updatedContent = content.replace("--verbose \\\n", "")
zos.write(updatedContent.getBytes("UTF-8"))
}
else {
zos.putNextEntry(ent)
var read = -1
while ({
read = is.read(buf)
read >= 0
}) {
if (read > 0)
zos.write(buf, 0, read)
}
}
} finally {
if (is != null)
is.close()
}
}
zos.finish()
} finally {
if (zf != null) zf.close()
if (zos != null) zos.close()
if (fos != null) fos.close()
}
PathRef(dest)
}
else
p
}
}
def nativeImageOptions = T{
super.nativeImageOptions() ++ Seq(
"--no-fallback"
)
}
def nativeImagePersist = System.getenv("CI") != null
def nativeImageGraalVmJvmId = "graalvm-java17:22.2.0"
def nativeImageName = "scalafmt"
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scalameta::scalafmt-cli:$scalafmtVersion"
)
def nativeImageMainClass = "org.scalafmt.cli.Cli"
def nameSuffix = ""
def copyToArtifacts(directory: String = "artifacts/") = T.command {
val _ = Upload.copyLauncher(
nativeImage().path,
directory,
"scalafmt",
compress = true,
suffix = nameSuffix
)
}
}
object native extends ScalafmtNativeImage
def csDockerVersion = "2.1.0-M5-18-gfebf9838c"
object `native-static` extends ScalafmtNativeImage {
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-mostly-static` extends ScalafmtNativeImage {
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"
)
)
}
def publishVersion = T{
val state = VcsVersion.vcsState()
if (state.commitsSinceLastTag > 0) {
val versionOrEmpty = state.lastTag
.filter(_ != "latest")
.map(_.stripPrefix("v"))
.flatMap { tag =>
val idx = tag.lastIndexOf(".")
if (idx >= 0) 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 upload(directory: String = "artifacts/") = T.command {
val version = publishVersion()
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("virtuslab", "scalafmt-native-image", ghToken, tag, dryRun = false, overwrite = overwriteAssets)(launchers: _*)
}