Skip to content

Commit d36e3ae

Browse files
authored
fix: improve script consistancy and format (#100)
1 parent 1e76daa commit d36e3ae

File tree

19 files changed

+174
-181
lines changed

19 files changed

+174
-181
lines changed

.github/actions/cache/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ runs:
123123
if: ${{ inputs.language == 'java' }}
124124
shell: bash
125125
run: mvn clean install -f clients/algoliasearch-client-java-2/pom.xml
126+
127+
- name: Download Java formatter
128+
if: ${{ inputs.language == 'java' || inputs.job == 'cts' }}
129+
shell: bash
130+
run: curl -L "https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar" > /tmp/java-formatter.jar

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: ./.github/actions/setup
2525

2626
- name: Lint GitHub actions
27-
run: yarn eslint --ext=yml .github/actions .github/workflows
27+
run: yarn github-actions:lint
2828

2929
outputs:
3030
RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }}

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ FROM node:$NODE_VERSION-alpine
44

55
ENV DOCKER=true
66

7-
RUN apk add openjdk11 maven jq bash perl curl
7+
RUN apk add openjdk11 maven jq bash perl
88
ENV JAVA_HOME=/usr/lib/jvm/default-jvm
99

10+
# Java formatter
11+
ADD https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar /tmp/java-formatter.jar
12+
1013
WORKDIR /app
1114

