blob: 0b63cbf1b2abde9c409ca6eda29e933f56270b00 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package mill.contrib
import mill.T
import mill.define.Target
import mill.api.{Ctx, Logger, PathRef}
import mill.scalalib.ScalaModule
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, log: Ctx.Log): Seq[PathRef] =
if (!members.isEmpty) {
val outputFile = dest.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}]")
os.write(
outputFile,
s"""|${buildInfoPackageName.map(p => s"package ${p}").getOrElse("")}
|object ${buildInfoObjectName} {
|$internalMembers
|}""".stripMargin
)
Seq(PathRef(outputFile))
} else {
log.log.debug("No build info member defined, skipping code generation")
Seq.empty[PathRef]
}
def buildInfo = T {
generateBuildInfo(buildInfoMembers())
}
override def generatedSources: Target[Seq[PathRef]] = T { super.generatedSources() ++ buildInfo() }
}
|