diff options
author | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-11-21 16:50:31 +0000 |
---|---|---|
committer | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-11-21 16:50:31 +0000 |
commit | be91cd08be3590f6cdbb9356b4d093725d5e06ff (patch) | |
tree | 60a300ebeaaa4875ffc705f62054f88220bad80f /src/compiler | |
parent | c63b3a7e7ad089d207dfd57267a356d615a2e03f (diff) | |
download | scala-be91cd08be3590f6cdbb9356b4d093725d5e06ff.tar.gz scala-be91cd08be3590f6cdbb9356b4d093725d5e06ff.tar.bz2 scala-be91cd08be3590f6cdbb9356b4d093725d5e06ff.zip |
Fixed #1440 and generally cleaned up things a bit.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/Global.scala | 20 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/MainGenericRunner.scala | 29 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ScriptRunner.scala | 85 |
3 files changed, 72 insertions, 62 deletions
diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index ac2be354eb..831d100558 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -655,16 +655,18 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable def compile(filenames: List[String]) { try { val scriptMain = settings.script.value - if (scriptMain != "" && filenames.length != 1) - error("can only compile one script at a time") - val sources = filenames map ( - if (scriptMain != "") - (x => ScriptRunner.wrappedScript(scriptMain, x, getSourceFile _)) - else - getSourceFile) - compileSources(sources) + // Are we compiling a script? + if (scriptMain != "") { + if(filenames.length != 1) + error("can only compile one script at a time") + val scriptFile = + ScriptRunner.wrappedScript(scriptMain, filenames.head, getSourceFile) + compileSources(List(scriptFile)) + // No we are compiling regular source files + } else { + compileSources(filenames map getSourceFile) + } } catch { - case ScriptRunner.ScriptException(msg) => error(msg) case ex: IOException => error(ex.getMessage()) } } diff --git a/src/compiler/scala/tools/nsc/MainGenericRunner.scala b/src/compiler/scala/tools/nsc/MainGenericRunner.scala index eddb72fb63..765785e6c9 100644 --- a/src/compiler/scala/tools/nsc/MainGenericRunner.scala +++ b/src/compiler/scala/tools/nsc/MainGenericRunner.scala @@ -7,7 +7,7 @@ package scala.tools.nsc -import java.io.File +import java.io.{File, IOException} import java.lang.{ClassNotFoundException, NoSuchMethodException} import java.lang.reflect.InvocationTargetException import java.net.URL @@ -91,6 +91,10 @@ object MainGenericRunner { return } + def exitSuccess : Nothing = exit(0) + def exitFailure : Nothing = exit(1) + def exitCond(b: Boolean) : Nothing = if(b) exitSuccess else exitFailure + def fileToURL(f: File): Option[URL] = try { Some(f.toURL) } catch { case e => Console.println(e); None } @@ -129,9 +133,9 @@ object MainGenericRunner { case _ if settings.execute.value != "" => val fullArgs = command.thingToRun.toList ::: command.arguments - ScriptRunner.runCommand(settings, - settings.execute.value, - fullArgs) + exitCond(ScriptRunner.runCommand(settings, + settings.execute.value, + fullArgs)) case None => (new InterpreterLoop).main(settings) @@ -146,28 +150,31 @@ object MainGenericRunner { } if (isObjectName) { - try { ObjectRunner.run(classpath, thingToRun, command.arguments) } catch { case e: ClassNotFoundException => Console.println(e) - exit(1) + exitFailure case e: NoSuchMethodException => Console.println(e) - exit(1) + exitFailure case e: InvocationTargetException => e.getCause.printStackTrace - exit(1) + exitFailure } - } else { try { - ScriptRunner.runScript(settings, thingToRun, command.arguments) + exitCond(ScriptRunner.runScript(settings, + thingToRun, + command.arguments)) } catch { + case e: IOException => + Console.println(e.getMessage()) + exitFailure case e: SecurityException => Console.println(e) - exit(1) + exitFailure } } } diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala index 15bee0f579..0d0bf27d51 100644 --- a/src/compiler/scala/tools/nsc/ScriptRunner.scala +++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala @@ -8,7 +8,7 @@ package scala.tools.nsc import java.io.{BufferedReader, File, FileInputStream, FileOutputStream, FileReader, InputStreamReader, PrintWriter, - FileWriter} + FileWriter, IOException} import java.lang.reflect.InvocationTargetException import java.net.URL import java.util.jar.{JarEntry, JarOutputStream} @@ -47,9 +47,6 @@ object ScriptRunner { /** Default name to use for the wrapped script */ val defaultScriptMain = "Main" - /** Exception used internally */ - case class ScriptException(msg: String) extends Exception - /** Pick a main object name from the specified settings */ def scriptMain(settings: Settings) = if (settings.script.value == "") @@ -135,9 +132,8 @@ object ScriptRunner { val matcher = (Pattern.compile("^(::)?!#.*(\\r|\\n|\\r\\n)", Pattern.MULTILINE) .matcher(fileContents)) - if (! matcher.find) - throw ScriptException("script file does not close its header with !# or ::!#") - + if (!matcher.find) + throw new IOException("script file does not close its header with !# or ::!#") return matcher.end } @@ -270,12 +266,13 @@ object ScriptRunner { /** Compile a script and then run the specified closure with * a classpath for the compiled script. + * + * @returns true if compilation and the handler succeeds, false otherwise. */ private def withCompiledScript (settings: GenericRunnerSettings, scriptFile: String) - (handler: String => Unit) - : Unit = - { + (handler: String => Boolean) + : Boolean = { import Interpreter.deleteRecursively /* If the script is running on pre-jvm-1.5 JVM, @@ -345,24 +342,26 @@ object ScriptRunner { // jar failed; run directly from the class files handler(compiledPath.getPath) } - case None => () + case None => false } } } else { // don't use a cache jar at all--just use the class files compile match { case Some(compiledPath) => handler(compiledPath.getPath) - case None => () + case None => false } } } - /** Run a script after it has been compiled */ + /** Run a script after it has been compiled + * + * @returns true if execution succeeded, false otherwise + */ private def runCompiled(settings: GenericRunnerSettings, compiledLocation: String, - scriptArgs: List[String]) - { + scriptArgs: List[String]) : Boolean = { def fileToURL(f: File): Option[URL] = try { Some(f.toURL) } catch { case e => Console.err.println(e); None } @@ -383,44 +382,50 @@ object ScriptRunner { classpath, scriptMain(settings), scriptArgs.toArray) + true } catch { + case e: ClassNotFoundException => + Console.println(e) + false + case e: NoSuchMethodException => + Console.println(e) + false case e:InvocationTargetException => e.getCause.printStackTrace - exit(1) + false } } /** Run a script file with the specified arguments and compilation * settings. + * + * @returns true if compilation and execution succeeded, false otherwise. */ - def runScript( - settings: GenericRunnerSettings, - scriptFile: String, - scriptArgs: List[String]) - { + def runScript(settings: GenericRunnerSettings, + scriptFile: String, + scriptArgs: List[String]) : Boolean = { val f = new File(scriptFile) if (!f.isFile) { - Console.err.println("no such file: " + scriptFile) - return - } - - try { - withCompiledScript(settings, scriptFile){compiledLocation => - runCompiled(settings, compiledLocation, scriptArgs) + throw new IOException("no such file: " + scriptFile) + } else { + try { + withCompiledScript(settings, scriptFile){compiledLocation => + runCompiled(settings, compiledLocation, scriptArgs) + } + } catch { + case e => throw e } - } catch { - case ScriptException(msg) => Console.err.println(msg) - case e => throw e } } - /** Run a command */ - def runCommand( - settings: GenericRunnerSettings, - command: String, - scriptArgs: List[String]) - { + /** Run a command + * + * @returns true if compilation and execution succeeded, false otherwise. + */ + def runCommand(settings: GenericRunnerSettings, + command: String, + scriptArgs: List[String]) : Boolean = { val scriptFile = File.createTempFile("scalacmd", ".scala") // save the command to the file @@ -432,14 +437,10 @@ object ScriptRunner { try { withCompiledScript(settings, scriptFile.getPath){compiledLocation => - scriptFile.delete() runCompiled(settings, compiledLocation, scriptArgs) } } catch { - case ScriptException(msg) => Console.err.println(msg) case e => throw e - } - - scriptFile.delete() // in case there was a compilation error + } finally scriptFile.delete() // in case there was a compilation error } } |