Skip to content

Commit

Permalink
Add support for Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliykmetyuk committed Oct 2, 2020
1 parent 375f4e8 commit 1f765ee
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
15 changes: 9 additions & 6 deletions scalalib/api/src/ZincWorkerApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ object CompilationResult {
case class CompilationResult(analysisFile: os.Path, classes: PathRef)

object Util {
def isDotty(scalaVersion: String) =
scalaVersion.startsWith("0.") ||
scalaVersion.startsWith("3.")
def isDotty0(scalaVersion: String) = scalaVersion.startsWith("0.")
def isDotty3(scalaVersion: String) = scalaVersion.startsWith("3.")
def isDotty(scalaVersion: String) = isDotty0(scalaVersion) || isDotty3(scalaVersion)

// eg, grepJar(classPath, name = "scala-library", versionPrefix = "2.13.")
// return first path in `classPath` that match:
Expand Down Expand Up @@ -82,15 +82,17 @@ object Util {
val PartialVersion = raw"""(\d+)\.(\d+)\.*""".r
val ReleaseVersion = raw"""(\d+)\.(\d+)\.(\d+)""".r
val MinorSnapshotVersion = raw"""(\d+)\.(\d+)\.([1-9]\d*)-SNAPSHOT""".r
val DottyVersion = raw"""(0|3)\.(\d+)\.(\d+).*""".r
val DottyVersion = raw"""0\.(\d+)\.(\d+).*""".r
val Scala3Version = raw"""3\.(\d+)\.(\d+)-(\w+).*""".r
val DottyNightlyVersion = raw"""(0|3)\.(\d+)\.(\d+)-bin-(.*)-NIGHTLY""".r
val TypelevelVersion = raw"""(\d+)\.(\d+)\.(\d+)-bin-typelevel.*""".r


def scalaBinaryVersion(scalaVersion: String) = scalaVersion match {
case ReleaseVersion(major, minor, _) => s"$major.$minor"
case MinorSnapshotVersion(major, minor, _) => s"$major.$minor"
case DottyVersion("0", minor, _) => s"0.$minor"
case DottyVersion(minor, _) => s"0.$minor"
case Scala3Version(minor, patch, milestone) => s"3.$minor.$patch-$milestone"
case TypelevelVersion(major, minor, _) => s"$major.$minor"
case _ => scalaVersion
}
Expand Down Expand Up @@ -140,7 +142,8 @@ object Util {
/** @return true if the compiler bridge can be downloaded as an already compiled jar */
def isBinaryBridgeAvailable(scalaVersion: String) = scalaVersion match {
case DottyNightlyVersion(major, minor, _, _) => major.toInt > 0 || minor.toInt >= 14 // 0.14.0-bin or more (not 0.13.0-bin)
case DottyVersion(major, minor, _) => major.toInt > 0 || minor.toInt >= 13 // 0.13.0-RC1 or more
case DottyVersion(minor, _) => minor.toInt >= 13 // 0.13.0-RC1 or more
case Scala3Version(_, _, _) => true
case _ => false
}
}
4 changes: 2 additions & 2 deletions scalalib/src/Dep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import upickle.default.{macroRW, ReadWriter => RW}
import CrossVersion._

case class Dep(dep: coursier.Dependency, cross: CrossVersion, force: Boolean) {
import mill.scalalib.api.Util.{isDotty, DottyVersion}
import mill.scalalib.api.Util.{isDotty, DottyVersion, Scala3Version}

def artifactName(binaryVersion: String, fullVersion: String, platformSuffix: String) = {
val suffix = cross.suffixString(binaryVersion, fullVersion, platformSuffix)
Expand Down Expand Up @@ -57,7 +57,7 @@ case class Dep(dep: coursier.Dependency, cross: CrossVersion, force: Boolean) {
case cross: Binary if isDotty(scalaVersion) =>
val compatSuffix =
scalaVersion match {
case DottyVersion("3", _) =>
case Scala3Version(_, _, _) =>
"_2.13"
case DottyVersion("0", minor, patch) =>
if (minor.toInt > 18 || minor.toInt == 18 && patch.toInt >= 1)
Expand Down
20 changes: 17 additions & 3 deletions scalalib/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,45 @@ object Lib{
)
}
def scalaCompilerIvyDeps(scalaOrganization: String, scalaVersion: String) =
if (mill.scalalib.api.Util.isDotty(scalaVersion))
if (mill.scalalib.api.Util.isDotty0(scalaVersion))
Agg(
ivy"$scalaOrganization::dotty-compiler:$scalaVersion".forceVersion()
)
else if (mill.scalalib.api.Util.isDotty3(scalaVersion))
Agg(
ivy"$scalaOrganization::scala3-compiler:$scalaVersion".forceVersion()
)
else
Agg(
ivy"$scalaOrganization:scala-compiler:$scalaVersion".forceVersion(),
ivy"$scalaOrganization:scala-reflect:$scalaVersion".forceVersion()
)

def scalaDocIvyDeps(scalaOrganization: String, scalaVersion: String) =
if (mill.scalalib.api.Util.isDotty(scalaVersion))
if (mill.scalalib.api.Util.isDotty0(scalaVersion))
Agg(
ivy"$scalaOrganization::dotty-doc:$scalaVersion".forceVersion()
)
else if (mill.scalalib.api.Util.isDotty3(scalaVersion))
Agg(
ivy"$scalaOrganization::scala3-doc:$scalaVersion".forceVersion()
)
else
// in Scala <= 2.13, the scaladoc tool is included in the compiler
scalaCompilerIvyDeps(scalaOrganization, scalaVersion)

def scalaRuntimeIvyDeps(scalaOrganization: String, scalaVersion: String) =
if (mill.scalalib.api.Util.isDotty(scalaVersion))
if (mill.scalalib.api.Util.isDotty0(scalaVersion)) {
Agg(
// note that dotty-library has a binary version suffix, hence the :: is necessary here
ivy"$scalaOrganization::dotty-library:$scalaVersion".forceVersion()
)
}
else if (mill.scalalib.api.Util.isDotty3(scalaVersion))
Agg(
// note that dotty-library has a binary version suffix, hence the :: is necessary here
ivy"$scalaOrganization::scala3-library:$scalaVersion".forceVersion()
)
else
Agg(
ivy"$scalaOrganization:scala-library:$scalaVersion".forceVersion()
Expand Down
8 changes: 5 additions & 3 deletions scalalib/src/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mill.define.{Command, Target, Task, TaskModule}
import mill.eval.{PathRef, Result}
import mill.modules.Jvm
import mill.modules.Jvm.createJar
import mill.scalalib.api.Util.isDotty
import mill.scalalib.api.Util.{ isDotty, isDotty0, isDotty3 }
import Lib._
import mill.api.Loose.Agg
import mill.api.DummyInputStream
Expand All @@ -29,7 +29,7 @@ trait ScalaModule extends JavaModule { outer =>
* @return
*/
def scalaOrganization: T[String] = T {
if (isDotty(scalaVersion()))
if (isDotty0(scalaVersion()))
"ch.epfl.lamp"
else
"org.scala-lang"
Expand All @@ -55,8 +55,10 @@ trait ScalaModule extends JavaModule { outer =>

override def mapDependencies = T.task{ d: coursier.Dependency =>
val artifacts =
if (isDotty(scalaVersion()))
if (isDotty0(scalaVersion()))
Set("dotty-library", "dotty-compiler")
else if (isDotty3(scalaVersion()))
Set("scala3-library", "scala3-compiler")
else
Set("scala-library", "scala-compiler", "scala-reflect")
if (!artifacts(d.module.name.value)) d
Expand Down
6 changes: 4 additions & 2 deletions scalalib/src/ZincWorkerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mill.T
import mill.api.{Ctx, FixSizedCache, KeyedLockedCache}
import mill.define.{Command, Discover, ExternalModule, Worker}
import mill.scalalib.Lib.resolveDependencies
import mill.scalalib.api.Util.{isBinaryBridgeAvailable, isDotty}
import mill.scalalib.api.Util.{isBinaryBridgeAvailable, isDotty, isDotty0}
import mill.scalalib.api.ZincWorkerApi
import mill.util.JsonFormatters._

Expand Down Expand Up @@ -83,7 +83,9 @@ trait ZincWorkerModule extends mill.Module with OfflineSupportModule { self: Cou
val (bridgeDep, bridgeName, bridgeVersion) =
if (isDotty(scalaVersion0)) {
val org = scalaOrganization
val name = "dotty-sbt-bridge"
val name =
if (isDotty0(scalaVersion0)) "dotty-sbt-bridge"
else "scala3-sbt-bridge"
val version = scalaVersion
(ivy"$org:$name:$version", name, version)
} else {
Expand Down
6 changes: 4 additions & 2 deletions scalalib/worker/src/ZincWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.jar.JarFile

import mill.api.Loose.Agg
import mill.api.{BuildProblemReporter, IO, Info, KeyedLockedCache, PathRef, Problem, ProblemPosition, Severity, Warn}
import mill.scalalib.api.Util.{grepJar, isDotty, scalaBinaryVersion}
import mill.scalalib.api.Util.{grepJar, isDotty, isDotty0, isDotty3, scalaBinaryVersion}
import mill.scalalib.api.{CompilationResult, ZincWorkerApi}
import sbt.internal.inc._
import sbt.internal.util.{ConsoleAppender, ConsoleLogger, ConsoleOut, MainAppender}
Expand Down Expand Up @@ -343,8 +343,10 @@ class ZincWorkerImpl(compilerBridge: Either[

compilerCache.withCachedValue(compilersSig){
val compilerJar =
if (isDotty(scalaVersion))
if (isDotty0(scalaVersion))
grepJar(compilerClasspath, s"dotty-compiler_${scalaBinaryVersion(scalaVersion)}", scalaVersion)
else if (isDotty3(scalaVersion))
grepJar(compilerClasspath, s"scala3-compiler_${scalaBinaryVersion(scalaVersion)}", scalaVersion)
else
compilerJarNameGrep(compilerClasspath, scalaVersion)

Expand Down

0 comments on commit 1f765ee

Please sign in to comment.