Skip to content

Commit

Permalink
Let prepareOffline accept a hint parameter
Browse files Browse the repository at this point in the history
This should support more fine-control in inheriting modules.

Also, fetch only essential dependency in Java and Scala module,
and keep the full fetch behint hints: all, source, ...
  • Loading branch information
lefou committed Dec 8, 2021
1 parent 82ec1de commit 0cdd2ac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
31 changes: 25 additions & 6 deletions scalalib/src/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,32 @@ trait JavaModule

def forkWorkingDir: Target[Path] = T { os.pwd }

/**
* @param hint "all" - fetch all dependencies,
* "source" | "sources" - fetch source dependencies
*/
@nowarn("msg=pure expression does nothing")
override def prepareOffline(): Command[Unit] = T.command {
super.prepareOffline()()
resolvedIvyDeps()
zincWorker.prepareOffline()()
resolvedRunIvyDeps()
()
override def prepareOffline(hint: String*): Command[Unit] = {
val withSources = hint.contains("all") || hint.contains("source") || hint.contains("sources")
val tasks = Seq(
if (withSources) Seq(
resolveDeps(
T.task {
transitiveCompileIvyDeps() ++ transitiveIvyDeps()
},
sources = true
)
)
else Seq()
).flatten
T.command {
super.prepareOffline(hint: _*)()
resolvedIvyDeps()
zincWorker.prepareOffline()()
resolvedRunIvyDeps()
T.sequence(tasks)()
()
}
}

@internal
Expand Down
67 changes: 40 additions & 27 deletions scalalib/src/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@ package mill
package scalalib

import scala.annotation.nowarn

import mill.define.{Command, Sources, Target, Task}
import mill.api.{DummyInputStream, PathRef, Result, internal}
import mill.modules.Jvm
import mill.modules.Jvm.createJar
import mill.scalalib.api.Util.{
isDotty,
isDottyOrScala3,
isScala3,
isScala3Milestone,
scalaBinaryVersion
}
import Lib._
import ch.epfl.scala.bsp4j.{BuildTargetDataKind, ScalaBuildTarget, ScalaPlatform}
import mill.api.Loose.Agg
import mill.define.Segment.Label
import mill.eval.{Evaluator, EvaluatorPathsResolver}
import mill.scalalib.api.CompilationResult
import mill.scalalib.api.{CompilationResult, ZincWorkerUtil}
import mill.scalalib.bsp.{BspBuildTarget, BspModule}
import os.SubPath

import scala.jdk.CollectionConverters._

/**
Expand All @@ -46,7 +37,7 @@ trait ScalaModule extends JavaModule { outer =>
* @return
*/
def scalaOrganization: T[String] = T {
if (isDotty(scalaVersion()))
if (ZincWorkerUtil.isDotty(scalaVersion()))
"ch.epfl.lamp"
else
"org.scala-lang"
Expand Down Expand Up @@ -76,9 +67,9 @@ trait ScalaModule extends JavaModule { outer =>
override def mapDependencies: Task[coursier.Dependency => coursier.Dependency] = T.task {
d: coursier.Dependency =>
val artifacts =
if (isDotty(scalaVersion()))
if (ZincWorkerUtil.isDotty(scalaVersion()))
Set("dotty-library", "dotty-compiler")
else if (isScala3(scalaVersion()))
else if (ZincWorkerUtil.isScala3(scalaVersion()))
Set("scala3-library", "scala3-compiler")
else
Set("scala-library", "scala-compiler", "scala-reflect")
Expand Down Expand Up @@ -134,7 +125,7 @@ trait ScalaModule extends JavaModule { outer =>

def scalaDocOptions: T[Seq[String]] = T {
val defaults =
if (isDottyOrScala3(scalaVersion()))
if (ZincWorkerUtil.isDottyOrScala3(scalaVersion()))
Seq(
"-project",
artifactName()
Expand Down Expand Up @@ -226,7 +217,9 @@ trait ScalaModule extends JavaModule { outer =>

override def docSources: Sources = T.sources {
// Scaladoc 3.0.0 is consuming tasty files
if (isScala3(scalaVersion()) && !isScala3Milestone(scalaVersion())) Seq(compile().classes)
if (
ZincWorkerUtil.isScala3(scalaVersion()) && !ZincWorkerUtil.isScala3Milestone(scalaVersion())
) Seq(compile().classes)
else allSources()
}

Expand Down Expand Up @@ -260,7 +253,9 @@ trait ScalaModule extends JavaModule { outer =>
}
}

if (isDotty(scalaVersion()) || isScala3Milestone(scalaVersion())) { // dottydoc
if (
ZincWorkerUtil.isDotty(scalaVersion()) || ZincWorkerUtil.isScala3Milestone(scalaVersion())
) { // dottydoc
val javadocDir = T.dest / "javadoc"
os.makeDir.all(javadocDir)

Expand Down Expand Up @@ -288,7 +283,7 @@ trait ScalaModule extends JavaModule { outer =>
javadocDir / "_site"
)

} else if (isScala3(scalaVersion())) { // scaladoc 3
} else if (ZincWorkerUtil.isScala3(scalaVersion())) { // scaladoc 3
val javadocDir = T.dest / "javadoc"
os.makeDir.all(javadocDir)

Expand Down Expand Up @@ -357,7 +352,7 @@ trait ScalaModule extends JavaModule { outer =>
} else {
Jvm.runSubprocess(
mainClass =
if (isDottyOrScala3(scalaVersion()))
if (ZincWorkerUtil.isDottyOrScala3(scalaVersion()))
"dotty.tools.repl.Main"
else
"scala.tools.nsc.MainGenericRunner",
Expand Down Expand Up @@ -438,7 +433,7 @@ trait ScalaModule extends JavaModule { outer =>
*/
def artifactScalaVersion: T[String] = T {
if (crossFullScalaVersion()) scalaVersion()
else mill.scalalib.api.Util.scalaBinaryVersion(scalaVersion())
else ZincWorkerUtil.scalaBinaryVersion(scalaVersion())
}

/**
Expand All @@ -448,13 +443,31 @@ trait ScalaModule extends JavaModule { outer =>

override def artifactId: T[String] = artifactName() + artifactSuffix()

/**
* @param hint "all" - fetch all dependencies,
* "ammonite" - fetch ammonite dependencies,
* "compiler" - fetch all compiler dependencies
*/
@nowarn("msg=pure expression does nothing")
override def prepareOffline(): Command[Unit] = T.command {
super.prepareOffline()()
resolveDeps(scalacPluginIvyDeps)()
resolveDeps(scalaDocPluginIvyDeps)()
resolvedAmmoniteReplIvyDeps()
()
override def prepareOffline(hint: String*): Command[Unit] = {
val withAmmonite = hint.contains("all") || hint.contains("ammonite")
val withCompiler = hint.contains("all") || hint.contains("compiler")

val tasks = Seq(
if (withAmmonite) Seq(resolvedAmmoniteReplIvyDeps) else Seq(),
if (withCompiler) Seq(T.task {
zincWorker.scalaCompilerBridgeJar(scalaVersion(), scalaOrganization(), repositoriesTask())
})
else Seq()
).flatten

T.command {
super.prepareOffline(hint: _*)()
resolveDeps(scalacPluginIvyDeps)()
resolveDeps(scalaDocPluginIvyDeps)()
T.sequence(tasks)()
()
}
}

override def manifest: T[Jvm.JarManifest] = T {
Expand All @@ -475,7 +488,7 @@ trait ScalaModule extends JavaModule { outer =>
new ScalaBuildTarget(
scalaOrganization(),
scalaVersion(),
scalaBinaryVersion(scalaVersion()),
ZincWorkerUtil.scalaBinaryVersion(scalaVersion()),
ScalaPlatform.JVM,
scalaCompilerClasspath().map(_.path.toNIO.toUri.toString).iterator.toSeq.asJava
)
Expand Down
6 changes: 3 additions & 3 deletions scalalib/src/ZincWorkerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ trait ZincWorkerModule extends mill.Module with OfflineSupportModule { self: Cou
}

@nowarn("msg=pure expression does nothing")
override def prepareOffline(): Command[Unit] =
override def prepareOffline(hint: String*): Command[Unit] = {
T.command {
super.prepareOffline()()
super.prepareOffline(hint: _*)()
classpath()
// worker()
()
}
}

@nowarn("msg=pure expression does nothing")
def prepareOfflineCompiler(scalaVersion: String, scalaOrganization: String): Command[Unit] =
Expand Down
2 changes: 1 addition & 1 deletion scalalib/src/mill/scalalib/OfflineSupportModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait OfflineSupportModule extends mill.Module {
/**
* Prepare the module for working offline. This should typically fetch (missing) resources like ivy dependencies.
*/
def prepareOffline(): Command[Unit] = T.command {
def prepareOffline(hint: String*): Command[Unit] = T.command {
// nothing to do
}

Expand Down

0 comments on commit 0cdd2ac

Please sign in to comment.