Skip to content

Commit e5e4fd8

Browse files
authored
chore(tooling): generation script for all language and clients APIC-221 (#32)
1 parent 7cc2671 commit e5e4fd8

File tree

9 files changed

+113
-15
lines changed

9 files changed

+113
-15
lines changed

.github/workflows/check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ jobs:
3939
uses: ./.github/actions/cache
4040

4141
- name: Generate search client
42-
run: yarn generate:js:search
42+
run: yarn generate javascript search
4343

4444
- name: Build search client
4545
run: yarn client:build-js:search
4646

4747
- name: Generate recommend client
48-
run: yarn generate:js:recommend
48+
run: yarn generate javascript recommend
4949

5050
- name: Build recommend client
5151
run: yarn client:build-js:recommend
5252

5353
- name: Generate personalization client
54-
run: yarn generate:js:personalization
54+
run: yarn generate javascript personalization
5555

5656
- name: Build personalization client
5757
run: yarn client:build-js:personalization

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ nvm use && yarn
1414
yarn generate
1515
```
1616

17+
Generic command:
18+
```bash
19+
yarn generate <language | all> <client | all>
20+
```
21+
1722
### Search client
1823

1924
```bash
20-
yarn generate:search
25+
yarn generate all search
2126
```
2227

2328
### Recommend client
2429

2530
```bash
26-
yarn generate:recommend
31+
yarn generate all recommend
2732
```
2833

2934
## Build generated clients

doc/contribution_addNewClient.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ Example for the JavaScript client
2020
openapi-generator author template -g typescript-node -o templates/algoliasearch-client-javascript/
2121
```
2222

23+
## Add the language/client to the tool chain
24+
25+
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.
27+
28+
Don't forget to write tests for it using the [CTS](./CTS.md)
2329
## Customize the template
2430

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

33+
The first thing to do is strip as much code as possible, because the generation include lots of useless feature.
34+
2735
You will need to implement:
2836

2937
- An `init` method

package.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,12 @@
2323
"client:build": "yarn client:build-js",
2424
"cts:generate": "yarn workspace tests cts:generate",
2525
"cts:test": "yarn workspace tests test",
26-
"lint:client:fix": "eslint --ext=ts ${CLIENT} --fix",
2726
"lint": "eslint --ext=ts .",
2827
"format:specs": "yarn prettier --write specs",
29-
"generate:js:personalization": "yarn openapi-generator-cli generate --generator-key javascript-personalization && CLIENT=client-personalization yarn utils:import-js && CLIENT=clients/algoliasearch-client-javascript/client-personalization/ yarn lint:client:fix",
30-
"generate:js:recommend": "yarn openapi-generator-cli generate --generator-key javascript-recommend && CLIENT=recommend yarn utils:import-js && CLIENT=clients/algoliasearch-client-javascript/recommend/ yarn lint:client:fix",
31-
"generate:js:search": "yarn openapi-generator-cli generate --generator-key javascript-search && CLIENT=client-search yarn utils:import-js && CLIENT=clients/algoliasearch-client-javascript/client-search/ yarn lint:client:fix",
32-
"generate:js": "yarn generate:js:search && yarn generate:js:recommend && yarn generate:js:personalization",
33-
"generate:personalization": "yarn generate:js:personalization",
34-
"generate:recommend": "yarn generate:js:recommend",
35-
"generate:search": "yarn generate:js:search",
36-
"generate": "yarn generate:js && yarn format:specs",
28+
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && ./scripts/post-gen/global.sh",
3729
"playground:js:personalization": "yarn workspace javascript-playground start:personalization",
3830
"playground:js:recommend": "yarn workspace javascript-playground start:recommend",
3931
"playground:js:search": "yarn workspace javascript-playground start:search",
40-
"utils:import-js": "mkdir -p -- clients/algoliasearch-client-javascript/${CLIENT}/utils && cp -R clients/algoliasearch-client-javascript/utils/ clients/algoliasearch-client-javascript/${CLIENT}/utils",
4132
"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"
4233
},
4334
"devDependencies": {
@@ -54,6 +45,7 @@
5445
"eslint-plugin-jsdoc": "37.1.0",
5546
"eslint-plugin-prettier": "4.0.0",
5647
"eslint-plugin-unused-imports": "2.0.0",
48+
"json": "11.0.0",
5749
"prettier": "2.5.0",
5850
"swagger-cli": "4.0.4",
5951
"typescript": "4.5.2"

scripts/generate.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
lang=$1
11+
client=$2
12+
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
28+
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

scripts/multiplexer.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
# Call this script with multiplexer.sh <lang | all> <client | all> <cmd>
3+
# to run the cmd for all the required lang-client combination
4+
5+
# Break on non-zero code
6+
set -e
7+
8+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
9+
# Move to the root (easier to locate other scripts)
10+
cd ${DIR}/..
11+
12+
CMD=$1
13+
LANGUAGE=$2
14+
CLIENT=$3
15+
16+
LANGUAGES=(javascript)
17+
CLIENTS=(search recommend personalization)
18+
19+
if [[ $LANGUAGE == "all" ]]; then
20+
LANGUAGE=("${LANGUAGES[@]}")
21+
elif [[ " ${LANGUAGES[*]} " =~ " ${LANGUAGE} " ]]; then
22+
LANGUAGE=($LANGUAGE)
23+
else
24+
echo "Unknown language ${LANGUAGE}"
25+
exit 1
26+
fi
27+
28+
if [[ $CLIENT == "all" ]]; then
29+
CLIENT=("${CLIENTS[@]}")
30+
elif [[ " ${CLIENTS[*]} " =~ " ${CLIENT} " ]]; then
31+
CLIENT=($CLIENT)
32+
else
33+
echo "Unknown client ${CLIENT}"
34+
exit 1
35+
fi
36+
37+
for lang in "${LANGUAGE[@]}"; do
38+
for client in "${CLIENT[@]}"; do
39+
$CMD $lang $client
40+
done
41+
done

scripts/post-gen/global.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarn format:specs

scripts/post-gen/javascript.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
client=$1
2+
3+
export CLIENT=$(cat openapitools.json | yarn json "generator-cli.generators.javascript-${client}.output" | sed 's/#{cwd}\///g')
4+
mkdir -p $CLIENT/utils
5+
cp -R clients/algoliasearch-client-javascript/utils/ $CLIENT/utils
6+
7+
eslint --ext=ts ${CLIENT} --fix

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ __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
2526
prettier: 2.5.0
2627
swagger-cli: 4.0.4
2728
typescript: 4.5.2
@@ -4264,6 +4265,15 @@ fsevents@^2.3.2:
42644265
languageName: node
42654266
linkType: hard
42664267

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+
42674277
"jsonfile@npm:^6.0.1":
42684278
version: 6.1.0
42694279
resolution: "jsonfile@npm:6.1.0"

0 commit comments

Comments
 (0)