Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created November 27, 2017 02:47
Show Gist options
  • Save lihaoyi/8010cb94fb4a85100359a36dc2bbfa57 to your computer and use it in GitHub Desktop.
Save lihaoyi/8010cb94fb4a85100359a36dc2bbfa57 to your computer and use it in GitHub Desktop.
def compileScala(scalaVersion: String,
sources: Path,
compileClasspath: Seq[Path],
scalacOptions: Seq[String],
javacOptions: Seq[String],
outputPath: Path): PathRef = {
val compileClasspathFiles = compileClasspath.map(_.toIO).toArray
pprint.log(compileClasspathFiles)
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 scalaInstance = new ScalaInstance(
version = scalaVersion,
loader = getClass.getClassLoader,
libraryJar = grepJar(s"scala-library-$scalaVersion.jar"),
compilerJar = grepJar(s"scala-compiler-$scalaVersion.jar"),
allJars = compileClasspathFiles,
explicitActual = None
)
val scalac = ZincUtil.scalaCompiler(
scalaInstance,
grepJar(s"compiler-bridge_$binaryScalaVersion-1.0.5.jar")
)
mkdir(outputPath)
val ic = new sbt.internal.inc.IncrementalCompilerImpl()
val logger = {
val console = ConsoleOut.systemOut
val consoleAppender = MainAppender.defaultScreen(console)
val l = LogExchange.logger("Hello")
LogExchange.unbindLoggerAppenders("Hello")
LogExchange.bindLoggerAppenders("Hello", (consoleAppender -> sbt.util.Level.Info) :: Nil)
l
}
val cs = ic.compilers(scalaInstance, ClasspathOptionsUtil.boot, None, scalac)
val lookup = MockedLookup(Function.const(Optional.empty[CompileAnalysis]))
val reporter = new ManagedLoggedReporter(10, logger)
val extra = Array(InterfaceUtil.t2(("key", "value")))
val ignoreProgress = new CompileProgress {
override def advance(current: Int, total: Int): Boolean = true
override def startUnit(phase: String, unitPath: String): Unit = ()
}
val zincFile = (outputPath/'zinc).toIO
val store = FileAnalysisStore.binary(zincFile)
val classesDir = (outputPath / 'classes).toIO
val newResult = ic.compile(
ic.inputs(
classpath = classesDir +: compileClasspathFiles,
sources = ls.rec(sources).filter(_.isFile).map(_.toIO).toArray,
classesDirectory = classesDir,
scalacOptions = scalacOptions.toArray,
javacOptions = javacOptions.toArray,
maxErrors = 10,
sourcePositionMappers = Array(),
order = CompileOrder.Mixed,
compilers = cs,
setup = ic.setup(
lookup,
skip = false,
zincFile,
compilerCache,
IncOptions.of(),
reporter,
Some(ignoreProgress),
extra
),
pr = {
val prev = store.get()
PreviousResult.of(prev.map(_.getAnalysis), prev.map(_.getMiniSetup))
}
),
logger = logger
)
store.set(
AnalysisContents.create(
newResult.analysis(),
newResult.setup()
)
)
PathRef(outputPath/'classes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment