Skip to content

Commit

Permalink
Merge pull request #165 from sbt/wip/options
Browse files Browse the repository at this point in the history
Support -Yno-imports
  • Loading branch information
eed3si9n authored Aug 9, 2020
2 parents 70b933c + 83e4162 commit 9874e1a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 71 deletions.
21 changes: 2 additions & 19 deletions src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
caseClassDefinitionBegin ++
buildInfoResults.flatMap(caseClassParameter).mkString(",\n").split("\n") ++
caseClassDefinitionEnd ++
toMapMethod(buildInfoResults) ++
toMapLines(buildInfoResults) ++
toJsonLines ++
caseClassEnd ++
List("") ++
caseObjectLine(buildInfoResults) ++
Expand All @@ -48,15 +49,6 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
)
}

private def toMapMethod(results: Seq[BuildInfoResult]) =
if (options.contains(BuildInfoOption.ToMap))
results
.map(result => " \"%s\" -> %s".format(result.identifier, result.identifier))
.mkString(" def toMap: Map[String, Any] = Map[String, Any](\n", ",\n", ")")
.split("\n")
.toList ::: List("")
else Nil

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

Expand All @@ -67,13 +59,4 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
s" val value = apply()",
s"}"
)

def toMapLine(results: Seq[BuildInfoResult]): Seq[String] =
if (options.contains(BuildInfoOption.ToMap) || options.contains(BuildInfoOption.ToJson))
results
.map(result => " \"%s\" -> %s".format(result.identifier, result.identifier))
.mkString(" val toMap: Map[String, Any] = Map[String, Any](\n", ",\n", ")")
.split("\n")
.toList ::: List("")
else Nil
}
42 changes: 4 additions & 38 deletions src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO

override def renderKeys(buildInfoResults: Seq[BuildInfoResult]) =
header ++
buildInfoResults.flatMap(line) ++ Seq(toStringLines(buildInfoResults)) ++
toMapLine(buildInfoResults) ++ toJsonLine ++
buildInfoResults.flatMap(line) ++
Seq(toStringLines(buildInfoResults)) ++
toMapLines(buildInfoResults) ++
toJsonLines ++
footer


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

