Skip to content

Commit

Permalink
fixed test for scriptFile in ScriptRunner.runScript
Browse files Browse the repository at this point in the history
git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@8283 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
  • Loading branch information
michelou committed Jul 28, 2006
1 parent 2ae858c commit eec9c3b
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions src/compiler/scala/tools/nsc/ScriptRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import scala.tools.nsc.reporters.ConsoleReporter
/** An object that runs Scala code in script files.
*
* For example, here is a complete Scala script on Unix:
*
*
* #!/bin/sh
* exec scala "$0" "$@"
* !#
Expand All @@ -42,14 +42,14 @@ object ScriptRunner {
*/
private def jarFileFor(scriptFile: String): File = {
val filename =
if(scriptFile.matches(".*\\.[^.\\\\/]*"))
if (scriptFile.matches(".*\\.[^.\\\\/]*"))
scriptFile.replaceFirst("\\.[^.\\\\/]*$", ".jar")
else
scriptFile + ".jar"

new File(filename)
}

/** Try to create a jar out of all the contents
* of a directory.
*/
Expand All @@ -58,12 +58,12 @@ object ScriptRunner {
val jarFileStream = new FileOutputStream(jarFile)
val jar = new JarOutputStream(jarFileStream)
val buf = new Array[byte](10240)

def addFromDir(dir: File, prefix: String): Unit = {
for(val entry <- dir.listFiles) {
if(entry.isFile) {
for (val entry <- dir.listFiles) {
if (entry.isFile) {
jar.putNextEntry(new JarEntry(prefix + entry.getName))

val input = new FileInputStream(entry)
var n = input.read(buf, 0, buf.length)
while (n >= 0) {
Expand All @@ -84,37 +84,37 @@ object ScriptRunner {
case _:Error => jarFile.delete // XXX what errors to catch?
}
}

/** Read the entire contents of a file as a String. */
private def contentsOfFile(filename: String): String = {
val strbuf = new StringBuffer
val reader = new FileReader(filename)
val cbuf = new Array[Char](1024)
while(true) {
val n = reader.read(cbuf)
if(n <= 0)
if (n <= 0)
return strbuf.toString
strbuf.append(cbuf, 0, n)
}
throw new Error("impossible")
}

/** Find the length of the header in the specified file, if
* there is one. The header part starts with "#!" or "::#!"
* and ends with a line that begins with "!#" or "::!#".
*/
private def headerLength(filename: String): Int = {
import java.util.regex._

val fileContents = contentsOfFile(filename)

if(!(fileContents.startsWith("#!") || fileContents.startsWith("::#!")))
if (!(fileContents.startsWith("#!") || fileContents.startsWith("::#!")))
return 0
val matcher =

val matcher =
(Pattern.compile("^(::)?!#.*(\\r|\\n|\\r\\n)", Pattern.MULTILINE)
.matcher(fileContents))
if(! matcher.find)
if (! matcher.find)
throw new Error("script file does not close its header with !# or ::!#")

return matcher.end
Expand All @@ -130,36 +130,37 @@ object ScriptRunner {
"object Main {\n" +
" def main(argv: Array[String]): Unit = {\n" +
" val args = argv;\n").toCharArray)

val middle =

val middle = {
val f = new File(filename)
new SourceFileFragment(
new SourceFile(new PlainFile(new File(filename))),
new SourceFile(new PlainFile(f)),
headerLength(filename),
new File(filename).length.asInstanceOf[Int])

f.length.asInstanceOf[Int])
}
val end = new SourceFile("<script trailer>", "\n} }\n".toCharArray)

new CompoundSourceFile(preamble, middle, end)
}

/** Compile a script using the fsc compilation deamon */
private def compileWithDaemon
(settings: GenericRunnerSettings, scriptFile: String)
:Boolean =
private def compileWithDaemon(
settings: GenericRunnerSettings,
scriptFile: String): Boolean =
{
val compSettingNames =
(new Settings(error)).allSettings.map(.name)

val compSettings =
settings.allSettings.filter(stg =>
compSettingNames.contains(stg.name))
val coreCompArgs =

val coreCompArgs =
compSettings.foldLeft[List[String]](Nil)((args, stg) =>
stg.unparse ::: args)

val compArgs = coreCompArgs ::: List("-Xscript", scriptFile)

val socket = CompileSocket.getOrCreateSocket("")
val out = new PrintWriter(socket.getOutputStream(), true)
val in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
Expand All @@ -168,13 +169,13 @@ object ScriptRunner {
out.println(compArgs.mkString("", "\0", ""))

var compok = true

var fromServer = in.readLine()
while (fromServer != null) {
System.out.println(fromServer)
if(fromServer.matches(".*errors? found.*"))
if (fromServer.matches(".*errors? found.*"))
compok = false

fromServer = in.readLine()
}
in.close()
Expand All @@ -183,18 +184,17 @@ object ScriptRunner {

compok
}



/** Compile a script and then run the specified closure with
* a classpath for the compiled script.
*/
private def withCompiledScript
(settings: GenericRunnerSettings, scriptFile: String)
(handler: String=>Unit)
:Unit =
(handler: String => Unit)
: Unit =
{
import Interpreter.deleteRecursively

/** Compiles the script file, and returns two things:
* the directory with the compiled class files,
* and a flag for whether the compilation succeeded.
Expand All @@ -203,39 +203,38 @@ object ScriptRunner {
val compiledPath = File.createTempFile("scalascript", "")
compiledPath.delete // the file is created as a file; make it a directory
compiledPath.mkdirs

settings.outdir.value = compiledPath.getPath

if(settings.nocompdaemon.value) {
if (settings.nocompdaemon.value) {
val reporter = new ConsoleReporter
val compiler = new Global(settings, reporter)
val cr = new compiler.Run
cr.compileSources(List(wrappedScript(scriptFile)))
cr.compileSources(List(wrappedScript(scriptFile)))
Pair(compiledPath, reporter.errors == 0)
} else {
val compok = compileWithDaemon(settings, scriptFile)
Pair(compiledPath, compok)
}
}


if(settings.savecompiled.value) {
if (settings.savecompiled.value) {
val jarFile = jarFileFor(scriptFile)

def jarOK = (jarFile.canRead &&
(jarFile.lastModified > new File(scriptFile).lastModified))

if(jarOK) {
if (jarOK) {
// pre-compiled jar is current
handler(jarFile.getAbsolutePath)
} else {
// The pre-compiled jar is old. Recompile the script.
jarFile.delete
val Pair(compiledPath, compok) = compile
try {
if(compok) {
if (compok) {
tryMakeJar(jarFile, compiledPath)
if(jarOK) {
if (jarOK) {
deleteRecursively(compiledPath)
handler(jarFile.getAbsolutePath)
} else {
Expand All @@ -252,35 +251,36 @@ object ScriptRunner {
// don't use the cache; just run from the interpreter's temporary directory
val Pair(compiledPath, compok) = compile
try {
if(compok)
if (compok)
handler(compiledPath.getPath)
} finally {
deleteRecursively(compiledPath)
}
}
}

/** Run a script file with the specified arguments and compilation
* settings.
*/
def runScript(
settings: GenericRunnerSettings,
scriptFile: String,
scriptArgs: List[String]): Unit =
settings: GenericRunnerSettings,
scriptFile: String,
scriptArgs: List[String]): Unit =
{
if(!(new File(scriptFile)).exists) {
val f = new File(scriptFile)
if (!f.exists || f.isDirectory) {
Console.println("no such file: " + scriptFile)
return ()
return
}

withCompiledScript(settings, scriptFile)(compiledLocation => {
def pparts(path: String) = path.split(File.pathSeparator).toList

val classpath =
pparts(settings.bootclasspath.value) :::
List(compiledLocation) :::
pparts(settings.classpath.value)

try {
ObjectRunner.run(
classpath,
Expand Down

0 comments on commit eec9c3b

Please sign in to comment.