Skip to content

Generate Libraries code source #1387

Generate Libraries code source

Generate Libraries code source #1387

name: Generate Libraries code source
on:
workflow_dispatch:
inputs:
release_name:
description: 'Release name for manual dispatch'
required: true
# Workflow dependencies, also to avoid concurrent commits
# Ref.: https://github.com/orgs/community/discussions/26238
workflow_run:
workflows: ["Generate Model"]
types:
- completed
jobs:
generate-libs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install node env 🏗
uses: actions/setup-node@v4
with:
node-version: 16
- name: Install openapi-generator-cli
run: npm install -g @openapitools/openapi-generator-cli
- name: Cleaning output directories
run: |
rm -r generator_ruby/gem/ generator_python/package/ generator_csharp/package/ || true
- name: Python - Generate classes
working-directory: ./generator_python
run: |
# Iterate over each file in the ./config directory, including the entire subfolder structure
# and then run @openapitools/openapi-generator-cli generate for each file found
# Important notice:
# Results of the find command are sorted in an alphabetic order before being passed to xargs
# This means that since the order of class generation is important, it's necessary to maintain an adequately
# named file structure in the ./config/** directories
# generator-config.json (if exists) -> usecase.generator-config.json -> wrapper.generator-config.json
# Add "| head -n 1" after sort for faster iterations while developing
find ./config/ -type f | sort -n | while read -r file; do npx @openapitools/openapi-generator-cli generate -c "$file" --skip-validate-spec; done
# There seems to be a bug in the handling of datetime & field_validator import in OpenAPI Generator
# ToDo: fix this cleanly with OpenAPI Generator Config
# For now, basic bash script that adds field_validator to all pydantic imports
find ./package/src/hubsante_model -type f -name "*.py" | while read -r file; do
# Check if 'field_validator' is missing in the 'from pydantic import' line
if grep -q 'field_validator' "$file" && ! grep -q '^from pydantic import .*field_validator.*$' "$file"; then
# Add field_validator to the import statement
sed -i '/^from pydantic import /s/$/, field_validator/' "$file"
fi
# Convert datetime to str
sed -i 's/: datetime = Field/: str = Field/g' "$file"
sed -i 's/: Optional\[datetime\] = Field/: Optional[str] = Field/g' "$file"
done
- name: Ruby - Generate classes
working-directory: ./generator_ruby
run: |
# Iterate over each file in the ./config directory, including the entire subfolder structure
# and then run @openapitools/openapi-generator-cli generate for each file found
# Important notice:
# Results of the find command are sorted in an alphabetic order before being passed to xargs
# This means that since the order of class generation is important, it's necessary to maintain an adequately
# named file structure in the ./config/** directories
# generator-config.json (if exists) -> usecase.generator-config.json -> wrapper.generator-config.json
find ./config/ -type f | sort -n | while read -r file; do npx @openapitools/openapi-generator-cli generate -c "$file" --skip-validate-spec; done
- name: Ruby - Prepare Gem files
working-directory: ./generator_ruby
run: |
# Move generated classes to the correct location
for dir in gem/*/; do
package=$(basename "$dir")
mkdir -p "gem/lib/hubsanteModel/models/$package/"
mv "$dir"/lib/hubsanteModel/models/* "gem/lib/hubsanteModel/models/$package/"
rmdir "$dir/lib/hubsanteModel/models" && rmdir "$dir/lib/hubsanteModel" && rmdir "$dir/lib" && rmdir "$dir"
done;
OUTPUT_FILE="hubsante_model.rb"
echo "# Auto-generated file - DO NOT EDIT" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
find gem/lib/hubsanteModel/models -type f -name "*.rb" \
| sort \
| sed 's|gem/lib/||' \
| sed 's|\.rb$||' \
| while read -r file; do
echo "require '$file'" >> "$OUTPUT_FILE"
done
# Add key gem files
cp $OUTPUT_FILE gem/lib/hubsanteModel/
cp hubsante_model.gemspec gem/
- name: C# - Generate classes
working-directory: ./generator_csharp
run: |
# Iterate over each file in the ./config directory, including the entire subfolder structure
# and then run @openapitools/openapi-generator-cli generate for each file found
# Important notice:
# Results of the find command are sorted in an alphabetic order before being passed to xargs
# This means that since the order of class generation is important, it's necessary to maintain an adequately
# named file structure in the ./config/** directories
# generator-config.json (if exists) -> usecase.generator-config.json -> wrapper.generator-config.json
find ./config/ -type f | sort -n | while read -r file; do npx @openapitools/openapi-generator-cli generate -c "$file" --skip-validate-spec; done
- name: Commit and push changes
if: ${{ !env.ACT }}
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: ⚙️ Auto-génération des librairies
exclude: |
.github/.tmp/**