From 91102940a0bcdd7efc1d13e43510c4a0e406ead2 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Fri, 12 Jan 2018 21:41:05 -0800 Subject: Break up scalalib/Module.scala --- .../src/main/scala/mill/scalalib/MiscModule.scala | 61 +++++++ scalalib/src/main/scala/mill/scalalib/Module.scala | 198 +++++---------------- .../main/scala/mill/scalalib/PublishModule.scala | 70 ++++++++ 3 files changed, 174 insertions(+), 155 deletions(-) create mode 100644 scalalib/src/main/scala/mill/scalalib/MiscModule.scala create mode 100644 scalalib/src/main/scala/mill/scalalib/PublishModule.scala diff --git a/scalalib/src/main/scala/mill/scalalib/MiscModule.scala b/scalalib/src/main/scala/mill/scalalib/MiscModule.scala new file mode 100644 index 00000000..60d9d1ce --- /dev/null +++ b/scalalib/src/main/scala/mill/scalalib/MiscModule.scala @@ -0,0 +1,61 @@ +package mill +package scalalib + +import mill.define.{Cross, Task} +import mill.eval.{PathRef, Result} +import mill.define.Cross.Resolver + + +trait SbtModule extends Module { outer => + override def sources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'scala)) } + override def resources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'resources)) } + trait Tests extends super.Tests { + override def basePath = outer.basePath + override def sources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'scala)) } + override def resources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'resources)) } + } +} + +trait CrossSbtModule extends SbtModule { outer => + override def basePath = super.basePath / ammonite.ops.up + implicit def crossSbtModuleResolver: Resolver[CrossSbtModule] = new Resolver[CrossSbtModule]{ + def resolve[V <: CrossSbtModule](c: Cross[V]): V = { + crossScalaVersion.split('.') + .inits + .takeWhile(_.length > 1) + .flatMap( prefix => + c.items.map(_._2).find(_.crossScalaVersion.split('.').startsWith(prefix)) + ) + .collectFirst{case x => x} + .getOrElse( + throw new Exception( + s"Unable to find compatible cross version between $crossScalaVersion and "+ + c.items.map(_._2.crossScalaVersion).mkString(",") + ) + ) + } + } + + def crossScalaVersion: String + def scalaVersion = crossScalaVersion + override def sources = T.input{ + super.sources() ++ + crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{ + s => PathRef{ basePath / 'src / 'main / s"scala-$s" } + } + + } + override def resources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'resources)) } + trait Tests extends super.Tests { + override def basePath = outer.basePath + override def sources = T.input{ + super.sources() ++ + crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{ + s => PathRef{ basePath / 'src / 'test / s"scala-$s" } + } + } + override def resources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'resources)) } + } +} + + diff --git a/scalalib/src/main/scala/mill/scalalib/Module.scala b/scalalib/src/main/scala/mill/scalalib/Module.scala index d3120721..d0041fb5 100644 --- a/scalalib/src/main/scala/mill/scalalib/Module.scala +++ b/scalalib/src/main/scala/mill/scalalib/Module.scala @@ -11,57 +11,10 @@ import mill.modules.Jvm.{createAssembly, createJar, interactiveSubprocess, subpr import Lib._ import mill.define.Cross.Resolver import sbt.testing.Status -object TestModule{ - def handleResults(doneMsg: String, results: Seq[TestRunner.Result]) = { - if (results.count(Set(Status.Error, Status.Failure)) == 0) Result.Success((doneMsg, results)) - else { - val grouped = results.map(_.status).groupBy(x => x).mapValues(_.length).filter(_._2 != 0).toList.sorted - - Result.Failure(grouped.map{case (k, v) => k + ": " + v}.mkString(",")) - } - } -} -trait TestModule extends Module with TaskModule { - override def defaultCommandName() = "test" - def testFramework: T[String] - - def forkWorkingDir = ammonite.ops.pwd - - def forkTest(args: String*) = T.command{ - mkdir(T.ctx().dest) - val outputPath = T.ctx().dest/"out.json" - - Jvm.subprocess( - mainClass = "mill.scalalib.TestRunner", - classPath = Jvm.gatherClassloaderJars(), - jvmOptions = forkArgs(), - options = Seq( - testFramework(), - runClasspath().map(_.path).distinct.mkString(" "), - Seq(compile().classes.path).mkString(" "), - args.mkString(" "), - outputPath.toString, - T.ctx().log.colored.toString - ), - workingDir = forkWorkingDir - ) - - val jsonOutput = upickle.json.read(outputPath.toIO) - val (doneMsg, results) = upickle.default.readJs[(String, Seq[TestRunner.Result])](jsonOutput) - TestModule.handleResults(doneMsg, results) - - } - def test(args: String*) = T.command{ - val (doneMsg, results) = TestRunner( - testFramework(), - runClasspath().map(_.path), - Seq(compile().classes.path), - args - ) - TestModule.handleResults(doneMsg, results) - } -} +/** + * Core configuration required to compile a single Scala compilation target + */ trait Module extends mill.Module with TaskModule { outer => def defaultCommandName() = "run" trait Tests extends TestModule{ @@ -303,119 +256,54 @@ trait Module extends mill.Module with TaskModule { outer => } -trait PublishModule extends Module { outer => - import mill.scalalib.publish._ - - def pomSettings: T[PomSettings] - def publishVersion: T[String] = "0.0.1-SNAPSHOT" - - def pom = T { - val dependencies = - ivyDeps().map(Artifact.fromDep(_, scalaVersion(), scalaBinaryVersion())) - val pom = Pom(artifact(), dependencies, artifactName(), pomSettings()) - - val pomPath = T.ctx().dest / s"${artifactId()}-${publishVersion()}.pom" - write.over(pomPath, pom) - PathRef(pomPath) - } - - def ivy = T { - val dependencies = - ivyDeps().map(Artifact.fromDep(_, scalaVersion(), scalaBinaryVersion())) - val ivy = Ivy(artifact(), dependencies) - val ivyPath = T.ctx().dest / "ivy.xml" - write.over(ivyPath, ivy) - PathRef(ivyPath) - } - def artifact: T[Artifact] = T { - Artifact(pomSettings().organization, artifactId(), publishVersion()) - } +object TestModule{ + def handleResults(doneMsg: String, results: Seq[TestRunner.Result]) = { + if (results.count(Set(Status.Error, Status.Failure)) == 0) Result.Success((doneMsg, results)) + else { + val grouped = results.map(_.status).groupBy(x => x).mapValues(_.length).filter(_._2 != 0).toList.sorted - def publishLocal(): define.Command[Unit] = T.command { - LocalPublisher.publish( - jar = jar().path, - sourcesJar = sourcesJar().path, - docsJar = docsJar().path, - pom = pom().path, - ivy = ivy().path, - artifact = artifact() - ) + Result.Failure(grouped.map{case (k, v) => k + ": " + v}.mkString(",")) + } } +} +trait TestModule extends Module with TaskModule { + override def defaultCommandName() = "test" + def testFramework: T[String] - def sonatypeUri: String = "https://oss.sonatype.org/service/local" + def forkWorkingDir = ammonite.ops.pwd - def sonatypeSnapshotUri: String = "https://oss.sonatype.org/content/repositories/snapshots" + def forkTest(args: String*) = T.command{ + mkdir(T.ctx().dest) + val outputPath = T.ctx().dest/"out.json" - def publish(credentials: String, gpgPassphrase: String): define.Command[Unit] = T.command { - val baseName = s"${artifactId()}-${publishVersion()}" - val artifacts = Seq( - jar().path -> s"${baseName}.jar", - sourcesJar().path -> s"${baseName}-sources.jar", - docsJar().path -> s"${baseName}-javadoc.jar", - pom().path -> s"${baseName}.pom" + Jvm.subprocess( + mainClass = "mill.scalalib.TestRunner", + classPath = Jvm.gatherClassloaderJars(), + jvmOptions = forkArgs(), + options = Seq( + testFramework(), + runClasspath().map(_.path).distinct.mkString(" "), + Seq(compile().classes.path).mkString(" "), + args.mkString(" "), + outputPath.toString, + T.ctx().log.colored.toString + ), + workingDir = forkWorkingDir ) - new SonatypePublisher( - sonatypeUri, - sonatypeSnapshotUri, - credentials, - gpgPassphrase, - T.ctx().log - ).publish(artifacts, artifact()) - } - -} - -trait SbtModule extends Module { outer => - override def sources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'scala)) } - override def resources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'resources)) } - trait Tests extends super.Tests { - override def basePath = outer.basePath - override def sources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'scala)) } - override def resources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'resources)) } - } -} - -trait CrossSbtModule extends SbtModule { outer => - override def basePath = super.basePath / ammonite.ops.up - implicit def crossSbtModuleResolver: Resolver[CrossSbtModule] = new Resolver[CrossSbtModule]{ - def resolve[V <: CrossSbtModule](c: Cross[V]): V = { - crossScalaVersion.split('.') - .inits - .takeWhile(_.length > 1) - .flatMap( prefix => - c.items.map(_._2).find(_.crossScalaVersion.split('.').startsWith(prefix)) - ) - .collectFirst{case x => x} - .getOrElse( - throw new Exception( - s"Unable to find compatible cross version between $crossScalaVersion and "+ - c.items.map(_._2.crossScalaVersion).mkString(",") - ) - ) - } - } - def crossScalaVersion: String - def scalaVersion = crossScalaVersion - override def sources = T.input{ - super.sources() ++ - crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{ - s => PathRef{ basePath / 'src / 'main / s"scala-$s" } - } + val jsonOutput = upickle.json.read(outputPath.toIO) + val (doneMsg, results) = upickle.default.readJs[(String, Seq[TestRunner.Result])](jsonOutput) + TestModule.handleResults(doneMsg, results) } - override def resources = T.input{ Seq(PathRef(basePath / 'src / 'main / 'resources)) } - trait Tests extends super.Tests { - override def basePath = outer.basePath - override def sources = T.input{ - super.sources() ++ - crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{ - s => PathRef{ basePath / 'src / 'test / s"scala-$s" } - } - } - override def resources = T.input{ Seq(PathRef(basePath / 'src / 'test / 'resources)) } + def test(args: String*) = T.command{ + val (doneMsg, results) = TestRunner( + testFramework(), + runClasspath().map(_.path), + Seq(compile().classes.path), + args + ) + TestModule.handleResults(doneMsg, results) } -} - - +} \ No newline at end of file diff --git a/scalalib/src/main/scala/mill/scalalib/PublishModule.scala b/scalalib/src/main/scala/mill/scalalib/PublishModule.scala new file mode 100644 index 00000000..1b1a0f44 --- /dev/null +++ b/scalalib/src/main/scala/mill/scalalib/PublishModule.scala @@ -0,0 +1,70 @@ +package mill +package scalalib + +import ammonite.ops._ +import mill.eval.{PathRef, Result} + +/** + * Configuration necessary for publishing a Scala module to Maven Central or similar + */ +trait PublishModule extends Module { outer => + import mill.scalalib.publish._ + + def pomSettings: T[PomSettings] + def publishVersion: T[String] = "0.0.1-SNAPSHOT" + + def pom = T { + val dependencies = + ivyDeps().map(Artifact.fromDep(_, scalaVersion(), scalaBinaryVersion())) + val pom = Pom(artifact(), dependencies, artifactName(), pomSettings()) + + val pomPath = T.ctx().dest / s"${artifactId()}-${publishVersion()}.pom" + write.over(pomPath, pom) + PathRef(pomPath) + } + + def ivy = T { + val dependencies = + ivyDeps().map(Artifact.fromDep(_, scalaVersion(), scalaBinaryVersion())) + val ivy = Ivy(artifact(), dependencies) + val ivyPath = T.ctx().dest / "ivy.xml" + write.over(ivyPath, ivy) + PathRef(ivyPath) + } + + def artifact: T[Artifact] = T { + Artifact(pomSettings().organization, artifactId(), publishVersion()) + } + + def publishLocal(): define.Command[Unit] = T.command { + LocalPublisher.publish( + jar = jar().path, + sourcesJar = sourcesJar().path, + docsJar = docsJar().path, + pom = pom().path, + ivy = ivy().path, + artifact = artifact() + ) + } + + def sonatypeUri: String = "https://oss.sonatype.org/service/local" + + def sonatypeSnapshotUri: String = "https://oss.sonatype.org/content/repositories/snapshots" + + def publish(credentials: String, gpgPassphrase: String): define.Command[Unit] = T.command { + val baseName = s"${artifactId()}-${publishVersion()}" + val artifacts = Seq( + jar().path -> s"${baseName}.jar", + sourcesJar().path -> s"${baseName}-sources.jar", + docsJar().path -> s"${baseName}-javadoc.jar", + pom().path -> s"${baseName}.pom" + ) + new SonatypePublisher( + sonatypeUri, + sonatypeSnapshotUri, + credentials, + gpgPassphrase, + T.ctx().log + ).publish(artifacts, artifact()) + } +} -- cgit v1.2.3