package scala.tools
package util
import java.net.{ URL, MalformedURLException }
import scala.tools.reflect.WrappedProperties.AccessControl
import nsc.{ Settings, GenericRunnerSettings }
import nsc.util.{ ClassPath, JavaClassPath, ScalaClassLoader }
import nsc.io.{ File, Directory, Path, AbstractFile }
import ClassPath.{ JavaContext, DefaultJavaContext, join, split }
import PartialFunction.condOpt
object PathResolver {
import AccessControl._
def firstNonEmpty(xs: String*) = xs find (_ != "") getOrElse ""
def makeAbsolute(cp: String) = ClassPath.map(cp, x => Path(x).toAbsolute.path)
def ppcp(s: String) = split(s) match {
case Nil => ""
case Seq(x) => x
case xs => xs map ("\n" + _) mkString
}
object Environment {
private def searchForBootClasspath = (
systemProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse ""
)
def classPathEnv = envOrElse("CLASSPATH", "")
def sourcePathEnv = envOrElse("SOURCEPATH", "")
def javaBootClassPath = propOrElse("sun.boot.class.path", searchForBootClasspath)
def javaExtDirs = propOrEmpty("java.ext.dirs")
def scalaHome = propOrEmpty("scala.home")
def scalaExtDirs = propOrEmpty("scala.ext.dirs")
def javaUserClassPath = propOrElse("java.class.path", "")
def useJavaClassPath = propOrFalse("scala.usejavacp")
override def toString = """
|object Environment {
| scalaHome = %s (useJavaClassPath = %s)
| javaBootClassPath = <%d chars>
| javaExtDirs = %s
| javaUserClassPath = %s
| scalaExtDirs = %s
|}""".trim.stripMargin.format(
scalaHome, useJavaClassPath,
javaBootClassPath.length,
ppcp(javaExtDirs),
ppcp(javaUserClassPath),
ppcp(scalaExtDirs)
)
}
object Defaults {
def scalaUserClassPath = firstNonEmpty(Environment.classPathEnv, ".")
def scalaSourcePath = Environment.sourcePathEnv
def javaBootClassPath = Environment.javaBootClassPath
def javaUserClassPath = Environment.javaUserClassPath
def javaExtDirs = Environment.javaExtDirs
def useJavaClassPath = Environment.useJavaClassPath
def scalaHome = Environment.scalaHome
def scalaHomeDir = Directory(scalaHome)
def scalaHomeExists = scalaHomeDir.isDirectory
def scalaLibDir = Directory(scalaHomeDir / "lib")
def scalaClassesDir = Directory(scalaHomeDir / "classes")
def scalaLibAsJar = File(scalaLibDir / "scala-library.jar")
def scalaLibAsDir = Directory(scalaClassesDir / "library")
def scalaLibDirFound: Option[Directory] =
if (scalaLibAsJar.isFile) Some(scalaLibDir)
else if (scalaLibAsDir.isDirectory) Some(scalaClassesDir)
else None
def scalaLibFound =
if (scalaLibAsJar.isFile) scalaLibAsJar.path
else if (scalaLibAsDir.isDirectory) scalaLibAsDir.path
else ""
def scalaBootClassPath = ""
def scalaExtDirs = Environment.scalaExtDirs
def scalaPluginPath = (scalaHomeDir / "misc" / "scala-devel" / "plugins").path
override def toString = """
|object Defaults {
| scalaHome = %s
| javaBootClassPath = %s
| scalaLibDirFound = %s
| scalaLibFound = %s
| scalaBootClassPath = %s
| scalaPluginPath = %s
|}""".trim.stripMargin.format(
scalaHome,
ppcp(javaBootClassPath),
scalaLibDirFound, scalaLibFound,
ppcp(scalaBootClassPath), ppcp(scalaPluginPath)
)
}
def fromPathString(path: String, context: JavaContext = DefaultJavaContext): JavaClassPath = {
val s = new Settings()
s.classpath.value = path
new PathResolver(s, context) result
}
def main(args: Array[String]): Unit = {
if (args.isEmpty) {
println(Environment)
println(Defaults)
}
else {
val settings = new Settings()
val rest = settings.processArguments(args.toList, false)._2
val pr = new PathResolver(settings)
println(" COMMAND: 'scala %s'".format(args.mkString(" ")))
println("RESIDUAL: 'scala %s'\n".format(rest.mkString(" ")))
pr.result.show
}
}
}
import PathResolver.{ Defaults, Environment, firstNonEmpty, ppcp }
class PathResolver(settings: Settings, context: JavaContext) {
def this(settings: Settings) = this(settings, if (settings.inline.value) new JavaContext else DefaultJavaContext)
private def cmdLineOrElse(name: String, alt: String) = {
(commandLineFor(name) match {
case Some("") => None
case x => x
}) getOrElse alt
}
private def commandLineFor(s: String): Option[String] = condOpt(s) {
case "javabootclasspath" => settings.javabootclasspath.value
case "javaextdirs" => settings.javaextdirs.value
case "bootclasspath" => settings.bootclasspath.value
case "extdirs" => settings.extdirs.value
case "classpath" | "cp" => settings.classpath.value
case "sourcepath" => settings.sourcepath.value
}
object Calculated {
def scalaHome = Defaults.scalaHome
def useJavaClassPath = settings.usejavacp.value || Defaults.useJavaClassPath
def javaBootClassPath = cmdLineOrElse("javabootclasspath", Defaults.javaBootClassPath)
def javaExtDirs = cmdLineOrElse("javaextdirs", Defaults.javaExtDirs)
def javaUserClassPath = if (useJavaClassPath) Defaults.javaUserClassPath else ""
def scalaBootClassPath = cmdLineOrElse("bootclasspath", Defaults.scalaBootClassPath)
def scalaExtDirs = cmdLineOrElse("extdirs", Defaults.scalaExtDirs)
def userClassPath = cmdLineOrElse("classpath", Defaults.scalaUserClassPath)
def sourcePath = cmdLineOrElse("sourcepath", Defaults.scalaSourcePath)
import context._
def basis = List[Traversable[ClassPath[AbstractFile]]](
classesInPath(javaBootClassPath),
contentsOfDirsInPath(javaExtDirs),
classesInExpandedPath(javaUserClassPath),
classesInPath(scalaBootClassPath),
contentsOfDirsInPath(scalaExtDirs),
classesInExpandedPath(userClassPath),
sourcesInPath(sourcePath)
)
lazy val containers = basis.flatten.distinct
override def toString = """
|object Calculated {
| scalaHome = %s
| javaBootClassPath = %s
| javaExtDirs = %s
| javaUserClassPath = %s
| useJavaClassPath = %s
| scalaBootClassPath = %s
| scalaExtDirs = %s
| userClassPath = %s
| sourcePath = %s
|}""".trim.stripMargin.format(
scalaHome,
ppcp(javaBootClassPath), ppcp(javaExtDirs), ppcp(javaUserClassPath),
useJavaClassPath,
ppcp(scalaBootClassPath), ppcp(scalaExtDirs), ppcp(userClassPath),
ppcp(sourcePath)
)
}
def containers = Calculated.containers
lazy val result = {
val cp = new JavaClassPath(containers.toIndexedSeq, context)
if (settings.Ylogcp.value) {
Console.println("Classpath built from " + settings.toConciseString)
Console.println("Defaults: " + PathResolver.Defaults)
Console.println("Calculated: " + Calculated)
val xs = (Calculated.basis drop 2).flatten.distinct
println("After java boot/extdirs classpath has %d entries:" format xs.size)
xs foreach (x => println(" " + x))
}
cp
}
def asURLs = result.asURLs
}