Skip to content

Commit

Permalink
Merge pull request #178 from nMoncho/java_time
Browse files Browse the repository at this point in the history
Add Java time objects as valid BuildInfoKey values
  • Loading branch information
eed3si9n committed Feb 16, 2022
2 parents 47d5a0f + 11a637e commit 31ae439
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/main/scala/sbtbuildinfo/JavaRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend
x0 <- tpeToReturnType(arg0)
x1 <- tpeToReturnType(arg1)
} yield s"java.util.Map.Entry<$x0, $x1>"

case TypeExpression("java.time.LocalDate", Nil) => Some("java.time.LocalDate")
case TypeExpression("java.time.Instant", Nil) => Some("java.time.Instant")

case _ => None
}
tpeToReturnType(typeExpr)
Expand All @@ -198,6 +202,8 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend
case url: java.net.URL => "internalAsUrl(%s)" format quote(url.toString)
case file: java.io.File => "new java.io.File(%s)" format quote(file.toString)
case attr: sbt.Attributed[_] => quote(attr.data)
case date: java.time.LocalDate => "java.time.LocalDate.parse(%s)" format quote(date.toString)
case instant: java.time.Instant => "java.time.Instant.parse(%s)" format quote(instant.toString)
case s => "\"%s\"" format encodeStringLiteral(s.toString)
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/sbtbuildinfo/ScalaRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ abstract class ScalaRenderer extends BuildInfoRenderer {
x0 <- tpeToReturnType(arg0)
x1 <- tpeToReturnType(arg1)
} yield s"($x0, $x1)"

case TypeExpression("java.time.LocalDate", Nil) => Some("java.time.LocalDate")
case TypeExpression("java.time.Instant", Nil) => Some("java.time.Instant")

case _ => None
}
tpeToReturnType(typeExpr)
Expand All @@ -96,6 +100,8 @@ abstract class ScalaRenderer extends BuildInfoRenderer {
case url: java.net.URL => "new java.net.URL(%s)" format quote(url.toString)
case file: java.io.File => "new java.io.File(%s)" format quote(file.toString)
case attr: sbt.Attributed[_] => quote(attr.data)
case date: java.time.LocalDate => "java.time.LocalDate.parse(%s)" format quote(date.toString)
case instant: java.time.Instant => "java.time.Instant.parse(%s)" format quote(instant.toString)
case s => "\"%s\"" format encodeStringLiteral(s.toString)
}

