Skip to content

Commit

Permalink
Deprecate BuildInfoKey.of and BuildInfoKey.ofN
Browse files Browse the repository at this point in the history
Ref #119
  • Loading branch information
eed3si9n committed Aug 8, 2020
1 parent 2b049ff commit 61539b1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 92 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
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})"

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lazy val root = (project in file(".")).
version := "0.1",
scalaVersion := "2.12.7",
TaskKey[Classpath]("someCp") := Seq(Attributed.blank(file("/tmp/f.txt"))),
buildInfoKeys := BuildInfoKey.ofN(
buildInfoKeys := Seq[BuildInfoKey](
name,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
scalaVersion,
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbt-buildinfo/simple/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ lazy val root = (project in file(".")).
version := "0.1",
scalaVersion := "2.12.7",
TaskKey[Classpath]("someCp") := Seq(Attributed.blank(file("/tmp/f.txt"))),
buildInfoKeys := BuildInfoKey.ofN(
buildInfoKeys := Seq[BuildInfoKey](
name,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
scalaVersion,
Expand Down
38 changes: 22 additions & 16 deletions src/sbt-test/sbt-buildinfo/task/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import StableState.{ counterOutOfTaskGraph, counterInTaskGraph }

scalaVersion in ThisBuild := "2.12.4"

val projOutOfTaskGraph1 = project settings (
sourceGenerators in Compile += Def.task { counterOutOfTaskGraph.incrementAndGet(); Nil }.taskValue
)
val projOutOfTaskGraph1 = project
.settings (
sourceGenerators in Compile += Def.task { counterOutOfTaskGraph.incrementAndGet(); Nil }.taskValue
)

val projOutOfTaskGraph2 = project dependsOn projOutOfTaskGraph1 settings (
BuildInfoPlugin.buildInfoDefaultSettings,
addBuildInfoToConfig(Test),
buildInfoKeys in Test += BuildInfoKey.outOfGraphUnsafe(fullClasspath in Compile),
)
val projOutOfTaskGraph2 = project
.dependsOn(projOutOfTaskGraph1)
.settings (
BuildInfoPlugin.buildInfoDefaultSettings,
addBuildInfoToConfig(Test),
buildInfoKeys in Test += BuildInfoKey.outOfGraphUnsafe(fullClasspath in Compile),
)

val projInTaskGraph1 = project settings (
sourceGenerators in Compile += Def.task { counterInTaskGraph.incrementAndGet(); Nil }.taskValue
)
val projInTaskGraph1 = project
.settings (
sourceGenerators in Compile += Def.task { counterInTaskGraph.incrementAndGet(); Nil }.taskValue
)

val projInTaskGraph2 = project dependsOn projInTaskGraph1 settings (
BuildInfoPlugin.buildInfoDefaultSettings,
addBuildInfoToConfig(Test),
buildInfoKeys in Test += BuildInfoKey.of(fullClasspath in Compile)
)
val projInTaskGraph2 = project
.dependsOn(projInTaskGraph1)
.settings (
BuildInfoPlugin.buildInfoDefaultSettings,
addBuildInfoToConfig(Test),
buildInfoKeys in Test += (fullClasspath in Compile: BuildInfoKey)
)

TaskKey[Unit]("checkOutOfTaskGraph") := {
val value = counterOutOfTaskGraph.get()
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ lazy val root = (project in file(".")).
version := "0.1",
scalaVersion := "2.12.7",
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.1.0",
buildInfoKeys := BuildInfoKey.ofN(
buildInfoKeys := Seq[BuildInfoKey](
name,
BuildInfoKey.map(version) { case (n, v) => "projectVersion" -> v.toDouble },
scalaVersion,
Expand Down
29 changes: 0 additions & 29 deletions src/test/scala/sbtbuildinfo/BuildInfoKeySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,4 @@ object BuildInfoKeySpec {
BuildInfoKey.map(version) { case (_, v) => "projectVersion" -> v.toDouble },
BuildInfoKey.map(fullClasspath) { case (ident, cp) => ident -> cp.files },
)


buildInfoKeys := BuildInfoKey.ofN(name, version) // test `:=` works with setting keys
buildInfoKeys := BuildInfoKey.ofN(products, fullClasspath) // test `:=` works with task keys
buildInfoKeys := BuildInfoKey.ofN(name, fullClasspath) // test `:=` works with setting and task keys
buildInfoKeys := BuildInfoKey.ofN( // test `:=` works with misc things
name,
fullClasspath,
"year" -> 2012,
BuildInfoKey.action("buildTime") { 1234L },
BuildInfoKey.map(version) { case (_, v) => "projectVersion" -> v.toDouble },
BuildInfoKey.map(fullClasspath) { case (ident, cp) => ident -> cp.files },
)

buildInfoKeys += BuildInfoKey.of(name) // test `+=` works with a setting key
buildInfoKeys += BuildInfoKey.of(fullClasspath) // test `+=` works with a task key

buildInfoKeys ++= BuildInfoKey.ofN(name, version) // test `++=` works with setting keys
buildInfoKeys ++= BuildInfoKey.ofN(fullClasspath) // test `++=` works with 1 task key
buildInfoKeys ++= BuildInfoKey.ofN(products, fullClasspath) // test `++=` works with n task keys
buildInfoKeys ++= BuildInfoKey.ofN(name, fullClasspath) // test `++=` works with setting and task keys
buildInfoKeys ++= BuildInfoKey.ofN( // test `++=` works with misc things
name,
fullClasspath,
"year" -> 2012,
BuildInfoKey.action("buildTime") { 1234L },
BuildInfoKey.map(version) { case (_, v) => "projectVersion" -> v.toDouble },
BuildInfoKey.map(fullClasspath) { case (ident, cp) => ident -> cp.files },
)
}

0 comments on commit 61539b1

Please sign in to comment.