1215
CMD ["bash"]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ yarn docker build:specs
6666
yarn docker build:specs recommend
6767
```
6868

69+
#### Fix the specs format
70+
71+
This is used by the build script and should not need to be called manually but if you want to format all specs file do:
72+
```bash
73+
yarn docker specs:fix
74+
```
75+
76+
If you just want to check the format (not override the files), run:
77+
```bash
78+
yarn docker specs:lint <client>
79+
yarn docker specs:lint search
80+
```
81+
6982
### Generate clients based on the [`specs`](./specs/)
7083

7184
#### Usage

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
"post:generate": "./scripts/post-gen/global.sh",
2323
"generate": "./scripts/multiplexer.sh ${2:-nonverbose} ./scripts/generate.sh ${0:-all} ${1:-all} && yarn post:generate ${0:-all}",
2424
"playground": "./scripts/multiplexer.sh ${2:-nonverbose} ./scripts/playground.sh ${0:-javascript} ${1:-search}",
25-
"specs:format": "yarn prettier --write specs",
26-
"specs:lint": "eslint --ext=yml specs/ .github/ && yarn openapi lint"
25+
"specs:fix": "eslint --ext=yml specs/ --fix",
26+
"specs:lint": "eslint --ext=yml specs/$0",
27+
"github-actions:lint": "eslint --ext=yml .github/"
2728
},
2829
"devDependencies": {
2930
"@openapitools/openapi-generator-cli": "2.4.18",

scripts/builds/clients.sh

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,34 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
77
# Move to the root (easier to locate other scripts)
88
cd ${DIR}/../..
99

10-
lang=$1
11-
client=$2
12-
generator="$lang-$client"
13-
package=$(cat openapitools.json | jq -r --arg generator "$generator" '."generator-cli".generators[$generator].additionalProperties.packageName')
14-
15-
# Commands are based on the lang
16-
build_client(){
17-
echo "> Building $generator..."
18-
19-
if [[ $lang == 'javascript' ]]; then
20-
yarn workspace $package build
21-
elif [[ $lang == 'java' ]]; then
22-
CMD="mvn install -f clients/$package/pom.xml"
23-
if [[ $VERBOSE == "true" ]]; then
24-
$CMD
25-
else
26-
set +e
27-
log=$($CMD)
28-
29-
if [[ $? != 0 ]]; then
30-
echo "$log"
31-
exit 1
32-
fi
33-
set -e
34-
fi
35-
fi
36-
}
10+
LANGUAGE=$1
11+
CLIENT=$2
12+
GENERATOR="$LANGUAGE-$CLIENT"
13+
PACKAGE=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].additionalProperties.packageName')
3714

15+
if [[ -z $PACKAGE ]]; then
16+
echo "Unknown package ${PACKAGE}"
17+
exit 1
18+
fi
3819

20+
# Commands are based on the LANGUAGE
21+
echo "> Building $GENERATOR..."
3922

40-
if [[ -z $package ]]; then
41-
echo "Unknown package ${package}"
42-
exit 1
23+
if [[ $LANGUAGE == 'javascript' ]]; then
24+
CMD="yarn workspace $PACKAGE build"
25+
elif [[ $LANGUAGE == 'java' ]]; then
26+
CMD="mvn install -f clients/$PACKAGE/pom.xml"
4327
fi
4428

45-
build_client
29+
if [[ $VERBOSE == "true" ]]; then
30+
$CMD
31+
else
32+
set +e
33+
log=$($CMD)
34+
35+
if [[ $? != 0 ]]; then
36+
echo "$log"
37+
exit 1
38+
fi
39+
set -e
40+
fi

scripts/builds/specs.sh

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,40 @@ fi
99
# Break on non-zero code
1010
set -e
1111

12-
SPEC=$1
13-
OUTPUT=$2
14-
15-
SPECS=()
16-
CLIENT=""
17-
1812
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
1913
# Move to the root (easier to locate other scripts)
2014
cd ${DIR}/../..
2115

22-
find_specs() {
23-
echo "> Searching for available specs..."
24-
25-
SPECS=($(find specs/*/spec.yml))
26-
27-
echo ""
28-
}
16+
CLIENT=$1
17+
OUTPUT=$2
2918

3019
check_format_spec() {
31-
local spec=$1
32-
33-
echo "> Checking format of input spec file ${spec}"
34-
35-
yarn eslint --ext=yml ${spec}
36-
20+
local client=$1
21+
echo "> Checking format of $client spec"
22+
yarn specs:lint $client
3723
echo ""
3824
}
3925

4026
build_spec() {
41-
local spec=$1
42-
43-
yarn openapi bundle ${spec} -o specs/dist/${CLIENT}.${OUTPUT} --ext ${OUTPUT}
44-
27+
local client=$1
28+
yarn openapi bundle specs/${client}/spec.yml -o specs/dist/${client}.${OUTPUT} --ext ${OUTPUT}
4529
echo ""
4630
}
4731

48-
validate_spec() {
49-
local spec=$1
50-
51-
yarn openapi lint specs/dist/${CLIENT}.${OUTPUT}
52-
32+
validate_output_spec() {
33+
local client=$1
34+
yarn openapi lint specs/dist/${client}.${OUTPUT}
5335
echo ""
5436
}
5537

56-
find_specs
38+
CLIENTS=$(find specs/*/spec.yml | awk -F / '{ print $(NF-1) }')
5739

58-
if [[ $SPEC == "all" ]]; then
59-
SPECS=("${SPECS[@]}")
60-
elif [[ ${SPECS[*]} =~ ${SPEC} ]]; then
61-
SPECS=("specs/${SPEC}/spec.yml")
40+
if [[ $CLIENT == "all" ]]; then
41+
CLIENTS=("${CLIENTS[@]}")
42+
elif [[ ${CLIENTS[*]} =~ ${CLIENT} ]]; then
43+
CLIENTS=($CLIENT)
6244
else
63-
echo "Unknown spec ${SPEC}"
45+
echo "Unknown spec ${CLIENT}"
6446
exit 1
6547
fi
6648

@@ -69,10 +51,8 @@ if [[ $OUTPUT != "yml" ]] && [[ $OUTPUT != "json" ]]; then
6951
exit 1
7052
fi
7153

72-
for spec in "${SPECS[@]}"; do
73-
CLIENT=$(echo $spec | awk -F / '{ print $(NF-1) }')
74-
75-
check_format_spec $spec
76-
build_spec $spec
77-
validate_spec $spec
54+
for client in "${CLIENTS[@]}"; do
55+
check_format_spec $client
56+
build_spec $client
57+
validate_output_spec $client
7858
done

scripts/ci/create-client-matrix.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LANGUAGE=$1
44
BASE_CHANGED=$2
55
BASE_BRANCH=$3
66

7-
clients=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".generators
7+
CLIENTS=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".generators
88
| with_entries(
99
if (.key | test($lang + "-.*")) then
1010
({key:.key,value:.value})
@@ -20,7 +20,7 @@ clients=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".g
2020
| .[]')
2121

2222
to_test='{"client": []}'
23-
for pair in $clients; do
23+
for pair in $CLIENTS; do
2424
name=$(echo $pair | jq '.name')
2525
folder=$(echo $pair | jq '.folder')
2626
spec_changed=$(git diff --shortstat origin/$BASE_BRANCH..HEAD -- specs/$name | wc -l)

scripts/ci/create-spec-matrix.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
BASE_CHANGED=$1
44
BASE_BRANCH=$2
55

6-
specs='{"client":[]}'
7-
generators=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )
6+
GENERATORS=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )
7+
SPECS=()
88

9-
for generator in "${generators[@]}"; do
9+
to_check='{"client":[]}'
10+
for generator in "${GENERATORS[@]}"; do
1011
client=${generator#*-}
11-
if [[ ! ${specs[*]} =~ $client ]]; then
12+
if [[ ! ${SPECS[*]} =~ $client ]]; then
1213
changed=$(git diff --shortstat origin/$BASE_BRANCH..HEAD -- specs/$client | wc -l)
1314
if [[ $BASE_CHANGED == "true" || $changed > 0 ]]; then
14-
specs=$(echo $specs | jq --arg client $client '.client |= .+ [$client]')
15+
to_check=$(echo $to_check | jq --arg client $client '.client |= .+ [$client]')
1516
fi
1617
fi
1718
done
1819

1920
# Convert the array to json for the matrix
20-
if [[ $(echo $specs | jq '.client | length') == 0 ]]; then
21+
if [[ $(echo $to_check | jq '.client | length') == 0 ]]; then
2122
# client cannot be empty or the matrix will fail
2223
matrix='{"client":["no-run"]}'
2324
else
24-
matrix=$(echo $specs | jq -c)
25+
matrix=$(echo $to_check | jq -c)
2526
fi
2627

2728
echo $matrix

scripts/formatter.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# Break on non-zero code
4+
set -e
5+
6+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
7+
# Move to the root (easier to locate other scripts)
8+
cd ${DIR}/..
9+
10+
LANGUAGE=$1
11+
FOLDER=$2
12+
13+
if [[ $LANGUAGE == 'javascript' ]]; then
14+
# jsdoc/require-hyphen-before-param-description fails to lint more than
15+
# 6 parameters, we re-run the script if failed to lint the rest
16+
CMD="yarn eslint --ext=ts ${FOLDER} --fix || yarn eslint --ext=ts ${FOLDER} --fix"
17+
elif [[ $LANGUAGE == 'java' ]]; then
18+
CMD="find $FOLDER -type f -name \"*.java\" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
19+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
20+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
21+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
22+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
23+
-jar /tmp/java-formatter.jar -r \
24+
&& yarn prettier --write $FOLDER"
25+
else
26+
echo "Cannot format unknow language $LANGUAGE"
27+
exit 1
28+
fi
29+
30+
echo "> Formatting ${LANGUAGE} in ${FOLDER}..."
31+
32+
if [[ $VERBOSE == "true" ]]; then
33+
# CAREFUL WITH EVAL (not safe)
34+
eval $CMD
35+
else
36+
set +e
37+
log=$(eval $CMD)
38+
39+
if [[ $? != 0 ]]; then
40+
echo "$log"
41+
exit 1
42+
fi
43+
set -e
44+
fi

scripts/generate.sh

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
77
# Move to the root (easier to locate other scripts)
88
cd ${DIR}/..
99

10-
lang=$1
11-
client=$2
12-
generator="$1-$2"
10+
LANGUAGE=$1
11+
CLIENT=$2
12+
GENERATOR="$1-$2"
1313

1414
# build spec before generating client
1515
build_spec() {
16-
yarn build:specs $client
16+
yarn build:specs $CLIENT
1717
}
1818

1919
# Run the pre generation script if it exists.
2020
run_pre_gen() {
21-
pregen="./scripts/pre-gen/${lang}.sh"
21+
pregen="./scripts/pre-gen/${LANGUAGE}.sh"
2222

2323
if [[ -f "$pregen" ]]; then
24-
echo "> Running pre-gen script for $generator..."
25-
$pregen $client
24+
echo "> Running pre-gen script for $GENERATOR..."
25+
$pregen $CLIENT
2626
fi
2727
}
2828

2929
generate_client() {
30-
echo "> Generating code for $generator..."
31-
CMD="yarn openapi-generator-cli generate --generator-key $generator"
30+
echo "> Generating code for $GENERATOR..."
31+
CMD="yarn openapi-generator-cli generate --generator-key $GENERATOR"
3232
if [[ $VERBOSE == "true" ]]; then
3333
$CMD
3434
else
@@ -45,12 +45,16 @@ generate_client() {
4545

4646
# Run the post generation script if it exists.
4747
run_post_gen() {
48-
postgen="./scripts/post-gen/${lang}.sh"
48+
postgen="./scripts/post-gen/${LANGUAGE}.sh"
49+
50+
folder=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].output' | sed 's/#{cwd}\///g')
4951

5052
if [[ -f "$postgen" ]]; then
51-
echo "> Running post-gen script for $generator..."
52-
$postgen "$generator"
53+
echo "> Running post-gen script for $GENERATOR..."
54+
$postgen $folder $GENERATOR
5355
fi
56+
57+
./scripts/formatter.sh $LANGUAGE $folder
5458
}
5559

5660
if [[ ! $CI ]]; then

scripts/playground.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
77
# Move to the root (easier to locate other scripts)
88
cd ${DIR}/..
99

10-
lang=$1
11-
client=$2
10+
LANGUAGE=$1
11+
CLIENT=$2
1212

1313
# Run the pre generation script if it exists.
1414
run_playground() {
15-
if [[ $lang == 'javascript' ]]; then
16-
yarn workspace javascript-playground start:$client
17-
elif [[ $lang == 'java' ]]; then
15+
if [[ $LANGUAGE == 'javascript' ]]; then
16+
yarn workspace javascript-playground start:$CLIENT
17+
elif [[ $LANGUAGE == 'java' ]]; then
1818
mvn clean compile exec:java -f playground/java/pom.xml
1919
fi
2020
}

0 commit comments

Comments
 (0)