forked from almond-sh/almond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sc
534 lines (478 loc) · 16.3 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import $ivy.`com.lihaoyi::mill-contrib-bloop:$MILL_VERSION`
import $ivy.`com.github.lolgab::mill-mima_mill0.9:0.0.4`
import $file.project.deps, deps.{Deps, DepOps, ScalaVersions}
import $file.project.jupyterserver, jupyterserver.jupyterServer
import $file.scripts.website.Website, Website.Relativize
import $file.project.settings, settings.{AlmondModule, AlmondRepositories, BootstrapLauncher, DependencyListResource, ExternalSources, HasTests, Mima, PropertyFile, Util}
import java.nio.charset.Charset
import java.nio.file.FileSystems
import mill._, scalalib._
import _root_.scala.concurrent.duration._
import _root_.scala.util.Properties
// Tell mill modules are under modules/
implicit def millModuleBasePath: define.BasePath =
define.BasePath(super.millModuleBasePath.value / "modules")
class LoggerScala2Macros(val crossScalaVersion: String) extends AlmondModule with HasTests {
def ivyDeps = T{
val sv = scalaVersion()
Agg(Deps.scalaReflect(sv))
}
}
class Logger(val crossScalaVersion: String) extends AlmondModule with HasTests {
def supports3 = true
def moduleDeps = Seq(
shared.`logger-scala2-macros`()
)
def ivyDeps = T{
val sv = scalaVersion()
val scalaReflect =
if (sv.startsWith("2.")) Agg(Deps.scalaReflect(sv))
else Agg(ivy"org.scala-lang:scala3-library_3:${scalaVersion()}")
scalaReflect
}
object test extends Tests
}
class Channels(val crossScalaVersion: String) extends AlmondModule with HasTests with Mima {
def moduleDeps = Seq(
shared.logger()
)
def ivyDeps = Agg(
Deps.fs2,
Deps.jeromq
)
object test extends Tests
}
class Protocol(val crossScalaVersion: String) extends AlmondModule with HasTests {
def moduleDeps = Seq(
shared.channels()
)
def ivyDeps = Agg(
Deps.jsoniterScalaCore.applyBinaryVersion213_3(scalaVersion())
)
def compileIvyDeps = Agg(
Deps.scalaReflect(scalaVersion()),
Deps.jsoniterScalaMacros.withConfiguration("provided")
)
object test extends Tests
}
class InterpreterApi(val crossScalaVersion: String) extends AlmondModule with Mima
class Interpreter(val crossScalaVersion: String) extends AlmondModule with HasTests {
def moduleDeps = Seq(
shared.`interpreter-api`(),
shared.protocol()
)
def ivyDeps = Agg(
Deps.collectionCompat,
Deps.scalatags.applyBinaryVersion213_3(scalaVersion()),
Deps.slf4jNop
)
object test extends Tests
}
class Kernel(val crossScalaVersion: String) extends AlmondModule with HasTests {
def moduleDeps = Seq(
shared.interpreter()
)
def ivyDeps = Agg(
Deps.caseAppAnnotations.withDottyCompat(scalaVersion(), ScalaVersions.cross2_3Version),
Deps.collectionCompat,
Deps.fs2
)
object test extends Tests {
def moduleDeps = super.moduleDeps ++ Seq(
shared.interpreter().test
)
}
}
class Test(val crossScalaVersion: String) extends AlmondModule {
def moduleDeps = Seq(
shared.`interpreter-api`()
)
}
class JupyterApi(val crossScalaVersion: String) extends AlmondModule with Mima {
def moduleDeps = Seq(
shared.`interpreter-api`()
)
def ivyDeps = Agg(
Deps.jvmRepr
)
}
class ScalaKernelApi(val crossScalaVersion: String) extends AlmondModule with DependencyListResource with ExternalSources with PropertyFile with Mima {
def crossFullScalaVersion = true
def moduleDeps = Seq(
shared.`interpreter-api`(),
scala.`jupyter-api`()
)
def ivyDeps = Agg(
Deps.ammoniteCompiler(crossScalaVersion),
Deps.ammoniteReplApi(crossScalaVersion),
Deps.jvmRepr
)
def propertyFilePath = "almond/almond.properties"
def propertyExtra = Seq(
"default-scalafmt-version" -> Deps.scalafmtDynamic.dep.version,
"scala-version" -> crossScalaVersion
)
}
class ScalaInterpreter(val crossScalaVersion: String) extends AlmondModule with HasTests {
def crossFullScalaVersion = true
def supports3 = true
def moduleDeps = Seq(
shared.interpreter(),
scala.`scala-kernel-api`()
)
def addMetabrowse = T{
val sv = scalaVersion()
val patch = sv
.split('.')
.drop(2)
.headOption
.flatMap(s => _root_.scala.util.Try(s.takeWhile(_.isDigit).toInt).toOption)
(sv.startsWith("2.12.") && patch.exists(_ <= 10)) ||
(sv.startsWith("2.13.") && patch.exists(_ <= 1))
}
def ivyDeps = T{
val metabrowse =
if (addMetabrowse()) Agg(Deps.metabrowseServer)
else Agg.empty
metabrowse ++ Agg(
Deps.coursier.withDottyCompat(scalaVersion(), ScalaVersions.cross2_3Version),
Deps.coursierApi,
Deps.directories,
Deps.jansi,
Deps.ammoniteCompiler(crossScalaVersion),
Deps.ammoniteRepl(crossScalaVersion)
)
}
def sources = T.sources {
val dirName =
if (addMetabrowse()) "scala-has-metabrowse"
else "scala-no-metabrowse"
val extra = PathRef(millSourcePath / "src" / "main" / dirName)
super.sources() ++ Seq(extra)
}
object test extends Tests {
def moduleDeps = {
val rx =
if (crossScalaVersion.startsWith("2.12.")) Seq(scala.`almond-rx`())
else Nil
super.moduleDeps ++
Seq(shared.kernel().test) ++
rx
}
}
}
class ScalaKernel(val crossScalaVersion: String) extends AlmondModule with HasTests with ExternalSources with BootstrapLauncher {
def crossFullScalaVersion = true
def moduleDeps = Seq(
shared.kernel(),
scala.`scala-interpreter`()
)
def ivyDeps = Agg(
Deps.caseApp,
Deps.scalafmtDynamic
)
object test extends Tests {
def moduleDeps = super.moduleDeps ++ Seq(
scala.`scala-interpreter`().test
)
}
def runClasspath =
super.runClasspath() ++
transitiveSources() ++
externalSources()
def launcherClassPath =
transitiveJars() ++
unmanagedClasspath() ++
resolvedRunIvyDeps() ++
transitiveSourceJars() ++
externalSources()
def launcherSharedClassPath =
scala.`scala-kernel-api`().transitiveJars() ++
scala.`scala-kernel-api`().unmanagedClasspath() ++
scala.`scala-kernel-api`().resolvedRunIvyDeps() ++
scala.`scala-kernel-api`().transitiveSourceJars() ++
scala.`scala-kernel-api`().externalSources()
def manifest = T{
import java.util.jar.Attributes.Name
val ver = publishVersion()
super.manifest().add(
Name.IMPLEMENTATION_TITLE.toString -> "scala-kernel",
Name.IMPLEMENTATION_VERSION.toString -> ver,
Name.SPECIFICATION_VENDOR.toString -> "sh.almond",
Name.SPECIFICATION_TITLE.toString -> "scala-kernel",
Name.IMPLEMENTATION_VENDOR_ID.toString -> "sh.almond",
Name.SPECIFICATION_VERSION.toString -> ver,
Name.IMPLEMENTATION_VENDOR.toString -> "sh.almond"
)
}
}
class AlmondSpark(val crossScalaVersion: String) extends AlmondModule with Mima {
def compileModuleDeps = Seq(
scala.`scala-kernel-api`()
)
def ivyDeps = Agg(
Deps.ammoniteSpark,
Deps.jsoniterScalaCore
)
def compileIvyDeps = Agg(
Deps.ammoniteReplApi(crossScalaVersion),
Deps.jsoniterScalaMacros,
Deps.sparkSql
)
// TODO?
// sources.in(Compile, doc) := Nil
}
class AlmondRx(val crossScalaVersion: String) extends AlmondModule with Mima {
def compileModuleDeps = Seq(
scala.`scala-kernel-api`()
)
def ivyDeps = Agg(
Deps.scalaRx
)
}
class Echo(val crossScalaVersion: String) extends AlmondModule with HasTests {
def moduleDeps = Seq(
shared.kernel()
)
def ivyDeps = Agg(
Deps.caseApp
)
def propertyFilePath = "almond/echo.properties"
object test extends Tests {
def moduleDeps = super.moduleDeps ++ Seq(
shared.test()
)
}
}
object shared extends Module {
object `logger-scala2-macros` extends Cross[LoggerScala2Macros](ScalaVersions.binaries: _*)
object logger extends Cross[Logger] (ScalaVersions.binaries: _*)
object channels extends Cross[Channels] (ScalaVersions.binaries: _*)
object protocol extends Cross[Protocol] (ScalaVersions.binaries: _*)
object `interpreter-api` extends Cross[InterpreterApi](ScalaVersions.binaries: _*)
object interpreter extends Cross[Interpreter] (ScalaVersions.binaries: _*)
object kernel extends Cross[Kernel] (ScalaVersions.binaries: _*)
object test extends Cross[Test] (ScalaVersions.binaries: _*)
}
// FIXME Can't use 'scala' because of macro hygiene issues in some mill macros
object scala extends Module {
implicit def millModuleBasePath: define.BasePath =
define.BasePath(super.millModuleBasePath.value / os.up / "scala")
object `jupyter-api` extends Cross[JupyterApi] (ScalaVersions.binaries: _*)
object `scala-kernel-api` extends Cross[ScalaKernelApi] (ScalaVersions.all: _*)
object `scala-interpreter` extends Cross[ScalaInterpreter](ScalaVersions.all: _*)
object `scala-kernel` extends Cross[ScalaKernel] (ScalaVersions.all: _*)
object `almond-spark` extends Cross[AlmondSpark] (ScalaVersions.scala212)
object `almond-rx` extends Cross[AlmondRx] (ScalaVersions.scala212)
}
object echo extends Cross[Echo](ScalaVersions.binaries: _*)
object docs extends ScalaModule with AlmondRepositories {
private def scalaVersion0 = ScalaVersions.scala213
def scalaVersion = scalaVersion0
def moduleDeps = Seq(
scala.`scala-kernel-api`(scalaVersion0)
)
def ivyDeps = Agg(
Deps.mdoc
)
def mainClass = Some("mdoc.Main")
def generate(args: String*) = T.command {
def processArgs(
npmInstall: Boolean,
yarnRunBuild: Boolean,
watch: Boolean,
relativize: Boolean,
args: List[String]
): (Boolean, Boolean, Boolean, Boolean, List[String]) =
args match {
case "--npm-install" :: rem => processArgs(true, yarnRunBuild, watch, relativize, rem)
case "--yarn-run-build" :: rem => processArgs(npmInstall, true, watch, relativize, rem)
case "--watch" :: rem => processArgs(npmInstall, yarnRunBuild, true, relativize, rem)
case "--relativize" :: rem => processArgs(npmInstall, yarnRunBuild, watch, true, rem)
case _ => (npmInstall, yarnRunBuild, watch, relativize, args)
}
val (npmInstall, yarnRunBuild, watch, relativize, args0) = processArgs(false, false, false, false, args.toList)
val ver = scala.`scala-kernel-api`(scalaVersion0).publishVersion()
val latestRelease = settings.latestTaggedVersion
val ammVer = Deps.ammoniteReplApi(scalaVersion0).dep.version
val scalaVer = scalaVersion0
val isSnapshot = ver.endsWith("SNAPSHOT")
val extraSbt =
if (isSnapshot) """resolvers += Resolver.sonatypeRepo("snapshots")""" + "\n"
else ""
val extraCoursierArgs =
if (isSnapshot) "-r sonatype:snapshots "
else ""
val outputDir = "docs/processed-pages"
val allArgs = Seq(
"--in", "docs/pages",
"--out", outputDir,
"--site.VERSION", ver,
"--site.LATEST_RELEASE", latestRelease,
"--site.EXTRA_SBT", extraSbt,
"--site.AMMONITE_VERSION", ammVer,
"--site.SCALA_VERSION", scalaVer,
"--site.EXTRA_COURSIER_ARGS", extraCoursierArgs
) ++ (if (watch) Seq("--watch") else Nil) ++ args0
// TODO Run yarn run thing right after, add --watch mode
val websiteDir = os.pwd / "docs" / "website"
if (npmInstall)
Util.run(Seq("npm", "install"), dir = websiteDir.toIO)
def runMdoc(): Unit =
// adapted from https://github.com/com-lihaoyi/mill/blob/c500ca986ab79af3ce59ba65a093146672092307/scalalib/src/JavaModule.scala#L488-L494
mill.modules.Jvm.runSubprocess(
finalMainClass(),
runClasspath().map(_.path),
Nil,
forkEnv(),
allArgs,
workingDir = forkWorkingDir()
)
if (watch) {
if (yarnRunBuild)
Util.withBgProcess(
Seq("yarn", "run", "start"),
dir = websiteDir.toIO,
waitFor = () => Util.waitForDir((os.pwd / outputDir.split('/').toSeq).toIO)
) {
runMdoc()
}
else
runMdoc()
} else {
runMdoc()
if (yarnRunBuild)
Util.run(Seq("yarn", "run", "build"), dir = websiteDir.toIO)
if (relativize)
Relativize.relativize((websiteDir / "build").toNIO)
}
}
}
def jupyter0(args: Seq[String], fast: Boolean) = {
val (sv, args0) = args match {
case Seq(sv, rem @ _*) if sv.startsWith("2.") || sv.startsWith("3.") =>
(sv, rem)
case _ => (ScalaVersions.scala213, args)
}
val launcher =
if (fast) scala.`scala-kernel`(sv).fastLauncher
else scala.`scala-kernel`(sv).launcher
T.command {
val jupyterDir = T.ctx().dest / "jupyter"
val launcher0 = launcher().path.toNIO
jupyterServer(launcher0, jupyterDir.toNIO, args0)
}
}
def jupyter(args: String*) =
jupyter0(args, fast = false)
def jupyterFast(args: String*) =
jupyter0(args, fast = true)
def publishSonatype(tasks: mill.main.Tasks[PublishModule.PublishData]) =
T.command {
val timeout = 10.minutes
val credentials = sys.env("SONATYPE_USERNAME") + ":" + sys.env("SONATYPE_PASSWORD")
val pgpPassword = sys.env("PGP_PASSWORD")
val data = define.Task.sequence(tasks.value)()
settings.publishSonatype(
credentials = credentials,
pgpPassword = pgpPassword,
data = data,
timeout = timeout,
log = T.ctx().log
)
}
def scala212() = T.command {
println(ScalaVersions.scala212)
}
def scala213() = T.command {
println(ScalaVersions.scala213)
}
def scala3() = T.command {
println(ScalaVersions.scala3)
}
def scalaVersions() = T.command {
for (sv <- ScalaVersions.all)
println(sv)
}
def launcher(scalaVersion: String = ScalaVersions.scala213) = T.command {
val launcher = scala.`scala-kernel`(scalaVersion).launcher().path.toNIO
println(launcher)
}
private val examplesDir = os.pwd / "examples"
def exampleNotebooks = T.sources {
os.list(examplesDir)
.filter(_.last.endsWith(".ipynb"))
.filter(os.isFile(_))
.map(PathRef(_))
}
def validateExamples(matcher: String = "") = {
val sv = "2.12.12"
val kernelId = "almond-sources-tmp"
val baseRepoRoot = os.rel / "out" / "repo"
def maybeEscapeArg(arg: String): String =
if (Properties.isWin && arg.exists(c => c == ' ' || c == '\"'))
"\"" + arg.replace("\"", "\\\"") + "\""
else arg
val pathMatcherOpt =
if (matcher.trim.isEmpty) None
else {
val m = FileSystems.getDefault.getPathMatcher("glob:" + matcher.trim)
Some(m)
}
T.command {
val launcher = scala.`scala-kernel`(sv).launcher().path.toNIO
val jupyterPath = T.dest / "jupyter"
val outputDir = T.dest / "output"
os.makeDir.all(outputDir)
val version = scala.`scala-kernel`(sv).publishVersion()
val repoRoot = baseRepoRoot / version
os.proc(
launcher.toString,
"--jupyter-path", jupyterPath / "kernels",
"--id", kernelId,
"--install", "--force",
"--trap-output",
"--predef-code", maybeEscapeArg("sys.props(\"almond.ids.random\") = \"0\""),
"--extra-repository", s"ivy:${repoRoot.toNIO.toUri.toASCIIString}/[defaultPattern]"
).call(cwd = examplesDir)
val nbFiles = exampleNotebooks()
.map(_.path)
.filter { p =>
pathMatcherOpt.fold(true) { m =>
m.matches(p.toNIO.getFileName)
}
}
var errorCount = 0
for (f <- nbFiles) {
val output = outputDir / f.last
os.proc(
"jupyter", "nbconvert",
"--to", "notebook",
"--execute",
s"--ExecutePreprocessor.kernel_name=$kernelId",
f,
s"--output=$output"
).call(cwd = examplesDir, env = Map("JUPYTER_PATH" -> jupyterPath.toString))
if (Properties.isWin) {
val rawOutput = os.read(output, Charset.defaultCharset())
val updatedOutput = rawOutput.replace("\r\n", "\n").replace("\\r\\n", "\\n")
// writing the updated notebook on disk for the diff below
os.write.over(output, updatedOutput.getBytes(Charset.defaultCharset()))
}
val result = os.read(output, Charset.defaultCharset())
val expected = os.read(f)
if (result != expected) {
System.err.println(s"${f.last} differs:")
System.err.println()
os.proc("diff", "-u", f, output).call(cwd = examplesDir)
errorCount += 1
}
}
if (errorCount != 0)
sys.error(s"Found $errorCount error(s)")
}
}
def launcherFast(scalaVersion: String = ScalaVersions.scala213) = T.command {
val launcher = scala.`scala-kernel`(scalaVersion).fastLauncher().path.toNIO
println(launcher)
}