-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbuild.mill
More file actions
468 lines (421 loc) · 15.7 KB
/
Copy pathbuild.mill
File metadata and controls
468 lines (421 loc) · 15.7 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//| mill-version: 1.0.6
import interfacebuild.*
import com.github.lolgab.mill.mima.*
import coursier.{Repositories, Resolve}
import coursier.core.{Dependency, Module, ModuleName, Organization, Repository}
import coursier.ivy.IvyRepository
import coursier.maven.MavenRepository
import coursier.version.VersionConstraint
import mill.*
import mill.api.BuildCtx
import mill.javalib.publish.{LocalIvyPublisher, LocalM2Publisher}
import mill.scalalib.*
import mill.util.Jvm
import java.util.Locale
import scala.util.{Properties, Using}
object Versions {
def scala213 = "2.13.17"
def scala212 = "2.12.20"
def scala = Seq(scala212, scala213)
def junit = "4.13.2"
def utest = "0.9.1"
}
object Deps {
def coursier = mvn"io.get-coursier::coursier:2.1.25-M19"
def coursierJvm = mvn"io.get-coursier::coursier-jvm:2.1.25-M19".exclude(("net.java.dev.jna", "jna"))
def scalaReflect(sv: String) = mvn"org.scala-lang:scala-reflect:$sv"
def slf4j = mvn"org.slf4j:slf4j-api:1.7.36"
def windowsJniCoursierApi = mvn"io.get-coursier.jniutils:windows-jni-utils-coursierapi:0.3.3"
def proguard = mvn"com.guardsquare:proguard-base:7.8.1"
}
trait CoursierInterfaceModule extends ScalaModule {
def scalacOptions = super.scalacOptions() ++ Seq(
"-deprecation",
"--release", "8"
)
def javacOptions = Task {
super.javacOptions() ++ Seq(
"-source", "8",
"-target", "8",
"-bootclasspath", Util.rtJar.toString
)
}
def javadocOptions = Task {
super.javadocOptions() ++ Seq(
"-source", "8",
"-bootclasspath", Util.rtJar.toString
)
}
}
object CoursierInterfacePublishedModule {
def organization = "io.get-coursier"
}
trait CoursierInterfacePublishedModule extends CoursierInterfaceModule with PublishModule {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = CoursierInterfacePublishedModule.organization,
url = "https://github.com/coursier/interface",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github("coursier", "interface"),
developers = Seq(
Developer("alexarchambault", "Alex Archambault", "https://github.com/alexarchambault")
)
)
def publishVersion = CoursierInterfaceVersion.buildVersion
}
def testPublishedDeps() = Task.Command {
def checkResolution(name: String, repositories: Seq[Repository]): Unit = {
val version = proguarded.publishVersion()
val allowed = Set(
s"${CoursierInterfacePublishedModule.organization}:interface:$version",
"org.slf4j:slf4j-api:1.7.36"
)
val interfaceDependency = Dependency(
Module(
Organization(CoursierInterfacePublishedModule.organization),
ModuleName("interface"),
Map.empty
),
VersionConstraint(version)
)
def resolve(repositories: Seq[Repository]): Set[String] =
val resolve = Resolve()
.withRepositories(repositories)
.addDependencies(interfaceDependency)
val resolution = resolve.io.unsafeRun(true)(using resolve.cache.ec)
resolution.dependenciesWithRetainedVersions.map { dep =>
val mod = dep.module
s"${mod.organization.value}:${mod.name.value}:${dep.versionConstraint.asString}"
}
val modules = resolve(repositories)
val unexpected = modules -- allowed
if (unexpected.nonEmpty)
sys.error(
s"Unexpected dependencies from $name publication:${System.lineSeparator()}" +
unexpected.toSeq.sorted.mkString(System.lineSeparator())
)
}
val publishInfos = proguarded.defaultPublishInfos(sources = true, docs = false)()
val metadata = proguarded.artifactMetadata()
val pom = proguarded.pom().path
def checkIvy(baseDir: os.Path): Unit = {
val ivyRepo = baseDir / "ivy"
os.makeDir.all(ivyRepo)
val ivy = proguarded.ivy().path
new LocalIvyPublisher(ivyRepo).publishLocal(pom, Right(ivy), metadata, publishInfos)
val ivyBase = ivyRepo.toNIO.toUri.toASCIIString.stripSuffix("/")
val ivyRepository =
IvyRepository.parse(s"$ivyBase/[organisation]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]")
.left.map(error => sys.error(s"Invalid Ivy repository pattern: $error"))
.merge
checkResolution(
"publishLocal",
Seq(ivyRepository, Repositories.central)
)
}
def checkM2(baseDir: os.Path): Unit = {
val m2Repo = baseDir / "m2"
os.makeDir.all(m2Repo)
new LocalM2Publisher(m2Repo).publish(pom, metadata, publishInfos)
val m2Base = m2Repo.toNIO.toUri.toASCIIString
checkResolution("publishM2Local", Seq(MavenRepository(m2Base), Repositories.central))
}
val tmp = os.temp.dir(prefix = "coursier-interface-published-deps-")
try {
checkIvy(tmp)
checkM2(tmp)
}
finally
os.remove.all(tmp)
}
trait CoursierInterfaceBinCompatModule extends JavaModule with InterfaceMima {
def mimaPreviousVersions = {
def stable(ver: String): Boolean =
ver.exists(c => c != '0' && c != '.') &&
ver
.replace("-RC", "-")
.forall(c => c == '.' || c == '-' || c.isDigit)
os.proc(("git", "tag", "--merged", "HEAD^"))
.call(cwd = BuildCtx.workspaceRoot, stdin = os.Inherit)
.out.lines()
.iterator
.map(_.trim)
.filter(_.startsWith("v"))
.map(_.stripPrefix("v"))
.filter(stable)
// filtering out non cross versioned module in 0.0.1 (published cross-versioned there, added below)
// TODO Mill: Add 0.0.1
.filter(_ != "0.0.1")
// borked release, jline and jansi not shaded in it
.filter(_ != "0.0.11")
.toSeq
}
}
object interface extends Cross[Interface](Versions.scala)
trait Interface extends CoursierInterfacePublishedModule with CrossSbtModule {
def artifactName = "interface-no-shading"
def mvnDeps = super.mvnDeps() ++ Seq(
Deps.coursier,
Deps.coursierJvm,
Deps.windowsJniCoursierApi,
Deps.slf4j // no need to shade that one…
)
object test extends CrossSbtTests with TestModule.Utest {
def utestVersion = Versions.utest
}
}
object proguarded extends CoursierInterfacePublishedModule {
def interfaceSv = Versions.scala213
def artifactId = "interface"
def scalaVersion = interface(interfaceSv).scalaVersion()
def mvnDeps = Seq(
Deps.slf4j
)
override def publishXmlDeps = Task {
super.publishXmlDeps().filterNot { dep =>
dep.artifact.group == "org.scala-lang" && dep.artifact.id == "scala-library"
}
}
override def ivy = Task {
import scala.xml.{Elem, XML}
val baseIvy = super.ivy()
val ivyXml = XML.loadFile(baseIvy.path.toIO)
val updated = ivyXml.copy(
child = ivyXml.child.map {
case dependencies: Elem if dependencies.label == "dependencies" =>
dependencies.copy(
child = dependencies.child.filter {
case dependency: Elem if dependency.label == "dependency" =>
dependency.attribute("org").forall(_.text != "org.scala-lang") ||
dependency.attribute("name").forall(_.text != "scala-library")
case _ => true
}
)
case other => other
}
)
val dest = Task.dest / "ivy.xml"
XML.save(dest.toString, updated, "UTF-8", xmlDecl = true)
PathRef(dest)
}
object mima extends CoursierInterfaceBinCompatModule {
def jar = Task {
val input = proguarded.jar().path
val dest = Task.dest / "interface.jar"
InterfaceMima.cleanUpJar(
proguarded.jar().path,
Task.dest / "interface.jar",
ignore = name => !name.startsWith("coursierapi/shaded/") || name.startsWith("coursierapi/internal/")
)
PathRef(dest)
}
def mimaPreviousArtifacts = Task {
mimaPreviousVersions().map(ver => mvn"${proguarded.pomSettings().organization}:${proguarded.artifactId()}:$ver")
}
}
def mimaBinaryIssueFilters = Seq(
ProblemFilter.exclude[Problem]("coursierapi.shaded.*"),
ProblemFilter.exclude[Problem]("coursierapi.internal.*")
)
def proguardedJar = Task {
val dest = Task.dest / "interface.jar"
val configFile = Task.dest / "config.pro"
val proguardOptions = {
val baseOptions = Seq(
"-dontnote",
"-dontwarn",
"-dontobfuscate",
"-dontoptimize",
"-keep class coursierapi.** {\n public protected *;\n}"
)
val maybeJava9Options =
if (Util.isJava9OrMore) {
val javaHome = os.Path(sys.props.getOrElse("java.home", ???))
Seq(
s"-libraryjars ${javaHome / "jmods/java.base.jmod"}",
s"-libraryjars ${javaHome / "jmods/java.xml.jmod"}"
)
}
else
Nil
val classPathOptions = interface(interfaceSv)
.runClasspath()
.map(_.path)
.filter(os.exists)
.flatMap { path =>
val isLibrary = path.last.startsWith("slf4j-api-") && path.last.endsWith(".jar")
if (isLibrary)
Seq("-libraryjars", path.toString)
else {
val filterOpt = path.last match {
case n if n.startsWith("interface") => None // keep META-INF from main JAR
case n if n.startsWith("windows-jni-utils") => Some("!META-INF/MANIFEST.MF")
case n if n.startsWith("coursier-core") => Some("!META-INF/**,!coursier.properties,!coursier/coursier.properties")
case n if n.startsWith("scala-xml") => Some("!META-INF/**,!scala-xml.properties")
case n if n.startsWith("scala-library") => Some("!META-INF/**,!library.properties,!rootdoc.txt")
case n if n.startsWith("tika-core") => Some("!META-INF/**,!pipes-fork-server-default-log4j2.xml")
case _ => Some("!META-INF/**")
}
Seq("-injars", path.toString + filterOpt.fold("")("(" + _ + ")"))
}
}
val outputOptions = Seq("-outjars", dest.toString)
baseOptions ++ maybeJava9Options ++ classPathOptions ++ outputOptions
}
os.write(configFile, proguardOptions.map(_ + System.lineSeparator()).mkString)
val proguardCp = defaultResolver()
.classpath(Seq(Deps.proguard))
.map(_.path)
Jvm.callProcess(
mainClass = "proguard.ProGuard",
mainArgs = Seq("-include", configFile.toString),
classPath = proguardCp,
jvmArgs = Seq("-Xmx3172M"),
stdin = os.Inherit,
stdout = os.Inherit,
stderr = os.Inherit
)
PathRef(dest)
}
def proguardedAndShadedJar = Task {
import com.eed3si9n.jarjar._
import com.eed3si9n.jarjar.util.StandaloneJarProcessor
val input = proguardedJar().path
def rename(from: String, to: String): Rule = {
val rule = new Rule
rule.setPattern(from)
rule.setResult(to)
rule
}
val rules = Seq(
rename("scala.**", "coursierapi.shaded.scala.@1"),
rename("coursier.**", "coursierapi.shaded.coursier.@1"),
rename("dependency.**", "coursierapi.shaded.dependency.@1"),
rename("org.fusesource.**", "coursierapi.shaded.org.fusesource.@1"),
rename("io.github.alexarchambault.windowsansi.**", "coursierapi.shaded.windowsansi.@1"),
rename("concurrentrefhashmap.**", "coursierapi.shaded.concurrentrefhashmap.@1"),
rename("org.apache.commons.codec.**", "coursierapi.shaded.commonscodec.@1"),
rename("org.apache.commons.compress.**", "coursierapi.shaded.commonscompress.@1"),
rename("org.apache.commons.io.**", "coursierapi.shaded.commonsio.@1"),
rename("org.codehaus.plexus.**", "coursierapi.shaded.plexus.@1"),
rename("org.tukaani.xz.**", "coursierapi.shaded.xz.@1"),
rename("org.iq80.snappy.**", "coursierapi.shaded.snappy.@1"),
rename("com.github.plokhotnyuk.jsoniter_scala.core.**", "coursierapi.shaded.jsoniter.@1"),
rename("com.github.luben.zstd.**", "coursierapi.shaded.zstd.@1"),
rename("io.airlift.compress.**", "coursierapi.shaded.compress.@1"),
rename("io.github.alexarchambault.isterminal.**", "coursierapi.shaded.isterminal.@1"),
rename("org.apache.commons.lang3.**", "coursierapi.shaded.lang3.@1"),
rename("org.apache.tika.**", "coursierapi.shaded.tika.@1")
)
val processor = new com.eed3si9n.jarjar.JJProcessor(
rules,
verbose = false,
skipManifest = true,
misplacedClassStrategy = "fatal"
)
val dest = Task.dest / "interface.jar"
StandaloneJarProcessor.run(input.toIO, dest.toIO, processor)
PathRef(dest)
}
def cleanedUpJar = Task {
val input = proguardedAndShadedJar().path
val intermediaryDest = Task.dest / "intermediary.jar"
val dest = Task.dest / "interface.jar"
val toBeRemoved = Set(
"LICENSE",
"NOTICE",
"README"
)
val directoriesToBeRemoved = Seq(
"licenses/"
)
assert(directoriesToBeRemoved.forall(_.endsWith("/")))
ZipUtil.removeFromZip(
input,
intermediaryDest,
name =>
toBeRemoved(name) || directoriesToBeRemoved.exists(dir =>
name.startsWith(dir)
)
)
val serviceContent =
ZipUtil.zipEntryContent(proguardedJar().path, "META-INF/services/coursier.jniutils.NativeApi").getOrElse {
sys.error(s"META-INF/services/coursier.jniutils.NativeApi not found in ${proguardedJar().path}")
}
ZipUtil.addOrOverwriteInZip(
intermediaryDest,
dest,
Seq(
"META-INF/services/coursierapi.shaded.coursier.jniutils.NativeApi" -> serviceContent
)
)
PathRef(dest)
}
def jar = Task {
val jar0 = cleanedUpJar()
Check.onlyNamespace("coursierapi", jar0.path.toIO)
jar0
}
def docJar = interface(interfaceSv).docJar()
def sourceJar = interface(interfaceSv).sourceJar()
}
trait DependsOnProguarded extends JavaModule {
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"${CoursierInterfacePublishedModule.organization}:interface:${proguarded.publishVersion()}"
)
def repositories = super.repositories() ++ Seq(
proguarded.publishLocalTestRepo().path.toNIO.toUri.toASCIIString
)
}
object interpolators extends Cross[Interpolators](Versions.scala)
trait Interpolators extends CoursierInterfacePublishedModule with CoursierInterfaceBinCompatModule with CrossSbtModule with DependsOnProguarded {
def compileMvnDeps = super.compileMvnDeps() ++ Seq(
Deps.scalaReflect(scalaVersion())
)
def mimaPreviousVersions = Task {
val ignore =
if (scalaVersion().startsWith("2.13.")) (0 to 8).map("0.0." + _).toSet
else Set()
if (ignore.isEmpty)
super.mimaPreviousVersions()
else
super.mimaPreviousVersions().filter(!ignore(_))
}
def mimaBinaryIssueFilters = Seq(
ProblemFilter.exclude[MissingClassProblem]("coursierapi.Interpolators$Macros$"),
)
object test extends CrossSbtTests with TestModule.Utest {
def utestVersion = Versions.utest
}
}
object `interface-test` extends Cross[InterfaceTest](Versions.scala)
trait InterfaceTest extends CoursierInterfaceModule with CrossSbtModule with DependsOnProguarded { interfaceTest =>
object test extends interfaceTest.SbtTests with TestModule.Junit4 {
def junit4Version = Versions.junit
}
}
object Util {
private def isArm64 =
Option(System.getProperty("os.arch")).map(_.toLowerCase(Locale.ROOT)) match {
case Some("aarch64" | "arm64") => true
case _ => false
}
private lazy val java8Home = Option(System.getenv("COURSIER_INTERFACE_JAVA8_HOME")).getOrElse {
val jvmId =
if (Properties.isMac && isArm64)
// no native JDK 8 on Mac ARM, using amd64 one
"https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u432-b06/OpenJDK8U-jdk_x64_mac_hotspot_8u432b06.tar.gz"
else
"adoptium:8"
os.proc("cs", "java-home", "--jvm", jvmId)
.call()
.out.trim()
}
lazy val rtJar = {
val path = os.Path(java8Home) / "jre/lib/rt.jar"
assert(os.isFile(path))
path
}
lazy val isJava9OrMore = sys.props.get("java.version").exists(!_.startsWith("1."))
}