summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/MainGenericRunner.scala
blob: 1a8a3e02b98f5e9705e29a9187e1c8c49c0be003 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* NSC -- new Scala compiler
 * Copyright 2006 LAMP/EPFL
 * @author  Lex Spoon
 */

// $Id$

package scala.tools.nsc

import java.lang.System.getProperty
import java.lang.{ClassNotFoundException, NoSuchMethodException}
import java.io.File
import java.lang.reflect.InvocationTargetException

/** An object that runs Scala code.  It has three possible
  * sources for the code to run: pre-compiled code, a script file,
  * or interactive entry.
  */
object MainGenericRunner {
  /** Append jars found in ${scala.home}/lib to
    * a specified classpath.  Also append "." if the
    * input classpath is empty; otherwise do not.
    */
  def addClasspathExtras(classpath: String): String = {
    val scalaHome = getProperty("scala.home")
    if (scalaHome == null)
      return classpath

    val libdir = new File(new File(scalaHome), "lib")
    if(!libdir.exists || libdir.isFile)
      return classpath

    val filesInLib = libdir.listFiles
    val jarsInLib =
      filesInLib.filter(f =>
        f.isFile && f.getName.endsWith(".jar"))
    val sep = File.pathSeparator
    val extraClassPath = jarsInLib.mkString("", sep, "")

    if(classpath == "")
      extraClassPath + sep + "."
    else
      classpath + sep + extraClassPath
  }

  def main(args: Array[String]): Unit = {
    def error(str: String) = Console.println(str)
    val command = new GenericRunnerCommand(args.toList, error)
    if (!command.ok) {
      Console.println(command.usageMessage)
      return ()
    }

    val settings = command.settings

    settings.classpath.value =
      addClasspathExtras(settings.classpath.value)

    settings.defines.applyToCurrentJVM

    if (settings.help.value || !command.ok) {
      Console.println(command.usageMessage)
      return
    }

    if (settings.version.value) {
      val version = getProperty("scala.tool.version", "unknown version")
      Console.println(
          "Scala code runner version " + version + " -- " +
           "(c) 2002-2006 LAMP/EPFL")
      return
    }

    def paths(str: String) = str.split(File.pathSeparator).toList

    val classpath =
      paths(settings.bootclasspath.value) :::
      paths(settings.classpath.value)


    command.thingToRun match {
      case None =>
        (new InterpreterLoop).main(settings)

      case Some(thingToRun) =>
        val isObjectName =
          settings.howtorun.value match {
            case "object" => true
            case "script" => false
            case "guess" =>
              ObjectRunner.classExists(classpath, thingToRun)
          }

        if (isObjectName) {

          try {
            ObjectRunner.run(classpath, thingToRun, command.arguments)
          } catch {
            case e: ClassNotFoundException =>
              Console.println(e)
            case e: NoSuchMethodException =>
              Console.println(e)
            case e: InvocationTargetException =>
              e.getCause.printStackTrace
          }
        } else {
          ScriptRunner.runScript(settings, thingToRun, command.arguments)
        }
    }
  }
}