Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created November 18, 2017 12:17
Show Gist options
  • Save lihaoyi/98637d65682763853645c7f84e0ca51e to your computer and use it in GitHub Desktop.
Save lihaoyi/98637d65682763853645c7f84e0ca51e to your computer and use it in GitHub Desktop.
val compilerCache = new CompilerCache(10)
def compileScala(scalaVersion: String,
sources: Path,
compileClasspath: Seq[Path],
outputPath: Path): PathRef = {
val binaryScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".")
def grepJar(s: String) = {
compileClasspath
.find(_.toString.endsWith(s))
.getOrElse(throw new Exception("Cannot find " + s))
.toIO
}
val scalac = ZincUtil.scalaCompiler(
new ScalaInstance(
version = scalaVersion,
loader = getClass.getClassLoader,
libraryJar = grepJar(s"scala-library-$scalaVersion.jar"),
compilerJar = grepJar(s"scala-compiler-$scalaVersion.jar"),
allJars = compileClasspath.toArray.map(_.toIO),
explicitActual = None
),
grepJar(s"compiler-bridge_$binaryScalaVersion-1.0.3.jar")
)
mkdir(outputPath)
scalac.apply(
sources = ls.rec(sources).filter(_.isFile).map(_.toIO).toArray,
changes = new DependencyChanges {
def isEmpty = true
def modifiedBinaries() = Array[File]()
def modifiedClasses() = Array[String]()
},
classpath = compileClasspath.map(_.toIO).toArray,
singleOutput = outputPath.toIO,
options = Array(),
callback = new xsbti.AnalysisCallback {
def startSource(source: File) = {
println("Compiling " + source)
}
def apiPhaseCompleted() = ()
def enabled() = true
def binaryDependency(onBinaryEntry: File, onBinaryClassName: String, fromClassName: String, fromSourceFile: File, context: DependencyContext) = ()
def generatedNonLocalClass(source: File, classFile: File, binaryClassName: String, srcClassName: String) = ()
def problem(what: String, pos: xsbti.Position, msg: String, severity: xsbti.Severity, reported: Boolean) = ()
def dependencyPhaseCompleted() = ()
def classDependency(onClassName: String, sourceClassName: String, context: DependencyContext) = ()
def generatedLocalClass(source: File, classFile: File) = ()
def api(sourceFile: File, classApi: ClassLike) = ()
def mainClass(sourceFile: File, className: String) = ()
def usedName(className: String, name: String, useScopes: java.util.EnumSet[xsbti.UseScope]) = ()
},
maximumErrors = 10,
cache = compilerCache,
log = {
val console = ConsoleOut.systemOut
val consoleAppender = MainAppender.defaultScreen(console)
val l = LogExchange.logger("Hello")
LogExchange.unbindLoggerAppenders("Hello")
LogExchange.bindLoggerAppenders("Hello", (consoleAppender -> sbt.util.Level.Warn) :: Nil)
l
}
)
PathRef(outputPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment