Skip to content

Commit e7c213a

Browse files
committed
chore(tooling): specs script
1 parent 959ac8b commit e7c213a

File tree

6 files changed

+134
-58
lines changed

6 files changed

+134
-58
lines changed

.github/workflows/check.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ jobs:
2020
- name: Restore cache
2121
uses: ./.github/actions/cache
2222

23-
- name: Build
24-
run: yarn build:spec
25-
26-
- name: Validate
27-
run: yarn validate
23+
- name: Building and validating
24+
run: yarn specs
2825

2926
- name: Lint
3027
run: yamllint specs
@@ -39,7 +36,7 @@ jobs:
3936
uses: ./.github/actions/cache
4037

4138
- name: Generate search client
42-
run: yarn generate javascript search
39+
run: yarn generate javascript client-search
4340

4441
- name: Build search client
4542
run: yarn client:build-js:search
@@ -51,7 +48,7 @@ jobs:
5148
run: yarn client:build-js:recommend
5249

5350
- name: Generate personalization client
54-
run: yarn generate javascript personalization
51+
run: yarn generate javascript client-personalization
5552

5653
- name: Build personalization client
5754
run: yarn client:build-js:personalization

package.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
"tests/"
88
],
99
"scripts": {
10-
"build:spec:personalization:json": "yarn swagger-cli bundle specs/personalization/spec.yml --outfile specs/dist/personalization.json --type json",
11-
"build:spec:recommend:json": "yarn swagger-cli bundle specs/recommend/spec.yml --outfile specs/dist/recommend.json --type json",
12-
"build:spec:search:json": "yarn swagger-cli bundle specs/search/spec.yml --outfile specs/dist/search.json --type json",
13-
"build:spec:personalization": "yarn swagger-cli bundle specs/personalization/spec.yml --outfile specs/dist/personalization.yml --type yaml",
14-
"build:spec:recommend": "yarn swagger-cli bundle specs/recommend/spec.yml --outfile specs/dist/recommend.yml --type yaml",
15-
"build:spec:search": "yarn swagger-cli bundle specs/search/spec.yml --outfile specs/dist/search.yml --type yaml",
16-
"build:spec:json": "yarn build:spec:search:json && yarn build:spec:recommend:json",
17-
"build:spec": "yarn build:spec:search && yarn build:spec:recommend && yarn build:spec:personalization",
1810
"clean": "rm -rf **/dist **/build **/node_modules",
1911
"client:build-js:personalization": "yarn workspace @algolia/client-personalization build",
2012
"client:build-js:recommend": "yarn workspace @algolia/recommend build",
@@ -30,7 +22,7 @@
3022
"playground:js:personalization": "yarn workspace javascript-playground start:personalization",
3123
"playground:js:recommend": "yarn workspace javascript-playground start:recommend",
3224
"playground:js:search": "yarn workspace javascript-playground start:search",
33-
"validate": "yarn swagger-cli validate specs/dist/search.yml && yarn swagger-cli validate specs/dist/recommend.yml && yarn swagger-cli validate specs/dist/personalization.yml"
25+
"specs": "./scripts/specs_build.sh ${0:-all} ${1:-yaml}"
3426
},
3527
"devDependencies": {
3628
"@openapitools/openapi-generator-cli": "2.4.18",
@@ -46,7 +38,6 @@
4638
"eslint-plugin-jsdoc": "37.1.0",
4739
"eslint-plugin-prettier": "4.0.0",
4840
"eslint-plugin-unused-imports": "2.0.0",
49-
"json": "11.0.0",
5041
"prettier": "2.5.0",
5142
"swagger-cli": "4.0.4",
5243
"typescript": "4.5.2"

scripts/generate.sh

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,72 @@ set -e
66
LANGUAGE=$1
77
CLIENT=$2
88

9-
LANGUAGES=(javascript)
10-
CLIENTS=(search recommend personalization)
9+
LANGUAGES=()
10+
CLIENTS=()
1111

1212
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
1313
# Move to the root (easier to locate other scripts)
1414
cd ${DIR}/..
1515

16+
find_clients_and_languages() {
17+
echo "> Searching for available languages and clients..."
18+
local generators=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )
19+
20+
for generator in "${generators[@]}"; do
21+
local lang=${generator%-*}
22+
local client=${generator#*-}
23+
24+
if [[ ! ${LANGUAGES[*]} =~ $lang ]]; then
25+
LANGUAGES+=($lang)
26+
fi
27+
28+
if [[ ! ${CLIENTS[*]} =~ $client ]]; then
29+
CLIENTS+=($client)
30+
fi
31+
done
32+
}
33+
1634
generate_client() {
17-
local lang=$1
18-
local client=$2
19-
echo "Generating code for ${lang}-${client}"
20-
yarn openapi-generator-cli generate --generator-key "${lang}-${client}"
35+
local client="${1}-${2}"
36+
37+
echo "> Generating code for ${client}..."
38+
39+
yarn openapi-generator-cli generate --generator-key $client
2140

2241
# run the post generation script if it exists (linting and additional files)
23-
local postgen="./scripts/post-gen/${lang}.sh"
42+
local postgen="./scripts/post-gen/${1}.sh"
43+
2444
if [[ -f "$postgen" ]]; then
2545
$postgen $client
2646
fi
2747
}
2848

29-
if [[ $LANGUAGE == "all" ]]; then
30-
LANGUAGE=("${LANGUAGES[@]}")
31-
elif [[ " ${LANGUAGES[*]} " =~ " ${LANGUAGE} " ]]; then
32-
LANGUAGE=($LANGUAGE)
33-
else
34-
echo "Unknown language ${LANGUAGE}"
35-
exit 1
36-
fi
37-
38-
if [[ $CLIENT == "all" ]]; then
39-
CLIENT=("${CLIENTS[@]}")
40-
elif [[ " ${CLIENTS[*]} " =~ " ${CLIENT} " ]]; then
41-
CLIENT=($CLIENT)
42-
else
43-
echo "Unknown client ${CLIENT}"
44-
exit 1
45-
fi
46-
47-
for lang in "${LANGUAGE[@]}"; do
48-
for client in "${CLIENT[@]}"; do
49-
generate_client $lang $client
49+
main() {
50+
find_clients_and_languages
51+
52+
if [[ $LANGUAGE == "all" ]]; then
53+
LANGUAGE=("${LANGUAGES[@]}")
54+
elif [[ ${LANGUAGES[*]} =~ ${LANGUAGE} ]]; then
55+
LANGUAGE=($LANGUAGE)
56+
else
57+
echo "Unknown language ${LANGUAGE}"
58+
exit 1
59+
fi
60+
61+
if [[ $CLIENT == "all" ]]; then
62+
CLIENT=("${CLIENTS[@]}")
63+
elif [[ ${CLIENTS[*]} =~ ${CLIENT} ]]; then
64+
CLIENT=($CLIENT)
65+
else
66+
echo "Unknown client ${CLIENT}"
67+
exit 1
68+
fi
69+
70+
for lang in "${LANGUAGE[@]}"; do
71+
for client in "${CLIENT[@]}"; do
72+
generate_client $lang $client
73+
done
5074
done
51-
done
75+
}
76+
77+
main

scripts/post-gen/javascript.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
client=$1
1+
export GENERATOR=$1
2+
export CLIENT=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].output' | sed 's/#{cwd}\///g')
23

