Skip to content

Commit

Permalink
Merge pull request #3 from cquiroz/java-time-packages
Browse files Browse the repository at this point in the history
Java time packages
  • Loading branch information
cquiroz committed Dec 8, 2016
2 parents 956a838 + 82ea058 commit 492575f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Both Scala 2.11 and Scala 2.12 (2.12.0-M5 and later) are supported.

To get started with SBT, add one (or both) of these dependencies:

- `libraryDependencies += "com.github.cquiroz" % "scala-java-time" % "2.0.0-M5"` (for Scala)
- `libraryDependencies += "com.github.cquiroz" %%% "scala-java-time" % "2.0.0-M5"` (for Scala.js, [Scala.js plugin](http://www.scala-js.org/tutorial/basic/#sbt-setup) required)
- `libraryDependencies += "io.github.cquiroz" % "scala-java-time" % "2.0.0-M6"` (for Scala)
- `libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % "2.0.0-M6"` (for Scala.js, [Scala.js plugin](http://www.scala-js.org/tutorial/basic/#sbt-setup) required)

#### Documentation

Expand Down
49 changes: 45 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ val crossScalaVer = Seq(scalaVer, "2.10.6", "2.12.0")
lazy val commonSettings = Seq(
name := "scala-java-time",
description := "java.time API implementation in Scala and Scala.js",
version := "2.0.0-M5",
organization := "com.github.cquiroz",
version := "2.0.0-M6",
organization := "io.github.cquiroz",
homepage := Some(url("https://github.com/cquiroz/scala-java-time")),
licenses := Seq("BSD 3-Clause License" -> url("https://opensource.org/licenses/BSD-3-Clause")),

Expand Down Expand Up @@ -47,13 +47,49 @@ lazy val root = project.in(file("."))
publishArtifact := false,
Keys.`package` := file(""))

/**
* Copy source files and translate them to the java.time package
*/
def copyAndReplace(srcDirs: Seq[File], destinationDir: File): Seq[File] = {
// Copy a directory and return the list of files
def copyDirectory(source: File, target: File, overwrite: Boolean = false, preserveLastModified: Boolean = false): Set[File] =
IO.copy(PathFinder(source).***.pair(Path.rebase(source, target)), overwrite, preserveLastModified)

val onlyScalaDirs = srcDirs.filter(_.getName.endsWith("scala"))
// Copy the source files from the base project, exclude classes on java.util and dirs
val generatedFiles: List[java.io.File] = onlyScalaDirs.foldLeft(Set.empty[File]) { (files, sourceDir) =>
files ++ copyDirectory(sourceDir, destinationDir, overwrite = true)
}.filterNot(_.isDirectory).filterNot(_.getParentFile.getName == "util").toList

// These replacements will in practice rename all the classes from
// org.threeten to java.time
def replacements(line: String): String = {
line
.replaceAll("package org.threeten$", "package java")
.replaceAll("package object bp", "package object time")
.replaceAll("package org.threeten.bp", "package java.time")
.replaceAll("import org.threeten.bp", "import java.time")
.replaceAll("private\\s*\\[bp\\]", "private[time]")
}

// Visit each file and read the content replacing key strings
generatedFiles.foreach { f =>
val replacedLines = IO.readLines(f).map(replacements)
IO.writeLines(f, replacedLines)
}
generatedFiles
}

lazy val scalajavatime = crossProject.crossType(CrossType.Full).in(file("."))
.jvmConfigure(_.enablePlugins(TestNGPlugin))
.jsConfigure(_.enablePlugins(TestNGScalaJSPlugin))
.settings(commonSettings: _*)
.jvmSettings(
micrositeExtraMdFiles := Map(file("README.md") -> "index.md"),

sourceGenerators in Compile += Def.task {
val srcDirs = (sourceDirectories in Compile).value
val destinationDir = (sourceManaged in Compile).value
copyAndReplace(srcDirs, destinationDir)
}.taskValue,
resolvers += Resolver.sbtPluginRepo("releases"),
// Fork the JVM test to ensure that the custom flags are set
fork in Test := true,
Expand All @@ -63,6 +99,11 @@ lazy val scalajavatime = crossProject.crossType(CrossType.Full).in(file("."))
javaOptions in Test ++= Seq("-Djava.locale.providers=CLDR"),
TestNGPlugin.testNGSuites := Seq(((resourceDirectory in Test).value / "testng.xml").absolutePath)
).jsSettings(
sourceGenerators in Compile += Def.task {
val srcDirs = (sourceDirectories in Compile).value
val destinationDir = (sourceManaged in Compile).value
copyAndReplace(srcDirs, destinationDir)
}.taskValue,
libraryDependencies ++= Seq(
"com.github.cquiroz" %%% "scala-java-locales" % "0.4.0-cldr30"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ final class DateTimeBuilder() extends TemporalAccessor with Cloneable {
resolveMakeChanges(targetField, lt)
changes += 1
scala.util.control.Breaks.break()
case cldt: ChronoLocalDateTime[ChronoLocalDate] =>
case cldt: ChronoLocalDateTime[_] =>
resolveMakeChanges(targetField, cldt.toLocalDate)
resolveMakeChanges(targetField, cldt.toLocalTime)
changes += 1
Expand Down

0 comments on commit 492575f

Please sign in to comment.