Skip to content

Commit

Permalink
Separate Joda module and add support for MomentJS (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
A. Alonso Dominguez authored Apr 12, 2017
1 parent e04620d commit 781397b
Show file tree
Hide file tree
Showing 23 changed files with 400 additions and 218 deletions.
43 changes: 36 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ lazy val commonJsSettings = Seq(
val a = (baseDirectory in LocalRootProject).value.toURI.toString
val g = "https://raw.githubusercontent.com/alonsodomin/cron4s/" + tagOrHash
s"-P:scalajs:mapSourceURI:$a->$g/"
}
},
jsEnv := PhantomJSEnv().value
)

lazy val noPublishSettings = Seq(
Expand Down Expand Up @@ -181,8 +182,8 @@ lazy val cron4sJS = (project in file(".js")).
settings(commonJsSettings: _*).
settings(publishSettings).
enablePlugins(ScalaJSPlugin).
aggregate(coreJS, testkitJS, testsJS).
dependsOn(coreJS, testkitJS, testsJS % "test")
aggregate(coreJS, momentjs, testkitJS, testsJS).
dependsOn(coreJS, momentjs, testkitJS, testsJS % Test)

lazy val cron4sJVM = (project in file(".jvm")).
settings(
Expand All @@ -192,8 +193,8 @@ lazy val cron4sJVM = (project in file(".jvm")).
settings(commonSettings: _*).
settings(commonJvmSettings: _*).
settings(publishSettings).
aggregate(coreJVM, testkitJVM, testsJVM).
dependsOn(coreJVM, testkitJVM, testsJVM % "test")
aggregate(coreJVM, joda, testkitJVM, testsJVM).
dependsOn(coreJVM, joda, testkitJVM, testsJVM % Test)

lazy val docs = project.
enablePlugins(MicrositesPlugin).
Expand All @@ -217,7 +218,6 @@ lazy val core = (crossProject in file("core")).
jsSettings(commonJsSettings: _*).
jsSettings(Dependencies.coreJS: _*).
jvmSettings(commonJvmSettings).
jvmSettings(Dependencies.coreJVM: _*).
jvmSettings(mimaSettings("core"): _*)

lazy val coreJS = core.js
Expand Down Expand Up @@ -260,12 +260,41 @@ lazy val testsJVM = tests.jvm

lazy val bench = (project in file("bench")).
enablePlugins(AutomateHeaderPlugin).
settings(name := "bench").
settings(
name := "bench",
moduleName := "cron4s-bench"
).
settings(commonSettings).
settings(noPublishSettings).
enablePlugins(JmhPlugin).
dependsOn(coreJVM)

// DateTime library extensions

lazy val joda = (project in file("time-lib/joda")).
enablePlugins(AutomateHeaderPlugin).
settings(
name := "joda",
moduleName := "cron4s-joda"
).
settings(commonSettings).
settings(commonJvmSettings).
settings(publishSettings).
settings(Dependencies.joda).
dependsOn(coreJVM, testkitJVM % Test)

lazy val momentjs = (project in file("time-lib/momentjs")).
enablePlugins(AutomateHeaderPlugin, ScalaJSPlugin).
settings(commonSettings).
settings(commonJsSettings).
settings(publishSettings).
settings(
name := "momentjs",
moduleName := "cron4s-momentjs"
).
settings(Dependencies.momentjs).
dependsOn(coreJS, testkitJS % Test)

// Utility command aliases

addCommandAlias("testJVM", "cron4sJVM/test")
Expand Down
2 changes: 1 addition & 1 deletion core/js/src/main/scala/cron4s/lib/js/JsDateInstance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private[js] final class JsDateInstance extends IsDateTime[Date] {
case Hours => Some(setter(d => d.setUTCHours(d.getUTCHours() + amount)))
case Days => Some(setter(d => d.setUTCDate(d.getUTCDate() + amount)))
case Months => Some(setter(d => d.setUTCMonth(d.getUTCMonth() + amount)))
case Weeks => Some(setter(d => d.setUTCDate(d.getUTCDate() + (amount * 7))))
case Weeks => Some(setter(d => d.setUTCDate(d.getUTCDate() + (amount * DaysInWeek))))
}
}

Expand Down
35 changes: 27 additions & 8 deletions docs/src/main/tut/docs/builtin_libs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ layout: docs
title: "Built-in libs"
---

## Built-in libs
## Built-in libraries and Modules

These are the libraries that are currently supported:
Cron4s provides support out of box for Java 8 Time in its `cron4s-core` module (even in ScalaJS). The integration is
available at `cron4s.lib.javatime` as many examples in this documentation have already shown. In ScalaJS, there is
an additional `cron4s.lib.js` package which provides integration with JavaScript's `Date` object.

### Scala JVM
Support for other libraries is provided via extension modules as follows:

* Java Time API (JSR-310): Package `cron4s.lib.javatime`
* Joda Time: Package `cron4s.lib.joda`
### Joda Time

### ScalaJS
**JVM Only**

* JavaScript `Date` API: Package `cron4s.lib.js`
* Java Time API: Package `cron4s.lib.javatime`. _Support is provided via [scala-java-time](https://github.com/cquiroz/scala-java-time)._
Integration with Joda Time is possible by including the `cron4s-joda` module among your dependencies:

```
libraryDependencies += "com.github.alonsodomin.cron4s" %% "cron4s-joda" % "x.y.z"
```

After that, importing the package `cron4s.lib.joda` should be enough to use your Cron expressions against instances
of Joda's `DateTime`, `LocalDateTime`, `LocalDate` and `LocalTime`.

### MomentJS

**JS Only**

To be able to use Cron4s with MomentJS you need to include the `cron4s-momentjs` module among your dependencies:

```
libraryDependencies += "com.github.alonsodomin.cron4s" %%% "cron4s-momentjs" % "x.y.z"
```

The relevant integration bridge is at package `cron4s.lib.momentjs`.
11 changes: 0 additions & 11 deletions docs/src/main/tut/docs/custom_datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,9 @@ The first thing to to is to provide an implementation of the `cron4s.testkit.Dat
is meant to instruct **cron4s** on how to create arbitrary instances of your date time object:

```tut:silent
import cron4s.CronUnit
import cron4s.testkit.DateTimeTestKitBase
import org.scalacheck.{Arbitrary, Gen}
trait MyTimeTestBase extends DateTimeTestKitBase[MyTime] {
import CronUnit._
override implicit lazy val arbitraryDateTime: Arbitrary[MyTime] = Arbitrary {
for {
second <- Gen.choose(Seconds.min, Seconds.max)
minute <- Gen.choose(Minutes.min, Minutes.max)
hour <- Gen.choose(Hours.min, Hours.max)
} yield MyTime(second, minute, hour)
}
override def createDateTime(seconds: Int, minutes: Int, hours: Int, dayOfMonth: Int, month: Int, year: Int): MyTime =
MyTime(seconds, minutes, hours)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/tut/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Or in ScalaJS:
libraryDependencies += "com.github.alonsodomin.cron4s" %%% "cron4s" % "x.y.z"
```

**cron4s** is cross compiled for Scala 2.11 and Scala 2.12.
**cron4s** is cross compiled for Scala 2.11 and Scala 2.12. Java 8 is required when using it in the JVM.

## License

Expand Down
30 changes: 17 additions & 13 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ object Dependencies {
object version {
val cats = "0.9.0"
val shapeless = "2.3.2"

val momentjs = "0.1.5"
val jodaTime = "2.9.7"
val jodaConvert = "1.8.1"
val fastparse = "0.4.2"

val scalacheck = "1.13.5"
val scalatest = "3.0.1"
val discipline = "0.7.3"
val catalysts = "0.0.5"

val scalaJavaTime = "2.0.0-M9"
val jodaTime = "2.9.7"
val jodaConvert = "1.8.1"
val momentjs = "0.7.0"
val scalaJavaTime = "2.0.0-M10"
}

val macroParadise = compilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
Expand All @@ -37,13 +35,6 @@ object Dependencies {
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % version.scalaJavaTime
}

lazy val coreJVM = Def.settings {
libraryDependencies ++= Seq(
"joda-time" % "joda-time" % version.jodaTime % Optional,
"org.joda" % "joda-convert" % version.jodaConvert % Optional
)
}

lazy val testkit = Def.settings {
libraryDependencies ++= compilerPlugins ++ Seq(
"org.typelevel" %%% "cats" % version.cats,
Expand Down Expand Up @@ -72,4 +63,17 @@ object Dependencies {
)
}

// Dependencies of extension libraries

lazy val joda = Def.settings {
libraryDependencies ++= Seq(
"joda-time" % "joda-time" % version.jodaTime,
"org.joda" % "joda-convert" % version.jodaConvert
)
}

lazy val momentjs = Def.settings(
libraryDependencies += "ru.pavkin" %%% "scala-js-momentjs" % version.momentjs
)

}
14 changes: 12 additions & 2 deletions testkit/src/main/scala/cron4s/testkit/DateTimeTestKitBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@

package cron4s.testkit

import java.time.YearMonth

import cron4s.CronUnit._

import org.scalacheck.{Arbitrary, Gen}

/**
* Created by alonsodomin on 29/08/2016.
*/
trait DateTimeTestKitBase[DateTime] {
implicit def arbitraryDateTime: Arbitrary[DateTime]

protected val yearGen: Gen[Int] = Gen.choose(2016, 2020)
implicit final lazy val arbitraryDateTime: Arbitrary[DateTime] = Arbitrary(for {
second <- Gen.choose(Seconds.min, Seconds.max)
minute <- Gen.choose(Minutes.min, Minutes.max)
hour <- Gen.choose(Hours.min, Hours.max)
year <- Gen.choose(2016, 2020)
yearMonth <- Gen.choose(Months.min, Months.max).map(YearMonth.of(year, _))
dayOfMonth <- Gen.choose(DaysOfMonth.min, yearMonth.lengthOfMonth())
} yield createDateTime(second, minute, hour, dayOfMonth, yearMonth.getMonthValue, year))

protected def createDateTime(seconds: Int, minutes: Int, hours: Int, dayOfMonth: Int, month: Int, year: Int): DateTime

Expand Down
15 changes: 0 additions & 15 deletions tests/js/src/test/scala/cron4s/lib/js/JSTestBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,14 @@

package cron4s.lib.js

import cron4s.CronUnit
import cron4s.testkit.DateTimeTestKitBase

import org.scalacheck.{Arbitrary, Gen}

import scala.scalajs.js.Date

/**
* Created by alonsodomin on 02/09/2016.
*/
trait JSTestBase extends DateTimeTestKitBase[Date] {
import CronUnit._

implicit lazy val arbitraryDateTime = Arbitrary(for {
seconds <- Gen.choose(Seconds.min, Seconds.max)
minutes <- Gen.choose(Minutes.min, Minutes.max)
hours <- Gen.choose(Hours.min, Hours.max)
month <- Gen.choose(Months.min, Months.max)
// Prevents choosing days in the 30-31 range, which cause non-deterministic results
dayOfMonth <- if (month == 2) Gen.choose(DaysOfMonth.min, 28)
else Gen.choose(DaysOfMonth.min, 30)
year <- yearGen
} yield createDateTime(seconds, minutes, hours, dayOfMonth, month, year))

protected def createDateTime(seconds: Int, minutes: Int, hours: Int, dayOfMonth: Int, month: Int, year: Int): Date =
new Date(Date.UTC(year, month - 1, dayOfMonth, hours, minutes, seconds, ms = 0))
Expand Down
97 changes: 0 additions & 97 deletions tests/jvm/src/test/scala/cron4s/lib/joda/JodaTestBase.scala

This file was deleted.

Loading

0 comments on commit 781397b

Please sign in to comment.