@@ -2,19 +2,12 @@ package dotty.tools.scripting
2
2
3
3
import java .nio .file .{ Files , Paths , Path }
4
4
import java .io .File
5
- import java .net .{ URL , URLClassLoader }
6
- import java .lang .reflect .{ Modifier , Method }
5
+ import java .net .{ URLClassLoader }
7
6
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 }
13
9
import 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 .*
18
11
19
12
class ScriptingDriver (compilerArgs : Array [String ], scriptFile : File , scriptArgs : Array [String ]) extends Driver :
20
13
def compileAndRun (pack: (Path , Seq [Path ], String ) => Boolean = null ): Unit =
@@ -31,7 +24,7 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
31
24
try
32
25
val classpath = s " ${ctx.settings.classpath.value}${pathsep}${sys.props(" java.class.path" )}"
33
26
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 )
35
28
val invokeMain : Boolean =
36
29
Option (pack) match
37
30
case Some (func) =>
@@ -48,58 +41,6 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
48
41
case None =>
49
42
end compileAndRun
50
43
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
-
103
44
end ScriptingDriver
104
45
105
46
case class ScriptingException (msg : String ) extends RuntimeException (msg)
0 commit comments