3-
export CLIENT=$(cat openapitools.json | yarn json "generator-cli.generators.javascript-${client}.output" | sed 's/#{cwd}\///g')
4+
echo "> Exporting utils for ${GENERATOR}..."
45
mkdir -p $CLIENT/utils
6+
echo "DONE"
7+
58
cp -R clients/algoliasearch-client-javascript/utils/ $CLIENT/utils
69

10+
echo "> Linting ${GENERATOR}..."
711
yarn lint:client:fix
12+
echo "DONE"

scripts/specs_build.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
# Break on non-zero code
4+
set -e
5+
6+
SPEC=$1
7+
OUTPUT=$2
8+
9+
SPECS=()
10+
11+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
12+
# Move to the root (easier to locate other scripts)
13+
cd ${DIR}/..
14+
15+
find_specs() {
16+
echo "> Searching for available specs..."
17+
local specs=( $(cat openapitools.json | jq -r '."generator-cli".generators[] | .glob') )
18+
19+
for spec in "${specs[@]}"; do
20+
if [[ ! ${SPECS[*]} =~ $spec ]]; then
21+
SPECS+=($spec)
22+
fi
23+
done
24+
}
25+
26+
build_spec() {
27+
local spec=$1
28+
local client=$(echo $spec | awk -F / '{ print $(NF-1) }')
29+
30+
echo "> Building specs: ${client} ${OUTPUT}"
31+
32+
yarn swagger-cli bundle ${spec} --outfile specs/dist/${client}.${OUTPUT} --type ${OUTPUT}
33+
}
34+
35+
validate_spec() {
36+
local spec=$1
37+
local client=$(echo $spec | awk -F / '{ print $(NF-1) }')
38+
39+
echo "> Validating specs: ${client}"
40+
41+
yarn swagger-cli validate specs/dist/${client}.${OUTPUT}
42+
}
43+
44+
main() {
45+
find_specs
46+
47+
if [[ $SPEC == "all" ]]; then
48+
SPECS=("${SPECS[@]}")
49+
elif [[ ${SPECS[*]} =~ ${SPEC} ]]; then
50+
SPECS=("specs/${SPEC}/spec.yml")
51+
else
52+
echo "Unknown spec ${SPEC}"
53+
exit 1
54+
fi
55+
56+
if [[ $OUTPUT != "yaml" ]] && [[ $OUTPUT != "json" ]]; then
57+
echo "Unknown output ${OUTPUT}"
58+
exit 1
59+
fi
60+
61+
for spec in "${SPECS[@]}"; do
62+
build_spec $spec
63+
validate_spec $spec
64+
done
65+
}
66+
67+
main

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ __metadata:
2222
eslint-plugin-jsdoc: 37.1.0
2323
eslint-plugin-prettier: 4.0.0
2424
eslint-plugin-unused-imports: 2.0.0
25-
json: 11.0.0
2625
prettier: 2.5.0
2726
swagger-cli: 4.0.4
2827
typescript: 4.5.2
@@ -4265,15 +4264,6 @@ fsevents@^2.3.2:
42654264
languageName: node
42664265
linkType: hard
42674266

4268-
"json@npm:11.0.0":
4269-
version: 11.0.0
4270-
resolution: "json@npm:11.0.0"
4271-
bin:
4272-
json: lib/json.js
4273-
checksum: 0beb8689722fc30251638543caf791bedc1a25199c18eb0281de115a0dadb00a07d4d36cbd7ff7680e04157fcb2bba76f29ed940b1e4e92f60c6fbaa74523fce
4274-
languageName: node
4275-
linkType: hard
4276-
42774267
"jsonfile@npm:^6.0.1":
42784268
version: 6.1.0
42794269
resolution: "jsonfile@npm:6.1.0"

0 commit comments

Comments
 (0)