-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathbuild.sbt
More file actions
326 lines (264 loc) · 13.9 KB
/
Copy pathbuild.sbt
File metadata and controls
326 lines (264 loc) · 13.9 KB
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
import sbt._
import Process._
import Keys._
import complete.DefaultParsers._
Global / onChangedBuildSource := ReloadOnSourceChanges
lazy val hello = taskKey[Unit]("Prints welcome message")
hello := println("""
======= WELCOME to the sbt build of lunduniversity/introprog =========
type 'build' for a complete pdf build including dependent gen tasks
DON'T PANIC: a full build can take >200sec on a 2.8GHz machine...
type 'pdf' generate all pdf files
type 'pdf<TAB>' to see individual pdf build commands
type 'gen' to generate plan files
type 'genquiz' to generate quiz files
type 'gengloss' to generate glossary files
type 'projects' to see all sub-projects
type 'project workspace' to change to sub-project workspace
type 'scalaVersion' to see all versions in (sub)projects
type 'hello' to see this message
--- English mirror (autotranslate sub-project) ---
NOTE: depends on ollama to be run locally or
using codeberg.org/bjornregnell/modly on LAN
if cache is dropped with --clean
type 'autotranslate' to (re)build the English mirror compendium-en/ and slides-en/
(copies .tex as X-en.tex, rewrites \input + assets; content NOT translated by this task)
for translation run e.g. 'autotranslateProject/run --only w01' (also --all),
'... --clean' (drop cache), '--selftest', '--dryrun', '--latextest'
translation backend + model is set in autotranslate/Translate.scala (SelectedModel);
uses the modly GPU server if reachable, else local Ollama, else keeps Swedish
type 'pdfCompendiumEn' to build the English compendium-en/compendium-en.pdf
type 'pdfSlidesEn w01' to build an English lecture, e.g. slides-en/lect-w01-en.pdf
=====================================================================
""")
lazy val myStartupTransition: State => State = { s: State =>
"hello" :: s
}
lazy val commonSettings = Seq(
organization := "se.lth.cs",
version := "2024.0.2",
scalaVersion := "3.8.3",
scalacOptions ++= Seq("-deprecation", "-feature")
)
lazy val plan = (project in file("plan")).settings(commonSettings: _*).
dependsOn(glossary). // plan reads glossary.explain to generate translations for muntabot
settings(
name := "plan",
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.10.2",
)
lazy val quiz = (project in file("quiz")).settings(commonSettings: _*).
settings(
name := "quiz",
)
lazy val workspace = (project in file("workspace")).settings(commonSettings: _*).
settings(
name := "workspace",
)
lazy val glossary = (project in file("glossary")).settings(commonSettings: _*).
settings(
name := "glossary",
)
lazy val autotranslateProject = (project in file("autotranslate")).settings(commonSettings: _*).
dependsOn(glossary). // typed access to glossary.explain.allConcepts (authoritative sv<->en)
settings(
name := "autotranslate",
libraryDependencies ++= Seq(
"com.lihaoyi" %% "os-lib" % "0.11.8",
"com.lihaoyi" %% "requests" % "0.9.3",
"com.lihaoyi" %% "ujson" % "4.4.3",
),
)
// `build` is a COMMAND alias (not a task): each step runs as its own command,
// so the final `gen` re-runs AFTER `pdf`. (As a task, `gen` is one node in the
// graph and sbt evaluates it once — the second `gen` would be skipped.) This
// makes headings-GENERATED.scala reflect the freshly built compendium pages.
addCommandAlias("build", "gen; genquiz; gengloss; pdf; gen")
lazy val gen = taskKey[Unit]("alias for plan/run")
gen := (plan/Compile/run).toTask("").value
lazy val genquiz = taskKey[Unit]("alias for quiz/run")
genquiz := (quiz/Compile/run).toTask("").value
lazy val gengloss = taskKey[Unit]("alias for glossary/run")
gengloss := (glossary/Compile/run).toTask("").value
lazy val autotranslate = taskKey[Unit]("alias for autotranslate/run")
autotranslate := (autotranslateProject/Compile/run).toTask("").value
// ************** cmd util functions
def showTail(fileName: String, n: Int = 40): Unit = {
// this method was created as tail does not work on windows
println(s"--- Last $n lines of $fileName: ")
val lines = sbt.io.IO.readLines(new java.io.File(fileName))
println(lines.takeRight(n).mkString("\n"))
}
def runPdfLatexCmd(texFile: File, workDir: File, stdOutSuffix: String = "-console.log", maxPasses: Int = 1): Unit = {
val cmd = scala.sys.process.Process(
Seq("pdflatex","-halt-on-error", texFile.getName),
workDir
)
val cmdOutputFile = workDir / texFile.getName.replace(".tex", stdOutSuffix)
// val bibtexCmd = Process(Seq("bibtex", texFile.getName.replace(".tex", ".aux")), workDir)
// Run pdflatex until the .toc / cross-refs converge, i.e. until LaTeX stops asking to rerun — capped at
// maxPasses (like a tiny latexmk; avoids guessing 2 vs 3). The Swedish tasks default to 1 because their working
// dir (compendium/, slides/) keeps .aux/.toc across builds; the English mirror dirs are regenerated fresh every
// run, so pass 1 writes the .toc but never reads it (no ToC), pass 2 typesets it (which shifts pages), and a
// 3rd pass may be needed for the ToC/\pageref page numbers to settle. En tasks pass a cap of 4.
println(s" ******* Compiling $texFile to pdf (up to $maxPasses pass(es)) *******")
var exitValue = 0; var pass = 0; var rerun = true
while (pass < math.max(1, maxPasses) && exitValue == 0 && rerun) {
exitValue = cmd.#>(cmdOutputFile).run.exitValue
pass += 1
rerun = exitValue == 0 && {
val log = scala.util.Try(IO.read(cmdOutputFile)).getOrElse("")
log.contains("Rerun to get") || log.contains("Label(s) may have changed")
}
}
println(s" ($pass pdflatex pass(es) run)")
if (exitValue != 0) {
println("*** ############ ERROR LOG STARTS HERE ############### ***")
//Process(Seq("cat", cmdOutputFile.getName), workDir).run
//scala.sys.process.Process(Seq("tail", "-40", cmdOutputFile.getName), workDir).run
showTail(s"$cmdOutputFile")
sys.error(s"\n*** ERROR: pdflatex exit code: $exitValue\nSee COMPLETE pdflatex output in: $cmdOutputFile")
} else println(s" Log file: $cmdOutputFile")
}
// **************
lazy val pdf = taskKey[Unit]("Latex all pdfs several times for xrefs & tocs to work)")
pdf := {
println("\n====== Compiling pdf documents -- this may take several minutes!")
println("\n=== Compiling slides:")
val workDir = file("slides")
val texFiles = (workDir * "*.tex").get
for (texFile <- texFiles) {
runPdfLatexCmd(texFile, workDir)
}
println("\n=== The main doc with all stuff in one pdf optimized for screen:")
runPdfLatexCmd(texFile = file("compendium.tex"), workDir = file("compendium"))
println("\n=== Docs optimized for print, two times for external xref to work:")
runPdfLatexCmd(texFile = file("compendium1.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("compendium2.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("compendium1.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("compendium2.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("lectures.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("exercises.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("labs.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("assignments.tex"), workDir = file("compendium"))
runPdfLatexCmd(texFile = file("solutions.tex"), workDir = file("compendium"))
}
//http://www.scala-sbt.org/0.13/docs/Howto-Triggered.html
// This does not seem to work on sbt 1.1 :( as pdf task are not triggered on change anymore
//watchSources ++= ((baseDirectory.value / "compendium") * "*.tex").get
//Found workaround here thanks to eatkins:
//https://github.com/sbt/sbt/issues/4272
def ws(base: sbt.io.syntax.File, includeFilter: sbt.io.FileFilter): sbt.internal.io.Source =
WatchSource(base, includeFilter, excludeFilter=NothingFilter)
// expands to e.g.:
//watchSources += WatchSource(baseDirectory.value / "compendium","*.tex", NothingFilter)
watchSources += ws(baseDirectory.value / "compendium", "*.tex")
watchSources += ws(baseDirectory.value / "compendium", "*.cls")
watchSources += ws(baseDirectory.value / "compendium" / "modules", "*.tex")
watchSources += ws(baseDirectory.value / "compendium" / "prechapters", "*.tex")
watchSources += ws(baseDirectory.value / "compendium" / "postchapters", "*.tex")
watchSources += ws(baseDirectory.value / "slides" / "body", "*.tex")
watchSources += ws(baseDirectory.value / "slides", "*.tex")
lazy val pdfExercises = taskKey[Unit]("Compile exercises.tex")
pdfExercises := {
runPdfLatexCmd(texFile = file("exercises.tex"), workDir = file("compendium"))
}
lazy val pdfSolutions = taskKey[Unit]("Compile solutions.tex")
pdfSolutions := {
runPdfLatexCmd(texFile = file("solutions.tex"), workDir = file("compendium"))
}
lazy val pdfLabs = taskKey[Unit]("Compile labs.tex")
pdfLabs := {
runPdfLatexCmd(texFile = file("labs.tex"), workDir = file("compendium"))
}
lazy val pdfCompendium = taskKey[Unit]("Compile compendium.tex")
pdfCompendium := {
runPdfLatexCmd(texFile = file("compendium.tex"), workDir = file("compendium"))
}
// Best-effort Swedish-% report via the autotranslate scanner (single source of truth: Code.swedishish),
// run AFTER an English PDF is built so the *En tasks ALWAYS show how close to 0% Swedish we are. Wrapped
// so a missing pdftotext / scanner error never fails the build.
def reportSwedishPct(autotranslateCp: String, pdf: File): Unit =
try { import scala.sys.process._; Seq("java", "-cp", autotranslateCp, "Main", "--pdf-swedish", pdf.getPath).!; () }
catch { case _: Throwable => println(s" (swedish-% report skipped: ${pdf.getName})") }
lazy val pdfCompendiumEn = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium-en.tex")
pdfCompendiumEn := {
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
runPdfLatexCmd(texFile = file("compendium-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
reportSwedishPct(cp, file("compendium-en/compendium-en.pdf"))
}
lazy val pdfCompendiumPrint = taskKey[Unit]("Compile compendium-print.tex")
pdfCompendiumPrint := {
runPdfLatexCmd(texFile = file("compendium-print.tex"), workDir = file("compendium"))
}
lazy val pdfCompendium1 = taskKey[Unit]("Compile compendium1.tex")
pdfCompendium1 := {
runPdfLatexCmd(texFile = file("compendium1.tex"), workDir = file("compendium"))
}
lazy val pdfCompendium2 = taskKey[Unit]("Compile compendium2.tex")
pdfCompendium2 := {
runPdfLatexCmd(texFile = file("compendium2.tex"), workDir = file("compendium"))
}
lazy val pdfCompendium1En = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium1-en.tex")
pdfCompendium1En := {
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
runPdfLatexCmd(texFile = file("compendium1-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
reportSwedishPct(cp, file("compendium-en/compendium1-en.pdf"))
}
lazy val pdfCompendium2En = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium2-en.tex")
pdfCompendium2En := {
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
runPdfLatexCmd(texFile = file("compendium2-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
reportSwedishPct(cp, file("compendium-en/compendium2-en.pdf"))
}
lazy val pdfSlides = inputKey[Unit]("run pdflatex slides/lect-w<weeknumber>.tex")
pdfSlides := {
// http://www.scala-sbt.org/1.0/docs/Input-Tasks.html#Basic+Input+Task+Definition
val args: Seq[String] = spaceDelimited("<arg>").parsed
val workDir = file("slides")
val weeks = if (args.isEmpty) {
val default = Seq.tabulate(7)(i => s"w0${i+1}")
println(s"""<args> is empty, using ${default.mkString(" ")}""")
default
} else args
for (w <- weeks) {
val f: String = if (w startsWith "w") "lect-" + w else w // hack to make it possible to give both just w01 as arg but also info-week00 as arg
val texFile = if (f.takeRight(4) != ".tex") file(f + ".tex") else file(f)
println(s"runPdfLatexCmd($texFile, $workDir)")
runPdfLatexCmd(texFile, workDir)
}
if (args.isEmpty) runPdfLatexCmd(file("all-lectures.tex"), workDir)
}
lazy val pdfSlidesEn = inputKey[Unit]("run pdflatex on the English mirror slides-en/lect-w<weeknumber>-en.tex")
pdfSlidesEn := {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
val workDir = file("slides-en")
val weeks = if (args.isEmpty) {
// empty args = build ALL decks (like pdfSlides), derived from the Swedish source set slides/lect-*.tex
val decks = Option(file("slides").listFiles).getOrElse(Array.empty)
.filter(f => f.getName.startsWith("lect-") && f.getName.endsWith(".tex"))
.map(_.getName.stripPrefix("lect-").stripSuffix(".tex"))
.sorted.toSeq
println(s"""<args> is empty, building all ${decks.size} decks: ${decks.mkString(" ")}""")
decks
} else args
for (w <- weeks) {
val f: String = if (w startsWith "w") "lect-" + w else w // allow both w01 and lect-w01 / file names
val name = if (f.takeRight(4) == ".tex") f.dropRight(4) + "-en.tex" else f + "-en.tex"
val texFile = file(name)
println(s"runPdfLatexCmd($texFile, $workDir)")
runPdfLatexCmd(texFile, workDir, maxPasses = 4)
reportSwedishPct(cp, new File(workDir, name.dropRight(4) + ".pdf"))
}
}
lazy val root = (project in file(".")).
aggregate(workspace, plan, quiz, glossary, autotranslateProject).
settings(commonSettings: _*).
settings(
name := "introprog root",
Global/onLoad := {
// https://www.scala-sbt.org/1.0/docs/offline/Howto-Startup.html
val old = (Global/onLoad).value
myStartupTransition.compose(old)
}
)