Skip to content

Commit

Permalink
chore(tooling): specs script (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Dec 13, 2021
1 parent e5e4fd8 commit dd301eb
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 44 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ jobs:
- name: Restore cache
uses: ./.github/actions/cache

- name: Build
run: yarn build:spec
- name: Checking search specs
run: yarn specs search

- name: Validate
run: yarn validate
- name: Checking recommend specs
run: yarn specs recommend

- name: Checking personalization specs
run: yarn specs personalization

- name: Lint
run: yamllint specs
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ yarn generate
```

Generic command:

```bash
yarn generate <language | all> <client | all>
```
Expand Down
4 changes: 2 additions & 2 deletions doc/contribution_addNewClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ openapi-generator author template -g typescript-node -o templates/algoliasearch-
## 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.
**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:

Expand Down
15 changes: 4 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
"tests/"
],
"scripts": {
"build:spec:personalization:json": "yarn swagger-cli bundle specs/personalization/spec.yml --outfile specs/dist/personalization.json --type json",
"build:spec:recommend:json": "yarn swagger-cli bundle specs/recommend/spec.yml --outfile specs/dist/recommend.json --type json",
"build:spec:search:json": "yarn swagger-cli bundle specs/search/spec.yml --outfile specs/dist/search.json --type json",
"build:spec:personalization": "yarn swagger-cli bundle specs/personalization/spec.yml --outfile specs/dist/personalization.yml --type yaml",
"build:spec:recommend": "yarn swagger-cli bundle specs/recommend/spec.yml --outfile specs/dist/recommend.yml --type yaml",
"build:spec:search": "yarn swagger-cli bundle specs/search/spec.yml --outfile specs/dist/search.yml --type yaml",
"build:spec:json": "yarn build:spec:search:json && yarn build:spec:recommend:json",
"build:spec": "yarn build:spec:search && yarn build:spec:recommend && yarn build:spec:personalization",
"clean": "rm -rf **/dist **/build **/node_modules",
"client:build-js:personalization": "yarn workspace @algolia/client-personalization build",
"client:build-js:recommend": "yarn workspace @algolia/recommend build",
Expand All @@ -24,12 +16,13 @@
"cts:generate": "yarn workspace tests cts:generate",
"cts:test": "yarn workspace tests test",
"lint": "eslint --ext=ts .",
"format:specs": "yarn prettier --write specs",
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && ./scripts/post-gen/global.sh",
"post:generate": "./scripts/post-gen/global.sh",
"generate": "./scripts/multiplexer.sh ./scripts/generate.sh ${0:-all} ${1:-all} && yarn post:generate",
"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",
"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"
"specs:format": "yarn prettier --write specs",
"specs": "./scripts/specs.sh ${0:-all} ${1:-yaml}"
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "2.4.18",
Expand Down
58 changes: 37 additions & 21 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ 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 pre generation script if it exists.
run_pre_gen() {
pregen="./scripts/pre-gen/${lang}.sh"

if [[ -f "$pregen" ]]; then
echo "> Running pre-gen script for ${lang}-${client}..."
$pregen $client
fi
}

generate_client() {
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.
run_post_gen() {
postgen="./scripts/post-gen/${lang}.sh"

if [[ -f "$postgen" ]]; then
echo "> Running post-gen script for ${lang}-${client}..."
$postgen "${lang}-${client}"
fi
}

# 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
run_pre_gen
generate_client
run_post_gen
27 changes: 24 additions & 3 deletions scripts/multiplexer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Call this script with multiplexer.sh <lang | all> <client | all> <cmd>
# Call this script with multiplexer.sh <cmd> <lang | all> <client | all>
# to run the cmd for all the required lang-client combination

# Break on non-zero code
Expand All @@ -13,8 +13,29 @@ CMD=$1
LANGUAGE=$2
CLIENT=$3

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

find_clients_and_languages() {
echo "> Searching for available languages and clients..."

local generators=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )

for generator in "${generators[@]}"; do
local lang=${generator%-*}
local client=${generator#*-}

if [[ ! ${LANGUAGES[*]} =~ $lang ]]; then
LANGUAGES+=($lang)
fi

if [[ ! ${CLIENTS[*]} =~ $client ]]; then
CLIENTS+=($client)
fi
done
}

find_clients_and_languages

if [[ $LANGUAGE == "all" ]]; then
LANGUAGE=("${LANGUAGES[@]}")
Expand Down
23 changes: 22 additions & 1 deletion scripts/post-gen/global.sh
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
yarn format:specs
#!/bin/bash

if [[ $CI ]]; then
exit 0
fi

format_specs() {
set +e

echo "> Formatting specs..."

log=$(yarn specs:format)

if [[ $? != 0 ]]; then
echo "$log"
exit 1
fi

set -e
}

format_specs
9 changes: 7 additions & 2 deletions scripts/post-gen/javascript.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
client=$1
#!/bin/bash

export CLIENT=$(cat openapitools.json | yarn json "generator-cli.generators.javascript-${client}.output" | sed 's/#{cwd}\///g')
export GENERATOR=$1
export CLIENT=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].output' | sed 's/#{cwd}\///g')

echo "> Exporting utils for ${GENERATOR}..."
mkdir -p $CLIENT/utils

cp -R clients/algoliasearch-client-javascript/utils/ $CLIENT/utils

echo "> Linting ${GENERATOR}..."
eslint --ext=ts ${CLIENT} --fix
63 changes: 63 additions & 0 deletions scripts/specs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

# Break on non-zero code
set -e

SPEC=$1
OUTPUT=$2

SPECS=()

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

find_specs() {
echo "> Searching for available specs..."
local specs=( $(cat openapitools.json | jq -r '."generator-cli".generators[] | .glob') )

for spec in "${specs[@]}"; do
if [[ ! ${SPECS[*]} =~ $spec ]]; then
SPECS+=($spec)
fi
done
}

build_spec() {
local spec=$1
local client=$(echo $spec | awk -F / '{ print $(NF-1) }')

echo "> Building specs: ${client} ${OUTPUT}"

yarn swagger-cli bundle ${spec} --outfile specs/dist/${client}.${OUTPUT} --type ${OUTPUT}
}

validate_spec() {
local spec=$1
local client=$(echo $spec | awk -F / '{ print $(NF-1) }')

echo "> Validating specs: ${client}"

yarn swagger-cli validate specs/dist/${client}.${OUTPUT}
}

find_specs

if [[ $SPEC == "all" ]]; then
SPECS=("${SPECS[@]}")
elif [[ ${SPECS[*]} =~ ${SPEC} ]]; then
SPECS=("specs/${SPEC}/spec.yml")
else
echo "Unknown spec ${SPEC}"
exit 1
fi

if [[ $OUTPUT != "yaml" ]] && [[ $OUTPUT != "json" ]]; then
echo "Unknown output ${OUTPUT}"
exit 1
fi

for spec in "${SPECS[@]}"; do
build_spec $spec
validate_spec $spec
done

0 comments on commit dd301eb

Please sign in to comment.