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 3 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
13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@
"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",
"lint:specs:fix": "yarn prettier --write specs",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier is a formatter hence the format instead of lint but I don't have a strong opinion

"generate": "./scripts/generate.sh ${0:-all} ${1:-all}",
"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 +46,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
51 changes: 51 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# Break on non-zero code
set -e

LANGUAGE=$1
CLIENT=$2

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

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

generate_client() {
local lang=$1
local client=$2
echo "Generating code for ${lang}-${client}"
yarn openapi-generator-cli generate --generator-key "${lang}-${client}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe silence the output of the command? I wonder if it's possible to do it only if it succeed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to silence the openapi-generator-cli part ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly wondering if we can silent unless it fails 🤔


# run the post generation script if it exists (linting and additional files)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to mention this part in our contribution guide

local postgen="./scripts/post-gen/${lang}.sh"
if [[ -f "$postgen" ]]; then
$postgen $client
fi
}

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
generate_client $lang $client
done
done
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

yarn lint: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