Skip to content

Commit 318e160

Browse files
authored
chore(tooling): specs script (#33)
1 parent b84ecee commit 318e160

File tree

9 files changed

+167
-44
lines changed

9 files changed

+167
-44
lines changed

.github/workflows/check.yml

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

23-
- name: Build
24-
run: yarn build:spec
23+
- name: Checking search specs
24+
run: yarn specs search
2525

26-
- name: Validate
27-
run: yarn validate
26+
- name: Checking recommend specs
27+
run: yarn specs recommend
28+
29+
- name: Checking personalization specs
30+
run: yarn specs personalization
2831

2932
- name: Lint
3033
run: yamllint specs

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ yarn generate
1515
```
1616

1717
Generic command:
18+
1819
```bash
1920
yarn generate <language | all> <client | all>
2021
```

doc/contribution_addNewClient.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ openapi-generator author template -g typescript-node -o templates/algoliasearch-
2323
## Add the language/client to the tool chain
2424

2525
Add each client in the file `openapitools.json`, following the others client structure.
26-
Then add it to the `scripts/multiplexer.sh` file to register it for the other commands.
2726

2827
Don't forget to write tests for it using the [CTS](./CTS.md)
28+
2929
## Customize the template
3030

3131
API clients require a custom Algolia logic in order to seamlessly work with our engine.
3232

33-
The first thing to do is strip as much code as possible, because the generation include lots of useless feature.
33+
**The first thing to do is strip as much code as possible, because the generation include lots of useless feature.**
3434

3535
You will need to implement:
3636

package.json

Lines changed: 4 additions & 11 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",
@@ -24,12 +16,13 @@
2416
"cts:generate": "yarn workspace tests cts:generate",
2517
"cts:test": "yarn workspace tests test",
2618
"lint": "eslint --ext=ts .",
27-
"format:specs": "yarn prettier --write specs",
28-
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && ./scripts/post-gen/global.sh",
19+
"post:generate": "./scripts/post-gen/global.sh",
20+
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && yarn post:generate",
2921
"playground:js:personalization": "yarn workspace javascript-playground start:personalization",
3022
"playground:js:recommend": "yarn workspace javascript-playground start:recommend",
3123
"playground:js:search": "yarn workspace javascript-playground start:search",
32-
"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"
24+
"specs:format": "yarn prettier --write specs",
25+
"specs": "./scripts/specs.sh ${0:-all} ${1:-yaml}"
3326
},
3427
"devDependencies": {
3528
"@openapitools/openapi-generator-cli": "2.4.18",

scripts/generate.sh

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,41 @@ cd ${DIR}/..
1010
lang=$1
1111
client=$2
1212

13-
# run the pre generation script if it exists (spec v)
14-
pregen="./scripts/pre-gen/${lang}.sh"
15-
if [[ -f "$pregen" ]]; then
16-
echo "Pre-gen for ${lang}-${client}"
17-
$pregen $client
18-
fi
19-
20-
set +e
21-
echo "Generating code for ${lang}-${client}"
22-
log=$(yarn openapi-generator-cli generate --generator-key "${lang}-${client}")
23-
if [[ $? != 0 ]]; then
24-
echo "$log"
25-
exit 1
26-
fi
27-
set -e
13+
# Run the pre generation script if it exists.
14+
run_pre_gen() {
15+
pregen="./scripts/pre-gen/${lang}.sh"
16+
17+
if [[ -f "$pregen" ]]; then
18+
echo "> Running pre-gen script for ${lang}-${client}..."
19+
$pregen $client
20+
fi
21+
}
22+
23+
generate_client() {
24+
set +e
25+
26+
echo "> Generating code for ${lang}-${client}..."
27+
28+
log=$(yarn openapi-generator-cli generate --generator-key "${lang}-${client}")
29+
30+
if [[ $? != 0 ]]; then
31+
echo "$log"
32+
exit 1
33+
fi
34+
35+
set -e
36+
}
37+
38+
# Run the post generation script if it exists.
39+
run_post_gen() {
40+
postgen="./scripts/post-gen/${lang}.sh"
41+
42+
if [[ -f "$postgen" ]]; then
43+
echo "> Running post-gen script for ${lang}-${client}..."
44+
$postgen "${lang}-${client}"
45+
fi
46+
}
2847

29-
# run the post generation script if it exists (linting and additional files)
30-
postgen="./scripts/post-gen/${lang}.sh"
31-
if [[ -f "$postgen" ]]; then
32-
echo "Post-gen for ${lang}-${client}"
33-
$postgen $client
34-
fi
48+
run_pre_gen
49+
generate_client
50+
run_post_gen

scripts/multiplexer.sh

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Call this script with multiplexer.sh <lang | all> <client | all> <cmd>
2+
# Call this script with multiplexer.sh <cmd> <lang | all> <client | all>
33
# to run the cmd for all the required lang-client combination
44

55
# Break on non-zero code
@@ -13,8 +13,29 @@ CMD=$1
1313
LANGUAGE=$2
1414
CLIENT=$3
1515

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

1940
if [[ $LANGUAGE == "all" ]]; then
2041
LANGUAGE=("${LANGUAGES[@]}")

scripts/post-gen/global.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
yarn format:specs
1+
#!/bin/bash
2+
3+
if [[ $CI ]]; then
4+
exit 0
5+
fi
6+
7+
format_specs() {
8+
set +e
9+
10+
echo "> Formatting specs..."
11+
12+
log=$(yarn specs:format)
13+
14+
if [[ $? != 0 ]]; then
15+
echo "$log"
16+
exit 1
17+
fi
18+
19+
set -e
20+
}
21+
22+
format_specs

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+
#!/bin/bash
22

3-
export CLIENT=$(cat openapitools.json | yarn json "generator-cli.generators.javascript-${client}.output" | sed 's/#{cwd}\///g')
3+
export GENERATOR=$1
4+
export CLIENT=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].output' | sed 's/#{cwd}\///g')
5+
6+
echo "> Exporting utils for ${GENERATOR}..."
47
mkdir -p $CLIENT/utils
8+
59
cp -R clients/algoliasearch-client-javascript/utils/ $CLIENT/utils
610

11+
echo "> Linting ${GENERATOR}..."
712
eslint --ext=ts ${CLIENT} --fix

scripts/specs.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
find_specs
45+
46+
if [[ $SPEC == "all" ]]; then
47+
SPECS=("${SPECS[@]}")
48+
elif [[ ${SPECS[*]} =~ ${SPEC} ]]; then
49+
SPECS=("specs/${SPEC}/spec.yml")
50+
else
51+
echo "Unknown spec ${SPEC}"
52+
exit 1
53+
fi
54+
55+
if [[ $OUTPUT != "yaml" ]] && [[ $OUTPUT != "json" ]]; then
56+
echo "Unknown output ${OUTPUT}"
57+
exit 1
58+
fi
59+
60+
for spec in "${SPECS[@]}"; do
61+
build_spec $spec
62+
validate_spec $spec
63+
done

0 commit comments

Comments
 (0)