mirror of https://github.com/sbt/sbt.git
[2.x] perf: short-circuit --version from build.properties (#8822)
Avoid launching sbt just to render --version by reading sbt.version directly from project/build.properties in the shell script, batch script, and sbtw wrapper. Tighten launcher integration assertions to verify version output no longer depends on the sbtVersion command output.
This commit is contained in:
parent
28d877f633
commit
58f34117ca
|
|
@ -12,11 +12,9 @@ abstract class RunnerScriptTest extends verify.BasicTestSuite with ShellScriptUt
|
|||
private def assertVersionOutput(out: List[String]): Unit =
|
||||
val lines =
|
||||
out.mkString(System.lineSeparator()).linesIterator.map(_.stripPrefix("[0J").trim).toList
|
||||
assert(
|
||||
lines.exists(_.matches("^sbt version in this project: " + versionPattern + "\\r?$")) ||
|
||||
lines.contains("sbtVersion")
|
||||
)
|
||||
assert(lines.exists(_.matches("^sbt version in this project: " + versionPattern + "\\r?$")))
|
||||
assert(lines.exists(_.matches("^sbt runner version: " + versionPattern + "\\r?$")))
|
||||
assert(!lines.exists(_.contains("sbtVersion")))
|
||||
assert(!lines.exists(_.contains("failed to connect to server")))
|
||||
|
||||
testOutput("sbt -no-colors")("compile", "-no-colors", "-v"): (out: List[String]) =>
|
||||
|
|
|
|||
|
|
@ -729,8 +729,12 @@ if !sbt_args_print_sbt_version! equ 1 (
|
|||
|
||||
if !sbt_args_print_version! equ 1 (
|
||||
if !is_this_dir_sbt! equ 1 (
|
||||
call :set_sbt_version
|
||||
echo sbt version in this project: !sbt_version!
|
||||
if defined build_props_sbt_version (
|
||||
call :set_sbt_version_from_build_props
|
||||
) else (
|
||||
call :set_sbt_version
|
||||
)
|
||||
if defined sbt_version echo sbt version in this project: !sbt_version!
|
||||
)
|
||||
echo sbt runner version: !init_sbt_version!
|
||||
>&2 echo.
|
||||
|
|
@ -1151,6 +1155,16 @@ for /F "usebackq tokens=1,2 delims= " %%a in (`CALL "!_JAVACMD!" -jar "!sbt_jar!
|
|||
if not defined sbt_version if defined build_props_sbt_version set "sbt_version=!build_props_sbt_version!"
|
||||
exit /B 0
|
||||
|
||||
:set_sbt_version_from_build_props
|
||||
set "sbt_version=!build_props_sbt_version!"
|
||||
for /F "tokens=* delims= " %%a in ("!sbt_version!") do set "sbt_version=%%a"
|
||||
:trim_version_end
|
||||
if "!sbt_version:~-1!" == " " (
|
||||
set "sbt_version=!sbt_version:~0,-1!"
|
||||
goto trim_version_end
|
||||
)
|
||||
exit /B 0
|
||||
|
||||
:error
|
||||
@endlocal
|
||||
exit /B 1
|
||||
|
|
|
|||
15
sbt
15
sbt
|
|
@ -587,7 +587,10 @@ run() {
|
|||
execRunner "$java_cmd" -jar "$sbt_jar" "sbtVersion" | tail -1 | sed -e 's/\[info\]//g'
|
||||
elif [[ $print_version ]]; then
|
||||
if [[ -n "$is_this_dir_sbt" ]]; then
|
||||
execRunner "$java_cmd" -jar "$sbt_jar" "sbtVersion" | tail -1 | sed -e 's/\[info\]/sbt version in this project:/g'
|
||||
local project_sbt_version
|
||||
if project_sbt_version="$(projectSbtVersion)"; then
|
||||
echo "sbt version in this project: $project_sbt_version"
|
||||
fi
|
||||
fi
|
||||
echo "sbt runner version: $init_sbt_version"
|
||||
echoerr ""
|
||||
|
|
@ -802,6 +805,16 @@ loadPropFile() {
|
|||
done <<< "$(cat "$1" | sed $'/^\#/d;s/\r$//')"
|
||||
}
|
||||
|
||||
projectSbtVersion() {
|
||||
local version
|
||||
version="$(trimString "$build_props_sbt_version")"
|
||||
if [[ -n "$version" ]]; then
|
||||
echo "$version"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
detectNativeClient() {
|
||||
if [[ "$sbtn_command" != "" ]]; then
|
||||
:
|
||||
|
|
|
|||
|
|
@ -113,34 +113,38 @@ object Main:
|
|||
if opts.scriptVersion then
|
||||
println(LauncherOptions.initSbtVersion)
|
||||
return 0
|
||||
val javaCmd = Runner.findJavaCmd(opts.javaHome)
|
||||
val sbtJar = opts.sbtJar
|
||||
.filter(p => new File(p).isFile)
|
||||
.getOrElse(new File(sbtBinDir, "sbt-launch.jar").getAbsolutePath)
|
||||
if !new File(sbtJar).isFile then
|
||||
System.err.println("[error] Launcher jar not found for version check")
|
||||
return 1
|
||||
if opts.numericVersion then
|
||||
try
|
||||
val out = Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
|
||||
println(out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse(""))
|
||||
return 0
|
||||
catch { case _: Exception => return 1 }
|
||||
if opts.version then
|
||||
if ConfigLoader.isSbtProjectDir(cwd) then
|
||||
val out =
|
||||
try Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
|
||||
catch { case _: Exception => "" }
|
||||
val ver = out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse("")
|
||||
println("sbt version in this project: " + ver)
|
||||
projectSbtVersion(cwd).foreach: version =>
|
||||
println("sbt version in this project: " + version)
|
||||
println("sbt runner version: " + LauncherOptions.initSbtVersion)
|
||||
System.err.println("[info] sbt runner (sbtw) is a runner to run any declared version of sbt.")
|
||||
System.err.println(
|
||||
"[info] Actual version of sbt is declared using project\\build.properties for each build."
|
||||
)
|
||||
return 0
|
||||
if opts.numericVersion then
|
||||
val javaCmd = Runner.findJavaCmd(opts.javaHome)
|
||||
val sbtJar = opts.sbtJar
|
||||
.filter(p => new File(p).isFile)
|
||||
.getOrElse(new File(sbtBinDir, "sbt-launch.jar").getAbsolutePath)
|
||||
if !new File(sbtJar).isFile then
|
||||
System.err.println("[error] Launcher jar not found for version check")
|
||||
return 1
|
||||
try
|
||||
val out = Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
|
||||
println(out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse(""))
|
||||
return 0
|
||||
catch { case _: Exception => return 1 }
|
||||
0
|
||||
|
||||
private def projectSbtVersion(cwd: File): Option[String] =
|
||||
ConfigLoader.sbtVersionFromBuildProperties(cwd).flatMap(normalizeVersion)
|
||||
|
||||
private def normalizeVersion(version: String): Option[String] =
|
||||
val trimmed = version.trim
|
||||
if trimmed.nonEmpty then Some(trimmed) else None
|
||||
|
||||
private def printUsage(): Int =
|
||||
println("""
|
||||
|Usage: sbtw [options]
|
||||
|
|
|
|||
Loading…
Reference in New Issue