Skip to content

Fixes ScalaCaseClassRenderer for #110 #111

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

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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 ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this as it, to avoid trailing spaces when there are no traits to mix in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I understand this change now. (How did I misunderstand??)


// 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running the scripted test I was having this error /tmp/sbt_4647108f/caseclassrenderer/target/scala-2.11/src_managed/main/sbt-buildinfo/BuildInfo.scala:8: not found: type Any
Searching into this I found this may be related bug sbt/sbt#2572

"",
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(",")})",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why you want to switch to using named parameters here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just see the generated code more clear than a whole line with the values.

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