aboutsummaryrefslogtreecommitdiff
path: root/libraries/scalatex/AbstractMain.scala
blob: ab876d999ea5555aa2f1fdcf9c1cfb87fad73087 (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
package cbt.plugins.scalatex.runtime

import java.io.File
import java.nio.file.{ Files, Paths }
import scalatags.Text.all._
trait AbstractMain{
  def dependencyClasspath: Seq[File]
  def files: Seq[(String,SeqFrag[Frag])]
  def htmlTarget: File
  def render: SeqFrag[Frag] => String => String
  def main(args: Array[String]): Unit = {
    val changed = files.flatMap{
      case (name, contents) =>
        val path = htmlTarget.toPath.resolve(name + ".html")
        import Files._
        createDirectories(path.getParent)
        val newContents = render(contents)(name)
        if( !exists(path) || new String( Files.readAllBytes( path ) ) != newContents ){
          write( path, newContents.getBytes )
          Some( path )
        } else None
    }
    if( args.lift(0).map(_=="open").getOrElse(false) )
      changed.headOption.foreach{ file =>
        System.out.println(file)
        val old = Option(System.getProperty("apple.awt.UIElement"))
        System.setProperty("apple.awt.UIElement", "true")
        java.awt.Desktop.getDesktop().open(file.toFile)
        old.foreach(System.setProperty("apple.awt.UIElement", _))
      }
    else System.out.println(changed.mkString("\\n"))
  }
  val repl = new cbt.plugins.scalatex.runtime.repl( this.getClass.getClassLoader, dependencyClasspath: _* )
}