Skip to content

chore(tooling): generation script for all language and clients APIC-221 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ jobs:
uses: ./.github/actions/cache

- name: Generate search client
run: yarn generate:js:search
run: yarn generate javascript search

- name: Build search client
run: yarn client:build-js:search

- name: Generate recommend client
run: yarn generate:js:recommend
run: yarn generate javascript recommend

- name: Build recommend client
run: yarn client:build-js:recommend

- name: Generate personalization client
run: yarn generate:js:personalization
run: yarn generate javascript personalization

- name: Build personalization client
run: yarn client:build-js:personalization
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ nvm use && yarn
yarn generate
```

Generic command:
```bash
yarn generate <language | all> <client | all>
```

### Search client

```bash
yarn generate:search
yarn generate all search
```

### Recommend client

```bash
yarn generate:recommend
yarn generate all recommend
```

## Build generated clients
Expand Down
8 changes: 8 additions & 0 deletions doc/contribution_addNewClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ Example for the JavaScript client
openapi-generator author template -g typescript-node -o templates/algoliasearch-client-javascript/
```

## Add the language/client to the tool chain

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

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

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

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

You will need to implement:

- An `init` method
Expand Down
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,12 @@
"client:build": "yarn client:build-js",
"cts:generate": "yarn workspace tests cts:generate",
"cts:test": "yarn workspace tests test",
"lint:client:fix": "eslint --ext=ts ${CLIENT} --fix",
"lint": "eslint --ext=ts .",
"format:specs": "yarn prettier --write specs",
"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",
"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",
"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",
"generate:js": "yarn generate:js:search && yarn generate:js:recommend && yarn generate:js:personalization",
"generate:personalization": "yarn generate:js:personalization",
"generate:recommend": "yarn generate:js:recommend",
"generate:search": "yarn generate:js:search",
"generate": "yarn generate:js && yarn format:specs",
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && ./scripts/post-gen/global.sh",
"playground:js:personalization": "yarn workspace javascript-playground start:personalization",
"playground:js:recommend": "yarn workspace javascript-playground start:recommend",
"playground:js:search": "yarn workspace javascript-playground start:search",
"utils:import-js": "mkdir -p -- clients/algoliasearch-client-javascript/${CLIENT}/utils && cp -R clients/algoliasearch-client-javascript/utils/ clients/algoliasearch-client-javascript/${CLIENT}/utils",
"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"
},
"devDependencies": {
Expand All @@ -54,6 +45,7 @@
"eslint-plugin-jsdoc": "37.1.0",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"json": "11.0.0",
"prettier": "2.5.0",
"swagger-cli": "4.0.4",
"typescript": "4.5.2"
Expand Down
34 changes: 34 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Break on non-zero code
set -e

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

lang=$1
client=$2

# run the pre generation script if it exists (spec v)
pregen="./scripts/pre-gen/${lang}.sh"
if [[ -f "$pregen" ]]; then
echo "Pre-gen for ${lang}-${client}"
$pregen $client
fi

set +e
echo "Generating code for ${lang}-${client}"
log=$(yarn openapi-generator-cli generate --generator-key "${lang}-${client}")
if [[ $? != 0 ]]; then
echo "$log"
exit 1
fi
set -e

# run the post generation script if it exists (linting and additional files)
postgen="./scripts/post-gen/${lang}.sh"
if [[ -f "$postgen" ]]; then
echo "Post-gen for ${lang}-${client}"
$postgen $client
fi
41 changes: 41 additions & 0 deletions scripts/multiplexer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# Call this script with multiplexer.sh <lang | all> <client | all> <cmd>
# to run the cmd for all the required lang-client combination

# Break on non-zero code
set -e

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

CMD=$1
LANGUAGE=$2
CLIENT=$3

LANGUAGES=(javascript)
CLIENTS=(search recommend personalization)

if [[ $LANGUAGE == "all" ]]; then
LANGUAGE=("${LANGUAGES[@]}")
elif [[ " ${LANGUAGES[*]} " =~ " ${LANGUAGE} " ]]; then
LANGUAGE=($LANGUAGE)
else
echo "Unknown language ${LANGUAGE}"
exit 1
fi

if [[ $CLIENT == "all" ]]; then
CLIENT=("${CLIENTS[@]}")
elif [[ " ${CLIENTS[*]} " =~ " ${CLIENT} " ]]; then
CLIENT=($CLIENT)
else
echo "Unknown client ${CLIENT}"
exit 1
fi

for lang in "${LANGUAGE[@]}"; do
for client in "${CLIENT[@]}"; do
$CMD $lang $client
done
done
1 change: 1 addition & 0 deletions scripts/post-gen/global.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn format:specs
7 changes: 7 additions & 0 deletions scripts/post-gen/javascript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
client=$1

export CLIENT=$(cat openapitools.json | yarn json "generator-cli.generators.javascript-${client}.output" | sed 's/#{cwd}\///g')
mkdir -p $CLIENT/utils
cp -R clients/algoliasearch-client-javascript/utils/ $CLIENT/utils

eslint --ext=ts ${CLIENT} --fix
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ __metadata:
eslint-plugin-jsdoc: 37.1.0
eslint-plugin-prettier: 4.0.0
eslint-plugin-unused-imports: 2.0.0
json: 11.0.0
prettier: 2.5.0
swagger-cli: 4.0.4
typescript: 4.5.2
Expand Down Expand Up @@ -4264,6 +4265,15 @@ fsevents@^2.3.2:
languageName: node
linkType: hard

"json@npm:11.0.0":
version: 11.0.0
resolution: "json@npm:11.0.0"
bin:
json: lib/json.js
checksum: 0beb8689722fc30251638543caf791bedc1a25199c18eb0281de115a0dadb00a07d4d36cbd7ff7680e04157fcb2bba76f29ed940b1e4e92f60c6fbaa74523fce
languageName: node
linkType: hard

"jsonfile@npm:^6.0.1":
version: 6.1.0
resolution: "jsonfile@npm:6.1.0"
Expand Down