summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbenjaminfrank <ben+github@pipsfrank.de>2018-08-16 21:28:43 +0200
committerGitHub <noreply@github.com>2018-08-16 21:28:43 +0200
commitd4de5f9646cb2005e79dd8f64ac20c66fa2baeda (patch)
tree2a7cddd22a0e868b577e19e21f59d46eef190578
parentb9b133610f14cc7207e1a3efad43a06a1521b92a (diff)
downloadmill-d4de5f9646cb2005e79dd8f64ac20c66fa2baeda.tar.gz
mill-d4de5f9646cb2005e79dd8f64ac20c66fa2baeda.tar.bz2
mill-d4de5f9646cb2005e79dd8f64ac20c66fa2baeda.zip
Simple BuildInfo plugin (#325)
* Simple BuildInfo plugin * BuildInfo readme include other known yet external plugins * Add tests
-rwxr-xr-xbuild.sc10
-rw-r--r--contrib/buildinfo/src/mill/contrib/BuildInfo.scala45
-rw-r--r--contrib/buildinfo/test/resources/buildinfo/src/Main.scala9
-rw-r--r--contrib/buildinfo/test/src/mill/contrib/BuildInfoTests.scala112
-rw-r--r--docs/pages/9 - Contrib Modules.md58
5 files changed, 234 insertions, 0 deletions
diff --git a/build.sc b/build.sc
index 27914803..d9b357f7 100755
--- a/build.sc
+++ b/build.sc
@@ -238,6 +238,16 @@ object contrib extends MillModule {
def moduleDeps = Seq(scalalib)
}
+ object buildinfo extends MillModule {
+ def moduleDeps = Seq(scalalib)
+ // why do I need this?
+ def testArgs = T{
+ Seq("-Djna.nosys=true") ++
+ scalalib.worker.testArgs() ++
+ scalalib.backgroundwrapper.testArgs()
+ }
+ }
+
}
diff --git a/contrib/buildinfo/src/mill/contrib/BuildInfo.scala b/contrib/buildinfo/src/mill/contrib/BuildInfo.scala
new file mode 100644
index 00000000..0eb94a6d
--- /dev/null
+++ b/contrib/buildinfo/src/mill/contrib/BuildInfo.scala
@@ -0,0 +1,45 @@
+package mill.contrib
+
+import ammonite.ops.write
+import mill.T
+import mill.define.Target
+import mill.eval.PathRef
+import mill.scalalib.ScalaModule
+import mill.util.Ctx
+
+trait BuildInfo extends ScalaModule {
+
+ def buildInfoPackageName: Option[String] = None
+
+ def buildInfoObjectName: String = "BuildInfo"
+
+ def buildInfoMembers: T[Map[String, String]] = T {
+ Map.empty[String, String]
+ }
+
+ private def generateBuildInfo(members: Map[String, Any])(implicit dest: Ctx.Dest): Seq[PathRef] =
+ if(!members.isEmpty){
+ val outputFile = dest.dest / "BuildInfo.scala"
+ val internalMembers =
+ members
+ .map {
+ case (name, value) => s""" def ${name} = "${value}""""
+ }
+ .mkString("\n")
+ write(outputFile,
+ s"""|${buildInfoPackageName.map(p => s"package ${p}").getOrElse("")}
+ |object ${buildInfoObjectName} {
+ |$internalMembers
+ |}""".stripMargin)
+ Seq(PathRef(outputFile))
+ } else {
+ Seq.empty[PathRef]
+ }
+
+ def buildInfo = T {
+ generateBuildInfo(buildInfoMembers())
+ }
+
+ override def generatedSources: Target[Seq[PathRef]] = super.generatedSources() ++ buildInfo()
+
+}
diff --git a/contrib/buildinfo/test/resources/buildinfo/src/Main.scala b/contrib/buildinfo/test/resources/buildinfo/src/Main.scala
new file mode 100644
index 00000000..3d672668
--- /dev/null
+++ b/contrib/buildinfo/test/resources/buildinfo/src/Main.scala
@@ -0,0 +1,9 @@
+import java.nio.file.{Files, Paths}
+
+object Main extends App {
+
+ val resultPath = Paths.get(args(0))
+ Files.createDirectories(resultPath.getParent)
+ Files.write(resultPath, BuildInfo.scalaVersion.getBytes)
+
+}
diff --git a/contrib/buildinfo/test/src/mill/contrib/BuildInfoTests.scala b/contrib/buildinfo/test/src/mill/contrib/BuildInfoTests.scala
new file mode 100644
index 00000000..9e1549de
--- /dev/null
+++ b/contrib/buildinfo/test/src/mill/contrib/BuildInfoTests.scala
@@ -0,0 +1,112 @@
+package mill.contrib
+
+import ammonite.ops._
+import java.util.jar.JarFile
+import mill._
+import mill.define.Target
+import mill.eval.Result._
+import mill.eval.{Evaluator, Result}
+import mill.modules.Assembly
+import mill.scalalib.publish.VersionControl
+import mill.scalalib.publish._
+import mill.util.{TestEvaluator, TestUtil}
+import scala.collection.JavaConverters._
+import utest._
+import utest.framework.TestPath
+
+
+object BuildInfoTests extends TestSuite {
+
+ val scalaVersionString = "2.12.4"
+ trait BuildInfoModule extends TestUtil.BaseModule with scalalib.ScalaModule with BuildInfo {
+ def millSourcePath = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
+ def scalaVersion = scalaVersionString
+ }
+
+ object EmptyBuildInfo extends BuildInfoModule
+
+ object BuildInfo extends BuildInfoModule {
+ def buildInfoMembers=T{
+ Map(
+ "scalaVersion" -> scalaVersion(),
+ )
+ }
+ }
+
+ object BuildInfoSettings extends BuildInfoModule {
+ def buildInfoPackageName = Some("foo")
+ def buildInfoObjectName = "bar"
+ def buildInfoMembers=T{
+ Map(
+ "scalaVersion" -> scalaVersion()
+ )
+ }
+ }
+
+ val resourcePath = pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo"
+
+ def workspaceTest[T, M <: TestUtil.BaseModule](m: M, resourcePath: Path = resourcePath)
+ (t: TestEvaluator[M] => T)
+ (implicit tp: TestPath): T = {
+ val eval = new TestEvaluator(m)
+ rm(m.millSourcePath)
+ rm(eval.outPath)
+ mkdir(m.millSourcePath / up)
+ cp(resourcePath, m.millSourcePath)
+ t(eval)
+ }
+
+ def tests: Tests = Tests {
+
+ 'buildinfo - {
+ 'createSourcefile - workspaceTest(BuildInfo){ eval =>
+ val expected =
+ s"""|
+ |object BuildInfo {
+ | def scalaVersion = "2.12.4"
+ |}""".stripMargin
+ val Right((result, evalCount)) = eval.apply(BuildInfo.buildInfo)
+ assert(
+ result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" &&
+ exists(result.head.path) &&
+ read! result.head.path == expected
+ )
+ }
+
+ 'notCreateEmptySourcefile - workspaceTest(EmptyBuildInfo){ eval =>
+ val Right((result, evalCount)) = eval.apply(EmptyBuildInfo.buildInfo)
+ assert(
+ result.isEmpty &&
+ !exists(eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala")
+ )
+ }
+
+ 'supportCustomSettings - workspaceTest(BuildInfoSettings){ eval =>
+ val expected =
+ s"""|package foo
+ |object bar {
+ | def scalaVersion = "2.12.4"
+ |}""".stripMargin
+ val Right((result, evalCount)) = eval.apply(BuildInfoSettings.buildInfo)
+ assert(
+ result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" &&
+ exists(result.head.path) &&
+ read! result.head.path == expected
+ )
+ }
+
+ 'compile - workspaceTest(BuildInfo){ eval =>
+ val Right((result, evalCount)) = eval.apply(BuildInfo.compile)
+ assert(true)
+ }
+
+ 'run - workspaceTest(BuildInfo){ eval =>
+ val runResult = eval.outPath / "hello-mill"
+ val Right((result, evalCount)) = eval.apply(BuildInfo.run(runResult.toString))
+ assert(
+ exists(runResult),
+ read(runResult) == scalaVersionString)
+ }
+ }
+ }
+}
diff --git a/docs/pages/9 - Contrib Modules.md b/docs/pages/9 - Contrib Modules.md
index 0360cc75..c49f6238 100644
--- a/docs/pages/9 - Contrib Modules.md
+++ b/docs/pages/9 - Contrib Modules.md
@@ -49,3 +49,61 @@ object example extends ScalaPBModule {
override def scalaPBOptions = "flat_package,java_conversions"
}
```
+
+### BuildInfo
+
+Generate scala code from your buildfile.
+This plugin generates a single object containing information from your build.
+
+To declare a module that uses BuildInfo you must extend the `mill.contrib.BuildInfo` trait when defining your module.
+
+Quickstart:
+ ```scala
+ object project extends BuildInfo {
+ val name = "poject-name"
+ def buildInfoMembers: T[Map[String, String]] = T {
+ Map(
+ "name" -> name),
+ "scalaVersion" -> scalaVersion()
+ )
+ }
+ }
+ ```
+
+#### Configuration options
+
+* `def buildInfoMembers: T[Map[String, String]]`
+ The map containing all member names and values for the generated info object.
+
+* `def buildInfoObjectName: String`, default: `BuildInfo`
+ The name of the object which contains all the members from `buildInfoMembers`.
+
+* `def buildInfoPackageName: Option[String]`, default: `None`
+ The package name of the object.
+
+
+### Other Mill Plugins
+
+- [ensime](https://github.com/yyadavalli/mill-ensime "mill-ensime")
+
+ Create an [.ensime](http://ensime.github.io/ "ensime") file for your build.
+
+ Quickstart:
+ ```scala
+ import $ivy.`fun.valycorp::mill-ensime:0.0.1`
+ ```
+ ```sh
+ sh> mill fun.valycorp.mill.GenEnsime/ensimeConfig
+ ```
+
+- [dgraph](https://github.com/ajrnz/mill-dgraph "mill-dgraph")
+
+ Show transitive dependencies of your build in your browser.
+
+ Quickstart:
+ ```scala
+ import $ivy.`com.github.ajrnz::mill-dgraph:0.2.0`
+ ```
+ ```sh
+ sh> mill plugin.dgraph.browseDeps(proj)()
+ ```