-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-java-bindings
executable file
·284 lines (248 loc) · 12.1 KB
/
generate-java-bindings
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env bash
DOC='Generate Java bindings for DAML.
It generates the package (mvn package) for the current directory DAML project.
Usage:
generate-java-bindings [options] <basepackage>
basepackage is the base Java package for the generated code (e.g. com.example)
Options:
-c --mvncommand=<maven subcommand> Possible values are `install` and `deploy` which directly binds to Maven [default: package].
-o --output=<dir> The output directory for the bindings generation [default: daml.java].
-r --release Generate a non-SNAPSHOT version (a release).
NOTE: When using the --mvncommand=deploy a file pom-distributionManagement.xml defining the POM section is required. This file must be in the project root.
'
# docopt parser below, refresh this parser with `docopt.sh generate-java-bindings`
# shellcheck disable=2016,1075
docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash
doc_hash=$(printf "%s" "$DOC" | shasum -a 256)
if [[ ${doc_hash:0:5} != "$digest" ]]; then
stderr "The current usage doc (${doc_hash:0:5}) does not match \
what the parser was generated with (${digest})
Run \`docopt.sh\` to refresh the parser."; _return 70; fi; fi; local root_idx=$1
shift; argv=("$@"); parsed_params=(); parsed_values=(); left=(); testdepth=0
local arg; while [[ ${#argv[@]} -gt 0 ]]; do if [[ ${argv[0]} = "--" ]]; then
for arg in "${argv[@]}"; do parsed_params+=('a'); parsed_values+=("$arg"); done
break; elif [[ ${argv[0]} = --* ]]; then parse_long
elif [[ ${argv[0]} = -* && ${argv[0]} != "-" ]]; then parse_shorts
elif ${DOCOPT_OPTIONS_FIRST:-false}; then for arg in "${argv[@]}"; do
parsed_params+=('a'); parsed_values+=("$arg"); done; break; else
parsed_params+=('a'); parsed_values+=("${argv[0]}"); argv=("${argv[@]:1}"); fi
done; local idx; if ${DOCOPT_ADD_HELP:-true}; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${shorts[$idx]} = "-h" || ${longs[$idx]} = "--help" ]]; then
stdout "$trimmed_doc"; _return 0; fi; done; fi
if [[ ${DOCOPT_PROGRAM_VERSION:-false} != 'false' ]]; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${longs[$idx]} = "--version" ]]; then stdout "$DOCOPT_PROGRAM_VERSION"
_return 0; fi; done; fi; local i=0; while [[ $i -lt ${#parsed_params[@]} ]]; do
left+=("$i"); ((i++)) || true; done
if ! required "$root_idx" || [ ${#left[@]} -gt 0 ]; then error; fi; return 0; }
parse_shorts() { local token=${argv[0]}; local value; argv=("${argv[@]:1}")
[[ $token = -* && $token != --* ]] || _return 88; local remaining=${token#-}
while [[ -n $remaining ]]; do local short="-${remaining:0:1}"
remaining="${remaining:1}"; local i=0; local similar=(); local match=false
for o in "${shorts[@]}"; do if [[ $o = "$short" ]]; then similar+=("$short")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ ${#similar[@]} -gt 1 ]]; then
error "${short} is specified ambiguously ${#similar[@]} times"
elif [[ ${#similar[@]} -lt 1 ]]; then match=${#shorts[@]}; value=true
shorts+=("$short"); longs+=(''); argcounts+=(0); else value=false
if [[ ${argcounts[$match]} -ne 0 ]]; then if [[ $remaining = '' ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${short} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
else value=$remaining; remaining=''; fi; fi; if [[ $value = false ]]; then
value=true; fi; fi; parsed_params+=("$match"); parsed_values+=("$value"); done
}; parse_long() { local token=${argv[0]}; local long=${token%%=*}
local value=${token#*=}; local argcount; argv=("${argv[@]:1}")
[[ $token = --* ]] || _return 88; if [[ $token = *=* ]]; then eq='='; else eq=''
value=false; fi; local i=0; local similar=(); local match=false
for o in "${longs[@]}"; do if [[ $o = "$long" ]]; then similar+=("$long")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ $match = false ]]; then i=0; for o in "${longs[@]}"; do
if [[ $o = $long* ]]; then similar+=("$long"); [[ $match = false ]] && match=$i
fi; ((i++)) || true; done; fi; if [[ ${#similar[@]} -gt 1 ]]; then
error "${long} is not a unique prefix: ${similar[*]}?"
elif [[ ${#similar[@]} -lt 1 ]]; then
[[ $eq = '=' ]] && argcount=1 || argcount=0; match=${#shorts[@]}
[[ $argcount -eq 0 ]] && value=true; shorts+=(''); longs+=("$long")
argcounts+=("$argcount"); else if [[ ${argcounts[$match]} -eq 0 ]]; then
if [[ $value != false ]]; then
error "${longs[$match]} must not have an argument"; fi
elif [[ $value = false ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${long} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
fi; if [[ $value = false ]]; then value=true; fi; fi; parsed_params+=("$match")
parsed_values+=("$value"); }; required() { local initial_left=("${left[@]}")
local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
return 0; }; optional() { local node_idx; for node_idx in "$@"; do
"node_$node_idx"; done; return 0; }; switch() { local i
for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
eval "((var_$1++))" || true; else eval "var_$1=true"; fi; return 0; fi; done
return 1; }; value() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; local value
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:0:767}
usage=${DOC:114:57}; digest=302cc; shorts=(-c -o -r)
longs=(--mvncommand --output --release); argcounts=(1 1 0); node_0(){
value __mvncommand 0; }; node_1(){ value __output 1; }; node_2(){
switch __release 2; }; node_3(){ value _basepackage_ a; }; node_4(){
optional 0 1 2; }; node_5(){ optional 4; }; node_6(){ required 5 3; }; node_7(){
required 6; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:114:57}" >&2; exit 1; }'; unset var___mvncommand \
var___output var___release var__basepackage_; parse 7 "$@"
local prefix=${DOCOPT_PREFIX:-''}; local docopt_decl=1
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_decl=2; unset "${prefix}__mvncommand" \
"${prefix}__output" "${prefix}__release" "${prefix}_basepackage_"
eval "${prefix}"'__mvncommand=${var___mvncommand:-package}'
eval "${prefix}"'__output=${var___output:-daml.java}'
eval "${prefix}"'__release=${var___release:-false}'
eval "${prefix}"'_basepackage_=${var__basepackage_:-}'; local docopt_i=0
for ((docopt_i=0;docopt_i<docopt_decl;docopt_i++)); do
declare -p "${prefix}__mvncommand" "${prefix}__output" "${prefix}__release" \
"${prefix}_basepackage_"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh generate-java-bindings`
check_args() {
if ! printf '%s\n' "${VALID_MVNCOMMANDS[@]}" | grep --quiet -P "^${__mvncommand}$"; then
docopt_exit "Invalid value for --mvncommand: ${__mvncommand}"
fi
}
check_env() {
# Check daml
[[ ! -x "$(command -v daml)" ]] &&
echo "daml command not found in PATH. Install DAML SDK and assure it is on PATH." &&
exit 11
# Check Maven
[[ ! -x "$(command -v mvn)" ]] &&
echo "mvn command not found in PATH. Install Maven and assure it is on PATH. You can install it with sdkman.io if you like :)" &&
exit 12
}
#---
# Initialize DAML SDK and contract versions array.
#---
set_daml_versions() {
[[ -f "$DAML_YAML" ]] || exit 13
sdk_version=$(grep '^sdk-version:' "$DAML_YAML" | cut -d ' ' -f2)
contract_version=$(grep '^version:' "$DAML_YAML" | cut -d ' ' -f2)
VERSIONS=("$sdk_version" "$contract_version")
}
#---
# Gives the expected path where the POM will reside.
#---
pom_file_path() {
echo "${PROJECT_ROOT}/${__output}/${PROJECT_NAME}/pom.xml"
}
#---
# Defined as function to dynamically resolve version placeholder.
#---
get_daml_bindings_dep() {
cat <<EOF
<dependency>
<groupId>com.daml</groupId>
<artifactId>bindings-rxjava</artifactId>
<version>${SDK_VERSION}</version>
</dependency>
EOF
}
generate_from_archetype() {
local pom_file=$(pom_file_path)
mvn -B archetype:generate \
-DgroupId="${_basepackage_}" \
-DartifactId="$PROJECT_NAME" \
-DarchetypeGroupId="org.apache.maven.archetypes" \
-DarchetypeArtifactId="maven-archetype-quickstart" &&
find . -type f -name '*.java' | xargs rm -f # rm code from archetype
# Fix compiler version
sed -i -e 's/<maven.compiler.source>1.7/<maven.compiler.source>1.8/' \
-e 's/<maven.compiler.target>1.7/<maven.compiler.target>1.8/' \
"$pom_file"
# Add DAML Java bindings required dependency to POM
local bindings_dep=$(get_daml_bindings_dep)
bindings_dep=${bindings_dep//$'\n'/\\$'\n'} # just escape \n with \\n for sed to work
sed -i "s|<dependencies>|<dependencies>\n${bindings_dep}|" "$pom_file"
}
#---
# Create an empty Maven project for generated bindings.
#---
create_maven_project() {
(
local pom_file=$(pom_file_path)
mkdir -p "${PROJECT_ROOT}/${__output}"
cd "${PROJECT_ROOT}/${__output}"
[[ -f "$pom_file" ]] || generate_from_archetype
)
}
#---
# Populate POM with artifact <version> taking it from daml.yaml contract version.
#---
populate_artifact_version() {
local version=$([[ $__release == "true" ]] && echo "$CONTRACT_VERSION" || echo "${CONTRACT_VERSION}-SNAPSHOT")
sed -i "0,/<version>/{s|<version>.*</version>|<version>${version}</version>|}" "$(pom_file_path)"
}
#---
# Populate POM with required plugins. These are read from a file.
#---
populate_pom_plugins() {
plugins=$(cat "${GENERATOR_HOME}/resources/pom-plugins.xml")
plugins=${plugins//$'\n'/\\$'\n'} # just escape \n with \\n for sed to work
sed -i "s| </build>|${plugins}\n </build>|" "$(pom_file_path)"
sed -i "s|<file>DAR_FILE_PLACEHOLDER</file>|<file>${PROJECT_ROOT}/${dar_file}</file>|" "$(pom_file_path)"
}
#---
# Populate POM with <distributionManagement> reading it from a file.
#---
populate_pom_distributionManagement() {
[[ ! -f "$DISTRIBUTION_MANAGEMENT_FILE" ]] &&
echo "Error: Missing file ${DISTRIBUTION_MANAGEMENT_FILE}" &&
echo "Define a piece of XML with the content, i.e. single or multiple <repository>, to be included in <distributionManagement> of the pom.xml." &&
exit 21
local dist_management=$(cat "$DISTRIBUTION_MANAGEMENT_FILE")
dist_management=${dist_management//$'\n'/\\$'\n'} # just escape \n with \\n for sed to work
sed -i "s|</project>|${dist_management}\n</project>|" "$(pom_file_path)"
}
mvn_build() {
(
populate_artifact_version
populate_pom_plugins
[[ "$__mvncommand" == "deploy" ]] && populate_pom_distributionManagement
cd "${PROJECT_ROOT}/${__output}/${PROJECT_NAME}"
mvn $__mvncommand
)
}
main() {
echo -e "\nGenerating Maven library with DAML bindings...\n"
echo "Binding generator called with the following command/arguments:"
echo " $0 $@"
echo
create_maven_project
echo -e "\n*****************\nBuilding DAR...\n"
dar_file=$(daml build | sed --quiet --regexp-extended -e 's/Created\s+(.*\.dar)/\1/p')
# and generate bindings into Maven project src dir
daml codegen java -o "${__output}/${PROJECT_NAME}/src/main/java" "${dar_file}=${_basepackage_}"
mvn_build
}
# Constants
DAML_YAML="daml.yaml"
VALID_MVNCOMMANDS=("package" "install" "deploy")
PROJECT_ROOT="$PWD"
GENERATOR_HOME=$(dirname $0)
DISTRIBUTION_MANAGEMENT_FILE="${PROJECT_ROOT}/pom-distributionManagement.xml"
# Parse arguments
eval "$(docopt "$@")" # See https://github.com/andsens/docopt.sh for the magic :)
check_args
check_env
PROJECT_NAME=$(grep '^name:' "$DAML_YAML" | cut -d ' ' -f2)
set_daml_versions
SDK_VERSION=${VERSIONS[0]}
CONTRACT_VERSION=${VERSIONS[1]}
main "$@"