Skip to content

Commit

Permalink
Merge pull request #111 from damdev/fix-caseclassrenderer-issue110
Browse files Browse the repository at this point in the history
Fixes ScalaCaseClassRenderer for #110
  • Loading branch information
dwijnand authored Aug 2, 2018
2 parents c686647 + e30ef4a commit 21b33ec
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
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 objTraits = if (traitNames.isEmpty) "" else "extends " ++ traitNames.mkString(" with ")

// 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 @@ -17,6 +17,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
s"package $pkg",
"",
"import scala.Predef._",
"import scala.Any",
"",
s"/** This file was generated by sbt-buildinfo. */"
)
Expand All @@ -32,7 +33,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
caseObjectLine(buildInfoResults)

private def caseClassDefinitionBegin = List(
s"case class $obj$objTraits("
s"case class $obj("
)

private def caseClassParameter(r: BuildInfoResult): Seq[String] = {
Expand All @@ -52,12 +53,12 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
.toList ::: List("")
else Nil

private def caseClassDefinitionEnd = List(") {", "")
private def caseClassDefinitionEnd = List(s") $objTraits {", "")
private def caseClassEnd = List("}")

private def caseObjectLine(buildInfoResults: Seq[BuildInfoResult]) = List(
s"case object $obj {",
s" def apply(): $obj = new $obj(${buildInfoResults.map(_.value).map(quote).mkString(",")})",
s" def apply(): $obj = new $obj(${buildInfoResults.map(r => s"\n ${r.identifier} = ${quote(r.value)}").mkString(",")})",
s" val get = apply()",
s" val value = apply()",
s"}"
Expand Down
79 changes: 79 additions & 0 deletions src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import sbtbuildinfo.ScalaCaseClassRenderer

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

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
name := "helloworld",
version := "0.1",
scalaVersion := "2.11.8",
buildInfoKeys := Seq(
name,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
scalaVersion,
ivyXML,
homepage,
licenses,
apiMappings,
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
BuildInfoKey.action("buildTime") { 1234L },
target),
buildInfoOptions += BuildInfoOption.Traits("traits.MyCustomTrait"),
buildInfoRenderFactory := ScalaCaseClassRenderer.apply,
buildInfoPackage := "hello",
homepage := Some(url("http://example.com")),
licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")),
scalacOptions ++= Seq("-Ywarn-unused-import", "-Xfatal-warnings", "-Yno-imports"),
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.0.5",
check := {
val f = (sourceManaged in Compile).value / "sbt-buildinfo" / ("%s.scala" format "BuildInfo")
val lines = scala.io.Source.fromFile(f).getLines.toList
lines match {
case """package hello""" ::
"""""" ::
"""import scala.Predef._""" ::
"""import scala.Any""" ::
"""""" ::
"""/** This file was generated by sbt-buildinfo. */""" ::
"""case class BuildInfo(""" ::
""" name: String,""" ::
""" projectVersion: Any,""" ::
""" scalaVersion: String,""" ::
""" ivyXml: scala.xml.NodeSeq,""" ::
""" homepage: scala.Option[java.net.URL],""" ::
""" licenses: scala.collection.Seq[(String, java.net.URL)],""" ::
""" apiMappings: Map[java.io.File, java.net.URL],""" ::
""" isSnapshot: scala.Boolean,""" ::
""" year: scala.Int,""" ::
""" sym: scala.Symbol,""" ::
""" buildTime: scala.Long,""" ::
""" target: java.io.File""" ::
""") extends traits.MyCustomTrait {""" ::
"""""" ::
"""}""" ::
"""""" ::
"""case object BuildInfo {""" ::
""" def apply(): BuildInfo = new BuildInfo(""" ::
""" name = "helloworld",""" ::
""" projectVersion = 0.1,""" ::
""" scalaVersion = "2.11.8",""" ::
""" ivyXml = scala.collection.Seq(),""" ::
""" homepage = scala.Some(new java.net.URL("http://example.com")),""" ::
""" licenses = scala.collection.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))),""" ::
""" apiMappings = Map(),""" ::
""" isSnapshot = false,""" ::
""" year = 2012,""" ::
""" sym = 'Foo,""" ::
""" buildTime = 1234L,""" ::
targetInfo ::
""" val get = apply()""" ::
""" val value = apply()""" ::
"""}""" :: Nil if (targetInfo contains "target = new java.io.File(") =>
case _ => sys.error("unexpected output: \n" + lines.mkString("\n"))
}
()
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package traits

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

> check

0 comments on commit 21b33ec

Please sign in to comment.