|
1 | 1 | package dotty.tools.scripting
|
2 | 2 |
|
3 | 3 | import java.io.File
|
| 4 | +import java.nio.file.Path |
| 5 | +import dotty.tools.dotc.config.Properties.isWin |
4 | 6 |
|
5 | 7 | /** Main entry point to the Scripting execution engine */
|
6 | 8 | object Main:
|
7 | 9 | /** All arguments before -script <target_script> are compiler arguments.
|
8 | 10 | All arguments afterwards are script arguments.*/
|
9 |
| - def distinguishArgs(args: Array[String]): (Array[String], File, Array[String]) = |
10 |
| - val (compilerArgs, rest) = args.splitAt(args.indexOf("-script")) |
| 11 | + private def distinguishArgs(args: Array[String]): (Array[String], File, Array[String], Boolean, Boolean) = |
| 12 | + val (leftArgs, rest) = args.splitAt(args.indexOf("-script")) |
| 13 | + assert(rest.size >= 2,s"internal error: rest == Array(${rest.mkString(",")})") |
| 14 | + |
11 | 15 | val file = File(rest(1))
|
12 | 16 | val scriptArgs = rest.drop(2)
|
13 |
| - (compilerArgs, file, scriptArgs) |
| 17 | + var saveJar = false |
| 18 | + var invokeFlag = true // by default, script main method is invoked |
| 19 | + val compilerArgs = leftArgs.filter { |
| 20 | + case "-save" | "-savecompiled" => |
| 21 | + saveJar = true |
| 22 | + false |
| 23 | + case "-compile-only" => |
| 24 | + invokeFlag = false // no call to script main method |
| 25 | + false |
| 26 | + case _ => |
| 27 | + true |
| 28 | + } |
| 29 | + (compilerArgs, file, scriptArgs, saveJar, invokeFlag) |
14 | 30 | end distinguishArgs
|
15 | 31 |
|
16 | 32 | def main(args: Array[String]): Unit =
|
17 |
| - val (compilerArgs, scriptFile, scriptArgs) = distinguishArgs(args) |
18 |
| - try ScriptingDriver(compilerArgs, scriptFile, scriptArgs).compileAndRun() |
| 33 | + val (compilerArgs, scriptFile, scriptArgs, saveJar, invokeFlag) = distinguishArgs(args) |
| 34 | + val driver = ScriptingDriver(compilerArgs, scriptFile, scriptArgs) |
| 35 | + try driver.compileAndRun { (outDir:Path, classpath:String, mainClass: String) => |
| 36 | + if saveJar then |
| 37 | + // write a standalone jar to the script parent directory |
| 38 | + writeJarfile(outDir, scriptFile, scriptArgs, classpath, mainClass) |
| 39 | + invokeFlag |
| 40 | + } |
19 | 41 | catch
|
20 | 42 | case ScriptingException(msg) =>
|
21 | 43 | println(s"Error: $msg")
|
22 | 44 | sys.exit(1)
|
| 45 | + |
| 46 | + case e: java.lang.reflect.InvocationTargetException => |
| 47 | + throw e.getCause |
| 48 | + |
| 49 | + private def writeJarfile(outDir: Path, scriptFile: File, scriptArgs:Array[String], |
| 50 | + classpath:String, mainClassName: String): Unit = |
| 51 | + |
| 52 | + val javaClasspath = sys.props("java.class.path") |
| 53 | + val runtimeClasspath = s"${classpath}$pathsep$javaClasspath" |
| 54 | + |
| 55 | + val jarTargetDir: Path = Option(scriptFile.toPath.getParent) match { |
| 56 | + case None => sys.error(s"no parent directory for script file [$scriptFile]") |
| 57 | + case Some(parent) => parent |
| 58 | + } |
| 59 | + |
| 60 | + def scriptBasename = scriptFile.getName.takeWhile(_!='.') |
| 61 | + val jarPath = s"$jarTargetDir/$scriptBasename.jar" |
| 62 | + |
| 63 | + val cpPaths = runtimeClasspath.split(pathsep).map(_.absPath) |
| 64 | + |
| 65 | + import java.util.jar.Attributes.Name |
| 66 | + val cpString:String = cpPaths.distinct.mkString(" ") |
| 67 | + val manifestAttributes:Seq[(Name, String)] = Seq( |
| 68 | + (Name.MANIFEST_VERSION, "1.0"), |
| 69 | + (Name.MAIN_CLASS, mainClassName), |
| 70 | + (Name.CLASS_PATH, cpString), |
| 71 | + ) |
| 72 | + import dotty.tools.io.{Jar, Directory} |
| 73 | + val jar = new Jar(jarPath) |
| 74 | + val writer = jar.jarWriter(manifestAttributes:_*) |
| 75 | + try |
| 76 | + writer.writeAllFrom(Directory(outDir)) |
| 77 | + finally |
| 78 | + writer.close() |
| 79 | + end writeJarfile |
| 80 | + |
| 81 | + def pathsep = sys.props("path.separator") |
| 82 | + |
| 83 | + |
| 84 | + extension(file: File){ |
| 85 | + def norm: String = file.toString.norm |
| 86 | + } |
| 87 | + |
| 88 | + extension(path: String) { |
| 89 | + // Normalize path separator, convert relative path to absolute |
| 90 | + def norm: String = |
| 91 | + path.replace('\\', '/') match { |
| 92 | + case s if s.secondChar == ":" => s.drop(2) |
| 93 | + case s if s.startsWith("./") => s.drop(2) |
| 94 | + case s => s |
| 95 | + } |
| 96 | + |
| 97 | + // convert to absolute path relative to cwd. |
| 98 | + def absPath: String = norm match |
| 99 | + case str if str.isAbsolute => norm |
| 100 | + case _ => s"/${sys.props("user.dir").norm}/$norm" |
| 101 | + |
| 102 | + def absFile: File = File(path.absPath) |
| 103 | + |
| 104 | + // Treat norm paths with a leading '/' as absolute. |
| 105 | + // Windows java.io.File#isAbsolute treats them as relative. |
| 106 | + def isAbsolute = path.norm.startsWith("/") || (isWin && path.secondChar == ":") |
| 107 | + def secondChar: String = path.take(2).drop(1).mkString("") |
| 108 | + } |
0 commit comments