ReportBuilder.scala 2.81 KB
Newer Older
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
package nl.lumc.sasc.biopet.core.report

import java.io.{ PrintWriter, File }

import nl.lumc.sasc.biopet.core.ToolCommand
import nl.lumc.sasc.biopet.core.summary.Summary
import org.fusesource.scalate.{TemplateSource, TemplateEngine}

import scala.io.Source

/**
 * Created by pjvan_thof on 3/27/15.
 */
trait ReportBuilder extends ToolCommand {

  case class Args(summary: File = null, outputDir: File = null) extends AbstractArgs

  class OptParser extends AbstractOptParser {
    opt[File]('s', "summary") required () maxOccurs (1) valueName ("<file>") action { (x, c) =>
      c.copy(summary = x)
    }
    opt[File]('o', "outputDir") required () maxOccurs (1) valueName ("<file>") action { (x, c) =>
      c.copy(outputDir = x)
    }
  }

  private var setSummary: Summary = _

  final def summary = setSummary

  def pageArgs: Map[String, Any] = Map()

  def main(args: Array[String]): Unit = {
    logger.info("Start")

    val argsParser = new OptParser
    val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)

    require(cmdArgs.outputDir.exists(), "Output dir does not exist")
    require(cmdArgs.outputDir.isDirectory, "Output dir is not a directory")

Peter van 't Hof's avatar
Peter van 't Hof committed
42
    logger.info("Write Base files")
43
44
45
46
47
48
49
    // Write css to output dir
    val cssDir = new File (cmdArgs.outputDir, "css")
    cssDir.mkdirs()
    val cssWriter = new PrintWriter(new File(cssDir, "biopet.css"))
    Source.fromInputStream(getClass.getResourceAsStream("biopet.css")).getLines().foreach(cssWriter.println(_))
    cssWriter.close()

Peter van 't Hof's avatar
Peter van 't Hof committed
50
    logger.info("Parsing summary")
51
52
    setSummary = new Summary(cmdArgs.summary)

Peter van 't Hof's avatar
Peter van 't Hof committed
53
    logger.info("Generate pages")
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
    generatePage(summary, indexPage, cmdArgs.outputDir,
      args = pageArgs ++ Map("summary" -> summary, "reportName" -> reportName, "indexPage" -> indexPage))

    logger.info("Done")
  }

  protected val engine = new TemplateEngine()

  def indexPage: ReportPage

  def reportName: String

  def generatePage(summary: Summary,
                   page: ReportPage,
                   outputDir: File,
                   path: List[String] = Nil,
                   args: Map[String, Any] = Map()): Unit = {

    val templateText = Source.fromInputStream(getClass.getResourceAsStream("main.ssp")).getLines().mkString("\n")

    val template = engine.compileText("ssp", templateText)

    val pageArgs = args ++ page.args ++ Map("page" -> page, "path" -> path)

    val output = engine.layout(template.source, pageArgs ++ Map("args" -> pageArgs))

    val file = new File(outputDir, path.mkString("", File.separator, File.separator) + "index.html")
    file.getParentFile.mkdirs()
    val writer = new PrintWriter(file)
    writer.println(output)
    writer.close()

    // Generating subpages
    for ((name, subPage) <- page.subPages) {
      generatePage(summary, subPage, outputDir, path ::: name :: Nil, pageArgs)
    }
  }
}