Skip to content

Commit 5f195f9

Browse files
committed
refactor: extract script patching to its own file
1 parent dc81e35 commit 5f195f9

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

app/build.gradle

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.gradle.plugins.ide.eclipse.model.EclipseWtp
55
plugins {
66
id 'buildlogic.picocli-conventions'
77
id 'buildlogic.java-special-tests-conventions'
8+
id 'buildlogic.java-application-min-java-version-conventions'
9+
id 'buildlogic.java-application-conventions'
810
id 'buildlogic.spotless-json-conventions'
911
}
1012

@@ -19,44 +21,6 @@ dependencies {
1921
implementation project(':provisioner')
2022
}
2123

22-
import org.gradle.api.GradleException
23-
24-
def javaExtension = extensions.getByType(JavaPluginExtension)
25-
def requiredMajor = javaExtension.toolchain.languageVersion.map { it.asInt() }.get()
26-
27-
tasks.named('startScripts', CreateStartScripts).configure {
28-
doLast {
29-
def unixMarker = 'exec "$JAVACMD" "$@"'
30-
def windowsMarker = '"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SPOTLESS_OPTS% -classpath "%CLASSPATH%" com.diffplug.spotless.cli.SpotlessCLI %*'
31-
32-
if (!unixScript.text.contains(unixMarker)) {
33-
throw new GradleException("Unable to patch Unix launcher: '${unixMarker}' not found.")
34-
}
35-
if (!windowsScript.text.contains(windowsMarker)) {
36-
throw new GradleException("Unable to patch Windows launcher: '${windowsMarker}' not found.")
37-
}
38-
39-
unixScript.text = unixScript.text.replace(unixMarker, """
40-
spotlessCheckJavaVersion() {
41-
local current=\$("\$JAVACMD" -version 2>&1 | awk -F[\\".] 'NR==1 {print \$2}')
42-
if [ "\$current" -lt $requiredMajor ]; then
43-
echo "Spotless CLI requires Java $requiredMajor or newer but found \$current. Make sure to install Java >= $requiredMajor and add it to the path or set JAVA_HOME env variable." >&2
44-
exit 1
45-
fi
46-
}
47-
spotlessCheckJavaVersion
48-
$unixMarker""")
49-
50-
windowsScript.text = windowsScript.text.replace(windowsMarker, """
51-
for /f "tokens=3 delims=." %%i in ('"%JAVA_EXE%" -version 2^>^&1 ^| findstr /r "[0-9][0-9]*\\.[0-9][0-9]*.*"') do set CURRENT_JAVA_MAJOR=%%i
52-
if %CURRENT_JAVA_MAJOR% LSS $requiredMajor (
53-
echo Spotless CLI requires Java $requiredMajor or newer but found %CURRENT_JAVA_MAJOR%. Make sure to install Java >= $requiredMajor and add it to the path or set JAVA_HOME env variable. >&2
54-
exit /b 1
55-
)
56-
$windowsMarker""")
57-
}
58-
}
59-
6024
application {
6125
mainClass = 'com.diffplug.spotless.cli.SpotlessCLI'
6226
applicationName = 'spotless'

build-logic/src/main/groovy/buildlogic.java-application-conventions.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* This file was generated by the Gradle 'init' task.
3-
*
4-
* This project uses @Incubating APIs which are subject to change.
5-
*/
6-
71
plugins {
82
// Apply the common convention plugin for shared build configuration between library and application projects.
93
id 'buildlogic.java-common-conventions'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.gradle.api.GradleException
2+
3+
plugins {
4+
// Apply the common convention plugin for shared build configuration between library and application projects.
5+
id 'buildlogic.java-common-conventions'
6+
7+
// Apply the application plugin to add support for building a CLI application in Java.
8+
id 'application'
9+
}
10+
11+
12+
def javaExtension = extensions.getByType(JavaPluginExtension)
13+
def requiredMajor = javaExtension.toolchain.languageVersion.map { it.asInt() }.get()
14+
15+
tasks.named('startScripts', CreateStartScripts).configure {
16+
doLast {
17+
def unixMarker = 'exec "$JAVACMD" "$@"'
18+
def windowsMarker = '"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SPOTLESS_OPTS% -classpath "%CLASSPATH%" com.diffplug.spotless.cli.SpotlessCLI %*'
19+
20+
if (!unixScript.text.contains(unixMarker)) {
21+
throw new GradleException("Unable to patch Unix launcher: '${unixMarker}' not found.\n\nScript:\n${unixScript.text}")
22+
}
23+
if (!windowsScript.text.contains(windowsMarker)) {
24+
throw new GradleException("Unable to patch Windows launcher: '${windowsMarker}' not found.\n\nScript:\n${windowsScript.text}")
25+
}
26+
27+
unixScript.text = unixScript.text.replace(unixMarker, """
28+
spotlessCheckJavaVersion() {
29+
local current=\$("\$JAVACMD" -version 2>&1 | awk -F[\\".] 'NR==1 {print \$2}')
30+
if [ "\$current" -lt $requiredMajor ]; then
31+
echo "Spotless CLI requires Java $requiredMajor or newer but found \$current. Make sure to install Java >= $requiredMajor and add it to the path or set JAVA_HOME env variable." >&2
32+
exit 1
33+
fi
34+
}
35+
spotlessCheckJavaVersion
36+
$unixMarker""")
37+
38+
windowsScript.text = windowsScript.text.replace(windowsMarker, """
39+
for /f "tokens=3 delims=." %%i in ('"%JAVA_EXE%" -version 2^>^&1 ^| findstr /r "[0-9][0-9]*\\.[0-9][0-9]*.*"') do set CURRENT_JAVA_MAJOR=%%i
40+
if %CURRENT_JAVA_MAJOR% LSS $requiredMajor (
41+
echo Spotless CLI requires Java $requiredMajor or newer but found %CURRENT_JAVA_MAJOR%. Make sure to install Java >= $requiredMajor and add it to the path or set JAVA_HOME env variable. >&2
42+
exit /b 1
43+
)
44+
$windowsMarker""")
45+
}
46+
}

0 commit comments

Comments
 (0)