blob: 910639f9bf18261a488f1d811eb2b531892cadfb (
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
67
68
69
|
package scalatex
import java.nio.file.Paths
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.VirtualFile
import scala.tools.nsc.{ Global, Phase }
import scala.tools.nsc.plugins.{ Plugin, PluginComponent }
class ScalaTexPlugin(val global: Global) extends Plugin {
import global._
println(options)
val name = "scalatex-plugin"
val description = "Enforces coding standards"
val components = List[PluginComponent](DemoComponent)
private object DemoComponent extends PluginComponent {
val global = ScalaTexPlugin.this.global
import global._
override val runsAfter = List("parser")
override val runsBefore = List("namer")
val phaseName = "Demo"
val scalatexRoot = "book/src/main/scalatex"
override def newPhase(prev: Phase) = new GlobalPhase(prev) {
override def run() = {
def recursiveListFiles(f: java.io.File): Array[java.io.File] = {
val these = f.listFiles
val (dirs, files) = f.listFiles().partition(_.isDirectory)
files ++ dirs.flatMap(recursiveListFiles)
}
for (file <- recursiveListFiles(new java.io.File(scalatexRoot))) {
val txt = io.Source.fromFile(file).mkString
val fakeJfile = new java.io.File(file.getName)
val virtualFile = new VirtualFile(file.getName) {
override def file = fakeJfile
}
val sourceFile = new BatchSourceFile(virtualFile, txt)
val unit = new CompilationUnit(sourceFile)
val name = file.getName
val objectName = name.slice(name.lastIndexOf('/'), name.lastIndexOf('.'))
val pkgName =
Paths.get(scalatexRoot)
.relativize(file.getParentFile.toPath)
.toString
.split("/")
.map(s => s"package $s")
.mkString("\n")
val shim = s"""
$pkgName
import Book._
import Utils.sect
import scalatags.Text.all._
object $objectName{
val template = scalatex.twf("${file.getPath}")
}
"""
unit.body = global.newUnitParser(shim).parse()
global.currentRun.compileLate(unit)
}
}
def name: String = phaseName
def apply(unit: global.CompilationUnit): Unit = {}
}
}
}
|