sbt/launch/Configuration.scala

52 lines
2.2 KiB
Scala
Raw Normal View History

2009-09-26 08:18:04 +02:00
package xsbt.boot
2009-10-18 04:40:02 +02:00
import Pre._
2009-09-26 08:18:04 +02:00
import java.io.{File, FileInputStream, InputStreamReader}
import java.net.{URI, URL}
object Configuration
{
def parse(file: URL, baseDirectory: File) = Using( new InputStreamReader(file.openStream, "utf8") )( (new ConfigurationParser).apply )
2009-10-18 04:40:02 +02:00
def find(args: List[String], baseDirectory: File): (URL, List[String]) =
2009-09-26 08:18:04 +02:00
args match
{
2009-10-18 04:40:02 +02:00
case head :: tail if head.startsWith("@")=> (configurationFromFile(head.substring(1), baseDirectory), tail)
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 =
{
2009-10-18 04:40:02 +02:00
resourcePaths.elements.map(getClass.getResource).find(_ ne null) getOrElse
( 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
}
val against = resolveAgainst(baseDirectory)
2009-10-18 04:40:02 +02:00
val resolving = against.elements.flatMap(e => resolve(e).toList.elements)
if(!resolving.hasNext) multiPartError("Could not find configuration file '" + path + "'. Searched:", against)
resolving.next()
2009-09-26 08:18:04 +02:00
}
2009-10-18 04:40:02 +02:00
def multiPartError[T](firstLine: String, lines: List[T]) = error( (firstLine :: lines).mkString("\n\t") )
2009-09-26 08:18:04 +02:00
val ConfigurationName = "sbt.boot.properties"
2009-09-26 08:18:04 +02:00
val JarBasePath = "/sbt/"
def userConfigurationPath = "/" + ConfigurationName
def defaultConfigurationPath = JarBasePath + ConfigurationName
2009-10-18 04:40:02 +02:00
def resourcePaths = List(userConfigurationPath, defaultConfigurationPath)
def resolveAgainst(baseDirectory: File) = List(baseDirectory toURI, new File(System.getProperty("user.home")) toURI, classLocation(getClass).toURI)
2009-09-26 08:18:04 +02:00
def classLocation(cl: Class[_]): URL =
{
val codeSource = cl.getProtectionDomain.getCodeSource
2009-10-18 04:40:02 +02:00
if(codeSource == null) error("No class location for " + cl)
2009-09-26 08:18:04 +02:00
else codeSource.getLocation
}
}