@@ -2,19 +2,12 @@ package dotty.tools.scripting
22
33import java .nio .file .{ Files , Paths , Path }
44import java .io .File
5- import java .net .{ URL , URLClassLoader }
6- import java .lang .reflect .{ Modifier , Method }
5+ import java .net .{ URLClassLoader }
76
8- import scala .jdk .CollectionConverters ._
9-
10- import dotty .tools .dotc .{ Driver , Compiler }
11- import dotty .tools .dotc .core .Contexts , Contexts .{ Context , ContextBase , ctx }
12- import dotty .tools .dotc .config .CompilerCommand
7+ import dotty .tools .dotc .Driver
8+ import dotty .tools .dotc .core .Contexts , Contexts .{ Context , ctx }
139import dotty .tools .io .{ PlainDirectory , Directory , ClassPath }
14- import dotty .tools .dotc .reporting .Reporter
15- import dotty .tools .dotc .config .Settings .Setting ._
16-
17- import sys .process ._
10+ import Util .*
1811
1912class ScriptingDriver (compilerArgs : Array [String ], scriptFile : File , scriptArgs : Array [String ]) extends Driver :
2013 def compileAndRun (pack: (Path , Seq [Path ], String ) => Boolean = null ): Unit =
@@ -31,7 +24,7 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
3124 try
3225 val classpath = s " ${ctx.settings.classpath.value}${pathsep}${sys.props(" java.class.path" )}"
3326 val classpathEntries : Seq [Path ] = ClassPath .expandPath(classpath, expandStar= true ).map { Paths .get(_) }
34- val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile)
27+ val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile.toString )
3528 val invokeMain : Boolean =
3629 Option (pack) match
3730 case Some (func) =>
@@ -48,58 +41,6 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
4841 case None =>
4942 end compileAndRun
5043
51- private def deleteFile (target : File ): Unit =
52- if target.isDirectory then
53- for member <- target.listFiles.toList
54- do deleteFile(member)
55- target.delete()
56- end deleteFile
57-
58- private def detectMainClassAndMethod (outDir : Path , classpathEntries : Seq [Path ],
59- scriptFile : File ): (String , Method ) =
60-
61- val classpathUrls = (classpathEntries :+ outDir).map { _.toUri.toURL }
62-
63- val cl = URLClassLoader (classpathUrls.toArray)
64-
65- def collectMainMethods (target : File , path : String ): List [(String , Method )] =
66- val nameWithoutExtension = target.getName.takeWhile(_ != '.' )
67- val targetPath =
68- if path.nonEmpty then s " ${path}. ${nameWithoutExtension}"
69- else nameWithoutExtension
70-
71- if target.isDirectory then
72- for
73- packageMember <- target.listFiles.toList
74- membersMainMethod <- collectMainMethods(packageMember, targetPath)
75- yield membersMainMethod
76- else if target.getName.endsWith(" .class" ) then
77- val cls = cl.loadClass(targetPath)
78- try
79- val method = cls.getMethod(" main" , classOf [Array [String ]])
80- if Modifier .isStatic(method.getModifiers) then List ((cls.getName, method)) else Nil
81- catch
82- case _ : java.lang.NoSuchMethodException => Nil
83- else Nil
84- end collectMainMethods
85-
86- val candidates = for
87- file <- outDir.toFile.listFiles.toList
88- method <- collectMainMethods(file, " " )
89- yield method
90-
91- candidates match
92- case Nil =>
93- throw ScriptingException (s " No main methods detected in script ${scriptFile}" )
94- case _ :: _ :: _ =>
95- throw ScriptingException (" A script must contain only one main method. " +
96- s " Detected the following main methods: \n ${candidates.mkString(" \n " )}" )
97- case m :: Nil => m
98- end match
99- end detectMainClassAndMethod
100-
101- def pathsep = sys.props(" path.separator" )
102-
10344end ScriptingDriver
10445
10546case class ScriptingException (msg : String ) extends RuntimeException (msg)
0 commit comments