diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/CompilerCommand.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/Global.scala | 7 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ScriptRunner.scala | 57 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/Settings.scala | 2 |
4 files changed, 53 insertions, 15 deletions
diff --git a/src/compiler/scala/tools/nsc/CompilerCommand.scala b/src/compiler/scala/tools/nsc/CompilerCommand.scala index eaafd7f6e0..22acb56a6b 100644 --- a/src/compiler/scala/tools/nsc/CompilerCommand.scala +++ b/src/compiler/scala/tools/nsc/CompilerCommand.scala @@ -80,7 +80,7 @@ class CompilerCommand(arguments: List[String], val settings: Settings, } else ok = settings.checkDependencies } - } else if (settings.Xscript.value || args.head.endsWith(fileEnding)) { + } else if ((settings.script.value != "") || args.head.endsWith(fileEnding)) { fs = args.head :: fs args = args.tail } else if (args.head.length == 0) {//quick fix diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index afedea0b1a..0c7e18c2e9 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -593,11 +593,12 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable def compile(filenames: List[String]) { try { - if (settings.Xscript.value && filenames.length != 1) + val scriptMain = settings.script.value + if (scriptMain != "" && filenames.length != 1) error("can only compile one script at a time") val sources = filenames map ( - if (settings.Xscript.value) - (x => ScriptRunner.wrappedScript(x, getSourceFile _)) + if (scriptMain != "") + (x => ScriptRunner.wrappedScript(scriptMain, x, getSourceFile _)) else getSourceFile) compileSources(sources) diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala index 2f0a0fdd4c..7afc0f5b5c 100644 --- a/src/compiler/scala/tools/nsc/ScriptRunner.scala +++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala @@ -43,6 +43,15 @@ import scala.tools.nsc.util.{CompoundSourceFile, SourceFile, SourceFileFragment} * of stdout... */ object ScriptRunner { + /** Default name to use for the wrapped script */ + val defaultScriptMain = "Main" + + /** Pick a main object name from the specified settings */ + def scriptMain(settings: Settings) = + if (settings.script.value == "") + defaultScriptMain + else + settings.script.value /** Choose a jar filename to hold the compiled version * of a script @@ -127,18 +136,39 @@ object ScriptRunner { return matcher.end } + /** Split a fully qualified object name into a + * package and an unqualified object name */ + private def splitObjectName(fullname: String): + (Option[String],String) = + { + val idx = fullname.lastIndexOf('.') + if (idx < 0) + (None, fullname) + else + (Some(fullname.substring(0,idx)), fullname.substring(idx+1)) + } + + /** Wrap a script file into a runnable object named * <code>scala.scripting.Main</code>. - * - * @param filename ... - * @param getSourceFile ... - * @return ... */ - def wrappedScript(filename: String, getSourceFile: PlainFile => SourceFile): SourceFile = { + def wrappedScript( + objectName: String, + filename: String, + getSourceFile: PlainFile => SourceFile): SourceFile = + { + val (maybePack, objName) = splitObjectName(objectName) + + val packageDecl = + maybePack match { + case Some(pack) => "package " + pack + "\n" + case None => "" + } + val preamble = new SourceFile("<script preamble>", - ("package $scalascript\n" + - "object Main {\n" + + (packageDecl + + "object " + objName + " {\n" + " def main(argv: Array[String]): Unit = {\n" + " val args = argv;\n").toCharArray) @@ -184,7 +214,9 @@ object ScriptRunner { compSettings.foldLeft[List[String]](Nil)((args, stg) => stg.unparse ::: args) - val compArgs = coreCompArgs ::: List("-Xscript", scriptFile) + val compArgs = + (coreCompArgs ::: + List("-script", scriptMain(settings), scriptFile)) val socket = CompileSocket.getOrCreateSocket("") val out = new PrintWriter(socket.getOutputStream(), true) @@ -235,7 +267,12 @@ object ScriptRunner { val reporter = new ConsoleReporter(settings) val compiler = new Global(settings, reporter) val cr = new compiler.Run - cr.compileSources(List(wrappedScript(scriptFile, compiler.getSourceFile _))) + val wrapped = + wrappedScript( + scriptMain(settings), + scriptFile, + compiler.getSourceFile _) + cr.compileSources(List(wrapped)) (compiledPath, !reporter.hasErrors) } else { val compok = compileWithDaemon(settings, scriptFile) @@ -324,7 +361,7 @@ object ScriptRunner { try { ObjectRunner.run( classpath, - "$scalascript.Main", + scriptMain(settings), scriptArgs.toArray) } catch { case e:InvocationTargetException => diff --git a/src/compiler/scala/tools/nsc/Settings.scala b/src/compiler/scala/tools/nsc/Settings.scala index c60756916b..a6f6e6120b 100644 --- a/src/compiler/scala/tools/nsc/Settings.scala +++ b/src/compiler/scala/tools/nsc/Settings.scala @@ -90,6 +90,7 @@ class Settings(error: String => Unit) { override def helpSyntax = "-P:<plugin>:<opt>" } val outdir = StringSetting ("-d", "directory", "Specify where to place generated class files", ".") + val script = new StringSetting("-script", "object", "compile as a script, wrapping the code into object.main()", "") { override def hiddenToIDE = true } val encoding = new StringSetting ("-encoding", "encoding", "Specify character encoding used by source files", encodingDefault) { override def hiddenToIDE = false } val target = ChoiceSetting ("-target", "Specify which backend to use", List("jvm-1.5", "jvm-1.4", "msil", "cldc"), "jvm-1.4") val checknull = BooleanSetting("-checknull", "Emit warning on selection of nullable reference") @@ -140,7 +141,6 @@ class Settings(error: String => Unit) { val Xlinearizer = ChoiceSetting ("-Xlinearizer", "Linearizer to use", List("normal", "dfs", "rpo", "dump"), "rpo") val Xgenerics = BooleanSetting("-Xgenerics", "Use generic Java types") val Xprintpos = BooleanSetting("-Xprintpos", "Print tree positions (as offsets)") - val Xscript = new BooleanSetting("-Xscript", "compile script file") { override def hiddenToIDE = true } val Xexperimental = BooleanSetting("-Xexperimental", "enable experimental extensions") val Xplugtypes = BooleanSetting("-Xplugtypes", "process annotations on types") //Xplugtypes.value = true // just while experimenting |