Skip to content

Commit dafab60

Browse files
authored
Windows bat script improvements (#1563)
* bat script: reduce nesting in process_args This will make it easier to support new arguments. It shouldn't change the functionality. * bat script: fix parsing of -J-XX: jvm args * bat script: add -java-home option Parse arguments before verifying the Java installation
1 parent 59f3385 commit dafab60

File tree

2 files changed

+40
-25
lines changed
  • src
    • main/resources/com/typesafe/sbt/packager/archetypes/scripts
    • sbt-test/windows/test-bat-template

2 files changed

+40
-25
lines changed

src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ if defined BUNDLED_JVM (
5757

5858
if "%_JAVACMD%"=="" set _JAVACMD=java
5959

60+
rem if configuration files exist, prepend their contents to the script arguments so it can be processed by this runner
61+
call :parse_config "%SCRIPT_CONF_FILE%" SCRIPT_CONF_ARGS
62+
63+
call :process_args %SCRIPT_CONF_ARGS% %%*
64+
6065
rem Detect if this java is ok to use.
6166
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
6267
if %%~j==java set JAVAINSTALLED=1
@@ -86,11 +91,6 @@ if "%JAVAOK%"=="false" (
8691
exit /B 1
8792
)
8893

89-
rem if configuration files exist, prepend their contents to the script arguments so it can be processed by this runner
90-
call :parse_config "%SCRIPT_CONF_FILE%" SCRIPT_CONF_ARGS
91-
92-
call :process_args %SCRIPT_CONF_ARGS% %%*
93-
9494
set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS!
9595

9696
if defined CUSTOM_MAIN_CLASS (
@@ -135,23 +135,36 @@ exit /B 0
135135
rem Processes incoming arguments and places them in appropriate global variables
136136
:process_args
137137
:param_loop
138-
call set _PARAM1=%%1
139-
set "_TEST_PARAM=%~1"
138+
shift
139+
call set _PARAM1=%%0
140+
set "_TEST_PARAM=%~0"
140141

141142
if ["!_PARAM1!"]==[""] goto param_afterloop
142143

144+
if "!_TEST_PARAM!"=="-main" (
145+
call set CUSTOM_MAIN_CLASS=%%1
146+
shift
147+
goto param_loop
148+
)
143149

144-
rem ignore arguments that do not start with '-'
145-
if "%_TEST_PARAM:~0,1%"=="-" goto param_java_check
146-
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
147-
shift
148-
goto param_loop
150+
if "!_TEST_PARAM!"=="-java-home" (
151+
set "JAVA_HOME=%~1"
152+
set "_JAVACMD=%~1\bin\java.exe"
153+
shift
154+
goto param_loop
155+
)
149156

150-
:param_java_check
151157
if "!_TEST_PARAM:~0,2!"=="-J" (
152158
rem strip -J prefix
153-
set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM:~2!
154-
shift
159+
call set _TEST_PARAM=!_TEST_PARAM:~2!
160+
if not "!_TEST_PARAM:~0,5!" == "-XX:+" if not "!_TEST_PARAM:~0,5!" == "-XX:-" if "!_TEST_PARAM:~0,3!" == "-XX" (
161+
rem special handling for -J-XX since '=' gets parsed away
162+
for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
163+
call set _TEST_PARAM=!_TEST_PARAM!=%%1
164+
shift
165+
)
166+
)
167+
set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM!
155168
goto param_loop
156169
)
157170

@@ -160,22 +173,18 @@ rem Processes incoming arguments and places them in appropriate global variables
160173
for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
161174
if not ["%%H"] == [""] (
162175
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
163-
) else if [%2] neq [] (
176+
) else if [%1] neq [] (
164177
rem it was a normal property: -Dprop=42 or -Drop="42"
165-
call set _PARAM1=%%1=%%2
178+
call set _PARAM1=%%0=%%1
166179
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
167180
shift
168181
)
169182
)
170-
) else (
171-
if "!_TEST_PARAM!"=="-main" (
172-
call set CUSTOM_MAIN_CLASS=%%2
173-
shift
174-
) else (
175-
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
176-
)
183+
goto param_loop
177184
)
178-
shift
185+
186+
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
187+
179188
goto param_loop
180189
:param_afterloop
181190

src/sbt-test/windows/test-bat-template/build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ TaskKey[Unit]("checkScript") := {
100100
"arg #0 is [first]\narg #1 is [-XX]\narg #2 is [last]\nproperty(test.hoge) is [huga]\nvmarg #0 is [-Dtest.hoge=huga]\nvmarg #1 is [-Xms6m]\nSUCCESS!",
101101
Map("show-vmargs" -> "true")
102102
)
103+
checkOutput(
104+
"with -J-XX java-opts",
105+
Seq("-J-XX:+UseG1GC", "-J-XX:-UnlockExperimentalVMOptions", "-J-XX:MaxGCPauseMillis=500"),
106+
"vmarg #0 is [-XX:+UseG1GC]\nvmarg #1 is [-XX:-UnlockExperimentalVMOptions]\nvmarg #2 is [-XX:MaxGCPauseMillis=500]\nSUCCESS!",
107+
Map("show-vmargs" -> "true")
108+
)
103109
checkOutput(
104110
"include space",
105111
Seq("""-Dtest.hoge=C:\Program Files\Java""", """"C:\Program Files\Java""""),

0 commit comments

Comments
 (0)