ReportBuilder.scala 3.02 KB
Newer Older
1
2
3
4
5
6
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
7
import org.fusesource.scalate.TemplateEngine
8
9
10
11
12
13
14
15

import scala.io.Source

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

16
  case class Args(summary: File = null, outputDir: File = null, pageArgs: Map[String, String] = Map()) extends AbstractArgs
17
18
19
20
21
22
23
24

  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)
    }
25
26
27
    opt[Map[String, String]]('a', "args") action { (x, c) =>
      c.copy(pageArgs = c.pageArgs ++ x)
    }
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  }

  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
45
    logger.info("Write Base files")
46
    // Write css to output dir
47
    val cssDir = new File(cmdArgs.outputDir, "css")
48
49
    cssDir.mkdirs()
    val cssWriter = new PrintWriter(new File(cssDir, "biopet.css"))
50
    Source.fromInputStream(getClass.getResourceAsStream("/nl/lumc/sasc/biopet/core/report/biopet.css")).getLines().foreach(cssWriter.println(_))
51
52
    cssWriter.close()

Peter van 't Hof's avatar
Peter van 't Hof committed
53
    logger.info("Parsing summary")
54
55
    setSummary = new Summary(cmdArgs.summary)

Peter van 't Hof's avatar
Peter van 't Hof committed
56
    logger.info("Generate pages")
57
    generatePage(summary, indexPage, cmdArgs.outputDir,
58
59
      args = pageArgs ++ cmdArgs.pageArgs ++
        Map("summary" -> summary, "reportName" -> reportName, "indexPage" -> indexPage))
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    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 = {

76
    val templateText = Source.fromInputStream(getClass.getResourceAsStream("/nl/lumc/sasc/biopet/core/report/main.ssp")).getLines().mkString("\n")
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    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)
    }
  }
}