private def line(result: BuildInfoResult): Seq[String] = {
Expand Down Expand Up @@ -62,39 +63,4 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
| )
| }""".stripMargin
}

def toMapLine(results: Seq[BuildInfoResult]): Seq[String] =
if (options.contains(BuildInfoOption.ToMap) || options.contains(BuildInfoOption.ToJson))
results
.map(result => " \"%s\" -> %s".format(result.identifier, result.identifier))
.mkString(" val toMap: Map[String, Any] = Map[String, Any](\n", ",\n", ")")
.split("\n")
.toList ::: List("")
else Nil

def toJsonLine: Seq[String] =
if (options contains BuildInfoOption.ToJson)
List(
"""| private def quote(x: Any): String = "\"" + x + "\""
| private def toJsonValue(value: Any): String = {
| value match {
| case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")
| case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")
| case elem: Map[_, Any] => elem.map {
| case (k, v) => toJsonValue(k.toString) + ":" + toJsonValue(v)
| }.mkString("{", ", ", "}")
| case d: Double => d.toString
| case f: Float => f.toString
| case l: Long => l.toString
| case i: Int => i.toString
| case s: Short => s.toString
| case bool: Boolean => bool.toString
| case str: String => quote(str)
| case other => quote(other.toString)
| }
| }
|
| val toJson: String = toJsonValue(toMap)""".stripMargin)
else Nil

}
36 changes: 36 additions & 0 deletions src/main/scala/sbtbuildinfo/ScalaRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ abstract class ScalaRenderer extends BuildInfoRenderer {

protected def pkg: String

def options: Seq[BuildInfoOption]

protected def toMapLines(results: Seq[BuildInfoResult]): Seq[String] =
if (options.contains(BuildInfoOption.ToMap) || options.contains(BuildInfoOption.ToJson))
results
.map(result => " \"%s\" -> %s".format(result.identifier, result.identifier))
.mkString(" val toMap: Map[String, scala.Any] = Map[String, scala.Any](\n", ",\n", ")")
.split("\n")
.toList ::: List("")
else Nil

protected def toJsonLines: Seq[String] =
if (options contains BuildInfoOption.ToJson)
List(
"""| private def quote(x: scala.Any): String = "\"" + x + "\""
| private def toJsonValue(value: scala.Any): String = {
| value match {
| case elem: scala.collection.Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")
| case elem: scala.Option[_] => elem.map(toJsonValue).getOrElse("null")
| case elem: scala.collection.Map[_, scala.Any] => elem.map {
| case (k, v) => toJsonValue(k.toString) + ":" + toJsonValue(v)
| }.mkString("{", ", ", "}")
| case d: scala.Double => d.toString
| case f: scala.Float => f.toString
| case l: scala.Long => l.toString
| case i: scala.Int => i.toString
| case s: scala.Short => s.toString
| case bool: scala.Boolean => bool.toString
| case str: String => quote(str)
| case other => quote(other.toString)
| }
| }
|
| val toJson: String = toJsonValue(toMap)""".stripMargin)
else Nil

protected def getType(typeExpr: TypeExpression): Option[String] = {
def tpeToReturnType(tpe: TypeExpression): Option[String] =
tpe match {
Expand Down
40 changes: 39 additions & 1 deletion src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ lazy val root = (project in file("."))
"sym" -> 'Foo,
BuildInfoKey.action("buildTime") { 1234L },
target),
buildInfoOptions += BuildInfoOption.Traits("traits.MyCustomTrait"),
buildInfoOptions ++= Seq(
BuildInfoOption.ToJson,
BuildInfoOption.ToMap,
BuildInfoOption.Traits("traits.MyCustomTrait"),
),
buildInfoRenderFactory := ScalaCaseClassRenderer.apply,
buildInfoPackage := "hello",
scalacOptions ++= Seq("-Ywarn-unused-import", "-Xfatal-warnings", "-Yno-imports"),
Expand Down Expand Up @@ -55,6 +59,40 @@ lazy val root = (project in file("."))
""" target: java.io.File""" ::
""") extends traits.MyCustomTrait {""" ::
"""""" ::
""" val toMap: Map[String, scala.Any] = Map[String, scala.Any](""" ::
""" "name" -> name,""" ::
""" "projectVersion" -> projectVersion,""" ::
""" "scalaVersion" -> scalaVersion,""" ::
""" "ivyXML" -> ivyXML,""" ::
""" "homepage" -> homepage,""" ::
""" "licenses" -> licenses,""" ::
""" "apiMappings" -> apiMappings,""" ::
""" "isSnapshot" -> isSnapshot,""" ::
""" "year" -> year,""" ::
""" "sym" -> sym,""" ::
""" "buildTime" -> buildTime,""" ::
""" "target" -> target)""" ::
"""""" ::
""" private def quote(x: scala.Any): String = "\"" + x + "\""""" ::
""" private def toJsonValue(value: scala.Any): String = {""" ::
""" value match {""" ::
""" case elem: scala.collection.Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")""" ::
""" case elem: scala.Option[_] => elem.map(toJsonValue).getOrElse("null")""" ::
""" case elem: scala.collection.Map[_, scala.Any] => elem.map {""" ::
""" case (k, v) => toJsonValue(k.toString) + ":" + toJsonValue(v)""" ::
""" }.mkString("{", ", ", "}")""" ::
""" case d: scala.Double => d.toString""" ::
""" case f: scala.Float => f.toString""" ::
""" case l: scala.Long => l.toString""" ::
""" case i: scala.Int => i.toString""" ::
""" case s: scala.Short => s.toString""" ::
""" case bool: scala.Boolean => bool.toString""" ::
""" case str: String => quote(str)""" ::
""" case other => quote(other.toString)""" ::
""" }""" ::
""" }""" ::
"""""" ::
""" val toJson: String = toJsonValue(toMap)""" ::
"""}""" ::
"""""" ::
"""case object BuildInfo {""" ::
Expand Down
26 changes: 13 additions & 13 deletions src/sbt-test/sbt-buildinfo/options/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lazy val root = (project in file("."))
BuildInfoOption.PackagePrivate),
homepage := Some(url("http://example.com")),
licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")),
scalacOptions ++= Seq("-Xlint", "-Xfatal-warnings"),
scalacOptions ++= Seq("-Xlint", "-Xfatal-warnings", "-Yno-imports"),
check := {
val f = (sourceManaged in Compile).value / "sbt-buildinfo" / ("%s.scala" format "BuildInfo")
val lines = scala.io.Source.fromFile(f).getLines.toList
Expand All @@ -41,24 +41,24 @@ lazy val root = (project in file("."))
""" name, scalaVersion""" ::
""" )""" ::
""" }""" ::
""" val toMap: Map[String, Any] = Map[String, Any](""" ::
""" val toMap: Map[String, scala.Any] = Map[String, scala.Any](""" ::
""" "name" -> name,""" ::
""" "scalaVersion" -> scalaVersion)""" ::
"""""" ::
""" private def quote(x: Any): String = "\"" + x + "\""""" ::
""" private def toJsonValue(value: Any): String = {""" ::
""" private def quote(x: scala.Any): String = "\"" + x + "\""""" ::
""" private def toJsonValue(value: scala.Any): String = {""" ::
""" value match {""" ::
""" case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")""" ::
""" case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")""" ::
""" case elem: Map[_, Any] => elem.map {""" ::
""" case elem: scala.collection.Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")""" ::
""" case elem: scala.Option[_] => elem.map(toJsonValue).getOrElse("null")""" ::
""" case elem: scala.collection.Map[_, scala.Any] => elem.map {""" ::
""" case (k, v) => toJsonValue(k.toString) + ":" + toJsonValue(v)""" ::
""" }.mkString("{", ", ", "}")""" ::
""" case d: Double => d.toString""" ::
""" case f: Float => f.toString""" ::
""" case l: Long => l.toString""" ::
""" case i: Int => i.toString""" ::
""" case s: Short => s.toString""" ::
""" case bool: Boolean => bool.toString""" ::
""" case d: scala.Double => d.toString""" ::
""" case f: scala.Float => f.toString""" ::
""" case l: scala.Long => l.toString""" ::
""" case i: scala.Int => i.toString""" ::
""" case s: scala.Short => s.toString""" ::
""" case bool: scala.Boolean => bool.toString""" ::
""" case str: String => quote(str)""" ::
""" case other => quote(other.toString)""" ::
""" }""" ::
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hello

import scala.Predef._

object Test extends scala.App {
val expected = """{"name":"helloworld", "scalaVersion":"2.12.12"}"""
val actual = hello.BuildInfo.toJson
assert(actual == expected, "expected " + expected + " but found " + actual)
}
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-buildinfo/options/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
$ exists target/scala-2.12/src_managed/main/sbt-buildinfo/BuildInfo.scala

> check

> run

0 comments on commit 9874e1a

Please sign in to comment.