Expand Down
8 changes: 8 additions & 0 deletions src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ lazy val root = (project in file("."))
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
"now" -> java.time.LocalDate.parse("2021-11-02"),
"instant" -> java.time.Instant.parse("2021-11-02T01:23:45.678Z"),
BuildInfoKey.action("buildTime") { 1234L },
target),
buildInfoOptions ++= Seq(
Expand Down Expand Up @@ -55,6 +57,8 @@ lazy val root = (project in file("."))
""" isSnapshot: scala.Boolean,""" ::
""" year: scala.Int,""" ::
""" sym: scala.Symbol,""" ::
""" now: java.time.LocalDate,""" ::
""" instant: java.time.Instant,""" ::
""" buildTime: scala.Long,""" ::
""" target: java.io.File""" ::
""") extends traits.MyCustomTrait {""" ::
Expand All @@ -70,6 +74,8 @@ lazy val root = (project in file("."))
""" "isSnapshot" -> isSnapshot,""" ::
""" "year" -> year,""" ::
""" "sym" -> sym,""" ::
""" "now" -> now,""" ::
""" "instant" -> instant,""" ::
""" "buildTime" -> buildTime,""" ::
""" "target" -> target)""" ::
"""""" ::
Expand Down Expand Up @@ -107,6 +113,8 @@ lazy val root = (project in file("."))
""" isSnapshot = false,""" ::
""" year = 2012,""" ::
""" sym = scala.Symbol("Foo"),""" ::
""" now = java.time.LocalDate.parse("2021-11-02"),""" ::
""" instant = java.time.Instant.parse("2021-11-02T01:23:45.678Z"),""" ::
""" buildTime = 1234L,""" ::
targetInfo ::
""" val get = apply()""" ::
Expand Down
10 changes: 8 additions & 2 deletions src/sbt-test/sbt-buildinfo/constantvalue/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ lazy val root = (project in file(".")).
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
"now" -> java.time.LocalDate.parse("2021-11-02"),
"instant" -> java.time.Instant.parse("2021-11-02T01:23:45.678Z"),
BuildInfoKey.action("buildTime") { 1234L },
TaskKey[Classpath]("someCp"),
target),
Expand Down Expand Up @@ -67,15 +69,19 @@ lazy val root = (project in file(".")).
""" final val year = 2012""" ::
""" /** The value is scala.Symbol("Foo"). */""" ::
""" final val sym = scala.Symbol("Foo")""" ::
""" /** The value is java.time.LocalDate.parse("2021-11-02"). */""" ::
""" val now: java.time.LocalDate = java.time.LocalDate.parse("2021-11-02")""" ::
""" /** The value is java.time.Instant.parse("2021-11-02T01:23:45.678Z"). */""" ::
""" val instant: java.time.Instant = java.time.Instant.parse("2021-11-02T01:23:45.678Z")""" ::
""" /** The value is 1234L. */""" ::
""" final val buildTime = 1234L""" ::
""" /** The value is scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt")). */""" ::
""" val someCp: scala.collection.immutable.Seq[java.io.File] = scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt"))""" ::
targetInfoComment ::
targetInfo :: // """
""" override val toString: String = {""" ::
""" "name: %s, version: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, version, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, someCp, target""" ::
""" "name: %s, version: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, now: %s, instant: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, version, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, now, instant, buildTime, someCp, target""" ::
""" )""" ::
""" }""" ::
"""}""" ::
Expand Down
12 changes: 10 additions & 2 deletions src/sbt-test/sbt-buildinfo/javasingletonrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ lazy val root = (project in file("."))
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
"now" -> java.time.LocalDate.parse("2021-11-02"),
"instant" -> java.time.Instant.parse("2021-11-02T01:23:45.678Z"),
BuildInfoKey.action("buildTime") { 1234L },
target),
buildInfoOptions ++= Seq(
Expand Down Expand Up @@ -62,14 +64,18 @@ lazy val root = (project in file("."))
""" public final Integer year = 2012;""" ::
""" /** The value is ("Foo").intern(). */""" ::
""" public final String sym = ("Foo").intern();""" ::
""" /** The value is java.time.LocalDate.parse("2021-11-02"). */""" ::
""" public final java.time.LocalDate now = java.time.LocalDate.parse("2021-11-02");""" ::
""" /** The value is java.time.Instant.parse("2021-11-02T01:23:45.678Z"). */""" ::
""" public final java.time.Instant instant = java.time.Instant.parse("2021-11-02T01:23:45.678Z");""" ::
""" /** The value is 1234L. */""" ::
""" public final Long buildTime = 1234L;""" ::
targetInfoComment ::
targetInfo ::
"""""" ::
""" public String toString() {""" ::
""" return String.format("name: %s, scalaVersion: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, target: %s",""" ::
""" name, scalaVersion, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, target""" ::
""" return String.format("name: %s, scalaVersion: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, now: %s, instant: %s, buildTime: %s, target: %s",""" ::
""" name, scalaVersion, homepage, licenses, apiMappings, isSnapshot, year, sym, now, instant, buildTime, target""" ::
""" );""" ::
""" }""" ::
"""""" ::
Expand All @@ -83,6 +89,8 @@ lazy val root = (project in file("."))
""" m.put("isSnapshot", isSnapshot);""" ::
""" m.put("year", year);""" ::
""" m.put("sym", sym);""" ::
""" m.put("now", now);""" ::
""" m.put("instant", instant);""" ::
""" m.put("buildTime", buildTime);""" ::
""" m.put("target", target);""" ::
""" return java.util.Collections.unmodifiableMap(m);""" ::
Expand Down
12 changes: 10 additions & 2 deletions src/sbt-test/sbt-buildinfo/javastaticfieldsrenderer/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ lazy val root = (project in file("."))
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
"now" -> java.time.LocalDate.parse("2021-11-02"),
"instant" -> java.time.Instant.parse("2021-11-02T01:23:45.678Z"),
BuildInfoKey.action("buildTime") { 1234L },
target),
buildInfoOptions ++= Seq(
Expand Down Expand Up @@ -60,14 +62,18 @@ lazy val root = (project in file("."))
""" public static final Integer year = 2012;""" ::
""" /** The value is ("Foo").intern(). */""" ::
""" public static final String sym = ("Foo").intern();""" ::
""" /** The value is java.time.LocalDate.parse("2021-11-02"). */""" ::
""" public static final java.time.LocalDate now = java.time.LocalDate.parse("2021-11-02");""" ::
""" /** The value is java.time.Instant.parse("2021-11-02T01:23:45.678Z"). */""" ::
""" public static final java.time.Instant instant = java.time.Instant.parse("2021-11-02T01:23:45.678Z");""" ::
""" /** The value is 1234L. */""" ::
""" public static final Long buildTime = 1234L;""" ::
targetInfoComment ::
targetInfo ::
"""""" ::
""" public static String makeString() {""" ::
""" return String.format("name: %s, scalaVersion: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, target: %s",""" ::
""" name, scalaVersion, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, target""" ::
""" return String.format("name: %s, scalaVersion: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, now: %s, instant: %s, buildTime: %s, target: %s",""" ::
""" name, scalaVersion, homepage, licenses, apiMappings, isSnapshot, year, sym, now, instant, buildTime, target""" ::
""" );""" ::
""" }""" ::
"""""" ::
Expand All @@ -81,6 +87,8 @@ lazy val root = (project in file("."))
""" m.put("isSnapshot", isSnapshot);""" ::
""" m.put("year", year);""" ::
""" m.put("sym", sym);""" ::
""" m.put("now", now);""" ::
""" m.put("instant", instant);""" ::
""" m.put("buildTime", buildTime);""" ::
""" m.put("target", target);""" ::
""" return java.util.Collections.unmodifiableMap(m);""" ::
Expand Down
7 changes: 5 additions & 2 deletions src/sbt-test/sbt-buildinfo/simple/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lazy val root = (project in file("."))
isSnapshot,
"year" -> 2012,
"sym" -> 'Foo,
"now" -> java.time.LocalDate.parse("2021-11-02"),
BuildInfoKey.action("buildTime") { 1234L },
TaskKey[Classpath]("someCp"),
target
Expand Down Expand Up @@ -61,15 +62,17 @@ lazy val root = (project in file("."))
""" val year: scala.Int = 2012""" ::
""" /** The value is scala.Symbol("Foo"). */""" ::
""" val sym: scala.Symbol = scala.Symbol("Foo")""" ::
""" /** The value is java.time.LocalDate.parse("2021-11-02"). */""" ::
""" val now: java.time.LocalDate = java.time.LocalDate.parse("2021-11-02")""" ::
""" /** The value is 1234L. */""" ::
""" val buildTime: scala.Long = 1234L""" ::
""" /** The value is scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt")). */""" ::
""" val someCp: scala.collection.immutable.Seq[java.io.File] = scala.collection.immutable.Seq(new java.io.File("/tmp/f.txt"))""" ::
targetInfoComment ::
targetInfo :: // """
""" override val toString: String = {""" ::
""" "name: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, buildTime, someCp, target""" ::
""" "name: %s, projectVersion: %s, scalaVersion: %s, ivyXML: %s, homepage: %s, licenses: %s, apiMappings: %s, isSnapshot: %s, year: %s, sym: %s, now: %s, buildTime: %s, someCp: %s, target: %s".format(""" ::
""" name, projectVersion, scalaVersion, ivyXML, homepage, licenses, apiMappings, isSnapshot, year, sym, now, buildTime, someCp, target""" ::
""" )""" ::
""" }""" ::
"""}""" ::
Expand Down

0 comments on commit 31ae439

Please sign in to comment.