-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·147 lines (139 loc) · 4.37 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
#!/usr/bin/env mksh
# -*- mode: sh -*-
#-
# Copyright © 2021
# mirabilos <t.glaser@qvest-digital.com>
# Licensor: Deutsche Telekom LLCTO
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
#-
# Run executable JAR.
function config {
# make sure this matches the POM
mainclass=org.evolvis.tartools.rfc822.CLI
}
function frobenv {
# extra environment setup, only export commands allowed, for example:
#export LD_LIBRARY_PATH=$top/target/native${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
}
# this must, indeed, use a here document
jarinfo11() { java --source 11 /dev/stdin "$@" <<\EOF
class JEP330Extractor {
public static void main(String[] args) throws Exception {
System.out.println(new java.util.jar.JarFile(args[1])
.getManifest().getMainAttributes()
.getValue(args[0]));
System.exit(0);
}
}
EOF
}
function makecmdline {
local mainclass
config
typeset -ft frobenv
# define local variables first
local top exe cp x m2repo=~/.m2/repository
set +U
# check mainclass begins with package or class
if [[ -z $1 || $mainclass != [a-zA-Z]* ]]; then
print -ru2 -- "[ERROR] This script, ${1@Q}, is misconfigured."
exit 255
fi
# find directory this script is located in
if ! top=$(realpath "$1/.."); then
print -ru2 -- '[ERROR] Could not determine top-level directory.'
exit 255
fi
shift
# determine Maven repository path
[[ -n $M2_REPO && -d $M2_REPO/. ]] && m2repo=$M2_REPO
# figure out whether Maven resource filtering has gifted us with info
cp=<<' end-of-cp'
${runtime.classpath}
end-of-cp
exe=<<' end-of-exe'
${runtime.jarname}
end-of-exe
# determine executable, either from above or by finding marker file
exe=${exe%%*($'\n'|$'\r')}
[[ $exe = [!\$]* ]] || exe=
[[ -z $exe ]] || if [[ -s $top/$exe ]]; then
exe=$top/$exe
else
print -ru2 -- "[WARNING] $exe not found, looking around..."
exe=
fi
[[ -n $exe ]] || for x in "$top"/target/*.cp; do
if [[ -n $exe ]]; then
print -ru2 -- '[ERROR] Found more than one JAR to run.'
exit 255
fi
[[ -s $x ]] || break
exe=${x%.cp}.jar
done
if [[ -z $exe ]]; then
print -ru2 -- '[ERROR] Found no JAR to run.'
exit 255
fi
if [[ ! -s $exe ]]; then
print -ru2 -- "[ERROR] $exe not found."
exit 255
fi
# determine JAR classpath, either from above or a .cp file or JAR manifest
set -U
if [[ $cp = '$'* && -s ${exe%.jar}.cp ]]; then
cp=$(<"${exe%.jar}.cp")
# use only if content is present
[[ $cp = 'classpath='* ]] || cp='$'
fi
if [[ $cp = '$'* ]]; then
if java --source 11 /dev/stdin \
<<<'class x { public static void main(String[] args) { System.exit(0); } }' \
>/dev/null 2>&1; then
cp=$(jarinfo11 x-tartools-cp "$exe") || cp=
elif ! whence jjs >/dev/null 2>&1; then
print -ru2 -- '[ERROR] jjs (from JRE) not installed.'
exit 255
elif ! cp=$(jjs -scripting - -- \
<<<'echo(new java.util.jar.JarFile($ARG[1]).getManifest().getMainAttributes().getValue($ARG[0]));' \
x-tartools-cp "$exe" 2>/dev/null) || \
[[ $cp = *'jjs>'* ]]; then
print -ru2 -- '[ERROR] Neither JEP 330 nor jjs work.'
exit 255
fi
if [[ $cp != $'\u0086classpath='* ]]; then
print -ru2 -- '[ERROR] Could not retrieve classpath' \
"from $exe manifest."
exit 255
fi
fi
cp=${cp#?($'\u0086')classpath=}
cp=${cp%%*($'\n'|$'\r'|$'\u0087')}
cp=${cp//$'\u0095'/"/"}
cp=${cp//$'\u009C'/":"}
cp=${cp//$'\u0096'M2REPO$'\u0097'/"$m2repo"}
set +U
# determine run CLASSPATH
cp=$exe${cp:+:$cp}${CLASSPATH:+:$CLASSPATH}
# put together command line
set -A _ java -cp "$cp" "$mainclass" "$@"
# additional environment setup
frobenv
}
makecmdline "$0" "$@"
set -x
exec "${_[@]}"