|
| 1 | +//> using scala "3.8.2" |
| 2 | +//> using dep "com.lihaoyi::cask:0.11.3" |
| 3 | +//> using dep "com.lihaoyi::os-lib:0.11.8" |
| 4 | +//> using mainClass "DocServer" |
| 5 | + |
| 6 | +/** Generates both doc sites and serves them locally for preview. |
| 7 | + * |
| 8 | + * Usage (from project root): |
| 9 | + * scala-cli run scripts/preview-docs.scala |
| 10 | + * scala-cli run scripts/preview-docs.scala -- --port 9000 |
| 11 | + */ |
| 12 | + |
| 13 | +object DocServer extends cask.MainRoutes: |
| 14 | + private var docsDir: String = "" |
| 15 | + private var portNum: Int = 8080 |
| 16 | + |
| 17 | + override def port = portNum |
| 18 | + override def host = "localhost" |
| 19 | + override def debugMode = false |
| 20 | + |
| 21 | + @cask.staticFiles("/") |
| 22 | + def staticFiles() = docsDir |
| 23 | + |
| 24 | + initialize() |
| 25 | + |
| 26 | + override def main(args: Array[String]): Unit = |
| 27 | + portNum = args.zipWithIndex |
| 28 | + .find(_._1 == "--port") |
| 29 | + .flatMap { case (_, i) => args.lift(i + 1).flatMap(_.toIntOption) } |
| 30 | + .getOrElse(8080) |
| 31 | + |
| 32 | + val projectRoot = os.pwd |
| 33 | + |
| 34 | + println("Generating core documentation...") |
| 35 | + os.proc("mill", "duck4s[3.8.2].docJar") |
| 36 | + .call(cwd = projectRoot, stdout = os.Inherit, stderr = os.Inherit) |
| 37 | + |
| 38 | + println("Generating cats-effect documentation...") |
| 39 | + os.proc("mill", "duck4s-cats-effect[3.8.2].docJar") |
| 40 | + .call(cwd = projectRoot, stdout = os.Inherit, stderr = os.Inherit) |
| 41 | + |
| 42 | + val coreDocsDir = projectRoot / "out" / "duck4s" / "3.8.2" / "docJar.dest" / "docs" |
| 43 | + val catsDocsDir = |
| 44 | + projectRoot / "out" / "duck4s-cats-effect" / "3.8.2" / "docJar.dest" / "docs" |
| 45 | + |
| 46 | + val previewDir = os.temp.dir(prefix = "duck4s-preview") |
| 47 | + println(s"Merging docs into $previewDir...") |
| 48 | + |
| 49 | + // Copy core docs into preview root |
| 50 | + os.list(coreDocsDir).foreach(p => |
| 51 | + os.copy(p, previewDir / p.last, replaceExisting = true, mergeFolders = true) |
| 52 | + ) |
| 53 | + |
| 54 | + // Copy cats-effect docs into cats-effect-api/ subdirectory |
| 55 | + val catsEffectOut = previewDir / "cats-effect-api" |
| 56 | + os.makeDir.all(catsEffectOut) |
| 57 | + os.list(catsDocsDir).foreach(p => |
| 58 | + os.copy(p, catsEffectOut / p.last, replaceExisting = true, mergeFolders = true) |
| 59 | + ) |
| 60 | + |
| 61 | + docsDir = previewDir.toString |
| 62 | + |
| 63 | + println(s"Docs ready. Open http://localhost:$portNum/index.html in your browser.") |
| 64 | + println("Press Ctrl+C to stop.\n") |
| 65 | + |
| 66 | + super.main(args) |
0 commit comments