Merge pull request #413 from nrinaudo/7195-maven-settings

This commit is contained in:
eugene yokota 2023-04-09 13:27:58 -04:00 committed by GitHub
commit 48dc7b24ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -391,6 +391,17 @@ private[librarymanagement] abstract class ResolverFunctions {
def defaultRetrievePattern =
"[type]s/[organisation]/[module]/" + PluginPattern + "[artifact](-[revision])(-[classifier]).[ext]"
final val PluginPattern = "(scala_[scalaVersion]/)(sbt_[sbtVersion]/)"
private[librarymanagement] def expandMavenSettings(str: String): String = {
// Aren't regular expressions beautifully clear and concise.
// This means "find all ${...}" blocks, with the first group of each being the text between curly brackets.
val findQuoted = "\\$\\{([^\\}]*)\\}".r
val env = "env\\.(.*)".r
findQuoted.replaceAllIn(str, _.group(1) match {
case env(variable) => sys.env.getOrElse(variable, "")
case property => sys.props.getOrElse(property, "")
})
}
private[this] def mavenLocalDir: File = {
def loadHomeFromSettings(f: () => File): Option[File] =
try {
@ -399,7 +410,7 @@ private[librarymanagement] abstract class ResolverFunctions {
else
((XML.loadFile(file) \ "localRepository").text match {
case "" => None
case e @ _ => Some(new File(e))
case e @ _ => Some(new File(expandMavenSettings(e)))
})
} catch {
// Occurs inside File constructor when property or environment variable does not exist

View File

@ -0,0 +1,43 @@
package sbt.librarymanagement
import verify.BasicTestSuite
import scala.annotation.nowarn
@nowarn // Necessary because our test cases look like interpolated strings.
object ResolverExtraTest extends BasicTestSuite {
test("expandMavenSettings should expand existing environment variables") {
assertExpansion(
input = "User home: ${env.HOME}",
expected = s"User home: ${env("HOME")}"
)
}
test("expandMavenSettings should expand existing system properties") {
assertExpansion(
input = "User dir: ${user.dir}",
expected = s"User dir: ${prop("user.dir")}"
)
}
test("expandMavenSettings should expand unknown system properties to the empty string") {
assertExpansion(
input = "Unknown system property: ${IF_THIS_EXISTS_WE_NEED_TO_HAVE_A_CHAT}",
expected = s"Unknown system property: "
)
}
test("expandMavenSettings should expand unknown environment variables to the empty string") {
assertExpansion(
input = "Unknown environment variable: ${IF_THIS_EXISTS_I_WORRY_ABOUT_YOU}",
expected = s"Unknown environment variable: "
)
}
// - Helper functions ----------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
def assertExpansion(input: String, expected: String) =
assert(Resolver.expandMavenSettings(input) == s"$expected")
def env(name: String) = sys.env.getOrElse(name, "")
def prop(name: String) = sys.props.getOrElse(name, "")
}