Skip to content

Commit

Permalink
Add simple way to use Scala 2 deps in a Dotty project
Browse files Browse the repository at this point in the history
This is similar to the withDottyCompat method in the sbt-dotty plugin.
  • Loading branch information
smarter committed Aug 5, 2018
1 parent 334617e commit a6b7886
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
31 changes: 31 additions & 0 deletions scalalib/src/mill/scalalib/Dep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import upickle.default.{macroRW, ReadWriter => RW}
import CrossVersion._

case class Dep(dep: coursier.Dependency, cross: CrossVersion, force: Boolean) {
import Dep.isDotty

def artifactName(binaryVersion: String, fullVersion: String, platformSuffix: String) = {
val suffix = cross.suffixString(binaryVersion, fullVersion, platformSuffix)
dep.module.name + suffix
Expand All @@ -17,12 +19,41 @@ case class Dep(dep: coursier.Dependency, cross: CrossVersion, force: Boolean) {
def toDependency(binaryVersion: String, fullVersion: String, platformSuffix: String) =
dep.copy(module = dep.module.copy(name = artifactName(binaryVersion, fullVersion, platformSuffix)))
def withConfiguration(configuration: String): Dep = copy(dep = dep.copy(configuration = configuration))

/**
* If scalaVersion is a Dotty version, replace the cross-version suffix
* by the Scala 2.x version that the Dotty version is retro-compatible with,
* otherwise do nothing.
*
* This setting is useful when your build contains dependencies that have only
* been published with Scala 2.x, if you have:
* {{{
* def ivyDeps = Agg(ivy"a::b:c")
* }}}
* you can replace it by:
* {{{
* def ivyDeps = Agg(ivy"a::b:c".withDottyCompat(scalaVersion()))
* }}}
* This will have no effect when compiling with Scala 2.x, but when compiling
* with Dotty this will change the cross-version to a Scala 2.x one. This
* works because Dotty is currently retro-compatible with Scala 2.x.
*/
def withDottyCompat(scalaVersion: String): Dep =
cross match {
case cross: Binary if isDotty(scalaVersion) =>
copy(cross = Constant(value = "_2.12", platformed = cross.platformed))
case _ =>
this
}
}

object Dep {

val DefaultConfiguration = "default(compile)"

def isDotty(scalaVersion: String) =
scalaVersion.startsWith("0.")

implicit def parse(signature: String): Dep = {
val parts = signature.split(';')
val module = parts.head
Expand Down
4 changes: 1 addition & 3 deletions scalalib/src/mill/scalalib/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import javax.tools.ToolProvider
import ammonite.ops._
import ammonite.util.Util
import coursier.{Cache, Dependency, Fetch, Repository, Resolution}
import Dep.isDotty
import mill.Agg
import mill.eval.{PathRef, Result}
import mill.modules.Jvm
Expand Down Expand Up @@ -67,9 +68,6 @@ object Lib{
}
}

def isDotty(scalaVersion: String) =
scalaVersion.startsWith("0.")

def grepJar(classPath: Agg[Path], name: String, version: String) = {
val mavenStylePath = s"$name-$version.jar"
val ivyStylePath = s"$version/$name.jar"
Expand Down
1 change: 1 addition & 0 deletions scalalib/src/mill/scalalib/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import mill.define.TaskModule
import mill.eval.{PathRef, Result}
import mill.modules.Jvm
import mill.modules.Jvm.{createJar, subprocess}
import Dep.isDotty
import Lib._
import mill.util.Loose.Agg
import mill.util.DummyInputStream
Expand Down
4 changes: 4 additions & 0 deletions scalalib/test/resources/hello-dotty/foo/src/Main.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cats._, cats.data._, cats.implicits._

trait Context

object Main {
Expand All @@ -7,6 +9,8 @@ object Main {
}

def main(args: Array[String]): Unit = {
val x = Applicative[List].pure(1)
assert(x == List(1))
val value = foo(implicit x => x + 1)
assert(value == 2)
}
Expand Down
1 change: 1 addition & 0 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ object HelloWorldTests extends TestSuite {
object HelloDotty extends HelloBase{
object foo extends ScalaModule {
def scalaVersion = "0.9.0-RC1"
def ivyDeps = Agg(ivy"org.typelevel::cats-core:1.2.0".withDottyCompat(scalaVersion()))
}
}

Expand Down
3 changes: 2 additions & 1 deletion scalalib/worker/src/mill/scalalib/worker/ScalaWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import mill.Agg
import mill.eval.PathRef
import mill.scalalib.{CompilationResult, Lib, TestRunner}
import xsbti.compile.{CompilerCache => _, FileAnalysisStore => _, ScalaInstance => _, _}
import mill.scalalib.Lib.{grepJar, isDotty, scalaBinaryVersion}
import mill.scalalib.Dep.isDotty
import mill.scalalib.Lib.{grepJar, scalaBinaryVersion}
import mill.util.{Ctx, PrintLogger}
import sbt.internal.inc._
import sbt.internal.util.{ConsoleOut, MainAppender}
Expand Down

0 comments on commit a6b7886

Please sign in to comment.