Skip to content

Commit

Permalink
fix: improve script consistancy and format (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jan 24, 2022
1 parent 1e76daa commit d36e3ae
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 181 deletions.
5 changes: 5 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ runs:
if: ${{ inputs.language == 'java' }}
shell: bash
run: mvn clean install -f clients/algoliasearch-client-java-2/pom.xml

- name: Download Java formatter
if: ${{ inputs.language == 'java' || inputs.job == 'cts' }}
shell: bash
run: curl -L "https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar" > /tmp/java-formatter.jar
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: ./.github/actions/setup

- name: Lint GitHub actions
run: yarn eslint --ext=yml .github/actions .github/workflows
run: yarn github-actions:lint

outputs:
RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }}
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ FROM node:$NODE_VERSION-alpine

ENV DOCKER=true

RUN apk add openjdk11 maven jq bash perl curl
RUN apk add openjdk11 maven jq bash perl
ENV JAVA_HOME=/usr/lib/jvm/default-jvm

# Java formatter
ADD https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar /tmp/java-formatter.jar

WORKDIR /app

CMD ["bash"]
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ yarn docker build:specs
yarn docker build:specs recommend
```

#### Fix the specs format

This is used by the build script and should not need to be called manually but if you want to format all specs file do:
```bash
yarn docker specs:fix
```

If you just want to check the format (not override the files), run:
```bash
yarn docker specs:lint <client>
yarn docker specs:lint search
```

### Generate clients based on the [`specs`](./specs/)

#### Usage
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"post:generate": "./scripts/post-gen/global.sh",
"generate": "./scripts/multiplexer.sh ${2:-nonverbose} ./scripts/generate.sh ${0:-all} ${1:-all} && yarn post:generate ${0:-all}",
"playground": "./scripts/multiplexer.sh ${2:-nonverbose} ./scripts/playground.sh ${0:-javascript} ${1:-search}",
"specs:format": "yarn prettier --write specs",
"specs:lint": "eslint --ext=yml specs/ .github/ && yarn openapi lint"
"specs:fix": "eslint --ext=yml specs/ --fix",
"specs:lint": "eslint --ext=yml specs/$0",
"github-actions:lint": "eslint --ext=yml .github/"
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "2.4.18",
Expand Down
57 changes: 26 additions & 31 deletions scripts/builds/clients.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,34 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
# Move to the root (easier to locate other scripts)
cd ${DIR}/../..

lang=$1
client=$2
generator="$lang-$client"
package=$(cat openapitools.json | jq -r --arg generator "$generator" '."generator-cli".generators[$generator].additionalProperties.packageName')

# Commands are based on the lang
build_client(){
echo "> Building $generator..."

if [[ $lang == 'javascript' ]]; then
yarn workspace $package build
elif [[ $lang == 'java' ]]; then
CMD="mvn install -f clients/$package/pom.xml"
if [[ $VERBOSE == "true" ]]; then
$CMD
else
set +e
log=$($CMD)

if [[ $? != 0 ]]; then
echo "$log"
exit 1
fi
set -e
fi
fi
}
LANGUAGE=$1
CLIENT=$2
GENERATOR="$LANGUAGE-$CLIENT"
PACKAGE=$(cat openapitools.json | jq -r --arg generator "$GENERATOR" '."generator-cli".generators[$generator].additionalProperties.packageName')

if [[ -z $PACKAGE ]]; then
echo "Unknown package ${PACKAGE}"
exit 1
fi

# Commands are based on the LANGUAGE
echo "> Building $GENERATOR..."

if [[ -z $package ]]; then
echo "Unknown package ${package}"
exit 1
if [[ $LANGUAGE == 'javascript' ]]; then
CMD="yarn workspace $PACKAGE build"
elif [[ $LANGUAGE == 'java' ]]; then
CMD="mvn install -f clients/$PACKAGE/pom.xml"
fi

build_client
if [[ $VERBOSE == "true" ]]; then
$CMD
else
set +e
log=$($CMD)

if [[ $? != 0 ]]; then
echo "$log"
exit 1
fi
set -e
fi
60 changes: 20 additions & 40 deletions scripts/builds/specs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,40 @@ fi
# Break on non-zero code
set -e

SPEC=$1
OUTPUT=$2

SPECS=()
CLIENT=""

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..."

SPECS=($(find specs/*/spec.yml))

echo ""
}
CLIENT=$1
OUTPUT=$2

check_format_spec() {
local spec=$1

echo "> Checking format of input spec file ${spec}"

yarn eslint --ext=yml ${spec}

local client=$1
echo "> Checking format of $client spec"
yarn specs:lint $client
echo ""
}

build_spec() {
local spec=$1

yarn openapi bundle ${spec} -o specs/dist/${CLIENT}.${OUTPUT} --ext ${OUTPUT}

local client=$1
yarn openapi bundle specs/${client}/spec.yml -o specs/dist/${client}.${OUTPUT} --ext ${OUTPUT}
echo ""
}

validate_spec() {
local spec=$1

yarn openapi lint specs/dist/${CLIENT}.${OUTPUT}

validate_output_spec() {
local client=$1
yarn openapi lint specs/dist/${client}.${OUTPUT}
echo ""
}

find_specs
CLIENTS=$(find specs/*/spec.yml | awk -F / '{ print $(NF-1) }')

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

Expand All @@ -69,10 +51,8 @@ if [[ $OUTPUT != "yml" ]] && [[ $OUTPUT != "json" ]]; then
exit 1
fi

for spec in "${SPECS[@]}"; do
CLIENT=$(echo $spec | awk -F / '{ print $(NF-1) }')

check_format_spec $spec
build_spec $spec
validate_spec $spec
for client in "${CLIENTS[@]}"; do
check_format_spec $client
build_spec $client
validate_output_spec $client
done
4 changes: 2 additions & 2 deletions scripts/ci/create-client-matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LANGUAGE=$1
BASE_CHANGED=$2
BASE_BRANCH=$3

clients=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".generators
CLIENTS=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".generators
| with_entries(
if (.key | test($lang + "-.*")) then
({key:.key,value:.value})
Expand All @@ -20,7 +20,7 @@ clients=$(cat openapitools.json | jq --arg lang $LANGUAGE -c '."generator-cli".g
| .[]')

to_test='{"client": []}'
for pair in $clients; do
for pair in $CLIENTS; do
name=$(echo $pair | jq '.name')
folder=$(echo $pair | jq '.folder')
spec_changed=$(git diff --shortstat origin/$BASE_BRANCH..HEAD -- specs/$name | wc -l)
Expand Down
15 changes: 8 additions & 7 deletions scripts/ci/create-spec-matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
BASE_CHANGED=$1
BASE_BRANCH=$2

specs='{"client":[]}'
generators=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )
GENERATORS=( $(cat openapitools.json | jq '."generator-cli".generators' | jq -r 'keys[]') )
SPECS=()

for generator in "${generators[@]}"; do
to_check='{"client":[]}'
for generator in "${GENERATORS[@]}"; do
client=${generator#*-}
if [[ ! ${specs[*]} =~ $client ]]; then
if [[ ! ${SPECS[*]} =~ $client ]]; then
changed=$(git diff --shortstat origin/$BASE_BRANCH..HEAD -- specs/$client | wc -l)
if [[ $BASE_CHANGED == "true" || $changed > 0 ]]; then
specs=$(echo $specs | jq --arg client $client '.client |= .+ [$client]')
to_check=$(echo $to_check | jq --arg client $client '.client |= .+ [$client]')
fi
fi
done

# Convert the array to json for the matrix
if [[ $(echo $specs | jq '.client | length') == 0 ]]; then
if [[ $(echo $to_check | jq '.client | length') == 0 ]]; then
# client cannot be empty or the matrix will fail
matrix='{"client":["no-run"]}'
else
matrix=$(echo $specs | jq -c)
matrix=$(echo $to_check | jq -c)
fi

echo $matrix
44 changes: 44 additions & 0 deletions scripts/formatter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/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}/..

LANGUAGE=$1
FOLDER=$2

if [[ $LANGUAGE == 'javascript' ]]; then
# jsdoc/require-hyphen-before-param-description fails to lint more than
# 6 parameters, we re-run the script if failed to lint the rest
CMD="yarn eslint --ext=ts ${FOLDER} --fix || yarn eslint --ext=ts ${FOLDER} --fix"
elif [[ $LANGUAGE == 'java' ]]; then
CMD="find $FOLDER -type f -name \"*.java\" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-jar /tmp/java-formatter.jar -r \
&& yarn prettier --write $FOLDER"
else
echo "Cannot format unknow language $LANGUAGE"
exit 1
fi

echo "> Formatting ${LANGUAGE} in ${FOLDER}..."

if [[ $VERBOSE == "true" ]]; then
# CAREFUL WITH EVAL (not safe)
eval $CMD
else
set +e
log=$(eval $CMD)

if [[ $? != 0 ]]; then
echo "$log"
exit 1
fi
set -e
fi
28 changes: 16 additions & 12 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
# Move to the root (easier to locate other scripts)
cd ${DIR}/..

lang=$1
client=$2
generator="$1-$2"
LANGUAGE=$1
CLIENT=$2
GENERATOR="$1-$2"

# build spec before generating client
build_spec() {
yarn build:specs $client
yarn build:specs $CLIENT
}

# Run the pre generation script if it exists.
run_pre_gen() {
pregen="./scripts/pre-gen/${lang}.sh"
pregen="./scripts/pre-gen/${LANGUAGE}.sh"

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

generate_client() {
echo "> Generating code for $generator..."
CMD="yarn openapi-generator-cli generate --generator-key $generator"
echo "> Generating code for $GENERATOR..."
CMD="yarn openapi-generator-cli generate --generator-key $GENERATOR"
if [[ $VERBOSE == "true" ]]; then
$CMD
else
Expand All @@ -45,12 +45,16 @@ generate_client() {

# Run the post generation script if it exists.
run_post_gen() {
postgen="./scripts/post-gen/${lang}.sh"
postgen="./scripts/post-gen/${LANGUAGE}.sh"

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

if [[ -f "$postgen" ]]; then
echo "> Running post-gen script for $generator..."
$postgen "$generator"
echo "> Running post-gen script for $GENERATOR..."
$postgen $folder $GENERATOR
fi

./scripts/formatter.sh $LANGUAGE $folder
}

if [[ ! $CI ]]; then
Expand Down
10 changes: 5 additions & 5 deletions scripts/playground.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
# Move to the root (easier to locate other scripts)
cd ${DIR}/..

lang=$1
client=$2
LANGUAGE=$1
CLIENT=$2

# Run the pre generation script if it exists.
run_playground() {
if [[ $lang == 'javascript' ]]; then
yarn workspace javascript-playground start:$client
elif [[ $lang == 'java' ]]; then
if [[ $LANGUAGE == 'javascript' ]]; then
yarn workspace javascript-playground start:$CLIENT
elif [[ $LANGUAGE == 'java' ]]; then
mvn clean compile exec:java -f playground/java/pom.xml
fi
}
Expand Down
Loading

0 comments on commit d36e3ae

Please sign in to comment.