-
Notifications
You must be signed in to change notification settings - Fork 91
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -17,6 +17,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob | |
s"package $pkg", | ||
"", | ||
"import scala.Predef._", | ||
"import scala.Any", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain why this is necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running the scripted test I was having this error |
||
"", | ||
s"/** This file was generated by sbt-buildinfo. */" | ||
) | ||
|
@@ -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] = { | ||
|
@@ -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(",")})", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"}" | ||
|
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 {} |
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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??)