From cbf9e4a90190beabd78a04c1ca2f5b036fdc61c2 Mon Sep 17 00:00:00 2001 From: michelou Date: Wed, 14 Jul 2004 13:05:34 +0000 Subject: - added 'scalapath' value. - added 'main' method for testing. - now accept both .jar files or 'classes' directory. --- sources/scala/tools/scala4ant/ScalaRuntime.scala | 106 +++++++++++++---------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/sources/scala/tools/scala4ant/ScalaRuntime.scala b/sources/scala/tools/scala4ant/ScalaRuntime.scala index ebb530856c..72cfdf0ae7 100644 --- a/sources/scala/tools/scala4ant/ScalaRuntime.scala +++ b/sources/scala/tools/scala4ant/ScalaRuntime.scala @@ -18,13 +18,14 @@ import org.apache.tools.ant.types.Path; * about the Scala runtime environment. * * @author Stephane Micheloud - * @version 1.0 + * @version 1.1 */ object ScalaRuntime { private val SCALA_RUNTIME_LIB = "lib"; private val SCALA_RUNTIME_SOURCES = "sources"; + private val SCALA_RUNTIME_CLASSES = "classes"; private val SCALA_JAR_SOME_CLASS = "scala.ScalaObject"; private val TOOLS_JAR_SOME_CLASS = getClass().getName(); // myself ! @@ -32,86 +33,101 @@ object ScalaRuntime { check(SCALA_JAR_SOME_CLASS, "scala.jar"); check(TOOLS_JAR_SOME_CLASS, "tools.jar"); - check(FJBG_JAR_SOME_CLASS, "fjbg.jar"); + check(FJBG_JAR_SOME_CLASS, "fjbg.jar"); val home: Path = { val p = Path.systemClasspath.createPath(); - val s = getJarFileName(SCALA_JAR_SOME_CLASS).split(SCALA_RUNTIME_LIB); - p.setPath(s(0)); + val name = getResourceLocation(SCALA_JAR_SOME_CLASS); + val i = Math.max(name.lastIndexOf(SCALA_RUNTIME_LIB), + name.lastIndexOf(SCALA_RUNTIME_CLASSES)); + p.setPath(if (i > 0) name.substring(0, i - 1) else name); p } - val classpath: Path = { - def getJarPath(classname: String) = { - val name = getJarFileName(classname); - if (name != null) { - val p = Path.systemClasspath.createPath(); - p.setPath(name); - p - } - else - null - } - val scalaPath = getJarPath(SCALA_JAR_SOME_CLASS); - val toolsPath = getJarPath(TOOLS_JAR_SOME_CLASS); - val fjbgPath = getJarPath(FJBG_JAR_SOME_CLASS); - val cp = if (scalaPath != null && toolsPath != null && fjbgPath != null) { - scalaPath.append(toolsPath); - scalaPath.append(fjbgPath); - scalaPath - } - else - Path.systemClasspath; - cp + val sourcepath: Path = { + val p = Path.systemClasspath.createPath(); + val s = home.toString() + java.io.File.separator + SCALA_RUNTIME_SOURCES; + p.setPath(s); + p } - val sourcepath = { + val scalapath: Path = + getResourcePath(SCALA_JAR_SOME_CLASS); + + val classpath: Path = { + val toolsPath = getResourcePath(TOOLS_JAR_SOME_CLASS); + val fjbgPath = getResourcePath(FJBG_JAR_SOME_CLASS); val p = Path.systemClasspath.createPath(); - val s = home.toString() + java.io.File.pathSeparator + SCALA_RUNTIME_SOURCES; - p.setPath(s); + p.append(scalapath); + p.append(toolsPath); + p.append(fjbgPath); p } - val bootclasspath = { - val p = classpath; + val bootclasspath: Path = { + val p = Path.systemClasspath.createPath(); + p.append(scalapath); p.append(sourcepath); p } /** - * Check if the required libraries are present. + * Check if the required Scala libraries are present. */ - private def check(classname: String, jarname: String) = try { - Class.forName(classname) + private def check(className: String, jarName: String) = try { + Class.forName(className) } catch { case e: ClassNotFoundException => throw new BuildException("Cannot run scala4ant.\n" - + "It seems " + jarname + " is not in your CLASSPATH."); + + "It seems " + jarName + " is not in your CLASSPATH."); } /** - * Return the full path string of the the jar file containing - * the class classname. + * Return the full path string of the the jar file or the + * directory containing the class className. * * @param classname * @return */ - private def getJarFileName(classname: String): String = { + private def getResourceLocation(className: String): String = { def asResourceName(resource: String) = { val name = if (! resource.startsWith("/")) "/" + resource else resource; name.replace('.', '/') + ".class" } - def findClass(className: String) = - getClass().getResource(asResourceName(className)); - val url = findClass(classname); + val rsrcName = asResourceName(className); + val url = getClass().getResource(rsrcName); if (url != null) { - val s = url.getFile(); - assert(s.startsWith("file:")); - s.substring(s.indexOf("file:") + 5, s.indexOf("!")) + val fn = url.getFile(); + val name = if (fn.startsWith("file:")) fn.substring(5) else fn; + val inx = name.lastIndexOf('!'); + val end = if (inx > 0) inx else name.lastIndexOf(rsrcName); + name.substring(0, end); } else - null + throw new BuildException("Cannot run scala4ant.\n" + + "Scala installation directory cannot be found."); } + /** + * Return the Ant path of the class className. + * + * @param classname + * @return + */ + private def getResourcePath(className: String) = { + val name = getResourceLocation(className); + val p = Path.systemClasspath.createPath(); + p.setPath(name); + p + } + + // for testing + def main(args: Array[String]): Unit = { + System.out.println("ScalaRuntime.home = " + ScalaRuntime.home); + System.out.println("ScalaRuntime.classpath = " + ScalaRuntime.classpath); + System.out.println("ScalaRuntime.bootclasspath = " + ScalaRuntime.bootclasspath); + System.out.println("ScalaRuntime.sourcepath = " + ScalaRuntime.sourcepath); + System.out.println("ScalaRuntime.scalapath = " + ScalaRuntime.scalapath); + } } -- cgit v1.2.3