forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Scalac.scala
62 lines (51 loc) · 1.9 KB
/
Scalac.scala
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
package scala.tools.tastytest
import scala.collection.immutable.ArraySeq
import scala.util.{ Try, Success, chaining }, chaining._
import scala.tools.nsc.{Global, Settings, reporters}, reporters.ConsoleReporter
import java.io.OutputStream
import java.io.PrintWriter
object Scalac extends Script.Command {
def scalac(out: String, additionalSettings: Seq[String], sources: String*): Try[Boolean] =
scalac(Console.out, out, additionalSettings, sources:_*)
def scalac(writer: OutputStream, out: String, additionalSettings: Seq[String], sources: String*) = {
def runCompile(global: Global): Boolean = {
global.reporter.reset()
new global.Run() compile sources.toList
val result = !global.reporter.hasErrors
global.reporter.finish()
result
}
def newCompiler(args: String*): Global =
fromSettings(new Settings().tap(_.processArguments(args.toList, true)))
def fromSettings(settings: Settings): Global = {
val pwriter = new PrintWriter(writer, true)
Global(settings, new ConsoleReporter(settings, Console.in, pwriter).tap(_.shortname = true))
}
def compile(args: String*) =
Try(runCompile(newCompiler(args: _*)))
if (sources.isEmpty) {
Success(true)
}
else {
val settings = Array(
"-d", out,
"-classpath", out,
"-deprecation",
"-Xfatal-warnings",
"-usejavacp"
) ++ additionalSettings
compile(ArraySeq.unsafeWrapArray(settings):_*)
}
}
val commandName: String = "scalac"
val describe: String = s"$commandName <out: Directory> <src: File> <args: String*>"
def process(args: String*): Int = {
if (args.length < 2) {
println(red(s"please provide at least 2 arguments in sub-command: $describe"))
return 1
}
val Seq(out, src, additionalArgs @ _*) = args: @unchecked
val success = scalac(out, additionalArgs, src).get
if (success) 0 else 1
}
}