summaryrefslogtreecommitdiff
path: root/book/src/main/scala/book/Main.scala
blob: 4b862d0a79fc6969958ac46c5df0c202e8a01a03 (plain) (blame)
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
package book
import acyclic.file
import java.io.InputStream
import java.nio.file.{Paths, Files}



object Main {
  def write(txt: String, dest: String) = {
    Paths.get(dest).toFile.getParentFile.mkdirs()
    Files.deleteIfExists(Paths.get(dest))
    Files.write(Paths.get(dest), txt.getBytes)
  }
  def copy(src: InputStream, dest: String) = {
    Paths.get(dest).toFile.getParentFile.mkdirs()
    Files.deleteIfExists(Paths.get(dest))
    Files.copy(src, Paths.get(dest))
  }

  def main(args: Array[String]): Unit = {
    println("Writing Book")
    val outputRoot = System.getProperty("output.root") + "/"
    write(Book.site, s"$outputRoot/index.html")

    val jsFiles = Book.autoResources.filter(_.endsWith(".js")).toSet
    val cssFiles = Book.autoResources.filter(_.endsWith(".css")).toSet
    val miscFiles = Book.autoResources -- cssFiles -- jsFiles

    for(res <- Book.manualResources ++ miscFiles) {
      copy(getClass.getResourceAsStream("/" + res), outputRoot + res)
    }

    for((resources, dest) <- Seq(jsFiles -> "scripts.js", cssFiles -> "styles.css")) {
      val blobs = for(res <- resources.iterator) yield {
        io.Source.fromInputStream(getClass.getResourceAsStream("/"+res)).mkString
      }

      write(blobs.mkString("\n"), outputRoot + dest)
    }

    val allNames = {
      def rec(n: Tree[String]): Seq[String] = {
        n.value +: n.children.flatMap(rec)
      }
      rec(sect.structure).toSet
    }
    val dupes = allNames.groupBy(x => x)
                        .values
                        .filter(_.size > 1)
                        .map(_.head)
                        .toSet

    assert(dupes.size == 0, s"Duplicate names: $dupes")

    val dangling = sect.usedRefs -- allNames

    assert(dangling.size == 0, s"Dangling Refs: $dangling")

    println("Writing Done")

    // can be used to verify that no links are broken
    // lnk.usedLinks
  }


}