2009-08-18 06:51:08 +02:00
|
|
|
package xsbt
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
|
|
object ComponentCompiler
|
|
|
|
|
{
|
|
|
|
|
val xsbtiID = "xsbti"
|
2009-08-24 04:21:15 +02:00
|
|
|
val srcExtension = "-src"
|
|
|
|
|
val binSeparator = "-bin_"
|
|
|
|
|
val compilerInterfaceID = "compiler-interface"
|
|
|
|
|
val compilerInterfaceSrcID = compilerInterfaceID + srcExtension
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
2009-08-24 04:21:15 +02:00
|
|
|
class ComponentCompiler(scalaVersion: String, compiler: RawCompiler, manager: ComponentManager)
|
2009-08-18 06:51:08 +02:00
|
|
|
{
|
|
|
|
|
import ComponentCompiler._
|
|
|
|
|
import FileUtilities.{copy, createDirectory, zip, jars, unzip, withTemporaryDirectory}
|
|
|
|
|
def apply(id: String): File =
|
|
|
|
|
{
|
2009-08-24 04:21:15 +02:00
|
|
|
val binID = binaryID(id, scalaVersion)
|
2009-08-18 06:51:08 +02:00
|
|
|
try { manager.file(binID) }
|
2009-08-24 04:21:15 +02:00
|
|
|
catch { case e: InvalidComponent => compileAndInstall(id, binID) }
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
2009-08-24 04:21:15 +02:00
|
|
|
private def binaryID(id: String, scalaVersion: String) = id + binSeparator + scalaVersion
|
2009-08-18 06:51:08 +02:00
|
|
|
private def compileAndInstall(id: String, binID: String): File =
|
|
|
|
|
{
|
2009-08-24 04:21:15 +02:00
|
|
|
val srcID = id + srcExtension
|
2009-08-18 06:51:08 +02:00
|
|
|
val binaryDirectory = manager.location(binID)
|
|
|
|
|
createDirectory(binaryDirectory)
|
|
|
|
|
val targetJar = new File(binaryDirectory, id + ".jar")
|
2009-08-24 04:21:15 +02:00
|
|
|
compileSources(manager.files(srcID), targetJar, id)
|
2009-08-18 06:51:08 +02:00
|
|
|
manager.cache(binID)
|
|
|
|
|
targetJar
|
|
|
|
|
}
|
|
|
|
|
/** Extract sources from source jars, compile them with the xsbti interfaces on the classpath, and package the compiled classes and
|
|
|
|
|
* any resources from the source jars into a final jar.*/
|
2009-08-24 04:21:15 +02:00
|
|
|
private def compileSources(sourceJars: Iterable[File], targetJar: File, id: String)
|
2009-08-18 06:51:08 +02:00
|
|
|
{
|
2009-08-29 16:19:00 +02:00
|
|
|
import Paths._
|
2009-08-18 06:51:08 +02:00
|
|
|
withTemporaryDirectory { dir =>
|
|
|
|
|
val extractedSources = (Set[File]() /: sourceJars) { (extracted, sourceJar)=> extracted ++ unzip(sourceJar, dir) }
|
|
|
|
|
val (sourceFiles, resources) = extractedSources.partition(_.getName.endsWith(".scala"))
|
|
|
|
|
withTemporaryDirectory { outputDirectory =>
|
2009-08-24 04:21:15 +02:00
|
|
|
val xsbtiJars = manager.files(xsbtiID)
|
2009-08-18 06:51:08 +02:00
|
|
|
val arguments = Seq("-d", outputDirectory.getAbsolutePath, "-cp", xsbtiJars.mkString(File.pathSeparator)) ++ sourceFiles.toSeq.map(_.getAbsolutePath)
|
2009-08-24 04:21:15 +02:00
|
|
|
compiler(arguments)
|
2009-08-29 16:19:00 +02:00
|
|
|
copy(resources x (FileMapper.rebase(dir, outputDirectory)))
|
|
|
|
|
zip((outputDirectory ***) x (PathMapper.relativeTo(outputDirectory)), targetJar)
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|