Skip to content

Commit

Permalink
Merge pull request #163 from sbt/wip/cleanup
Browse files Browse the repository at this point in the history
Test using both Scala 2.13 and 2.12 with -Xfatal-warnings
  • Loading branch information
eed3si9n authored Aug 8, 2020
2 parents 2b049ff + 5a3cc04 commit 8a40b48
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 210 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ lazy val root = (project in file("."))
.settings(
name := "sbt-buildinfo",
pluginCrossBuild / sbtVersion := "1.2.8",
scalacOptions := Seq("-Xfuture", "-unchecked", "-deprecation", "-feature", "-language:implicitConversions"),
scalacOptions := Seq("-Xlint", "-Xfatal-warnings", "-unchecked", "-deprecation", "-feature", "-language:implicitConversions"),
scalacOptions += "-language:experimental.macros",
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided,
description := "sbt plugin to generate build info",
Expand Down
14 changes: 14 additions & 0 deletions src/main/scala/sbtbuildinfo/BuildInfoKeyMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sbtbuildinfo

import scala.reflect.macros.blackbox

final class BuildInfoKeyMacros(val c: blackbox.Context) {
import c.universe._

val BuildInfoKey = q"_root_.sbtbuildinfo.BuildInfoKey"

def taskImpl(key: Tree): Tree = {
val A = key.tpe.typeArgs.head
q"$BuildInfoKey.sbtbuildinfoTaskValueEntry[$A]($key.taskValue)($key.key.manifest.typeArguments.head.asInstanceOf[_root_.scala.reflect.Manifest[$A]])"
}
}
2 changes: 0 additions & 2 deletions src/main/scala/sbtbuildinfo/BuildInfoOption.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package sbtbuildinfo

import sbt._

sealed trait BuildInfoOption

object BuildInfoOption {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
| value match {
| case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")
| case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")
| case elem: Map[String, Any] => elem.map {
| case (k, v) => toJsonValue(k) + ":" + toJsonValue(v)
| 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ case class ScalaFinalCaseObjectRenderer(options: Seq[BuildInfoOption], pkg: Stri
| value match {
| case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")
| case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")
| case elem: Map[String, Any] => elem.map {
| case (k, v) => toJsonValue(k) + ":" + toJsonValue(v)
| 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
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/sbtbuildinfo/ScalaRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ abstract class ScalaRenderer extends BuildInfoRenderer {
}

protected def quote(v: Any): String = v match {
case x @ ( _: Int | _: Double | _: Boolean | _: Symbol) => x.toString
case x @ ( _: Int | _: Double | _: Boolean) => x.toString
case x: Symbol => s"""scala.Symbol("${x.name}")"""
case x: Long => x.toString + "L"
case node: scala.xml.NodeSeq if node.toString().trim.nonEmpty => node.toString()
case (k, _v) => "(%s -> %s)" format(quote(k), quote(_v))
Expand Down
48 changes: 7 additions & 41 deletions src/main/scala/sbtbuildinfo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import sbt._
package object sbtbuildinfo {
type BuildInfoKey = BuildInfoKey.Entry[_]
object BuildInfoKey {
implicit def setting[A](key: SettingKey[A]): Entry[A] = Setting(key)
implicit def task[A](key: TaskKey[A]): Entry[A] = macro BuildInfoKeyMacros.taskImpl
implicit def taskValue[A: Manifest](task: sbt.Task[A]): Entry[A] = TaskValue(task)
implicit def constant[A: Manifest](tuple: (String, A)): Entry[A] = Constant(tuple)
implicit def sbtbuildinfoSettingEntry[A](key: SettingKey[A]): Entry[A] = Setting(key)
implicit def sbtbuildinfoTaskEntry[A](key: TaskKey[A]): Entry[A] = macro BuildInfoKeyMacros.taskImpl
implicit def sbtbuildinfoTaskValueEntry[A: Manifest](task: sbt.Task[A]): Entry[A] = TaskValue(task)
implicit def sbtbuildinfoConstantEntry[A: Manifest](tuple: (String, A)): Entry[A] = Constant(tuple)

def apply[A](key: SettingKey[A]): Entry[A] = Setting(key)
def apply[A](key: TaskKey[A]): Entry[A] = macro BuildInfoKeyMacros.taskImpl
def apply[A: Manifest](tuple: (String, A)): Entry[A] = Constant(tuple)
def map[A, B: Manifest](from: Entry[A])(fun: ((String, A)) => (String, B)): Entry[B] =
BuildInfoKey.Mapped(from, fun)
def action[A: Manifest](name: String)(fun: => A): Entry[A] = Action(name, () => fun)

@deprecated("use += (x: BuildInfoKey) instead", "0.10.0")
def of[A](x: BuildInfoKey.Entry[A]): BuildInfoKey.Entry[A] = x
@deprecated("use ++= Seq[BuildInfoKey](...) instead", "0.10.0")
def ofN(xs: BuildInfoKey*): Seq[BuildInfoKey] = xs

def outOfGraphUnsafe[A](key: TaskKey[A]): Entry[A] = Task(key)
Expand Down Expand Up @@ -44,40 +46,4 @@ package object sbtbuildinfo {
private[sbtbuildinfo] def manifest: Manifest[A]
}
}

import scala.reflect.macros.blackbox

final class BuildInfoKeyMacros(val c: blackbox.Context) {
import c.universe._

val BuildInfoKey = q"_root_.sbtbuildinfo.BuildInfoKey"

def taskImpl(key: Tree): Tree = {
val A = key.tpe.typeArgs.head
q"$BuildInfoKey.taskValue[$A]($key.taskValue)($key.key.manifest.typeArguments.head.asInstanceOf[_root_.scala.reflect.Manifest[$A]])"
}

@deprecated("No longer used", "0.9.0")
def ofImpl(x: Tree): Tree = {
x.tpe match {
case tpe if tpe <:< typeOf[SettingKey[_]] =>
val A = tpe.typeArgs.head
q"$BuildInfoKey.setting[$A]($x)"

case tpe if tpe <:< typeOf[TaskKey[_]] =>
val A = tpe.typeArgs.head
q"$BuildInfoKey.taskValue[$A]($x.taskValue)($x.key.manifest.typeArguments.head.asInstanceOf[_root_.scala.reflect.Manifest[$A]])"

case tpe if tpe <:< typeOf[(_, _)] =>
val A = tpe.typeArgs.tail.head
q"$BuildInfoKey.constant[$A]($x)"

case tpe if tpe <:< typeOf[BuildInfoKey] => x
}
}

@deprecated("No longer used", "0.9.0")
def ofNImpl(xs: Tree*): Tree = q"_root_.scala.collection.immutable.Seq(..${xs map ofImpl})"

}
}
39 changes: 20 additions & 19 deletions src/sbt-test/sbt-buildinfo/append/build.sbt
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
lazy val check = taskKey[Unit]("check")

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin, ScriptedPlugin).
settings(
ThisBuild / scalaVersion := "2.12.12"
ThisBuild / organization := "com.example"
ThisBuild / version := "0.1"
ThisBuild / homepage := Some(url("http://example.com"))
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))

lazy val root = (project in file("."))
.enablePlugins(BuildInfoPlugin, ScriptedPlugin)
.settings(
name := "helloworld",
organization := "com.eed3si9n",
version := "0.1",
scalaVersion := "2.12.7",
scalacOptions ++= Seq("-Xlint", "-Xfatal-warnings", "-Yno-imports"),
buildInfoKeys ++= Seq[BuildInfoKey](name, organization, version, scalaVersion,
libraryDependencies, libraryDependencies in Test),
buildInfoKeys += BuildInfoKey(resolvers),
buildInfoPackage := "hello",
homepage := Some(url("http://example.com")),
licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")),
resolvers ++= Seq("Sonatype Public" at "https://oss.sonatype.org/content/groups/public"),
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 """// $COVERAGE-OFF$""" ::
case """// $COVERAGE-OFF$""" ::
"""package hello""" ::
"""""" ::
"""import scala.Predef._""" ::
Expand All @@ -29,28 +31,27 @@ lazy val root = (project in file(".")).
""" val name: String = "helloworld"""" ::
""" /** The value is "0.1". */""" ::
""" val version: String = "0.1"""" ::
""" /** The value is "2.12.7". */""" ::
""" val scalaVersion: String = "2.12.7"""" ::
""" /** The value is "2.12.12". */""" ::
""" val scalaVersion: String = "2.12.12"""" ::
""" /** The value is "1.2.8". */""" ::
""" val sbtVersion: String = "1.2.8"""" ::
""" /** The value is "com.eed3si9n". */""" ::
""" val organization: String = "com.eed3si9n"""" ::
""" /** The value is scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.7", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch"). */""" ::
""" val libraryDependencies: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.7", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch")""" ::
""" /** The value is scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.7", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch"). */""" ::
""" val test_libraryDependencies: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.7", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch")""" ::
""" /** The value is "com.example". */""" ::
""" val organization: String = "com.example"""" ::
""" /** The value is scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.12", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch"). */""" ::
""" val libraryDependencies: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.12", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch")""" ::
""" /** The value is scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.12", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch"). */""" ::
""" val test_libraryDependencies: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("org.scala-lang:scala-library:2.12.12", "org.scala-sbt:scripted-sbt:1.2.8:scripted-sbt", "org.scala-sbt:sbt-launch:1.2.8:scripted-sbt-launch")""" ::
""" /** The value is scala.collection.immutable.Seq("Sonatype Public: https://oss.sonatype.org/content/groups/public"). */""" ::
""" val resolvers: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("Sonatype Public: https://oss.sonatype.org/content/groups/public")""" ::
""" override val toString: String = {""" ::
""" "name: %s, version: %s, scalaVersion: %s, sbtVersion: %s, organization: %s, libraryDependencies: %s, test_libraryDependencies: %s, resolvers: %s".format(""" ::
""" name, version, scalaVersion, sbtVersion, organization, libraryDependencies, test_libraryDependencies, resolvers""" ::
""" )""" ::
""" }""" ::
"""}""" ::
"""}""" ::
"""// $COVERAGE-ON$""" :: Nil =>
case _ => sys.error("unexpected output: \n" + lines.mkString("\n"))
}
()
}
)

23 changes: 13 additions & 10 deletions src/sbt-test/sbt-buildinfo/buildtime/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import scala.collection.immutable.::

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

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
ThisBuild / scalaVersion := "2.12.12"
ThisBuild / organization := "com.example"
ThisBuild / version := "0.1"
ThisBuild / homepage := Some(url("http://example.com"))
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))

lazy val root = (project in file("."))
.enablePlugins(BuildInfoPlugin)
.settings(
name := "helloworld",
version := "0.1",
scalaVersion := "2.12.7",
buildInfoKeys := Seq(
name,
version,
scalaVersion
),
buildInfoPackage := "hello",
buildInfoOptions := Seq(BuildInfoOption.BuildTime),
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", "-Yno-imports"),
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 """// $COVERAGE-OFF$""" ::
case """// $COVERAGE-OFF$""" ::
"""package hello""" ::
"""""" ::
"""import scala.Predef._""" ::
Expand All @@ -32,8 +35,8 @@ lazy val root = (project in file(".")).
""" val name: String = "helloworld"""" ::
""" /** The value is "0.1". */"""::
""" val version: String = "0.1"""" ::
""" /** The value is "2.12.7". */""" ::
""" val scalaVersion: String = "2.12.7"""" ::
""" /** The value is "2.12.12". */""" ::
""" val scalaVersion: String = "2.12.12"""" ::
builtAtStringComment ::
builtAtString ::
builtAtMillisComment ::
Expand Down
19 changes: 11 additions & 8 deletions src/sbt-test/sbt-buildinfo/caching/build.sbt
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
lazy val check = taskKey[Unit]("check")

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
ThisBuild / scalaVersion := "2.12.12"
ThisBuild / organization := "com.example"
ThisBuild / version := "0.1"
ThisBuild / homepage := Some(url("http://example.com"))
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))

lazy val root = (project in file("."))
.enablePlugins(BuildInfoPlugin)
.settings(
name := "helloworld",
version := "0.1",
scalaVersion := "2.12.7",
buildInfoKeys := Seq(name, version),
buildInfoPackage := "hello",
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", "-Yno-imports"),
check := {
val dir = (sourceManaged in Compile).value
val f = dir / "sbt-buildinfo" / ("%s.scala" format "BuildInfo")
val lines = scala.io.Source.fromFile(f).getLines.toList
lines match {
case """// $COVERAGE-OFF$""" ::
case """// $COVERAGE-OFF$""" ::
"""package hello""" ::
"""""" ::
"""import scala.Predef._""" ::
Expand Down
21 changes: 11 additions & 10 deletions src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import sbtbuildinfo.ScalaCaseClassRenderer

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

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
ThisBuild / version := "0.1"
ThisBuild / scalaVersion := "2.12.12"
ThisBuild / homepage := Some(url("http://example.com"))
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))

lazy val root = (project in file("."))
.enablePlugins(BuildInfoPlugin)
.settings(
name := "helloworld",
version := "0.1",
scalaVersion := "2.11.12",
buildInfoKeys := Seq(
name,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
Expand All @@ -24,15 +27,13 @@ lazy val root = (project in file(".")).
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 """// $COVERAGE-OFF$""" ::
case """// $COVERAGE-OFF$""" ::
"""package hello""" ::
"""""" ::
"""import scala.Predef._""" ::
Expand Down Expand Up @@ -60,14 +61,14 @@ lazy val root = (project in file(".")).
""" def apply(): BuildInfo = new BuildInfo(""" ::
""" name = "helloworld",""" ::
""" projectVersion = 0.1,""" ::
""" scalaVersion = "2.11.12",""" ::
""" scalaVersion = "2.12.12",""" ::
""" ivyXML = scala.collection.immutable.Seq(),""" ::
""" homepage = scala.Some(new java.net.URL("http://example.com")),""" ::
""" licenses = scala.collection.immutable.Seq(("MIT License" -> new java.net.URL("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))),""" ::
""" apiMappings = Map(),""" ::
""" isSnapshot = false,""" ::
""" year = 2012,""" ::
""" sym = 'Foo,""" ::
""" sym = scala.Symbol("Foo"),""" ::
""" buildTime = 1234L,""" ::
targetInfo ::
""" val get = apply()""" ::
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbt-buildinfo/caseclassrenderer/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> compile
$ exists target/scala-2.11/src_managed/main/sbt-buildinfo/BuildInfo.scala
$ exists target/scala-2.12/src_managed/main/sbt-buildinfo/BuildInfo.scala

> check
Loading

0 comments on commit 8a40b48

Please sign in to comment.