Skip to content

Commit

Permalink
Expand Java properties in class paths passed via --extra-jars
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Apr 11, 2023
1 parent 39d7ea5 commit bba2350
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import utest._

import scala.util.Properties

abstract class LaunchTests extends TestSuite {
abstract class LaunchTests extends TestSuite with LauncherOptions {

def launcher: String

Expand Down Expand Up @@ -175,5 +175,38 @@ abstract class LaunchTests extends TestSuite {
assert(output.containsSlice(expectedFirstLines))
}
}

test("extra jars with properties") {
if (acceptsJOptions)
extraJarsWithProperties()
else
"Disabled"
}
def extraJarsWithProperties(): Unit = {
val files = os.proc(launcher, "fetch", "org.scala-lang:scala3-compiler_3:3.1.3")
.call()
.out.lines()
.map(os.Path(_, os.pwd))
TestUtil.withTempDir { tmpDir0 =>
val tmpDir = os.Path(tmpDir0, os.pwd)
val dir = tmpDir / "cp"
for (f <- files)
os.copy.into(f, dir, createFolders = true)
val output = os.proc(
launcher,
s"-J-Dthe.directory=$dir",
"launch",
"--extra-jars",
s"$${the.directory}/*",
"-M",
"dotty.tools.MainGenericCompiler"
).call(mergeErrIntoOut = true).out.lines()
val expectedFirstLines = Seq(
"Usage: scalac <options> <source files>",
"where possible standard options include:"
)
assert(output.containsSlice(expectedFirstLines))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package coursier.clitests

object PackLaunchTests extends LaunchTests {
object PackLaunchTests extends LaunchTests with PackLauncherOptions {
val launcher = LauncherTestUtil.launcher
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ object SharedLaunchParams {
}

// check if those exist?
val extraJars = options.extraJars.flatMap(ClassPathUtil.classPath)
val extraJars = options.extraJars.flatMap(ClassPathUtil.classPath(_, sys.props.get))

(resolveV, artifactV, sharedLoaderV, propertiesV).mapN {
(resolve, artifact, sharedLoader, properties) =>
Expand Down
34 changes: 28 additions & 6 deletions modules/cli/src/main/scala/coursier/cli/util/ClassPathUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@ package coursier.cli.util

import java.io.File
import java.nio.file.{Files, Path, Paths}
import java.util.regex.{Matcher, Pattern}

import scala.collection.JavaConverters._

object ClassPathUtil {

def classPath(input: String): Seq[Path] =
private val propertyRegex = Pattern.compile(
Pattern.quote("${") + "[^" + Pattern.quote("{[()]}") + "]*" + Pattern.quote("}")
)

def classPath(input: String, getProperty: String => Option[String]): Seq[Path] =
input.split(File.pathSeparator).filter(_.nonEmpty).flatMap { elem =>
val processedElem = {
var value = elem
var matcher: Matcher = null

while ({
matcher = propertyRegex.matcher(value)
matcher.find()
}) {
val start = matcher.start(0)
val end = matcher.end(0)
val subKey = value.substring(start + 2, end - 1)
val subValue = getProperty(subKey).getOrElse("")
value = value.substring(0, start) + subValue + value.substring(end)
}

value
}
def allJarsOf(dir: Path): Seq[Path] =
Files.list(dir)
.iterator
Expand All @@ -19,16 +41,16 @@ object ClassPathUtil {
name.substring(name.length() - ".jar".length).equalsIgnoreCase(".jar")
}
.toVector
if (elem.endsWith("/*")) {
val dir = Paths.get(elem.stripSuffix("/*"))
if (processedElem.endsWith("/*")) {
val dir = Paths.get(processedElem.stripSuffix("/*"))
allJarsOf(dir)
}
else if (elem.endsWith("/*.jar")) {
val dir = Paths.get(elem.stripSuffix("/*.jar"))
else if (processedElem.endsWith("/*.jar")) {
val dir = Paths.get(processedElem.stripSuffix("/*.jar"))
allJarsOf(dir)
}
else
Seq(Paths.get(elem))
Seq(Paths.get(processedElem))
}

}

0 comments on commit bba2350

Please sign in to comment.