diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-04 03:28:14 -0700 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-04 03:28:14 -0700 |
commit | c5f3cb7fdcc610bd167460b75b22863f61d6fb4b (patch) | |
tree | d043f35276adc935dfb525835ac155651cd2bde9 /scalaplugin | |
parent | 2071c7094d7f966136a5cf6dddcb4ee8fb81b688 (diff) | |
download | mill-c5f3cb7fdcc610bd167460b75b22863f61d6fb4b.tar.gz mill-c5f3cb7fdcc610bd167460b75b22863f61d6fb4b.tar.bz2 mill-c5f3cb7fdcc610bd167460b75b22863f61d6fb4b.zip |
Implement `ScalaDep` ADT to encapsulate handling on scala cross-version suffixes
Diffstat (limited to 'scalaplugin')
-rw-r--r-- | scalaplugin/src/main/scala/forge/scalaplugin/Subproject.scala | 65 | ||||
-rw-r--r-- | scalaplugin/src/test/scala/forge/scalaplugin/MetacircularTests.scala | 16 |
2 files changed, 62 insertions, 19 deletions
diff --git a/scalaplugin/src/main/scala/forge/scalaplugin/Subproject.scala b/scalaplugin/src/main/scala/forge/scalaplugin/Subproject.scala index 6c66902c..52a58bba 100644 --- a/scalaplugin/src/main/scala/forge/scalaplugin/Subproject.scala +++ b/scalaplugin/src/main/scala/forge/scalaplugin/Subproject.scala @@ -7,6 +7,7 @@ import ammonite.ops.{Path, ls, mkdir, pwd} import coursier.{Cache, Dependency, Fetch, MavenRepository, Module, Repository, Resolution} import forge.{Target => T} import forge.util.PathRef +import play.api.libs.json._ import sbt.internal.inc.{FreshCompilerCache, ScalaInstance, ZincUtil} import sbt.internal.util.{ConsoleOut, MainAppender} import sbt.util.LogExchange @@ -85,23 +86,59 @@ object Subproject{ } def createJar(sourceDirs: T[Seq[PathRef]]) = ??? def resolveDependencies(repositories: Seq[Repository], - deps: Seq[coursier.Dependency]): Seq[PathRef] = { - val start = Resolution(deps.toSet) + scalaVersion: String, + scalaBinaryVersion: String, + deps: Seq[ScalaDep]): Seq[PathRef] = { + val flattened = deps.map{ + case ScalaDep.Java(dep) => dep + case ScalaDep.Scala(dep) => dep.copy(module = dep.module.copy(name = dep.module.name + "_" + scalaBinaryVersion)) + case ScalaDep.PointScala(dep) => dep.copy(module = dep.module.copy(name = dep.module.name + "_" + scalaVersion)) + }.toSet + val start = Resolution(flattened) + val fetch = Fetch.from(repositories, Cache.fetch()) val resolution = start.process.run(fetch).unsafePerformSync - val localArtifacts: Seq[File] = Task.gatherUnordered( - resolution.artifacts.map(Cache.file(_).run) - ).unsafePerformSync.flatMap(_.toOption) + val localArtifacts: Seq[File] = Task + .gatherUnordered(resolution.artifacts.map(Cache.file(_).run)) + .unsafePerformSync + .flatMap(_.toOption) localArtifacts.map(p => PathRef(Path(p))) } - def scalaCompilerIvyDeps(scalaVersion: String, scalaBinaryVersion: String) = Seq( + def scalaCompilerIvyDeps(scalaVersion: String) = Seq[ScalaDep]( Dependency(Module("org.scala-lang", "scala-compiler"), scalaVersion), - Dependency(Module("org.scala-sbt", s"compiler-bridge_$scalaBinaryVersion"), "1.0.3") + ScalaDep.Scala(Dependency(Module("org.scala-sbt", s"compiler-bridge"), "1.0.3")) ) - def scalaRuntimeIvyDeps(scalaVersion: String) = Seq( + def scalaRuntimeIvyDeps(scalaVersion: String) = Seq[ScalaDep]( Dependency(Module("org.scala-lang", "scala-library"), scalaVersion) ) + sealed trait ScalaDep + object ScalaDep{ + case class Java(dep: coursier.Dependency) extends ScalaDep + implicit def default(dep: coursier.Dependency): ScalaDep = new Java(dep) + case class Scala(dep: coursier.Dependency) extends ScalaDep + case class PointScala(dep: coursier.Dependency) extends ScalaDep + implicit def formatter: Format[ScalaDep] = new Format[ScalaDep]{ + def writes(o: ScalaDep) = o match{ + case Java(dep) => Json.obj("Java" -> Json.toJson(dep)) + case Scala(dep) => Json.obj("Scala" -> Json.toJson(dep)) + case PointScala(dep) => Json.obj("PointScala" -> Json.toJson(dep)) + } + + def reads(json: JsValue) = json match{ + case obj: JsObject => + obj.fields match{ + case Seq(("Java", dep)) => Json.fromJson[coursier.Dependency](dep).map(Java) + case Seq(("Scala", dep)) => Json.fromJson[coursier.Dependency](dep).map(Scala) + case Seq(("PointScala", dep)) => Json.fromJson[coursier.Dependency](dep).map(PointScala) + case _ => JsError("Invalid JSON object to parse ScalaDep") + } + + + case _ => JsError("Expected JSON object to parse ScalaDep") + } + } + } } import Subproject._ @@ -109,9 +146,9 @@ abstract class Subproject { val scalaVersion: T[String] val scalaBinaryVersion = T{ scalaVersion().split('.').dropRight(1).mkString(".") } - val ivyDeps = T{ Seq[coursier.Dependency]() } - val compileIvyDeps = T{ Seq[coursier.Dependency]() } - val runIvyDeps = T{ Seq[coursier.Dependency]() } + val ivyDeps = T{ Seq[ScalaDep]() } + val compileIvyDeps = T{ Seq[ScalaDep]() } + val runIvyDeps = T{ Seq[ScalaDep]() } val basePath: T[Path] val repositories: Seq[Repository] = Seq( @@ -123,13 +160,17 @@ abstract class Subproject { val compileDepClasspath = T[Seq[PathRef]] { depClasspath() ++ resolveDependencies( repositories, - ivyDeps() ++ compileIvyDeps() ++ scalaCompilerIvyDeps(scalaVersion(), scalaBinaryVersion()) + scalaVersion(), + scalaBinaryVersion(), + ivyDeps() ++ compileIvyDeps() ++ scalaCompilerIvyDeps(scalaVersion()) ) } val runDepClasspath = T[Seq[PathRef]] { depClasspath() ++ resolveDependencies( repositories, + scalaVersion(), + scalaBinaryVersion(), ivyDeps() ++ runIvyDeps() ++ scalaRuntimeIvyDeps(scalaVersion()) ) } diff --git a/scalaplugin/src/test/scala/forge/scalaplugin/MetacircularTests.scala b/scalaplugin/src/test/scala/forge/scalaplugin/MetacircularTests.scala index 5a4bcd68..028665c8 100644 --- a/scalaplugin/src/test/scala/forge/scalaplugin/MetacircularTests.scala +++ b/scalaplugin/src/test/scala/forge/scalaplugin/MetacircularTests.scala @@ -3,6 +3,7 @@ package scalaplugin import ammonite.ops.pwd import coursier.{Dependency => Dep, Module => Mod} +import forge.scalaplugin.Subproject.ScalaDep import forge.util.{OSet, PathRef} import utest._ @@ -10,18 +11,18 @@ object MetacircularTests extends TestSuite{ object Core extends Subproject { val scalaVersion = T{ "2.12.4" } override val compileIvyDeps = T{ - Seq( + Seq[ScalaDep]( Dep(Mod("org.scala-lang", "scala-reflect"), scalaVersion(), configuration = "provided") ) } override val ivyDeps = T{ - Seq( - Dep(Mod("com.lihaoyi", "sourcecode_" + scalaBinaryVersion()), "0.1.4"), - Dep(Mod("com.lihaoyi", "pprint_" + scalaBinaryVersion()), "0.5.3"), - Dep(Mod("com.lihaoyi", "ammonite_" + scalaVersion()), "1.0.3"), - Dep(Mod("com.typesafe.play", "play-json_" + scalaBinaryVersion()), "2.6.6"), - Dep(Mod("org.scala-sbt", "zinc_" + scalaBinaryVersion()), "1.0.3") + Seq[ScalaDep]( + ScalaDep.Scala(Dep(Mod("com.lihaoyi", "sourcecode"), "0.1.4")), + ScalaDep.Scala(Dep(Mod("com.lihaoyi", "pprint"), "0.5.3")), + ScalaDep.PointScala(Dep(Mod("com.lihaoyi", "ammonite"), "1.0.3")), + ScalaDep.Scala(Dep(Mod("com.typesafe.play", "play-json"), "2.6.6")), + ScalaDep.Scala(Dep(Mod("org.scala-sbt", "zinc"), "1.0.3")) ) } @@ -34,6 +35,7 @@ object MetacircularTests extends TestSuite{ val scalaVersion = T{ "2.12.4" } override val depClasspath = T{ Seq(Core.compiled()) } + override val ivyDeps = T{ Core.ivyDeps } val basePath = T{ pwd / 'scalaplugin } override val sources = T{ PathRef(pwd/'scalaplugin/'src/'main/'scala) } override val resources = T{ sources } |