Skip to content

Commit eecea01

Browse files
qwe2muuki88
andauthored
Fix the -jvm-debug and -h flags in the ash template (#1630)
* Add bash and ash tests for the -jvm-debug and -h flags * Add missing functions to the ash template * replace bash syntax with ash compatible one * Revert "Fixes remote debug connections closing (#1546)" This reverts commit 665e4e2. * fix sbt 2.0 cross-build for new tests --------- Co-authored-by: Muki Seiler <muuki88@users.noreply.github.com>
1 parent 5a12bdb commit eecea01

18 files changed

Lines changed: 164 additions & 6 deletions

File tree

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/sh
22

3+
die() {
4+
echo "$@" 1>&2
5+
exit 1
6+
}
7+
38
realpath () {
49
(
510
TARGET_FILE="$1"
@@ -51,6 +56,19 @@ addResidual () {
5156
residual_args="$residual_args $(shellEscape "$1")"
5257
}
5358

59+
addDebugger () {
60+
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
61+
}
62+
63+
require_arg () {
64+
local type="$1"
65+
local opt="$2"
66+
local arg="$3"
67+
if [ -z "$arg" ] || [ "${arg#-}" != "$arg" ]; then
68+
die "$opt requires <$type> argument"
69+
fi
70+
}
71+
5472
# Allow user to specify java options. These get listed first per bash-template.
5573
if [ -n "$JAVA_OPTS" ]
5674
then
@@ -81,10 +99,6 @@ process_args () {
8199
case "$1" in
82100
--) shift && no_more_snp_opts=1 && break ;;
83101
-h|-help) usage; exit 1 ;;
84-
-v|-verbose) verbose=1 && shift ;;
85-
-d|-debug) debug=1 && shift ;;
86-
87-
-no-version-check) no_version_check=1 && shift ;;
88102

89103
-mem) echo "!! WARNING !! -mem option is ignored. Please use -J-Xmx and -J-Xms" && shift 2 ;;
90104
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
@@ -99,13 +113,41 @@ process_args () {
99113
esac
100114
done
101115

102-
if [ $no_more_snp_opts ]; then
116+
if [ $no_more_snp_opts -ne 0 ]; then
103117
while [ $# -gt 0 ]; do
104118
addResidual "$1" && shift
105119
done
106120
fi
107121
}
108122

123+
usage() {
124+
cat <<EOM
125+
Usage: $script_name [options]
126+
127+
-h | -help print this message
128+
-main <classname> Define a custom main class
129+
-jvm-debug <port> Turn on JVM debugging, open at the given port.
130+
131+
# java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
132+
-java-home <path> alternate JAVA_HOME
133+
134+
# jvm options and output control
135+
JAVA_OPTS environment variable, if unset uses "$java_opts"
136+
-Dkey=val pass -Dkey=val directly to the java runtime
137+
-J-X pass option -X directly to the java runtime
138+
(-J is stripped)
139+
140+
# special option
141+
-- To stop parsing built-in commands from the rest of the command-line.
142+
e.g.) enabling debug and sending -d as app argument
143+
\$ ./start-script -d -- -d
144+
145+
In the case of duplicated or conflicting options, basically the order above
146+
shows precedence: JAVA_OPTS lowest, command line options highest except "--".
147+
${{available_main_classes}}
148+
EOM
149+
}
150+
109151
app_commands=""
110152
residual_args=""
111153
real_script_path="$(realpath "$0")"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ addResidual () {
154154
residual_args+=( "$1" )
155155
}
156156
addDebugger () {
157-
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$1"
157+
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
158158
}
159159

160160
require_arg () {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enablePlugins(JavaAppPackaging, AshScriptPlugin)
2+
3+
name := "script-debug"
4+
5+
version := "0.1.0"
6+
7+
TaskKey[Unit]("runCheck") := {
8+
val cwd = (Universal / stagingDirectory).value
9+
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-jvm-debug", "0")
10+
val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "")
11+
12+
assert(output.contains("Listening for transport dt_socket at address:"),
13+
"Application did not start in debug mode: \n" + output)
14+
assert(output.contains("SUCCESS!"), "Application did not run successfully: \n" + output)
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object MainApp extends App {
2+
println("SUCCESS!")
3+
}

src/sbt-test/ash/script-debug/test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Run the staging and check the script.
2+
> stage
3+
$ exists target/**/universal/stage/bin/script-debug
4+
> runCheck
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
enablePlugins(JavaAppPackaging, AshScriptPlugin)
2+
3+
name := "script-help"
4+
5+
version := "0.1.0"
6+
7+
TaskKey[Unit]("runCheck") := {
8+
val cwd = (Universal / stagingDirectory).value
9+
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-h")
10+
11+
val buffer = new StringBuffer
12+
val code = sys.process.Process(cmd, cwd).run(scala.sys.process.BasicIO(false, buffer, None)).exitValue()
13+
assert(code == 1, "Exit code for -h was not 1: " + code)
14+
15+
val output = buffer.toString.replaceAll("\n", "")
16+
17+
val expectedHelpSamples = Seq(
18+
"-h | -help", "print this message",
19+
"-jvm-debug",
20+
"JAVA_OPTS",
21+
"special option"
22+
)
23+
24+
assert(expectedHelpSamples.forall(output.contains(_)),
25+
s"Application did not print the correct help message: \n" + output)
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object MainApp extends App {
2+
println("SUCCESS!")
3+
}

src/sbt-test/ash/script-help/test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Run the staging and check the script.
2+
> stage
3+
$ exists target/**/universal/stage/bin/script-help
4+
> runCheck

0 commit comments

Comments
 (0)