-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
BloopImpl.scala
437 lines (391 loc) · 14.4 KB
/
BloopImpl.scala
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
package mill.contrib.bloop
import _root_.bloop.config.{Config => BloopConfig, Tag => BloopTag}
import mill._
import mill.api.Result
import mill.define.{Discover, ExternalModule, Module => MillModule}
import mill.eval.Evaluator
import mill.scalalib.internal.{JavaModuleUtils, ModuleUtils}
import mill.scalajslib.ScalaJSModule
import mill.scalajslib.api.{JsEnvConfig, ModuleKind}
import mill.scalalib._
import mill.scalanativelib.ScalaNativeModule
import mill.scalanativelib.api.ReleaseMode
/**
* Implementation of the Bloop related tasks. Inherited by the
* `mill.contrib.bloop.Bloop` object, and usable in tests by passing
* a custom evaluator.
*/
class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer =>
import BloopFormats._
private val bloopDir = wd / ".bloop"
/**
* Generates bloop configuration files reflecting the build,
* under pwd/.bloop.
*/
def install() = T.command {
val res = T.traverse(computeModules)(_.bloop.writeConfigFile())()
val written = res.map(_._2).map(_.path)
// Make bloopDir if it doesn't exists
if (!os.exists(bloopDir)) {
os.makeDir(bloopDir)
}
// Cleaning up configs that weren't generated in this run.
os.list(bloopDir)
.filter(_.ext == "json")
.filterNot(written.contains)
.foreach(os.remove)
res
}
/**
* Trait that can be mixed-in to quickly access the bloop config
* of the module.
*
* {{{
* object myModule extends ScalaModule with Bloop.Module {
* ...
* }
* }}}
*/
trait Module extends MillModule { self: JavaModule =>
/**
* Allows to tell Bloop whether it should use "fullOptJs" or
* "fastOptJs" when compiling. Used exclusively with ScalaJsModules.
*/
def linkerMode: T[Option[BloopConfig.LinkerMode]] = None
object bloop extends MillModule {
def config = T {
new BloopOps(self).bloop.config()
}
}
/**
* Setting to true enables skipping the bloop configuration generation
*/
def skipBloop: Boolean = false
}
/**
* Extension class used to ensure that the config related tasks are
* cached alongside their respective modules, without requesting the user
* to extend a specific trait.
*
* This also ensures that we're not duplicating work between the global
* "install" task that traverse all modules in the build, and "local" tasks
* that traverse only their transitive dependencies.
*/
private implicit class BloopOps(jm: JavaModule) extends MillModule {
override def millOuterCtx = jm.millOuterCtx
object bloop extends MillModule {
def config = T { outer.bloopConfig(jm) }
def writeConfigFile(): Command[(String, PathRef)] = T.command {
os.makeDir.all(bloopDir)
val path = bloopConfigPath(jm)
_root_.bloop.config.write(config(), path.toNIO)
T.log.info(s"Wrote $path")
name(jm) -> PathRef(path)
}
@deprecated("Use writeConfigFile instead.", "Mill after 0.10.9")
def writeConfig: Target[(String, PathRef)] = T {
writeConfigFile()()
}
}
def asBloop: Option[Module] = jm match {
case m: Module => Some(m)
case _ => None
}
}
// Compute all transitive modules from build children and via moduleDeps
@deprecated("Use mill.internal.JavaModuleUtils.transitiveModules instead", since = "mill 0.10.3")
def transitiveModules(
mod: define.Module,
found: Seq[define.Module] = Seq.empty
): Seq[define.Module] = {
JavaModuleUtils.transitiveModules(mod, accept)
}
protected def computeModules: Seq[JavaModule] = {
val eval = ev()
if (eval != null)
JavaModuleUtils.transitiveModules(eval.rootModule, accept)
.collect { case jm: JavaModule => jm }
else
Seq.empty
}
// class-based pattern matching against path-dependant types doesn't seem to work.
private def accept(module: MillModule): Boolean =
if (module.isInstanceOf[JavaModule] && module.isInstanceOf[outer.Module])
!module.asInstanceOf[outer.Module].skipBloop
else true
/**
* Computes sources files paths for the whole project. Cached in a way
* that does not get invalidated upon sourcefile change. Mainly called
* from module#sources in bloopInstall
*/
def moduleSourceMap = T.input {
val sources = T.traverse(computeModules) { m =>
m.allSources.map { paths =>
name(m) -> paths.map(_.path)
}
}()
Result.Success(sources.toMap)
}
protected def name(m: JavaModule): String = ModuleUtils.moduleDisplayName(m) match {
case "" => "root-module"
case n => n
}
protected def bloopConfigPath(module: JavaModule): os.Path =
bloopDir / s"${name(module)}.json"
// ////////////////////////////////////////////////////////////////////////////
// Computation of the bloop configuration for a specific module
// ////////////////////////////////////////////////////////////////////////////
def bloopConfig(module: JavaModule): Task[BloopConfig.File] = {
import _root_.bloop.config.Config
def out(m: JavaModule) = bloopDir / "out" / name(m)
def classes(m: JavaModule) = out(m) / "classes"
val javaConfig =
module.javacOptions.map(opts => Some(Config.Java(options = opts.toList)))
// //////////////////////////////////////////////////////////////////////////
// Scalac
// //////////////////////////////////////////////////////////////////////////
val scalaConfig = module match {
case s: ScalaModule =>
T.task {
Some(
BloopConfig.Scala(
organization = s.scalaOrganization(),
name = "scala-compiler",
version = s.scalaVersion(),
options = s.allScalacOptions().toList,
jars = s.scalaCompilerClasspath().map(_.path.toNIO).toList,
analysis = None,
setup = None
)
)
}
case _ => T.task(None)
}
// //////////////////////////////////////////////////////////////////////////
// Platform (Jvm/Js/Native)
// //////////////////////////////////////////////////////////////////////////
def jsLinkerMode(m: JavaModule): Task[Config.LinkerMode] =
(m.asBloop match {
case Some(bm) => T.task(bm.linkerMode())
case None => T.task(None)
}).map(_.getOrElse(Config.LinkerMode.Debug))
// //////////////////////////////////////////////////////////////////////////
// Classpath
// //////////////////////////////////////////////////////////////////////////
val classpath = T.task {
val depModules = (module.compileModuleDepsChecked ++ module.recursiveModuleDeps).distinct
// dep modules ++ ivy deps ++ unmanaged
depModules.map(classes) ++
module.resolvedIvyDeps().map(_.path) ++
module.unmanagedClasspath().map(_.path)
}
val runtimeClasspath = T.task {
// dep modules ++ ivy deps ++ unmanaged
module.recursiveModuleDeps.map(classes) ++
module.resolvedRunIvyDeps().map(_.path) ++
module.unmanagedClasspath().map(_.path)
}
val resources = T.task(module.resources().map(_.path.toNIO).toList)
val platform: Task[BloopConfig.Platform] = module match {
case m: ScalaJSModule =>
T.task {
BloopConfig.Platform.Js(
BloopConfig.JsConfig.empty.copy(
version = m.scalaJSVersion(),
mode = jsLinkerMode(m)(),
kind = m.moduleKind() match {
case ModuleKind.NoModule => Config.ModuleKindJS.NoModule
case ModuleKind.CommonJSModule =>
Config.ModuleKindJS.CommonJSModule
case ModuleKind.ESModule => Config.ModuleKindJS.ESModule
},
emitSourceMaps = m.jsEnvConfig() match {
case c: JsEnvConfig.NodeJs => c.sourceMap
case _ => false
},
jsdom = Some(false)
),
mainClass = module.mainClass()
)
}
case m: ScalaNativeModule =>
T.task {
BloopConfig.Platform.Native(
BloopConfig.NativeConfig.empty.copy(
version = m.scalaNativeVersion(),
mode = m.releaseMode() match {
case ReleaseMode.Debug => BloopConfig.LinkerMode.Debug
case ReleaseMode.ReleaseFast => BloopConfig.LinkerMode.Release
case ReleaseMode.ReleaseFull => BloopConfig.LinkerMode.Release
case ReleaseMode.ReleaseSize => BloopConfig.LinkerMode.Release
},
gc = m.nativeGC(),
targetTriple = m.nativeTarget(),
clang = m.nativeClang().toNIO,
clangpp = m.nativeClangPP().toNIO,
options = Config.NativeOptions(
m.nativeLinkingOptions().toList,
m.nativeCompileOptions().toList
),
linkStubs = m.nativeLinkStubs()
),
mainClass = module.mainClass()
)
}
case _ =>
T.task {
BloopConfig.Platform.Jvm(
BloopConfig.JvmConfig(
home = T.env.get("JAVA_HOME").map(s => os.Path(s).toNIO),
options = {
// See https://github.com/scalacenter/bloop/issues/1167
val forkArgs = module.forkArgs().toList
if (forkArgs.exists(_.startsWith("-Duser.dir="))) forkArgs
else s"-Duser.dir=$wd" :: forkArgs
}
),
mainClass = module.mainClass(),
runtimeConfig = None,
classpath = Some(runtimeClasspath().map(_.toNIO).toList),
resources = Some(module.resources().map(_.path.toNIO).toList)
)
}
}
// //////////////////////////////////////////////////////////////////////////
// Tests
// //////////////////////////////////////////////////////////////////////////
val testConfig = module match {
case m: TestModule =>
T.task {
Some(
BloopConfig.Test(
frameworks = Seq(m.testFramework())
.map(f => Config.TestFramework(List(f)))
.toList,
options = Config.TestOptions(
excludes = List(),
arguments = List()
)
)
)
}
case _ => T.task(None)
}
// //////////////////////////////////////////////////////////////////////////
// Ivy dependencies + sources
// //////////////////////////////////////////////////////////////////////////
/**
* Resolves artifacts using coursier and creates the corresponding
* bloop config.
*/
def artifacts(
repos: Seq[coursier.Repository],
deps: Seq[coursier.Dependency]
): List[BloopConfig.Module] = {
import coursier._
import coursier.util._
def source(r: Resolution) = Resolution(
r.dependencies
.map(d =>
d.withAttributes(
d.attributes.withClassifier(coursier.Classifier("sources"))
)
)
.toSeq
)
import scala.concurrent.ExecutionContext.Implicits.global
val unresolved = Resolution(deps)
val fetch =
ResolutionProcess.fetch(repos, coursier.cache.Cache.default.fetch)
val gatherTask =
for {
resolved <- unresolved.process.run(fetch)
resolvedSources <- source(resolved).process.run(fetch)
all = resolved.dependencyArtifacts() ++ resolvedSources.dependencyArtifacts()
gathered <- Gather[Task].gather(all.distinct.map {
case (dep, pub, art) =>
coursier.cache.Cache.default.file(art).run.map(dep -> _)
})
} yield gathered
.collect {
case (dep, Right(file)) if os.Path(file).ext == "jar" =>
(
dep.module.organization,
dep.module.name,
dep.version,
Option(dep.attributes.classifier).filter(_.nonEmpty),
file
)
}
.groupBy {
case (org, mod, version, _, _) => (org, mod, version)
}
.mapValues {
_.map {
case (_, mod, _, classifier, file) =>
BloopConfig.Artifact(mod.value, classifier.map(_.value), None, file.toPath)
}.toList
}
.map {
case ((org, mod, version), artifacts) =>
BloopConfig.Module(
organization = org.value,
name = mod.value,
version = version,
configurations = None,
artifacts = artifacts
)
}
gatherTask.unsafeRun().toList
}
val bloopResolution: Task[BloopConfig.Resolution] = T.task {
val repos = module.repositoriesTask()
// same as input of resolvedIvyDeps
val allIvyDeps = module.transitiveIvyDeps() ++ module.transitiveCompileIvyDeps()
val coursierDeps = allIvyDeps.map(_.dep).toList
BloopConfig.Resolution(artifacts(repos, coursierDeps))
}
// //////////////////////////////////////////////////////////////////////////
// Tying up
// //////////////////////////////////////////////////////////////////////////
val project = T.task {
val mSources = moduleSourceMap()
.get(name(module))
.toSeq
.flatten
.map(_.toNIO)
.toList
val tags =
module match { case _: TestModule => List(BloopTag.Test); case _ => List(BloopTag.Library) }
BloopConfig.Project(
name = name(module),
directory = module.millSourcePath.toNIO,
workspaceDir = Some(wd.toNIO),
sources = mSources,
sourcesGlobs = None,
sourceRoots = None,
dependencies =
(module.moduleDepsChecked ++ module.compileModuleDepsChecked).map(name).toList,
classpath = classpath().map(_.toNIO).toList,
out = out(module).toNIO,
classesDir = classes(module).toNIO,
resources = Some(resources()),
`scala` = scalaConfig(),
java = javaConfig(),
sbt = None,
test = testConfig(),
platform = Some(platform()),
resolution = Some(bloopResolution()),
tags = Some(tags),
sourceGenerators = None // TODO: are we supposed to hook generated sources here?
)
}
T.task {
BloopConfig.File(
version = BloopConfig.File.LatestVersion,
project = project()
)
}
}
lazy val millDiscover: Discover[this.type] = Discover[this.type]
}