-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
137 lines (118 loc) · 5.1 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
import scala.sys.process._
import java.io.IOException
val appVersion= "1.0"
lazy val scalaV2 = "2.13.12"
lazy val scalaV3 = "3.3.1"
val bootstrapVersion = "5.3.2"
import com.typesafe.sbt.web.PathMapping
import com.typesafe.sbt.web.pipeline.Pipeline
val removeLibs = taskKey[Pipeline.Stage]("Removes libraries")
val moveLibs = taskKey[Pipeline.Stage]("Moves libraries")
val removeUnversionedAssets = taskKey[Pipeline.Stage]("Removes unversioned assets")
val genHtmlDir = settingKey[File]("Output directory for HTML generated files")
val PublicDirName = "statistiky" // TODO: DRY this constant from routes
val YearDir = "^[0-9]+(?:$|/.*)".r
def write(file: File, content: String) = {
println(s"Writing $file…")
IO.write(file, content)
file
}
def download(out: File, source: URL) = {
if(out.exists) {
println(s"Source $source is already downloaded at $out.")
} else {
val tmpFile = file(out + ".tmp")
println(s"Downloading $source…")
tmpFile.getParentFile.mkdirs()
source #> tmpFile !;
if(! tmpFile.renameTo(out)) {
throw new IOException(s"Renaming $tmpFile to $out failed!")
}
println(s"Downloaded $source as $out")
}
out
}
// Generates other assets than client JS, plus contains a server for development purposes
lazy val server = (project in file("server")).settings(
version := appVersion,
name := "zbdb-stats-server",
scalacOptions ++= Seq("-deprecation", "-feature"),
scalaVersion := scalaV2,
scalaJSProjects := Seq(client),
Assets / pipelineStages := Seq(scalaJSPipeline),
pipelineStages := Seq(concat, removeLibs, filter, digest, simpleUrlUpdate/*, digest*/, removeUnversionedAssets, moveLibs),
digest / includeFilter := "*",
digest / excludeFilter := "*.html" || "*.csv" || "*.json" || "*.json.new",
filter / excludeFilter := "*.scss" || "*.note" || "*.source" || "*.css" - "main.css" || "*.js" - "main.min.js",
filter / includeFilter := "*.css" || "*.html" || "*.js" || "*.csv" || "*.svg" || "*.woff" || "*.ttf" || "*.eot" || "*.woff2" || "*.json.new",
genHtmlDir := target.value / "web" / "html" / "main",
Assets / resourceDirectories += genHtmlDir.value,
Assets / resourceGenerators += Def.task {
val yearHtmlFiles = for(year <- PageGenerator.Years) yield {
write(
file = genHtmlDir.value / s"${year.year}" / PublicDirName / s"index.html",
content = PageGenerator.forYear(year, PublicDirName)
)
}
val allYearsListJsonFile = write(genHtmlDir.value / "years.json.new", PageGenerator.allYearsJsonString)
yearHtmlFiles :+ allYearsListJsonFile
}.taskValue,
Assets / resourceGenerators += Def.task {
for(year <- PageGenerator.Years if year.dataSource.csvDownloadUrl startsWith "https://") yield {
download(
out = genHtmlDir.value / s"${year.year}" / PublicDirName / s"${year.year}.csv",
source = url(year.dataSource.csvDownloadUrl)
)
}
}.taskValue,
removeUnversionedAssets := { mappings: Seq[PathMapping] =>
mappings.filter{case (file, name) => !(name.startsWith("main.") )}
},
removeLibs := { mappings: Seq[PathMapping] => // Most of libs are already included in a CSS/JS file, so skip them
mappings.filter{case (file, name) => (!name.startsWith("lib/")) || name.startsWith("lib/bootstrap/fonts")}
},
moveLibs := { mappings: Seq[PathMapping] =>
mappings.map {
case other @ (_, YearDir()) => other
case (file, name) => (file, PublicDirName + "/" + name)
}
},
simpleUrlUpdate / includeFilter := "*.css" || "*.js" || "*.html",
// triggers scalaJSPipeline when using compile or continuous compilation
Compile / compile := ((Compile / compile) dependsOn scalaJSPipeline).value,
Concat.groups := Seq("main.min.js" -> group(Seq("zbdb-stats-client-opt-bundle.js"))),
libraryDependencies ++= Seq(
"com.vmunier" %% "scalajs-scripts" % "1.2.0",
guice,
"org.webjars" % "bootstrap" % bootstrapVersion,
specs2 % Test
),
).enablePlugins(PlayScala, SbtWeb, WebScalaJSBundlerPlugin)
// Generates client JS; other assets are generated by the server subproject
lazy val client = (project in file("client")).settings(
name := "zbdb-stats-client",
version := appVersion,
scalacOptions ++= Seq("-deprecation", "-feature"),
scalaJSStage := FullOptStage,
scalaVersion := scalaV3,
scalaJSUseMainModuleInitializer := true,
Test / scalaJSUseMainModuleInitializer := false,
webpack / version := "5.88.2", // https://github.com/ScalablyTyped/Converter/issues/546
webpackConfigFile := Some(baseDirectory.value / "custom.webpack.config.js"),
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "2.7.0",
"com.lihaoyi" %%% "scalatags" % "0.12.0",
),
Compile / npmDependencies ++= Seq(
"comma-separated-values" -> "3.6.4",
"bootstrap" -> bootstrapVersion,
"moment" -> "2.10.6",
"moment-timezone" -> "0.4.0",
"chart.js" -> "4.4.0",
"chartjs-adapter-moment" -> "1.0.1",
),
stIgnore ++= List("moment", "moment-timezone", "bootstrap", "chart.js", "chartjs-adapter-moment",
"comma-separated-values"),
).enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin, ScalablyTypedConverterPlugin)
name := "zbdb-stats"
version := appVersion