-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate.sh
executable file
·316 lines (280 loc) · 11.2 KB
/
generate.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
set -e
if [ "$1" == "-h" ]; then
echo "Usage: $(basename $0) [library] [operation]"
echo "[library] is required: Possible values: "
echo " * all: it generates all java and javascripts options"
echo " * java: it generates all the java options"
echo " * jersey2: it generates jersey2 java version"
echo " * vertx: it generates vertx java version"
echo " * okhttp-gson: it generates okhttp-gson java version"
echo " * typescript: it generates typescript-node and typescript-fetch version"
echo " * typescript-node: it generates typescript-node javascript version"
echo " * typescript-fetch: it generates typescript-fetch javascript version"
echo " * python: it generates python version"
echo "[operation] is optional. Possible values: "
echo " * no value | unknown value: It generates and builds the libraries."
echo " * publish | main: It generates, builds, and publish the libraries and documentation to npm, pypi, maven repos and/or github pages. "
echo " * release: It generates, builds, and publish the libraries and documentation to npm, pypi, maven repos and/or github pages updating the version to a release."
exit 0
fi
LIBRARY_ARG="$1"
OPERATION_ARG="$2"
arg1Values=['all','java','jersey2','vertx','okhttp-gson','typescript','typescript-node','typescript-fetch','python']
if [[ " ${arg1Values[*]} " != *"$LIBRARY_ARG"* || "" == "$LIBRARY_ARG" ]]; then
echo "Usage: $(basename $0) [library] [operation]"
echo "Invalid library argument '$LIBRARY_ARG'. Possible values are ${arg1Values[*]} "
exit 1
fi
SNAPSHOT_PREFIX=-SNAPSHOT
VERSION=$(head -n 1 version.txt)
TRAVIS_REPO_SLUG="${TRAVIS_REPO_SLUG:-nemtech/symbol-openapi-generator}"
GIT_USER_ID="$(cut -d'/' -f1 <<<"$TRAVIS_REPO_SLUG")"
GIT_REPO_ID="$(cut -d'/' -f2 <<<"$TRAVIS_REPO_SLUG")"
INPUT_PATCHED="openapi3-any-of-patch.yml"
INPUT="openapi3.yml"
BUILD_DIR="./build"
echo "Operation: $OPERATION_ARG"
if [[ $OPERATION_ARG == "main" ]]; then
OPERATION_ARG="publish"
fi
if [[ $OPERATION_ARG == "release" ]]; then
SNAPSHOT=false
FULL_VERSION="$VERSION"
else
SNAPSHOT=true
FULL_VERSION="$VERSION$SNAPSHOT_PREFIX"
fi
echo "Library: $LIBRARY_ARG"
echo "Operation: $OPERATION_ARG"
echo "Full Version: $FULL_VERSION"
echo "Version: $VERSION"
echo "Snapshot: $SNAPSHOT"
echo "Travis Repo Slug: $TRAVIS_REPO_SLUG"
echo "Git User ID: $GIT_USER_ID"
echo "Git Repo ID: $GIT_REPO_ID"
echo "Open Api generator version: $(openapi-generator version)"
export JAVA_OPTS="-Dlog.level=error"
buildJava() {
OPERATION="$1"
echo "Build Java running operation $OPERATION"
echo "./gradlew install"
./gradlew install
if [[ $OPERATION == "publish" || $OPERATION == "release" ]]; then
echo "./gradlew publish"
./gradlew publish
fi
if [[ $OPERATION == "release" ]]; then
echo "./gradlew closeAndReleaseRepository"
./gradlew closeAndReleaseRepository
fi
if [[ $OPERATION == "publish" || $OPERATION == "release" ]]; then
echo "./gradlew gitPublishPush"
./gradlew gitPublishPush
fi
}
generateJava() {
LIBRARY="$1"
OPERATION="$2"
ARTIFACT_ID="symbol-openapi-$LIBRARY-client"
echo "Generating $LIBRARY"
rm -rf "$BUILD_DIR/$ARTIFACT_ID"
openapi-generator generate -g java \
-o "$BUILD_DIR/$ARTIFACT_ID" \
-t "java-templates/" \
-i "$INPUT_PATCHED" \
--additional-properties="apiPackage=io.nem.symbol.sdk.openapi.$LIBRARY.api" \
--additional-properties="invokerPackage=io.nem.symbol.sdk.openapi.$LIBRARY.invoker" \
--additional-properties="modelPackage=io.nem.symbol.sdk.openapi.$LIBRARY.model" \
--additional-properties=library="$LIBRARY" \
--additional-properties=groupId="io.nem" \
--additional-properties="artifactId=$ARTIFACT_ID" \
--additional-properties=artifactVersion="$FULL_VERSION" \
--additional-properties=dateLibrary=java8 \
--additional-properties=java8=true \
--type-mappings=x-number-string=java.math.BigInteger \
--type-mappings=Timestamp=java.math.BigInteger
buildJava $OPERATION
return 0
}
generateTypescript() {
LIBRARY="$1"
OPERATION="$2"
ARTIFACT_ID="symbol-openapi-$LIBRARY-client"
echo "Generating $LIBRARY"
rm -rf "$BUILD_DIR/$ARTIFACT_ID"
openapi-generator generate -g "$LIBRARY" \
-o "$BUILD_DIR/$ARTIFACT_ID" \
-t "$LIBRARY-templates/" \
-i "$INPUT" \
--git-user-id "$GIT_USER_ID" \
--git-repo-id "$GIT_REPO_ID" \
--additional-properties="supportsES6=true" \
--additional-properties="legacyDiscriminatorBehavior=false" \
--additional-properties="npmName=$ARTIFACT_ID" \
--additional-properties=gitUserId=$GIT_USER_ID \
--additional-properties=gitRepoId=$GIT_REPO_ID \
--additional-properties="npmVersion=$VERSION" \
--additional-properties="snapshot=$SNAPSHOT" \
--additional-properties="useSingleRequestParameter=false" \
--additional-properties="typescriptThreePlus=true" \
--type-mappings=x-number-string=string
cp "$LIBRARY-templates/.npmignore" "$BUILD_DIR/$ARTIFACT_ID"
cp "$LIBRARY-templates/README.md" "$BUILD_DIR/$ARTIFACT_ID"
sh -c "cd $BUILD_DIR/$ARTIFACT_ID && npm install"
sh -c "cd $BUILD_DIR/$ARTIFACT_ID && npm run-script build"
if [[ $OPERATION == "publish" ]]; then
cp "$LIBRARY-templates/.npmrc" "$BUILD_DIR/$ARTIFACT_ID/.npmrc"
sh -c "cd $BUILD_DIR/$ARTIFACT_ID && npm publish --tag snapshot"
fi
if [[ $OPERATION == "release" ]]; then
cp "$LIBRARY-templates/.npmrc" "$BUILD_DIR/$ARTIFACT_ID/.npmrc"
sh -c "cd $BUILD_DIR/$ARTIFACT_ID && npm publish"
fi
return 0
}
generatePython() {
LIBRARY="$1"
OPERATION="$2"
ARTIFACT_ID="symbol-openapi-$LIBRARY-client"
PACKAGE_NAME="symbol_openapi_client"
LICENSE_INFO="Apache-2.0"
INFO_NAME="nemtech"
INFO_EMAIL="[email protected]"
# Prerelease and snapshot must follow PEP 440 to upload to PyPI.
PRERELEASE_VERSION="a1"
SNAPSHOT_DATETIME=".$(date -u +'%Y%m%d.%H%M%S')" # UTC time for snapshots
# Set the full package version
PACKAGE_VERSION="${VERSION}${SNAPSHOT_DATETIME}${PRERELEASE_VERSION}" # alpha publish version
if [[ $OPERATION == "release" ]]; then
PACKAGE_VERSION="${VERSION}" # release version
fi
# Patch openapi yaml for python test generator
PY_INPUT=$INPUT_PATCHED
cp $INPUT $PY_INPUT
echo "python_openapi3_patch.sh $PY_INPUT $INPUT"
bash python_openapi3_patch.sh $PY_INPUT $INPUT
echo "The command \"bash python_openapi3_patch.sh \$PY_INPUT \$INPUT\" exited with $?."
# Generate the python openapi library
echo "Generating $LIBRARY"
rm -rf "$BUILD_DIR/$ARTIFACT_ID"
openapi-generator generate -g "$LIBRARY" \
-o "$BUILD_DIR/$ARTIFACT_ID" \
-t "$LIBRARY-templates/" \
-i "$PY_INPUT" \
-p "projectName=$ARTIFACT_ID" \
-p "packageName=$PACKAGE_NAME" \
-p "packageVersion=$PACKAGE_VERSION" \
--additional-properties="licenseInfo=$LICENSE_INFO" \
--additional-properties="infoName=$INFO_NAME" \
--additional-properties="infoEmail=$INFO_EMAIL" \
--additional-properties="snapshot=$SNAPSHOT" \
--type-mappings=x-number-string=int
# Build, test, publish/release
buildPython "$BUILD_DIR" "$ARTIFACT_ID" "$PACKAGE_VERSION" "$OPERATION"
rm $PY_INPUT
return 0
}
buildPython() {
BUILD_DIR="$1"
ARTIFACT_ID="$2"
PACKAGE_VERSION="$3"
OPERATION="$4"
# Go to artifact build dir
ORIGINAL_DIR="$(pwd)" # we'll return to this directory after the build
cd "$BUILD_DIR/$ARTIFACT_ID"
echo "Build Python running operation '$OPERATION'"
# Build
echo "python3 setup.py sdist bdist_wheel build"
PYTHONPATH=".:${PYTHONPATH}" python3 setup.py sdist bdist_wheel build
# Patch openapi generated test files
# TEST_DIR="$ORIGINAL_DIR/build/$ARTIFACT_ID/test"
# echo "bash $ORIGINAL_DIR/python_test_files_patch.sh $TEST_DIR"
# bash "$ORIGINAL_DIR/python_test_files_patch.sh" "$TEST_DIR"
# echo "The command \"bash \$ORIGINAL_DIR/python_test_files_patch.sh \$TEST_DIR\" exited with $?."
# Tests
# Commented out pytest as the openapi generated test files have many errors that can't be easily patched.
# echo "python3 -m pytest -v --color=yes --showlocals --maxfail=100"
# PYTHONPATH=".:${PYTHONPATH}" python3 -m pytest -v --color=yes --showlocals --maxfail=100
# Publish/Release
UPLOAD=false # default to disable upload to artifact repo
if [[ $OPERATION == "publish" ]]|| [[ $OPERATION == "release" ]]; then
UPLOAD=true
REPO="pypi"
echo "Enabled upload to $REPO"
fi
if [[ $OPERATION == "test" ]] || { [[ -n ${TEST_PYPI_USER} ]] && [[ -n ${TEST_PYPI_PASS} ]]; }; then
UPLOAD=true
REPO="testpypi"
REPO_URL="https://test.pypi.org/legacy/"
echo "Enabled upload to $REPO url $REPO_URL"
fi
if [[ $UPLOAD == true ]]; then
# Log intention
if [[ $OPERATION == "release" ]]; then
echo "Releasing python artifact[$ARTIFACT_ID $PACKAGE_VERSION] to $REPO"
else
echo "Publishing python artifact[$ARTIFACT_ID $PACKAGE_VERSION] to $REPO"
fi
# Upload
if [[ $REPO == "pypi" ]]; then
if [[ -n ${PYPI_USER} ]] && [[ -n ${PYPI_PASS} ]]; then
echo "PYPI_USER and PYPI_PASS already set: Uploading to PyPI"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload -u "$PYPI_USER" -p "$PYPI_PASS" dist/*
else
echo "PYPI_USER and/or PYPI_PASS not set: Cancelled upload to PyPI"
fi
else
if [[ -n ${TEST_PYPI_USER} ]] && [[ -n ${TEST_PYPI_PASS} ]]; then
echo "TEST_PYPI_USER and TEST_PYPI_PASS already set: Uploading to Test PyPI"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload --repository-url "$REPO_URL" -u "$TEST_PYPI_USER" -p "$TEST_PYPI_PASS" dist/*
elif [[ $OPERATION == "test" ]]; then
echo "OPERATION=test -> Initiate manual upload"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload --repository "$REPO" dist/*
fi
fi
else
echo "REPO=${REPO:-N/A} ARTIFACT_ID=$ARTIFACT_ID PACKAGE_VERSION=$PACKAGE_VERSION"
fi
cd "$ORIGINAL_DIR" # return to original directory
}
if [[ $LIBRARY_ARG == "all" ]]; then
echo "Generating $LIBRARY_ARG and running operation $OPERATION_ARG"
generateJava "jersey2" "build"
generateJava "vertx" "build"
generateJava "okhttp-gson" "build"
buildJava "$OPERATION_ARG"
generateTypescript "typescript-node" "$OPERATION_ARG"
generateTypescript "typescript-fetch" "$OPERATION_ARG"
generatePython "python" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "java" ]]; then
echo "Generating $LIBRARY_ARG and running operation $OPERATION_ARG"
generateJava "jersey2"
generateJava "vertx"
generateJava "okhttp-gson"
buildJava "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "jersey2" ]]; then
generateJava "$LIBRARY_ARG" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "vertx" ]]; then
generateJava "$LIBRARY_ARG" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "okhttp-gson" ]]; then
generateJava "$LIBRARY_ARG" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "typescript" ]]; then
echo "Generating $LIBRARY_ARG and running operation $OPERATION_ARG"
generateTypescript "typescript-node" "$OPERATION_ARG"
generateTypescript "typescript-fetch" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "typescript-node" ]]; then
generateTypescript "$LIBRARY_ARG" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "typescript-fetch" ]]; then
generateTypescript "$LIBRARY_ARG" "$OPERATION_ARG"
fi
if [[ $LIBRARY_ARG == "python" ]]; then
generatePython "$LIBRARY_ARG" "$OPERATION_ARG"
fi