Skip to content

Commit

Permalink
Merge pull request #164 from sbt/wip/constantvalue
Browse files Browse the repository at this point in the history
BuildInfoOption.ConstantValue
  • Loading branch information
eed3si9n authored Aug 9, 2020
2 parents 8a40b48 + 0842feb commit 70b933c
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 124 deletions.
1 change: 1 addition & 0 deletions src/main/scala/sbtbuildinfo/BuildInfoOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ object BuildInfoOption {
case class Traits(names: String*) extends BuildInfoOption
case object BuildTime extends BuildInfoOption
case object PackagePrivate extends BuildInfoOption
case object ConstantValue extends BuildInfoOption
}
17 changes: 14 additions & 3 deletions src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
override def extension = "scala"
val traitNames = options.collect{case BuildInfoOption.Traits(ts @ _*) => ts}.flatten
val objTraits = if (traitNames.isEmpty) "" else " extends " ++ traitNames.mkString(" with ")
val constantValue = options.contains(BuildInfoOption.ConstantValue)

// It is safe to add `import scala.Predef` even though we need to keep `-Ywarn-unused-import` in mind
// because we always generate code that has a reference to `String`. If the "base" generated code were to be
Expand All @@ -31,13 +32,23 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
toMapLine(buildInfoResults) ++ toJsonLine ++
footer


private val constantTypes = Set("scala.Int", "scala.Long", "scala.Double", "scala.Boolean", "scala.Symbol", "String")

private def line(result: BuildInfoResult): Seq[String] = {
import result._
val typeDecl = getType(result.typeExpr) map { ": " + _ } getOrElse ""

val (typeDecl, modifier) =
getType(result.typeExpr) match {
case Some(tp) if !constantValue || !constantTypes(tp) =>
(s": $tp", "")
case _ if constantValue =>
("", "final ")
case _ =>
("", "")
}
List(
s" /** The value is ${quote(value)}. */",
s" val $identifier$typeDecl = ${quote(value)}"
s" ${modifier}val $identifier$typeDecl = ${quote(value)}"
)
}

Expand Down
89 changes: 0 additions & 89 deletions src/main/scala/sbtbuildinfo/ScalaFinalCaseObjectRenderer.scala

This file was deleted.

1 change: 1 addition & 0 deletions src/main/scala/sbtbuildinfo/ScalaRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ abstract class ScalaRenderer extends BuildInfoRenderer {
case x: Symbol => s"""scala.Symbol("${x.name}")"""
case x: Long => x.toString + "L"
case node: scala.xml.NodeSeq if node.toString().trim.nonEmpty => node.toString()
case node: scala.xml.NodeSeq => "scala.xml.NodeSeq.Empty"
case (k, _v) => "(%s -> %s)" format(quote(k), quote(_v))
case mp: Map[_, _] => mp.toList.map(quote(_)).mkString("Map(", ", ", ")")
case seq: collection.Seq[_] => seq.map(quote).mkString("scala.collection.immutable.Seq(", ", ", ")")
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ lazy val root = (project in file("."))
""" name = "helloworld",""" ::
""" projectVersion = 0.1,""" ::
""" scalaVersion = "2.12.12",""" ::
""" ivyXML = scala.collection.immutable.Seq(),""" ::
""" ivyXML = scala.xml.NodeSeq.Empty,""" ::
""" homepage = scala.Some(new java.net.URL("http://example.com")),""" ::
""" licenses = scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))),""" ::
""" apiMappings = Map(),""" ::
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import sbtbuildinfo.ScalaFinalCaseObjectRenderer

lazy val check = taskKey[Unit]("checks this plugin")

ThisBuild / version := "0.1"
ThisBuild / scalaVersion := "2.12.12"
ThisBuild / scalaVersion := "2.13.3"
ThisBuild / homepage := Some(url("http://example.com"))
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))

