Skip to content

Commit

Permalink
More thorough review of options processing
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Jan 23, 2022
1 parent 1af99f5 commit 5b135f1
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions scalalib/src/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package mill
package scalalib

import scala.annotation.nowarn

import mill.define.{Command, Sources, Target, Task}
import mill.api.{DummyInputStream, PathRef, Result, internal}
import mill.api.{DummyInputStream, Loose, PathRef, Result, internal}
import mill.modules.Jvm
import mill.modules.Jvm.createJar
import Lib._
Expand All @@ -13,8 +12,8 @@ import mill.api.Loose.Agg
import mill.eval.{Evaluator, EvaluatorPathsResolver}
import mill.scalalib.api.{CompilationResult, ZincWorkerUtil}
import mill.scalalib.bsp.{BspBuildTarget, BspModule}
import scala.jdk.CollectionConverters._

import scala.jdk.CollectionConverters._
import mainargs.Flag

/**
Expand Down Expand Up @@ -86,44 +85,53 @@ trait ScalaModule extends JavaModule { outer =>
publish.Artifact.fromDep(
_: Dep,
scalaVersion(),
mill.scalalib.api.Util.scalaBinaryVersion(scalaVersion()),
ZincWorkerUtil.scalaBinaryVersion(scalaVersion()),
platformSuffix()
)
}

/**
* Allows you to make use of Scala compiler plugins from maven central
*/
def scalacPluginIvyDeps = T { Agg.empty[Dep] }
def scalacPluginIvyDeps: Target[Agg[Dep]] = T { Agg.empty[Dep] }

def scalaDocPluginIvyDeps = T { scalacPluginIvyDeps() }
def scalaDocPluginIvyDeps: Target[Agg[Dep]] = T { scalacPluginIvyDeps() }

/**
* Mandatory command-line options to pass to the Scala compiler
* that shouldn't be removed by overriding `scalacOptions`
*/
protected def mandatoryScalacOptions = T { Seq.empty[String] }
protected def mandatoryScalacOptions: Target[Seq[String]] = T { Seq.empty[String] }

/**
* Scalac options to active the compiler plugins.
*/
protected def enablePluginScalacOption: Target[Seq[String]] = T {
private def enablePluginScalacOptions: Target[Seq[String]] = T {
val resolvedJars = resolveDeps(scalacPluginIvyDeps.map(_.map(_.exclude("*" -> "*"))))()
resolvedJars.iterator.toSeq.map(jar => s"-Xplugin:${jar}")
resolvedJars.iterator.map(jar => s"-Xplugin:${jar.path}").toSeq
}

/**
* Scalac options to active the compiler plugins.
*/
private def enableScalaDocPluginScalacOption: Target[Seq[String]] = T {
val resolvedJars = resolveDeps(scalaDocPluginIvyDeps.map(_.map(_.exclude("*" -> "*"))))()
resolvedJars.iterator.map(jar => s"-Xplugin:${jar.path}").toSeq
}

/**
* Command-line options to pass to the Scala compiler defined by the user.
* Consumers should use `allScalacOptions` to read them.
*/
def scalacOptions = T { Seq.empty[String] }
def scalacOptions: Target[Seq[String]] = T { Seq.empty[String] }

/**
* Aggregation of all the options passed to the Scala compiler.
* In most cases, instead of overriding this Target you want to override `scalacOptions` instead.
*/
def allScalacOptions =
T { mandatoryScalacOptions() ++ enablePluginScalacOption() ++ scalacOptions() }
def allScalacOptions: Target[Seq[String]] = T {
mandatoryScalacOptions() ++ enablePluginScalacOptions() ++ scalacOptions()
}

def scalaDocOptions: T[Seq[String]] = T {
val defaults =
Expand All @@ -133,7 +141,7 @@ trait ScalaModule extends JavaModule { outer =>
artifactName()
)
else Seq()
allScalacOptions() ++ defaults
mandatoryScalacOptions() ++ enableScalaDocPluginScalacOption() ++ scalacOptions() ++ defaults
}

/**
Expand Down Expand Up @@ -232,8 +240,6 @@ trait ScalaModule extends JavaModule { outer =>
}

override def docJar: T[PathRef] = T {
val pluginOptions =
scalaDocPluginClasspath().map(pluginPathRef => s"-Xplugin:${pluginPathRef.path}")
val compileCp = Seq(
"-classpath",
compileClasspath()
Expand All @@ -252,7 +258,7 @@ trait ScalaModule extends JavaModule { outer =>
scalaOrganization(),
scalaDocClasspath().map(_.path),
scalacPluginClasspath().map(_.path),
files ++ options ++ pluginOptions ++ compileCp ++ scalaDocOptions()
files ++ options ++ compileCp ++ scalaDocOptions()
) match {
case true =>
Result.Success(createJar(Agg(javadocDir))(T.dest))
Expand Down

0 comments on commit 5b135f1

Please sign in to comment.