Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toMap and toJson methods to BuildInfo #958

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions contrib/buildinfo/src/BuildInfo.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package mill.contrib.buildinfo

import mill.T
import mill.api.Logger
import mill.api.PathRef
import mill.scalalib.ScalaModule
import mill.Agg
import mill.api.{ Logger, Loose, PathRef }
import mill.define.Target
import mill.scalalib.{ Dep, ScalaModule, DepSyntax }

trait BuildInfo extends ScalaModule {

override def ivyDeps = super.ivyDeps() ++ Agg(ivy"com.lihaoyi::ujson-circe:1.2.0")

def buildInfoPackageName: Option[String] = None

def buildInfoObjectName: String = "BuildInfo"
Expand All @@ -26,14 +29,25 @@ trait BuildInfo extends ScalaModule {
case (name, value) => s""" def ${name} = "${value}""""
}
.mkString("\n")
val map = members.map {
case (name, _) => s""""${name}" -> ${name}"""
}.mkString(",")
logger.debug(s"Generating object [${buildInfoPackageName.map(_ + ".").getOrElse("")}${buildInfoObjectName}] with [${members.size}] members to [${outputFile}]")
os.write(
outputFile,
s"""|${buildInfoPackageName.map(packageName => s"package ${packageName}\n").getOrElse("")}
|import ujson._
|
|object ${buildInfoObjectName} {
|$internalMembers
|
| val toMap = Map[String, String](
| $map)
|
| val toJson = Js.Obj(
| $map)
|}""".stripMargin
)
)
(Seq(PathRef(outputFile)), PathRef(T.dest))
} else {
logger.debug("No build info member defined, skipping code generation")
Expand Down
59 changes: 41 additions & 18 deletions contrib/buildinfo/test/src/BuildInfoTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import os.Path
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 {
trait BuildInfoModule
extends TestUtil.BaseModule
with scalalib.ScalaModule
with BuildInfo {
// override build root to test custom builds/modules
override def millSourcePath: Path = TestUtil.getSrcPathStatic()
override def scalaVersion = scalaVersionString
Expand All @@ -23,7 +25,7 @@ object BuildInfoTests extends TestSuite {
object EmptyBuildInfo extends BuildInfoModule

object BuildInfo extends BuildInfoModule {
def buildInfoMembers=T{
def buildInfoMembers = T {
Map(
"scalaVersion" -> scalaVersion(),
)
Expand All @@ -33,18 +35,18 @@ object BuildInfoTests extends TestSuite {
object BuildInfoSettings extends BuildInfoModule {
def buildInfoPackageName = Some("foo")
def buildInfoObjectName = "bar"
def buildInfoMembers=T{
def buildInfoMembers = T {
Map(
"scalaVersion" -> scalaVersion()
)
}
}

val testModuleSourcesPath: Path = os.pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo"
val testModuleSourcesPath
: Path = os.pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo"

def workspaceTest[T](m: TestUtil.BaseModule)
(t: TestEvaluator => T)
(implicit tp: TestPath): T = {
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)
Expand All @@ -56,51 +58,72 @@ object BuildInfoTests extends TestSuite {
def tests: Tests = Tests {

'buildinfo - {
'createSourcefile - workspaceTest(BuildInfo){ eval =>
'createSourcefile - workspaceTest(BuildInfo) { eval =>
val expected =
s"""|
|import ujson._
|
|object BuildInfo {
| def scalaVersion = "2.12.4"
|
| val toMap = Map[String, String](
| "scalaVersion" -> scalaVersion)
|
| val toJson = Js.Obj(
| "scalaVersion" -> scalaVersion)
|}""".stripMargin
val Right(((result, _), evalCount)) = eval.apply(BuildInfo.generatedBuildInfo)
val Right(((result, _), evalCount)) =
eval.apply(BuildInfo.generatedBuildInfo)
assert(
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.generatedBuildInfo)
'notCreateEmptySourcefile - workspaceTest(EmptyBuildInfo) { eval =>
val Right(((result, _), evalCount)) =
eval.apply(EmptyBuildInfo.generatedBuildInfo)
assert(
result.isEmpty &&
!os.exists(eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala")
!os.exists(
eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala")
)
}

'supportCustomSettings - workspaceTest(BuildInfoSettings){ eval =>
'supportCustomSettings - workspaceTest(BuildInfoSettings) { eval =>
val expected =
s"""|package foo
|
|import ujson._
|
|object bar {
| def scalaVersion = "2.12.4"
|
| val toMap = Map[String, String](
| "scalaVersion" -> scalaVersion)
|
| val toJson = Js.Obj(
| "scalaVersion" -> scalaVersion)
|}""".stripMargin
val Right(((result, _), evalCount)) = eval.apply(BuildInfoSettings.generatedBuildInfo)
val Right(((result, _), evalCount)) =
eval.apply(BuildInfoSettings.generatedBuildInfo)
assert(
result.head.path == eval.outPath / 'generatedBuildInfo / 'dest / "BuildInfo.scala" &&
os.exists(result.head.path) &&
os.read(result.head.path) == expected
)
}

'compile - workspaceTest(BuildInfo){ eval =>
'compile - workspaceTest(BuildInfo) { eval =>
val Right((result, evalCount)) = eval.apply(BuildInfo.compile)
assert(true)
}

'run - workspaceTest(BuildInfo){ eval =>
'run - workspaceTest(BuildInfo) { eval =>
val runResult = eval.outPath / "hello-mill"
val Right((result, evalCount)) = eval.apply(BuildInfo.run(runResult.toString))
val Right((result, evalCount)) =
eval.apply(BuildInfo.run(runResult.toString))
assert(
os.exists(runResult),
os.read(runResult) == scalaVersionString
Expand Down