docspell/project/ZolaPlugin.scala

148 lines
4.6 KiB
Scala
Raw Normal View History

2020-07-30 18:31:53 +00:00
package docspell.build
import sbt._
import sbt.Keys._
2022-01-27 19:23:15 +00:00
import sbt.nio.file.FileTreeView
2020-07-30 18:31:53 +00:00
import scala.sys.process._
object ZolaPlugin extends AutoPlugin {
object autoImport {
2021-09-22 15:23:24 +00:00
val zolaRootDir = settingKey[File]("The root directory of zola")
2020-07-30 18:31:53 +00:00
val zolaOutputDir = settingKey[File]("The directory to put the final site")
2021-09-22 15:23:24 +00:00
val zolaCommand = settingKey[String]("The zola executable")
2020-07-30 18:31:53 +00:00
val zolaTestBaseUrl =
settingKey[String]("The base-url to use when building the test site.")
val zolaBuild = taskKey[Unit](
"Build the site using zola, which is then available in 'zolaOutputDir'."
)
val zolaBuildTest = taskKey[Unit](
"Build the site using zola, which is then available in 'zolaOutputDir'. " +
"It uses a different base-url. So the final site can be tested using " +
"'python -m SimpleHTTPServer 1234' for example."
)
val zolaCheck = taskKey[Unit]("Runs zola check to check links")
}
import autoImport._
def zolaSettings: Seq[Setting[_]] =
Seq(
2021-09-22 15:23:24 +00:00
zolaRootDir := baseDirectory.value / "site",
zolaOutputDir := target.value / "zola-site",
zolaCommand := "zola",
2020-07-30 18:31:53 +00:00
zolaTestBaseUrl := "http://localhost:1234",
zolaBuild := {
val logger = streams.value.log
logger.info("Building web site using zola ...")
2021-04-10 14:31:58 +00:00
(Compile / resources).value
2020-07-30 18:31:53 +00:00
buildSite(zolaCommand.value, zolaRootDir.value, zolaOutputDir.value, None, logger)
logger.info("Website built")
},
zolaBuildTest := {
2021-09-22 15:23:24 +00:00
val logger = streams.value.log
2020-07-30 18:31:53 +00:00
val baseurl = zolaTestBaseUrl.value
logger.info("Building web site (test) using zola ...")
2021-04-10 14:31:58 +00:00
(Compile / resources).value
2020-07-30 18:31:53 +00:00
buildSite(
zolaCommand.value,
zolaRootDir.value,
zolaOutputDir.value,
Some(baseurl),
logger
)
logger.info(s"Website built. Check it with base-url $baseurl")
},
zolaCheck := {
val logger = streams.value.log
logger.info("Checking web site using zola ...")
checkSite(zolaCommand.value, zolaRootDir.value, logger)
}
)
override def projectSettings: Seq[Setting[_]] =
zolaSettings
def buildSite(
zolaCmd: String,
inDir: File,
outDir: File,
base: Option[String],
logger: Logger
): Unit = {
val baseUrl = base match {
case Some(url) =>
Seq("--base-url", url)
case None =>
Seq.empty
}
2022-01-27 19:23:15 +00:00
runYarnInstall("yarn", inDir.getParentFile, logger)
runElmCompile("elm", inDir.getParentFile, inDir, logger)
runTailwind("npx", inDir.getParentFile, inDir, logger)
2020-07-31 13:13:07 +00:00
Cmd.run(
Seq(zolaCmd, "build", "-o", outDir.absolutePath.toString) ++ baseUrl,
inDir,
logger
)
2020-07-30 18:31:53 +00:00
}
2020-07-31 13:13:07 +00:00
def checkSite(zolaCmd: String, inDir: File, logger: Logger): Unit =
Cmd.run(Seq(zolaCmd, "check"), inDir, logger)
2020-07-30 18:31:53 +00:00
2020-07-31 13:13:07 +00:00
def runYarnInstall(yarnCmd: String, inDir: File, logger: Logger): Unit =
Cmd.run(Seq(yarnCmd, "install"), inDir, logger)
2020-07-30 18:31:53 +00:00
def runElmCompile(elmCmd: String, inDir: File, zolaRoot: File, logger: Logger): Unit =
2020-07-31 13:13:07 +00:00
Cmd.run(
Seq(
elmCmd,
"make",
"--output",
(zolaRoot / "static" / "js" / "bundle.js").absolutePath.toString,
2020-07-31 13:13:07 +00:00
"--optimize",
2020-09-29 22:17:18 +00:00
(inDir / "elm" / "Main.elm").toString,
(inDir / "elm" / "Search.elm").toString
2020-07-31 13:13:07 +00:00
),
inDir,
logger
2020-07-30 18:31:53 +00:00
)
2022-01-27 19:23:15 +00:00
def runTailwind(npx: String, inDir: File, zolaRoot: File, logger: Logger): Unit = {
val fontTarget = zolaRoot / "static" / "files"
val iconTarget = zolaRoot / "static" / "webfonts"
IO.createDirectories(Seq(fontTarget, iconTarget))
val fontIn = Glob(inDir / "node_modules" / "@fontsource") / * / "files" / *
val fontInFiles = FileTreeView.default.list(fontIn).map(_._1.toFile())
logger.info(s"Copy ${fontInFiles.size} webfonts from node_modules to ${fontTarget}")
IO.copy(fontInFiles.pair(Path.flat(fontTarget)))
val iconIn =
Glob(inDir / "node_modules" / "@fortawesome" / "fontawesome-free" / "webfonts") / *
val iconInFiles = FileTreeView.default.list(iconIn).map(_._1.toFile())
logger.info(s"Copy ${iconInFiles.size} icons from node_modules to ${iconTarget}")
IO.copy(iconInFiles.pair(Path.flat(iconTarget)))
logger.info("Running tailwind…")
Cmd.run(
Seq(
npx,
"tailwindcss",
"-i",
(inDir / "styles" / "input.css").toString,
"-o",
(zolaRoot / "static" / "styles.css").toString,
"--config",
(inDir / "tailwind.config.js").toString,
"--postcss",
(inDir / "postcss.config.js").toString,
"--minify"
),
inDir,
logger
)
}
2020-07-30 18:31:53 +00:00
}