forked from binkley/modern-java-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·149 lines (123 loc) · 3.32 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
# shellcheck disable=SC2214,SC2215
# Edit these to suit
readonly package=hm.binkley.md
readonly artifactId=modern-java-practices
readonly version=0-SNAPSHOT
build_tool=maven # or gradle -- this sets the default
# No editable parts below here
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}():} '
set -e
set -u
set -o pipefail
readonly progname="${0##*/}"
case $build_tool in
gradle | maven) ;;
*)
echo "$progname: BUG: Build with 'gradle' or 'maven': $build_tool" >&2
exit 2
;;
esac
function print-help() {
cat <<EOH
Usage: $progname [-GJMXdh] [CLASS] [ARGUMENTS]
Runs a single-jar Kotlin project.
With no CLASS, assume the jar is executable.
-G, --gradle build with Gradle$([[ gradle == "$build_tool" ]] && echo ' (default)')
-J, --java treat CLASS as a Java class
-M, --maven build with Maven$([[ maven == "$build_tool" ]] && echo ' (default)')
-X, --executable stop processing command line
-d, --debug print script execution to STDERR
-h, --help display this help and exit
Examples:
$progname Runs the executable jar with no arguments to main
$progname -X an-arg Runs the executable jar passing "an-arg" to main
$progname a-class Runs the main from "a-class"
EOH
}
function bad-option() {
local opt="$1"
cat <<EOM
$progname: invalid option -- '$opt'
Try '$progname --help' for more information.
EOM
}
function mangle-kotlin-classname() {
local IFS=.
local -a parts
read -r -a parts <<<"$1"
local last="${parts[-1]}"
case "$last" in
*Kt) ;;
*) last="${last}Kt" ;;
esac
last="${last//-/_}"
last=""${last^}
parts[-1]="$last"
echo "${parts[*]}"
}
function rebuild-if-needed() {
# TODO: Rebuild if build script is newer than jar
[[ -e "$jar" && -z "$(find src/main -type f -newer "$jar")" ]] && return
case $build_tool in
gradle) ./gradlew --warning-mode=all jar ;;
maven) ./mvnw -C -Dmaven.test.skip=true package ;;
esac
}
debug=false
executable=false
kotlin=true
while getopts :GJMXdh-: opt; do
[[ $opt == - ]] && opt=${OPTARG%%=*} OPTARG=${OPTARG#*=}
case $opt in
G | gradle) build_tool=gradle ;;
J | java) kotlin=false ;;
M | maven) build_tool=maven ;;
X | executable)
executable=true
break
;;
d | debug) debug=true ;;
h | help)
print-help
exit 0
;;
*)
bad-option "$opt"
exit 2
;;
esac
done
shift $((OPTIND - 1))
case $build_tool in
gradle)
if [[ ! -f build.gradle && ! -f build.gradle.kts ]]; then
echo "$0: BUG: build tool 'gradle' build not supported" >&2
exit 2
fi
readonly jar=build/libs/$artifactId-$version.jar
;;
maven)
if [[ ! -f pom.xml ]]; then
echo "$0: BUG: build tool 'maven' build not supported" >&2
exit 2
fi
readonly jar=target/$artifactId-$version-jar-with-dependencies.jar
;;
esac
$debug && set -x
((0 == $#)) && executable=true
if $executable; then
set - -jar "$jar" "$@"
else
if $kotlin; then
readonly class="$(mangle-kotlin-classname "$package.$1")"
else
readonly class="$package.$1"
fi
shift
set - -cp "$jar" "$class" "$@"
fi
$debug && set -x # "set - ..." clears the -x flag
rebuild-if-needed
exec java -ea --enable-preview "$@"