forked from soc/scala-java-time
-
Notifications
You must be signed in to change notification settings - Fork 33
/
build.sbt
267 lines (247 loc) · 9.17 KB
/
build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
val scala213 = "2.13.14"
val scala3 = "3.3.3"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := Seq("2.12.17", scala213, scala3)
ThisBuild / tlBaseVersion := "2.6"
ThisBuild / githubWorkflowBuildMatrixFailFast := Some(false)
val javaDistro = JavaSpec.corretto("11")
ThisBuild / githubWorkflowJavaVersions := Seq(javaDistro)
ThisBuild / githubWorkflowSbtCommand := "./sbt"
ThisBuild / githubWorkflowBuildMatrixExclusions ++= Seq(
MatrixExclude(Map("scala" -> "3", "project" -> "rootJVM")), // TODO
MatrixExclude(
Map("scala" -> "3", "project" -> "rootNative", "os" -> "ubuntu-latest")
) // run on macOS instead
)
ThisBuild / githubWorkflowBuildMatrixInclusions +=
MatrixInclude(Map("scala" -> "3", "java" -> javaDistro.render, "project" -> "rootNative"),
Map("os" -> "macos-latest")
)
val tzdbVersion = "2019c"
val scalajavaLocalesVersion = "1.5.4"
Global / onChangedBuildSource := ReloadOnSourceChanges
lazy val downloadFromZip: TaskKey[Unit] =
taskKey[Unit]("Download the tzdb tarball and extract it")
inThisBuild(
List(
organization := "io.github.cquiroz",
licenses := Seq("BSD 3-Clause License" -> url("https://opensource.org/licenses/BSD-3-Clause")),
developers := List(
Developer("cquiroz",
"Carlos Quiroz",
url("https://github.com/cquiroz")
)
),
tlSonatypeUseLegacyHost := true,
tlMimaPreviousVersions := Set(),
tlCiReleaseBranches := Seq("master"),
tlCiHeaderCheck := false
)
)
lazy val root = tlCrossRootProject.aggregate(core, tzdb, tests, demo)
lazy val commonSettings = Seq(
description := "java.time API implementation in Scala and Scala.js",
versionScheme := Some("always"),
// Don't include threeten on the binaries
Compile / packageBin / mappings := (Compile / packageBin / mappings).value.filter { case (_, s) =>
!s.contains("threeten")
},
Compile / packageSrc / mappings := (Compile / packageSrc / mappings).value.filter { case (_, s) =>
!s.contains("threeten")
},
Compile / scalacOptions ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, scalaMajor)) if scalaMajor == 13 =>
Seq("-deprecation:false")
case _ =>
Seq.empty
}
},
Compile / doc / scalacOptions := {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, scalaMajor)) if scalaMajor >= 11 =>
Seq("-deprecation:false")
case _ =>
Seq.empty
}
},
scalacOptions --= {
if (tlIsScala3.value)
List(
"-Xfatal-warnings",
"-source:3.0-migration"
)
else
List(
)
},
javaOptions ++= Seq("-Dfile.encoding=UTF8"),
Compile / doc / sources := Seq()
)
/**
* 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).allPaths.pair(Path.rebase(source, target)).toTraversable,
overwrite,
preserveLastModified,
false
)
val onlyScalaDirs = srcDirs.filter(_.getName.matches(".*scala(-\\d)?"))
// 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)
.filter(_.getName.endsWith(".scala"))
.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(\..*)?(\.[A-Z_{][^\.]*)""", "import java.time$1$2")
.replaceAll("import zonedb.threeten", "import zonedb.java")
.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 core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("core"))
.settings(commonSettings)
.settings(
name := "scala-java-time",
libraryDependencies += ("org.portable-scala" %%% "portable-scala-reflect" % "1.1.3")
.cross(CrossVersion.for3Use2_13)
)
.jsSettings(
scalacOptions ++= {
if (tlIsScala3.value) Seq("-scalajs-genStaticForwardersForNonTopLevelObjects")
else Seq("-P:scalajs:genStaticForwardersForNonTopLevelObjects")
},
Compile / sourceGenerators += Def.task {
val srcDirs = (Compile / sourceDirectories).value
val destinationDir = (Compile / sourceManaged).value
copyAndReplace(srcDirs, destinationDir)
}.taskValue,
libraryDependencies ++= Seq(
"io.github.cquiroz" %%% "scala-java-locales" % scalajavaLocalesVersion
)
)
.nativeSettings(
scalacOptions += "-P:scalanative:genStaticForwardersForNonTopLevelObjects",
Compile / sourceGenerators += Def.task {
val srcDirs = (Compile / sourceDirectories).value
val destinationDir = (Compile / sourceManaged).value
copyAndReplace(srcDirs, destinationDir)
}.taskValue,
libraryDependencies ++= Seq(
"io.github.cquiroz" %%% "scala-java-locales" % scalajavaLocalesVersion
)
)
lazy val tzdb = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("tzdb"))
.settings(commonSettings)
.settings(
name := "scala-java-time-tzdb",
includeTTBP := true,
dbVersion := TzdbPlugin.Version(tzdbVersion)
)
.jsSettings(
Compile / sourceGenerators += Def.task {
val srcDirs = (Compile / sourceManaged).value
val destinationDir = (Compile / sourceManaged).value
copyAndReplace(Seq(srcDirs), destinationDir)
}.taskValue
)
.nativeSettings(
tzdbPlatform := TzdbPlugin.Platform.Native,
Compile / sourceGenerators += Def.task {
val srcDirs = (Compile / sourceManaged).value
val destinationDir = (Compile / sourceManaged).value
copyAndReplace(Seq(srcDirs), destinationDir)
}.taskValue
)
.jvmSettings(
tzdbPlatform := TzdbPlugin.Platform.Jvm
)
.dependsOn(core)
.enablePlugins(TzdbPlugin)
lazy val tests = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("tests"))
.enablePlugins(NoPublishPlugin)
.settings(commonSettings)
.settings(
name := "tests",
Keys.`package` := file(""),
libraryDependencies +=
"org.scalatest" %%% "scalatest" % "3.2.18" % Test,
scalacOptions ~= (_.filterNot(
Set("-Wnumeric-widen", "-Ywarn-numeric-widen", "-Ywarn-value-discard", "-Wvalue-discard")
))
)
.jvmSettings(
// Fork the JVM test to ensure that the custom flags are set
Test / fork := true,
Test / baseDirectory := baseDirectory.value.getParentFile,
// Use CLDR provider for locales
// https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
Test / javaOptions ++= Seq("-Duser.language=en",
"-Duser.country=US",
"-Djava.locale.providers=CLDR"
),
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat
)
.platformsSettings(JSPlatform, NativePlatform)(
Test / parallelExecution := false,
Test / sourceGenerators += Def.task {
val srcDirs = (Test / sourceDirectories).value
val destinationDir = (Test / sourceManaged).value
copyAndReplace(srcDirs, destinationDir)
}.taskValue,
libraryDependencies ++= Seq(
"io.github.cquiroz" %%% "locales-full-db" % scalajavaLocalesVersion
)
)
.dependsOn(core, tzdb)
val zonesFilterFn = (x: String) => x == "Europe/Helsinki" || x == "America/Santiago"
lazy val demo = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("demo"))
.dependsOn(core)
.enablePlugins(TzdbPlugin, NoPublishPlugin)
.settings(
name := "demo",
Keys.`package` := file(""),
zonesFilter := zonesFilterFn,
dbVersion := TzdbPlugin.Version(tzdbVersion),
// delegate test to run, so that it is invoked during test step in ci
Test / test := (Compile / run).toTask("").value
)
.jsSettings(
scalaJSUseMainModuleInitializer := true
)
.jvmSettings(
tzdbPlatform := TzdbPlugin.Platform.Jvm
)
.nativeSettings(
tzdbPlatform := TzdbPlugin.Platform.Native
)