2009-08-18 06:51:08 +02:00
|
|
|
package xsbt
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
|
|
/** A component manager provides access to the pieces of xsbt that are distributed as components.
|
|
|
|
|
* There are two types of components. The first type is compiled subproject jars with their dependencies.
|
|
|
|
|
* The second type is a subproject distributed as a source jar so that it can be compiled against a specific
|
|
|
|
|
* version of Scala.
|
|
|
|
|
*
|
|
|
|
|
* The component manager provides services to install and retrieve components to the local repository.
|
2009-08-20 06:02:06 +02:00
|
|
|
* This is used for compiled source jars so that the compilation need not be repeated for other projects on the same
|
2009-08-18 06:51:08 +02:00
|
|
|
* machine.
|
|
|
|
|
*/
|
2009-10-10 01:12:14 +02:00
|
|
|
class ComponentManager(provider: xsbti.ComponentProvider, val log: IvyLogger) extends NotNull
|
2009-08-18 06:51:08 +02:00
|
|
|
{
|
2009-08-20 06:02:06 +02:00
|
|
|
/** Get all of the files for component 'id', throwing an exception if no files exist for the component. */
|
2009-08-18 06:51:08 +02:00
|
|
|
def files(id: String): Iterable[File] =
|
|
|
|
|
{
|
2009-09-27 20:39:26 +02:00
|
|
|
val existing = provider.component(id)
|
|
|
|
|
val fs = if(existing.isEmpty) { update(id); provider.component(id) } else existing
|
2009-08-20 06:02:06 +02:00
|
|
|
if(!fs.isEmpty) fs else invalid("Could not find required component '" + id + "'")
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
2009-08-20 06:02:06 +02:00
|
|
|
/** Get the file for component 'id', throwing an exception if no files or multiple files exist for the component. */
|
2009-08-18 06:51:08 +02:00
|
|
|
def file(id: String): File =
|
|
|
|
|
files(id).toList match {
|
|
|
|
|
case x :: Nil => x
|
2009-08-20 06:02:06 +02:00
|
|
|
case xs => invalid("Expected single file for component '" + id + "', found: " + xs.mkString(", "))
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
2009-08-20 06:02:06 +02:00
|
|
|
private def invalid(msg: String) = throw new InvalidComponent(msg)
|
|
|
|
|
private def invalid(e: NotInCache) = throw new InvalidComponent(e.getMessage, e)
|
|
|
|
|
|
2009-09-27 20:39:26 +02:00
|
|
|
def define(id: String, files: Iterable[File]) = provider.defineComponent(id, files.toSeq.toArray)
|
2009-08-20 06:02:06 +02:00
|
|
|
/** Retrieve the file for component 'id' from the local repository. */
|
2009-08-18 06:51:08 +02:00
|
|
|
def update(id: String): Unit =
|
2009-09-27 20:39:26 +02:00
|
|
|
try { IvyCache.withCachedJar(sbtModuleID(id), log)(jar => define(id, Seq(jar)) ) }
|
2009-08-20 06:02:06 +02:00
|
|
|
catch { case e: NotInCache => invalid(e) }
|
|
|
|
|
|
2009-09-27 20:39:26 +02:00
|
|
|
def sbtModuleID(id: String) = ModuleID("org.scala-tools.sbt", id, xsbti.Versions.Sbt)
|
2009-08-20 06:02:06 +02:00
|
|
|
/** Install the files for component 'id' to the local repository. This is usually used after writing files to the directory returned by 'location'. */
|
|
|
|
|
def cache(id: String): Unit = IvyCache.cacheJar(sbtModuleID(id), file(id), log)
|
|
|
|
|
def clearCache(id: String): Unit = IvyCache.clearCachedJar(sbtModuleID(id), log)
|
|
|
|
|
}
|
|
|
|
|
class InvalidComponent(msg: String, cause: Throwable) extends RuntimeException(msg, cause)
|
|
|
|
|
{
|
|
|
|
|
def this(msg: String) = this(msg, null)
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|