From e30ef4a4bd5684291a1f945ece748f1ec1d534b7 Mon Sep 17 00:00:00 2001 From: dfranetovich Date: Sun, 24 Dec 2017 12:32:11 -0300 Subject: [PATCH] test + fix #110 --- .../sbtbuildinfo/ScalaCaseClassRenderer.scala | 9 ++- .../sbt-buildinfo/caseclassrenderer/build.sbt | 79 +++++++++++++++++++ .../caseclassrenderer/project/plugins.sbt | 7 ++ .../src/main/scala/traits/MyCustomTrait.scala | 3 + .../sbt-buildinfo/caseclassrenderer/test | 4 + 5 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt create mode 100644 src/sbt-test/sbt-buildinfo/caseclassrenderer/project/plugins.sbt create mode 100644 src/sbt-test/sbt-buildinfo/caseclassrenderer/src/main/scala/traits/MyCustomTrait.scala create mode 100644 src/sbt-test/sbt-buildinfo/caseclassrenderer/test diff --git a/src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala b/src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala index ecaa13c..da72cba 100644 --- a/src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala +++ b/src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala @@ -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", "", 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(",")})", s" val get = apply()", s" val value = apply()", s"}" diff --git a/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt b/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt new file mode 100644 index 0000000..9a5f371 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt @@ -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")) + } + () + } + ) diff --git a/src/sbt-test/sbt-buildinfo/caseclassrenderer/project/plugins.sbt b/src/sbt-test/sbt-buildinfo/caseclassrenderer/project/plugins.sbt new file mode 100644 index 0000000..e392da8 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/caseclassrenderer/project/plugins.sbt @@ -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) +} diff --git a/src/sbt-test/sbt-buildinfo/caseclassrenderer/src/main/scala/traits/MyCustomTrait.scala b/src/sbt-test/sbt-buildinfo/caseclassrenderer/src/main/scala/traits/MyCustomTrait.scala new file mode 100644 index 0000000..80cb426 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/caseclassrenderer/src/main/scala/traits/MyCustomTrait.scala @@ -0,0 +1,3 @@ +package traits + +trait MyCustomTrait {} \ No newline at end of file diff --git a/src/sbt-test/sbt-buildinfo/caseclassrenderer/test b/src/sbt-test/sbt-buildinfo/caseclassrenderer/test new file mode 100644 index 0000000..15dee98 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/caseclassrenderer/test @@ -0,0 +1,4 @@ +> compile +$ exists target/scala-2.11/src_managed/main/sbt-buildinfo/BuildInfo.scala + +> check