updated file scala/tools/nsc/MainScript.scala to run it in the DOS console

added executables bin/scalascript* to the Scala distribution



git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@7449 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
michelou 2006-05-18 15:48:23 +00:00
parent c45b05998f
commit 55b21add90
2 changed files with 76 additions and 28 deletions

View File

@ -57,6 +57,7 @@ PROPERTIES
<property name="scalac.exec.name" value="scalac"/>
<property name="scaladoc.exec.name" value="scaladoc"/>
<property name="scalaint.exec.name" value="scalaint"/>
<property name="scalascript.exec.name" value="scalascript"/>
<!-- ===========================================================================
INITIALISATION
@ -338,6 +339,12 @@ BUILD LOCAL REFERENCE (LOCKER) LAYER
class="scala.tools.nsc.MainInterpreter"
version="${version.number}"
copyright="${copyright.string}"/>
<lockertool
file="${locker.dir}/bin/${scalascript.exec.name}"
name="Scala scripting tool"
class="scala.tools.nsc.MainScript"
version="${version.number}"
copyright="${copyright.string}"/>
<chmod perm="ugo+rx"
file="${locker.dir}/bin/${scalac.exec.name}"/>
<chmod perm="ugo+rx"
@ -346,6 +353,8 @@ BUILD LOCAL REFERENCE (LOCKER) LAYER
file="${locker.dir}/bin/${scaladoc.exec.name}"/>
<chmod perm="ugo+rx"
file="${locker.dir}/bin/${scalaint.exec.name}"/>
<chmod perm="ugo+rx"
file="${locker.dir}/bin/${scalascript.exec.name}"/>
<!-- Mark LOCKER as being completely built -->
<touch file="${locker.dir}/complete" verbose="no"/>
</target>
@ -450,6 +459,12 @@ BUILD QUICK-TEST LAYER
class="scala.tools.nsc.MainInterpreter"
version="${version.number}"
copyright="${copyright.string}"/>
<quicktool
file="${quick.dir}/bin/${scalascript.exec.name}"
name="Scala scripting tool"
class="scala.tools.nsc.MainScript"
version="${version.number}"
copyright="${copyright.string}"/>
<chmod perm="ugo+rx"
file="${quick.dir}/bin/${scalac.exec.name}"/>
<chmod perm="ugo+rx"
@ -458,6 +473,8 @@ BUILD QUICK-TEST LAYER
file="${quick.dir}/bin/${scaladoc.exec.name}"/>
<chmod perm="ugo+rx"
file="${quick.dir}/bin/${scalaint.exec.name}"/>
<chmod perm="ugo+rx"
file="${quick.dir}/bin/${scalascript.exec.name}"/>
</target>
<target name="test.quick" depends="build">
@ -564,6 +581,12 @@ TEST
class="scala.tools.nsc.MainInterpreter"
version="${version.number}"
copyright="${copyright.string}"/>
<straptool
file="${strap.dir}/bin/${scalascript.exec.name}"
name="Scala scripting tool"
class="scala.tools.nsc.MainScript"
version="${version.number}"
copyright="${copyright.string}"/>
<chmod perm="ugo+rx"
file="${strap.dir}/bin/${scalac.exec.name}"/>
<chmod perm="ugo+rx"
@ -572,6 +595,8 @@ TEST
file="${strap.dir}/bin/${scaladoc.exec.name}"/>
<chmod perm="ugo+rx"
file="${strap.dir}/bin/${scalaint.exec.name}"/>
<chmod perm="ugo+rx"
file="${strap.dir}/bin/${scalascript.exec.name}"/>
</target>
<!-- Compares quick and test level -->
@ -722,6 +747,7 @@ GENERATES A DISTRIBUTION
<chmod perm="ugo+rx" file="${dist.current.dir}/bin/${scala.exec.name}"/>
<chmod perm="ugo+rx" file="${dist.current.dir}/bin/${scaladoc.exec.name}"/>
<chmod perm="ugo+rx" file="${dist.current.dir}/bin/${scalaint.exec.name}"/>
<chmod perm="ugo+rx" file="${dist.current.dir}/bin/${scalascript.exec.name}"/>
<!-- Copy the API, examples and man -->
<copy todir="${dist.current.dir}/doc/scala">
<fileset dir="${docs.dir}" includes="README,LICENSE"/>

