Skip to content

Commit f5daaa2

Browse files
committed
leverage dotty.tools.io.ClassPath.expandPath ; allow windows */
1 parent 54a2dc9 commit f5daaa2

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

compiler/src/dotty/tools/io/ClassPath.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object ClassPath {
132132
dir.list.filter(x => filt(x.name) && (x.isDirectory || isJarOrZip(x))).map(_.path).toList
133133

134134
if (pattern == "*") lsDir(Directory("."))
135-
else if (pattern.endsWith(wildSuffix)) lsDir(Directory(pattern dropRight 2))
135+
else if (pattern.endsWith(wildSuffix) || pattern.endsWith("/*")) lsDir(Directory(pattern dropRight 2))
136136
else if (pattern.contains('*')) {
137137
try {
138138
val regexp = ("^" + pattern.replace("""\*""", """.*""") + "$").r

compiler/src/dotty/tools/scripting/ScriptingDriver.scala

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import scala.jdk.CollectionConverters._
1010
import dotty.tools.dotc.{ Driver, Compiler }
1111
import dotty.tools.dotc.core.Contexts, Contexts.{ Context, ContextBase, ctx }
1212
import dotty.tools.dotc.config.CompilerCommand
13-
import dotty.tools.io.{ PlainDirectory, Directory }
13+
import dotty.tools.io.{ PlainDirectory, Directory, ClassPath }
1414
import dotty.tools.dotc.reporting.Reporter
1515
import dotty.tools.dotc.config.Settings.Setting._
1616

@@ -29,17 +29,7 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
2929

3030
try
3131
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
32-
val classpathEntries: Seq[Path] =
33-
classpath.split(pathsep).toIndexedSeq.flatMap { entry =>
34-
val f = Paths.get(entry).toAbsolutePath.normalize.toFile
35-
// expand wildcard classpath entries
36-
if (f.getName == "*" && f.getParentFile.isDirectory){
37-
f.getParentFile.listFiles.filter { _.getName.toLowerCase.endsWith(".jar") }.map { _.toPath }.toSeq
38-
} else {
39-
Seq(f.toPath)
40-
}
41-
}.toIndexedSeq
42-
32+
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
4333
val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile)
4434
val invokeMain: Boolean =
4535
Option(pack) match
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dotty.tools.io
2+
3+
import org.junit.Test
4+
5+
import java.io.File
6+
import dotty.tools.io.AbstractFile
7+
import java.nio.file.{Files, Paths}
8+
import java.nio.file.StandardCopyOption._
9+
import java.nio.file.attribute.PosixFilePermissions
10+
import dotty.tools.io.{ PlainDirectory, Directory, ClassPath }
11+
12+
class ClasspathTest {
13+
14+
def pathsep = sys.props("path.separator")
15+
16+
//
17+
// Cope with wildcard classpath entries, exercised with -classpath <cp>
18+
//
19+
// Verify that Windows users not forced to use backslash in classpath.
20+
//
21+
@Test def testWildcards(): Unit =
22+
import dotty.tools.io.ClassPath
23+
val outDir = Files.createTempDirectory("classpath-test")
24+
try
25+
val compilerLib = "dist/target/pack/lib"
26+
for src <- Paths.get(compilerLib).toFile.listFiles.toList.take(5) do
27+
val dest = Paths.get(s"$outDir/${src.getName}")
28+
printf("copy: %s\n",Files.copy(src.toPath,dest)) // ,REPLACE_EXISTING,COPY_ATTRIBUTES))
29+
30+
//outDir.toFile.listFiles.toList.foreach { printf("%s\n",_) }
31+
val cp = Seq(s"$compilerLib/*",s"$outDir/*","not-a-real-directory/*").mkString(pathsep).replace('\\','/')
32+
33+
// need to expand wildcard classpath entries
34+
val entries = ClassPath.expandPath(cp)
35+
for entry <- entries.take(10) do
36+
println(entry)
37+
38+
finally
39+
deleteFile(outDir.toFile)
40+
41+
42+
private def deleteFile(target: File): Unit =
43+
if target.isDirectory then
44+
for member <- target.listFiles.toList
45+
do deleteFile(member)
46+
target.delete()
47+
end deleteFile
48+
}

0 commit comments

Comments
 (0)