2009-09-26 08:18:04 +02:00
|
|
|
package xsbt.boot
|
|
|
|
|
|
|
|
|
|
import java.io.{File, FileInputStream, InputStreamReader}
|
|
|
|
|
import java.net.{URI, URL}
|
|
|
|
|
|
|
|
|
|
object Configuration
|
|
|
|
|
{
|
2009-10-02 04:59:02 +02:00
|
|
|
def parse(file: URL, baseDirectory: File) = Using( new InputStreamReader(file.openStream, "utf8") )( (new ConfigurationParser).apply )
|
2009-09-26 08:18:04 +02:00
|
|
|
def find(args: Seq[String], baseDirectory: File): (URL, Seq[String]) =
|
|
|
|
|
args match
|
|
|
|
|
{
|
|
|
|
|
case Seq(head, tail @ _*) if head.startsWith("@")=> (configurationFromFile(head.substring(1), baseDirectory), tail)
|
2009-09-27 20:39:26 +02:00
|
|
|
case _ =>
|
|
|
|
|
val propertyConfigured = System.getProperty("sbt.boot.properties")
|
|
|
|
|
val url = if(propertyConfigured == null) configurationOnClasspath else configurationFromFile(propertyConfigured, baseDirectory)
|
|
|
|
|
(url , args)
|
2009-09-26 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
def configurationOnClasspath: URL =
|
|
|
|
|
{
|
|
|
|
|
resourcePaths.toStream.map(getClass.getResource).find(_ ne null) getOrElse
|
2009-09-27 20:39:26 +02:00
|
|
|
( multiPartError("Could not finder sbt launch configuration. Searched classpath for:", resourcePaths))
|
2009-09-26 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
def configurationFromFile(path: String, baseDirectory: File): URL =
|
|
|
|
|
{
|
|
|
|
|
def resolve(against: URI): Option[URL] =
|
|
|
|
|
{
|
|
|
|
|
val resolved = against.resolve(path)
|
|
|
|
|
val exists = try { (new File(resolved)).exists } catch { case _: IllegalArgumentException => false }
|
|
|
|
|
if(exists) Some(resolved.toURL) else None
|
|
|
|
|
}
|
2009-09-27 20:39:26 +02:00
|
|
|
val against = resolveAgainst(baseDirectory)
|
|
|
|
|
against.toStream.flatMap(resolve).firstOption.getOrElse(multiPartError("Could not find configuration file '" + path + "'. Searched:", against))
|
2009-09-26 08:18:04 +02:00
|
|
|
}
|
2009-09-27 20:39:26 +02:00
|
|
|
def multiPartError[T](firstLine: String, lines: Seq[T]) = throw new BootException( (Seq(firstLine) ++ lines).mkString("\n\t") )
|
2009-09-26 08:18:04 +02:00
|
|
|
|
2009-09-27 20:39:26 +02:00
|
|
|
val ConfigurationName = "sbt.boot.properties"
|
2009-09-26 08:18:04 +02:00
|
|
|
val JarBasePath = "/sbt/"
|
2009-09-27 20:39:26 +02:00
|
|
|
def userConfigurationPath = "/" + ConfigurationName
|
|
|
|
|
def defaultConfigurationPath = JarBasePath + ConfigurationName
|
2009-09-26 08:18:04 +02:00
|
|
|
def resourcePaths = Seq(userConfigurationPath, defaultConfigurationPath)
|
|
|
|
|
def resolveAgainst(baseDirectory: File) = Seq(baseDirectory toURI, new File(System.getProperty("user.home")) toURI, classLocation(getClass).toURI)
|
|
|
|
|
|
|
|
|
|
def classLocation(cl: Class[_]): URL =
|
|
|
|
|
{
|
|
|
|
|
val codeSource = cl.getProtectionDomain.getCodeSource
|
|
|
|
|
if(codeSource == null) throw new BootException("No class location for " + cl)
|
|
|
|
|
else codeSource.getLocation
|
|
|
|
|
}
|
|
|
|
|
}
|