-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.sbt
405 lines (370 loc) · 14.3 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import scala.scalanative.build.*
lazy val `scala-2.13` = "2.13.16"
lazy val `scala-3` = "3.3.5"
ThisBuild / tlBaseVersion := "0.5"
ThisBuild / startYear := Some(2023)
ThisBuild / scalaVersion := `scala-3`
ThisBuild / crossScalaVersions := Seq(`scala-3`, `scala-2.13`)
ThisBuild / organization := "dev.rolang"
ThisBuild / licenses := Seq(License.MIT)
ThisBuild / developers := List(
Developer(id = "rolang", name = "Roman Langolf", email = "[email protected]", url = url("https://rolang.dev"))
)
ThisBuild / versionScheme := Some("early-semver")
ThisBuild / description := "Simple database migration tool for Scala + Postgres"
ThisBuild / homepage := Some(url("https://github.com/rolang/dumbo"))
ThisBuild / scalafmt := true
ThisBuild / scalafmtSbtCheck := true
ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision // use Scalafix compatible version
// githubWorkflow
lazy val macOsArm = "macos-14"
lazy val macOsIntel = "macos-13"
lazy val macOses = Seq(macOsIntel, macOsArm)
ThisBuild / githubWorkflowOSes ++= Seq(macOsIntel, macOsArm)
ThisBuild / githubWorkflowBuildMatrixExclusions ++= macOses.map(os =>
MatrixExclude(Map("os" -> os, "project" -> "rootJVM"))
)
ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("21"), JavaSpec.temurin("17"))
ThisBuild / tlCiHeaderCheck := true
ThisBuild / tlCiScalafixCheck := false
lazy val llvmVersion = "17"
lazy val brewFormulas = Set("s2n", "utf8proc")
lazy val isTagCond = "startsWith(github.ref, 'refs/tags/')"
ThisBuild / githubWorkflowBuildPreamble ++= Seq(
WorkflowStep.Run(
commands = List(
s"/home/linuxbrew/.linuxbrew/bin/brew install llvm@$llvmVersion ${brewFormulas.mkString(" ")}",
s"""echo "LLVM_BIN=/home/linuxbrew/.linuxbrew/opt/llvm@$llvmVersion/bin" >> $$GITHUB_ENV""",
),
cond = Some("(matrix.project == 'rootNative') && startsWith(matrix.os, 'ubuntu')"),
name = Some("Install native dependencies (ubuntu)"),
)
)
ThisBuild / githubWorkflowBuildPreamble ++= List(
macOsIntel -> "/usr/local/opt",
macOsArm -> "/opt/homebrew/opt",
).map { case (os, llvmBase) =>
WorkflowStep.Run(
commands = List(
s"brew install llvm@$llvmVersion ${brewFormulas.mkString(" ")}",
s"""echo "LLVM_BIN=$llvmBase/llvm@$llvmVersion/bin" >> $$GITHUB_ENV""",
),
cond = Some(s"(matrix.project == 'rootNative') && matrix.os == '$os'"),
name = Some(s"Install native dependencies ($os)"),
)
}
ThisBuild / githubWorkflowJobSetup ++= Seq(
WorkflowStep.Run(
commands = List("docker compose up -d"),
name = Some("Start up Postgres"),
cond = Some("startsWith(matrix.os, 'ubuntu')"),
)
)
ThisBuild / githubWorkflowBuild := {
WorkflowStep.Sbt(
List("Test/copyResources; scalafixAll --check; all scalafmtSbtCheck scalafmtCheckAll"),
name = Some("Check scalafix/scalafmt lints"),
cond = Some(
"matrix.java == 'temurin@21' && (matrix.scala == '3') && matrix.project == 'rootJVM' && startsWith(matrix.os, 'ubuntu')"
),
) +: (ThisBuild / githubWorkflowBuild).value
}
// override Test step to run on ubuntu only due to requirement of docker
ThisBuild / githubWorkflowBuild := {
(ThisBuild / githubWorkflowBuild).value.map {
case s if s.name.contains("Test") =>
WorkflowStep.Sbt(
List("test"),
name = Some("Test"),
cond = Some("startsWith(matrix.os, 'ubuntu')"),
)
case s => s
}
}
ThisBuild / githubWorkflowBuild += WorkflowStep.Run(
commands = List("sbt cliNative/test"),
name = Some("CLI test"),
cond = Some("matrix.project == 'rootNative' && (matrix.scala == '3')"),
)
ThisBuild / githubWorkflowBuild ++= List(
"ubuntu" -> Map("SCALANATIVE_LTO" -> LTO.thin.toString()),
"macos" -> Map.empty,
).flatMap { case (os, envs) =>
val base = WorkflowStep.Run(
commands = List("sbt buildCliBinary"),
name = Some(s"Generate CLI native binary"),
cond = Some(s"matrix.project == 'rootNative' && (matrix.scala == '3') && startsWith(matrix.os, '$os')"),
)
List(
// debug mode
base.withName(base.name.map(_ + s" ($os, debug)")).withCond(base.cond.map(_ + s" && !$isTagCond")),
// release mode (takes long time...)
base
.withName(base.name.map(_ + s" ($os, release)"))
.withEnv(env = Map("SCALANATIVE_MODE" -> Mode.releaseFast.toString()) ++ envs)
.withCond(base.cond.map(_ + s" && $isTagCond")),
)
}
ThisBuild / githubWorkflowBuild += WorkflowStep.Use(
ref = UseRef.Public("actions", "upload-artifact", "v4"),
params = Map("name" -> "cli-bin-${{ matrix.os }}", "path" -> "modules/cli/native/target/bin/*"),
name = Some("Upload command line binaries"),
cond = Some(s"matrix.project == 'rootNative' && (matrix.scala == '3') && $isTagCond"),
)
ThisBuild / githubWorkflowGeneratedCI := (ThisBuild / githubWorkflowGeneratedCI).value.flatMap {
case j if j.id == "publish" =>
List(
j,
WorkflowJob(
id = "publish-cli-bin",
name = "Publish command line binaries",
needs = List("build"),
cond = Some(isTagCond),
scalas = Nil,
javas = List(JavaSpec.temurin("21")),
steps = List(
WorkflowStep.Use(
ref = UseRef.Public("actions", "download-artifact", "v4"),
params = Map("pattern" -> "cli-bin-*", "path" -> "target-cli/bin", "merge-multiple" -> "true"),
name = Some("Download command line binaries"),
),
WorkflowStep.Use(
ref = UseRef.Public("softprops", "action-gh-release", "v1"),
name = Some("Upload release binaries"),
params = Map("files" -> "target-cli/bin/*"),
),
),
),
WorkflowJob(
id = "publish-cli-docker",
name = "Publish command line docker image",
needs = List("build"),
cond = Some(isTagCond),
scalas = Nil,
javas = List(JavaSpec.temurin("21")),
steps = List(
WorkflowStep.Checkout,
WorkflowStep.Use(
name = Some("Download command line linux build"),
ref = UseRef.Public("actions", "download-artifact", "v4"),
params = Map("name" -> "target-${{ matrix.os }}-${{ matrix.java }}-3-rootNative"),
),
WorkflowStep.Run(
name = Some("Inflate command line linux build"),
commands = List("tar xf targets.tar", "rm targets.tar"),
),
WorkflowStep.Run(
name = Some("Release docker image"),
commands = List(
"""echo -n "${DOCKER_PASSWORD}" | docker login docker.io -u rolang --password-stdin""",
"export RELEASE_TAG=${GITHUB_REF_NAME#'v'}",
"cp -r modules/cli/native/target/bin docker-build/bin",
"docker build ./docker-build -t rolang/dumbo:${RELEASE_TAG}-alpine",
"docker run rolang/dumbo:${RELEASE_TAG}-alpine", // run for health-checking the docker image
"docker tag rolang/dumbo:${RELEASE_TAG}-alpine rolang/dumbo:latest-alpine",
"docker push rolang/dumbo:${RELEASE_TAG}-alpine",
"docker push rolang/dumbo:latest-alpine",
),
env = Map(
"DOCKER_PASSWORD" -> "${{ secrets.DOCKER_PASSWORD }}"
),
),
),
),
)
case j => List(j)
}
ThisBuild / githubWorkflowBuild += WorkflowStep.Sbt(
List("example/run"),
name = Some("Run example (covers reading resources from a jar at runtime)"),
cond = Some("matrix.project == 'rootJVM'"),
)
ThisBuild / githubWorkflowBuild += WorkflowStep.Run(
commands = List("sbt -Dsample_lib_test 'testLib/clean; sampleLib/publishLocal; testLib/run'"),
name = Some("Test reading resources from a jar at compile time"),
cond = Some("matrix.project == 'rootJVM' && matrix.scala == '3'"),
)
addCommandAlias("fix", "; +Test/copyResources; +scalafixAll; +scalafmtAll; scalafmtSbt")
addCommandAlias("check", "; +Test/copyResources; +scalafixAll --check; +scalafmtCheckAll; scalafmtSbtCheck")
lazy val commonSettings = List(
// Headers
headerMappings := headerMappings.value + (HeaderFileType.scala -> HeaderCommentStyle.cppStyleLineComment),
headerLicense := Some(
HeaderLicense.Custom(
"""|Copyright (c) 2023 by Roman Langolf
|This software is licensed under the MIT License (MIT).
|For more information see LICENSE or https://opensource.org/licenses/MIT
|""".stripMargin
)
),
libraryDependencies ++= {
if (scalaVersion.value == `scala-3`)
Seq()
else
Seq(
compilerPlugin(("org.typelevel" %% "kind-projector" % "0.13.3").cross(CrossVersion.full)),
compilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"),
)
},
Compile / scalacOptions ++= {
if (scalaVersion.value == `scala-3`)
Seq("-source:future")
else
Seq("-Xsource:3")
},
)
lazy val root = tlCrossRootProject
.settings(name := "dumbo")
.aggregate(core, tests, testsFlyway, example)
.settings(commonSettings)
lazy val skunkVersion = "1.0.0-M10"
lazy val epollcatVersion = "0.1.6"
lazy val munitVersion = "1.0.0"
lazy val munitCEVersion = "2.0.0"
lazy val core = crossProject(JVMPlatform, NativePlatform)
.crossType(CrossType.Full)
.enablePlugins(AutomateHeaderPlugin, BuildInfoPlugin)
.in(file("modules/core"))
.settings(
name := "dumbo",
libraryDependencies ++= Seq(
"org.tpolecat" %%% "skunk-core" % skunkVersion
),
buildInfoPackage := "dumbo",
buildInfoKeys := {
val isNative = crossProjectPlatform.value.identifier == "native"
Seq[BuildInfoKey](
version,
scalaVersion,
scalaBinaryVersion,
) ++ (if (isNative) Seq(BuildInfoKey("scalaNativeVersion" -> nativeVersion)) else Seq())
},
)
.settings(commonSettings)
lazy val cli = crossProject(NativePlatform)
.crossType(CrossType.Full)
.enablePlugins(AutomateHeaderPlugin)
.in(file("modules/cli"))
.dependsOn(core)
.settings(
scalaVersion := `scala-3`,
name := "dumbo-cli",
libraryDependencies ++= Seq(
"org.scalameta" %%% "munit" % munitVersion % Test
),
)
.settings(commonSettings)
.nativeEnablePlugins(ScalaNativeBrewedConfigPlugin)
.nativeSettings(
libraryDependencies += "com.armanbilge" %%% "epollcat" % epollcatVersion,
nativeBrewFormulas ++= brewFormulas,
)
lazy val cliNative = cli.native
lazy val buildCliBinary = taskKey[File]("")
buildCliBinary := {
def normalise(s: String) = s.toLowerCase.replaceAll("[^a-z0-9]+", "")
val props = sys.props.toMap
val os = normalise(props.getOrElse("os.name", "")) match {
case p if p.startsWith("linux") => "linux"
case p if p.startsWith("windows") => "windows"
case p if p.startsWith("osx") || p.startsWith("macosx") => "macosx"
case _ => "unknown"
}
val arch = (
normalise(props.getOrElse("os.arch", "")),
props.getOrElse("sun.arch.data.model", "64"),
) match {
case ("amd64" | "x64" | "x8664" | "x86", bits) => s"x86_${bits}"
case ("aarch64" | "arm64", bits) => s"aarch$bits"
case _ => "unknown"
}
val name = s"dumbo-cli-$arch-$os"
val built = (cliNative / Compile / nativeLink).value
val destBin = (cliNative / target).value / "bin" / name
val destZip = (cliNative / target).value / "bin" / s"$name.zip"
IO.copyFile(built, destBin)
sLog.value.info(s"Built cli binary in $destBin")
IO.zip(Seq((built, "dumbo")), destZip, None)
sLog.value.info(s"Built cli binary zip in $destZip")
destBin
}
lazy val tests = crossProject(JVMPlatform, NativePlatform)
.crossType(CrossType.Full)
.enablePlugins(AutomateHeaderPlugin, NoPublishPlugin)
.in(file("modules/tests"))
.dependsOn(core)
.settings(commonSettings)
.settings(
publish / skip := true,
Test / scalacOptions -= "-Werror",
libraryDependencies ++= Seq(
"org.scalameta" %%% "munit" % munitVersion,
"org.typelevel" %%% "munit-cats-effect" % munitCEVersion,
),
testFrameworks += new TestFramework("munit.Framework"),
testOptions += {
if (System.getProperty("os.arch").startsWith("aarch64")) {
Tests.Argument(TestFrameworks.MUnit, "--exclude-tags=X86ArchOnly")
} else Tests.Argument()
},
)
.nativeEnablePlugins(ScalaNativeBrewedConfigPlugin)
.nativeSettings(
libraryDependencies += "com.armanbilge" %%% "epollcat" % epollcatVersion,
Test / nativeBrewFormulas ++= brewFormulas,
Test / envVars ++= Map("S2N_DONT_MLOCK" -> "1"),
nativeConfig ~= {
_.withEmbedResources(true)
},
)
lazy val flywayVersion = "11.0.0"
lazy val postgresqlVersion = "42.7.5"
lazy val testsFlyway = project
.in(file("modules/tests-flyway"))
.enablePlugins(AutomateHeaderPlugin, NoPublishPlugin)
.dependsOn(core.jvm, tests.jvm % "compile->compile;test->test")
.settings(commonSettings)
.settings(
publish / skip := true,
Test / scalacOptions -= "-Werror",
libraryDependencies ++= Seq(
"org.flywaydb" % "flyway-core" % flywayVersion,
"org.flywaydb" % "flyway-database-postgresql" % flywayVersion,
"org.postgresql" % "postgresql" % postgresqlVersion,
),
testFrameworks += new TestFramework("munit.Framework"),
)
lazy val example = project
.in(file("modules/example"))
.enablePlugins(NoPublishPlugin)
.dependsOn(core.jvm)
.settings(commonSettings)
.settings(
Compile / run / fork := true,
Compile / headerCheck := Nil,
scalacOptions -= "-Werror",
)
lazy val sampleLib = project
.in(file("modules/sample-lib"))
.enablePlugins((if (!sys.props.contains("sample_lib_test")) Seq(NoPublishPlugin) else Nil) *)
.settings(
version := "0.0.1-SNAPSHOT",
scalaVersion := `scala-3`,
crossScalaVersions := Seq(`scala-3`),
name := "sample-lib",
Compile / headerCheck := Nil,
)
lazy val testLib = project
.in(file("modules/test-lib"))
.enablePlugins(NoPublishPlugin)
.settings(commonSettings)
.dependsOn(core.jvm)
.settings(
Compile / run / fork := true,
libraryDependencies += "dev.rolang" %% "sample-lib" % "0.0.1-SNAPSHOT",
scalaVersion := `scala-3`,
crossScalaVersions := Seq(`scala-3`),
name := "sample-lib-test",
Compile / headerCheck := Nil,
)