diff options
author | Vitalii Lysov <vitalii.lysov@gmail.com> | 2019-04-17 07:49:57 +0200 |
---|---|---|
committer | Tobias Roeser <le.petit.fou@web.de> | 2019-04-17 07:49:57 +0200 |
commit | 32a86977bbd03137f55981f3ce4ca8e24f239649 (patch) | |
tree | 2aadf2c103e572ba02ac3fe31ed638a93138659f /contrib | |
parent | 5d7c40ad91758d753a856abd2501cf0874ad6e96 (diff) | |
download | mill-32a86977bbd03137f55981f3ce4ca8e24f239649.tar.gz mill-32a86977bbd03137f55981f3ce4ca8e24f239649.tar.bz2 mill-32a86977bbd03137f55981f3ce4ca8e24f239649.zip |
contrib BuildInfo plugin: fix generatedSources, it must be a folder (#589)
* fix BuildInfo generatedSources, it must be a folder
generatedSources must be a folder,
but till now it was a reference to a generated file
This commit fixes this issue.
There are 2 reasons for this fix:
(1) Because generatedSources must be folders by the definition:
Folders containing source files that are generated
(2) If you use intellij IDEA and build your project from the IDE,
building module, which includes BuildInfo plugin, fails.
* test: make explicit check that generatedSources is a folder
* correct T.ctx -> T.ctx()
* workaround to get `dest` property of the task into other
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/buildinfo/src/BuildInfo.scala | 36 | ||||
-rw-r--r-- | contrib/buildinfo/test/src/BuildInfoTests.scala | 54 |
2 files changed, 51 insertions, 39 deletions
diff --git a/contrib/buildinfo/src/BuildInfo.scala b/contrib/buildinfo/src/BuildInfo.scala index fe252b97..0804b9a5 100644 --- a/contrib/buildinfo/src/BuildInfo.scala +++ b/contrib/buildinfo/src/BuildInfo.scala @@ -1,8 +1,8 @@ package mill.contrib.buildinfo import mill.T -import mill.define.Target -import mill.api.{Ctx, Logger, PathRef} +import mill.api.Logger +import mill.api.PathRef import mill.scalalib.ScalaModule trait BuildInfo extends ScalaModule { @@ -15,33 +15,35 @@ trait BuildInfo extends ScalaModule { Map.empty[String, String] } - private def generateBuildInfo(members: Map[String, Any])(implicit dest: Ctx.Dest, log: Ctx.Log): Seq[PathRef] = - if (!members.isEmpty) { - val outputFile = dest.dest / "BuildInfo.scala" + def generatedBuildInfo: T[(Seq[PathRef], PathRef)] = T { + val logger: Logger = T.ctx().log + val members: Map[String, String] = buildInfoMembers() + if (members.nonEmpty) { + val outputFile = T.ctx().dest / "BuildInfo.scala" val internalMembers = members .map { case (name, value) => s""" def ${name} = "${value}"""" } .mkString("\n") - log.log.debug(s"Generating object [${buildInfoPackageName.map(_ + ".").getOrElse("")}${buildInfoObjectName}] with [${members.size}] members to [${outputFile}]") + logger.debug(s"Generating object [${buildInfoPackageName.map(_ + ".").getOrElse("")}${buildInfoObjectName}] with [${members.size}] members to [${outputFile}]") os.write( outputFile, - s"""|${buildInfoPackageName.map(p => s"package ${p}").getOrElse("")} - |object ${buildInfoObjectName} { - |$internalMembers - |}""".stripMargin + s"""|${buildInfoPackageName.map(packageName => s"package ${packageName}\n").getOrElse("")} + |object ${buildInfoObjectName} { + |$internalMembers + |}""".stripMargin ) - Seq(PathRef(outputFile)) + (Seq(PathRef(outputFile)), PathRef(T.ctx().dest)) } else { - log.log.debug("No build info member defined, skipping code generation") - Seq.empty[PathRef] + logger.debug("No build info member defined, skipping code generation") + (Seq.empty[PathRef], PathRef(T.ctx().dest)) } - - def buildInfo = T { - generateBuildInfo(buildInfoMembers()) } - override def generatedSources: Target[Seq[PathRef]] = T { super.generatedSources() ++ buildInfo() } + override def generatedSources = T { + val (_, destPathRef) = generatedBuildInfo() + super.generatedSources() :+ destPathRef + } } diff --git a/contrib/buildinfo/test/src/BuildInfoTests.scala b/contrib/buildinfo/test/src/BuildInfoTests.scala index c6d0256d..9b840ed9 100644 --- a/contrib/buildinfo/test/src/BuildInfoTests.scala +++ b/contrib/buildinfo/test/src/BuildInfoTests.scala @@ -1,15 +1,12 @@ package mill.contrib.buildinfo -import java.util.jar.JarFile import mill._ +import mill.define.Sources import mill.define.Target -import mill.api.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 mill.scalalib.ScalaModule +import mill.util.TestEvaluator +import mill.util.TestUtil +import os.Path import utest._ import utest.framework.TestPath @@ -18,8 +15,9 @@ 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 + // override build root to test custom builds/modules + override def millSourcePath: Path = TestUtil.getSrcPathStatic() + override def scalaVersion = scalaVersionString } object EmptyBuildInfo extends BuildInfoModule @@ -42,16 +40,16 @@ object BuildInfoTests extends TestSuite { } } - val resourcePath = os.pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo" + val testModuleSourcesPath: Path = os.pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo" - def workspaceTest[T](m: TestUtil.BaseModule, resourcePath: os.Path = resourcePath) + def workspaceTest[T](m: TestUtil.BaseModule) (t: TestEvaluator => T) (implicit tp: TestPath): T = { val eval = new TestEvaluator(m) os.remove.all(m.millSourcePath) os.remove.all(eval.outPath) os.makeDir.all(m.millSourcePath / os.up) - os.copy(resourcePath, m.millSourcePath) + os.copy(testModuleSourcesPath, m.millSourcePath) t(eval) } @@ -59,36 +57,37 @@ object BuildInfoTests extends TestSuite { 'buildinfo - { 'createSourcefile - workspaceTest(BuildInfo){ eval => - val expected = + val expected = s"""| |object BuildInfo { | def scalaVersion = "2.12.4" |}""".stripMargin - val Right((result, evalCount)) = eval.apply(BuildInfo.buildInfo) + val Right(((result, _), evalCount)) = eval.apply(BuildInfo.generatedBuildInfo) assert( - result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" && + result.head.path == eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala" && os.exists(result.head.path) && os.read(result.head.path) == expected ) } 'notCreateEmptySourcefile - workspaceTest(EmptyBuildInfo){ eval => - val Right((result, evalCount)) = eval.apply(EmptyBuildInfo.buildInfo) + val Right(((result, _), evalCount)) = eval.apply(EmptyBuildInfo.generatedBuildInfo) assert( result.isEmpty && - !os.exists(eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala") + !os.exists(eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala") ) } 'supportCustomSettings - workspaceTest(BuildInfoSettings){ eval => - val expected = + val expected = s"""|package foo + | |object bar { | def scalaVersion = "2.12.4" |}""".stripMargin - val Right((result, evalCount)) = eval.apply(BuildInfoSettings.buildInfo) + val Right(((result, _), evalCount)) = eval.apply(BuildInfoSettings.generatedBuildInfo) assert( - result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" && + result.head.path == eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala" && os.exists(result.head.path) && os.read(result.head.path) == expected ) @@ -104,7 +103,18 @@ object BuildInfoTests extends TestSuite { val Right((result, evalCount)) = eval.apply(BuildInfo.run(runResult.toString)) assert( os.exists(runResult), - os.read(runResult) == scalaVersionString) + os.read(runResult) == scalaVersionString + ) + } + + "generatedSources must be a folder" - workspaceTest(BuildInfo) { eval => + val buildInfoGeneratedSourcesFolder = eval.outPath / 'generatedBuildInfo / 'dest + val Right((result, evalCount)) = eval.apply(BuildInfo.generatedSources) + assert( + result.size == 1, + os.isDir(result.head.path), + result.head.path == buildInfoGeneratedSourcesFolder + ) } } } |