View File

@ -1,14 +1,32 @@
/* NSC -- new Scala compiler
* Copyright 2005-2006 LAMP/EPFL
* @author Martin Odersky
*/
// $Id: $
package scala.tools.nsc
import java.io.{BufferedReader,FileReader}
/** A main routine to support putting Scala code into scripts. An example
* script would look like this:
/** A main routine to support putting Scala code into scripts.
*
* An shell script example on Unix would look like this:
*
* #!/bin/sh
* scalascript $0 "$@"
* exec scalascript "$0" "$@"
* !#
* Console.println("Hello, world!")
* argv.toList foreach Console.println
*
* A batch file example on Windows XP would look like this:
*
* ::#!
* @echo off
* call scalascript %0 %*
* goto :eof
* ::!#
* Console.println("Hello, world!")
* argv.toList foreach Console.println
*
* TODO: It would be better if error output went to stderr instead
* of stdout....
@ -18,6 +36,9 @@ object MainScript {
* part if there is one. The header part starts with "#!"
* and ends with a line that begins with "!#".
*/
val startRegexp = "^(::)?#!.*"
val endRegexp = "^(::)?!#.*"
def readFileSkippingHeader(filename: String): String = {
val file =
new BufferedReader(
@ -26,15 +47,15 @@ object MainScript {
// skip the header, if there is one
val firstLine = file.readLine
if(firstLine == null)
if (firstLine == null)
return ""
else if(firstLine.startsWith("#!")) {
else if (firstLine.matches(startRegexp)) {
// skip until !# is seen
def lp:Unit = {
def lp: Unit = {
val line = file.readLine
if(line == null)
if (line == null)
()
else if(!line.startsWith("!#"))
else if (!line.matches(endRegexp))
lp
}
lp
@ -45,7 +66,7 @@ object MainScript {
// now read the rest of the file
def lp: Unit = {
val line = file.readLine
if(line == null)
if (line == null)
return ()
contents.append(line)
contents.append("\n")
@ -77,21 +98,21 @@ object MainScript {
def parseArgs(args: List[String])
:Tuple3[List[String], String, List[String]] =
{
if(args.length == 0)
if (args.length == 0)
usageExit
if(args(0).startsWith("-")) {
if (args(0).startsWith("-")) {
// the line includes compiler arguments
val hyphenIndex = args.indexOf("-")
if(hyphenIndex < 0)
if (hyphenIndex < 0)
usageExit
if(hyphenIndex == (args.length - 1))
if (hyphenIndex == (args.length - 1))
usageExit
Tuple3(
args.subseq(0, hyphenIndex).toList,
args(hyphenIndex+1),
args.subseq(hyphenIndex+2, args.length - hyphenIndex - 2).toList)
args(hyphenIndex + 1),
args.subseq(hyphenIndex + 2, args.length - hyphenIndex - 2).toList)
} else {
Tuple3(
Nil,
@ -115,7 +136,7 @@ object MainScript {
if (!command.ok || command.settings.help.value) {
// either the command line is wrong, or the user
// explicitly requested a help listing
if(!command.ok)
if (!command.ok)
Console.println
usageExit
}
@ -130,9 +151,10 @@ object MainScript {
val interpreter = new Interpreter(command.settings)
interpreter.beQuiet
if(!interpreter.compileString(toRun))
if (!interpreter.compileString(toRun))
return () // compilation error
interpreter.bind("argv", "Array[String]", scriptArgs)
interpreter.interpret("scala.scripting.Main.main(argv)")
}
}