Expand All @@ -14,6 +12,7 @@ lazy val root = (project in file(".")).
TaskKey[Classpath]("someCp") := Seq(Attributed.blank(file("/tmp/f.txt"))),
buildInfoKeys := Seq[BuildInfoKey](
name,
version,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
scalaVersion,
ivyXML,
Expand All @@ -26,12 +25,12 @@ lazy val root = (project in file(".")).
BuildInfoKey.action("buildTime") { 1234L },
TaskKey[Classpath]("someCp"),
target),
buildInfoOptions += BuildInfoOption.Traits("traits.MyCustomTrait"),
buildInfoRenderFactory := ScalaFinalCaseObjectRenderer.apply,
buildInfoOptions ++= Seq(BuildInfoOption.Traits("traits.MyCustomTrait"), BuildInfoOption.ConstantValue),
buildInfoPackage := "hello",
scalacOptions ++= Seq("-Ywarn-unused-import", "-Xfatal-warnings", "-Yno-imports"),
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.0.5",
scalacOptions ++= Seq("-Xlint", "-Xfatal-warnings", "-Yno-imports"),
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.3.0",
check := {
val sv = scalaVersion.value
val f = (sourceManaged in Compile).value / "sbt-buildinfo" / ("%s.scala" format "BuildInfo")
val lines = scala.io.Source.fromFile(f).getLines.toList
lines match {
Expand All @@ -43,38 +42,41 @@ lazy val root = (project in file(".")).
"""/** This object was generated by sbt-buildinfo. */""" ::
"""case object BuildInfo extends traits.MyCustomTrait {""" ::
""" /** The value is "helloworld". */"""::
""" final val name: String = "helloworld"""" ::
""" final val name = "helloworld"""" ::
""" /** The value is "0.1". */"""::
""" final val version = "0.1"""" ::
""" /** The value is 0.1. */"""::
""" final val projectVersion = 0.1""" ::
""" /** The value is "2.12.12". */""" ::
""" final val scalaVersion: String = "2.12.12"""" ::
""" /** The value is scala.collection.immutable.Seq(). */""" ::
""" final val ivyXML: scala.xml.NodeSeq = scala.collection.immutable.Seq()""" ::
scalaVersionInfoComment ::
scalaVersionInfo ::
""" /** The value is scala.xml.NodeSeq.Empty. */""" ::
""" val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" ::
""" /** The value is scala.Some(new java.net.URL("http://example.com")). */""" ::
""" final val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URL("http://example.com"))""" ::
""" val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URL("http://example.com"))""" ::
""" /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))). */""" ::
""" final val licenses: scala.collection.immutable.Seq[(String, java.net.URL)] = scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")))""" ::
""" val licenses: scala.collection.immutable.Seq[(String, java.net.URL)] = scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")))""" ::
""" /** The value is Map(). */""" ::
""" final val apiMappings: Map[java.io.File, java.net.URL] = Map()""" ::
""" val apiMappings: Map[java.io.File, java.net.URL] = Map()""" ::
""" /** The value is false. */""" ::
""" final val isSnapshot: scala.Boolean = false""" ::
""" final val isSnapshot = false""" ::
""" /** The value is 2012. */""" ::
""" final val year: scala.Int = 2012""" ::
""" final val year = 2012""" ::
""" /** The value is scala.Symbol("Foo"). */""" ::
""" final val sym: scala.Symbol = scala.Symbol("Foo")""" ::
""" final val sym = scala.Symbol("Foo")""" ::
""" /** The value is 1234L. */""" ::
""" final val buildTime: scala.Long = 1234L""" ::
""" final val buildTime = 1234L""" ::
""" /** The value is scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt")). */""" ::
""" final val someCp: scala.collection.immutable.Seq[java.io.File] = scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt"))""" ::
""" val someCp: scala.collection.immutable.Seq[java.io.File] = scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt"))""" ::
targetInfoComment ::
targetInfo :: // """
""" override val toString: String = {""" ::
""" "name: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, someCp, target""" ::
""" "name: %s, version: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, version, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, someCp, target""" ::
""" )""" ::
""" }""" ::
"""}""" ::
"""// $COVERAGE-ON$""" :: Nil if targetInfo contains "target: java.io.File = new java.io.File(" =>
"""// $COVERAGE-ON$""" :: Nil if (targetInfo contains "target: java.io.File = new java.io.File(") &&
(scalaVersionInfo.trim == s"""final val scalaVersion = "$sv"""") => ()
case _ => sys.error("unexpected output: \n" + lines.mkString("\n"))
}
()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.deprecated

@deprecated("No longer used", hello.BuildInfo.version)
object Test {

}
4 changes: 4 additions & 0 deletions src/sbt-test/sbt-buildinfo/constantvalue/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> compile
$ exists target/scala-2.13/src_managed/main/sbt-buildinfo/BuildInfo.scala

> check
4 changes: 0 additions & 4 deletions src/sbt-test/sbt-buildinfo/finalcaseobjectrenderer/test

This file was deleted.

4 changes: 2 additions & 2 deletions src/sbt-test/sbt-buildinfo/simple/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ lazy val root = (project in file("."))
""" val projectVersion = 0.1""" ::
scalaVersionInfoComment ::
scalaVersionInfo ::
""" /** The value is scala.collection.immutable.Seq(). */""" ::
""" val ivyXML: scala.xml.NodeSeq = scala.collection.immutable.Seq()""" ::
""" /** The value is scala.xml.NodeSeq.Empty. */""" ::
""" val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" ::
""" /** The value is scala.Some(new java.net.URL("http://example.com")). */""" ::
""" val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URL("http://example.com"))""" ::
""" /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))). */""" ::
Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ lazy val root = (project in file("."))
""" val projectVersion = 0.1""" ::
""" /** The value is "2.12.12". */""" ::
""" val scalaVersion: String = "2.12.12"""" ::
""" /** The value is scala.collection.immutable.Seq(). */""" ::
""" val ivyXML: scala.xml.NodeSeq = scala.collection.immutable.Seq()""" ::
""" /** The value is scala.xml.NodeSeq.Empty. */""" ::
""" val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" ::
""" /** The value is scala.Some(new java.net.URL("http://example.com")). */""" ::
""" val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URL("http://example.com"))""" ::
""" /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))). */""" ::
Expand Down

0 comments on commit 70b933c

Please sign in to comment.