factored out the choice of wrapper code, so that subclasses can tweak it

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@12067 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
spoon 2007-06-18 13:42:30 +00:00
parent 80d05c8f00
commit 6ade027f43
1 changed files with 27 additions and 14 deletions

View File

@ -152,6 +152,29 @@ class ScriptRunner {
(Some(fullname.substring(0,idx)), fullname.substring(idx+1))
}
/** Code that is added to the beginning of a script file to make
* it a complete Scala compilation unit.
*/
protected def preambleCode(objectName: String) = {
val (maybePack, objName) = splitObjectName(objectName)
val packageDecl =
maybePack match {
case Some(pack) => "package " + pack + "\n"
case None => ""
}
(packageDecl +
"object " + objName + " {\n" +
" def main(argv: Array[String]): Unit = {\n" +
" val args = argv;\n")
}
/** Code that is added to the end of a script file to make
* it a complete Scala compilation unit.
*/
val endCode = "\n} }\n"
/** Wrap a script file into a runnable object named
* <code>scala.scripting.Main</code>.
@ -161,20 +184,9 @@ class ScriptRunner {
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 =
val preamble =
new SourceFile("<script preamble>",
(packageDecl +
"object " + objName + " {\n" +
" def main(argv: Array[String]): Unit = {\n" +
" val args = argv;\n").toCharArray)
preambleCode(objectName).toCharArray)
val middle = {
val f = new File(filename)
@ -183,7 +195,8 @@ class ScriptRunner {
headerLength(filename),
f.length.asInstanceOf[Int])
}
val end = new SourceFile("<script trailer>", "\n} }\n".toCharArray)
val end = new SourceFile("<script trailer>", endCode.toCharArray)
new CompoundSourceFile(preamble, middle, end)
}