-
Notifications
You must be signed in to change notification settings - Fork 0
246 lines (212 loc) · 10.8 KB
/
generate-libs.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
name: Generate Libraries
on:
pull_request:
branches:
- '**'
release:
types:
- published
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
# PyPi trusted publishing | Ref.: https://github.com/pypa/gh-action-pypi-publish?tab=readme-ov-file#trusted-publishing
permissions:
contents: write # For final commit
packages: write # For package upload
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
- name: Set RELEASE_VERSION based on the trigger type
run: |
if [[ "${{ github.event_name }}" == "release" ]]; then
RELEASE_VERSION="${{ github.ref_name }}"
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
RELEASE_VERSION="${{ github.event.inputs.release_name }}"
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
# Include first chars of commit SHA and run attempt number to avoid collisions
COMMIT_SHA="${{ github.event.pull_request.head.sha }}"
SHORT_COMMIT_SHA=${COMMIT_SHA::7}
RELEASE_VERSION="0.0.0+${{ github.head_ref }}.$SHORT_COMMIT_SHA.${{ github.run_attempt }}"
fi
# Make the RELEASE_VERSION semver compatible (replacing non letter/digit/./+ chars with .)
RELEASE_VERSION=$(echo "$RELEASE_VERSION" | sed 's/[^a-zA-Z0-9\.\+]/./g')
# Display the RELEASE_VERSION for verification
echo "The RELEASE_VERSION is: $RELEASE_VERSION"
# Check if RELEASE_VERSION matches the semver allowed pattern
if [[ ! $RELEASE_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9].* ]]; then
echo "RELEASE_VERSION is NOT semver compatible (1.1.1.xxx)"
exit 1
fi
# Export the RELEASE_VERSION environment variable for future steps (in env.RELEASE_VERSION)
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
- 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: Python - Prepare package files
working-directory: ./generator_python/
run: |
# Add key package files
cp pyproject.toml ./package/
cp README.md ./package/
# Generate __init__.py file to import all the *_wrapper classes
echo "# Auto-generated imports" > ./package/src/hubsante_model/__init__.py
find ./package/src/hubsante_model -type f -name "*_wrapper.py" | while read -r file; do
# Extract the relative path from hubsante_model/
rel_path=${file#*hubsante_model/}
# Convert path to Python import format
import_path=${rel_path%.py}
import_path=${import_path//\//.}
# Add import line
echo "from .$import_path import *" >> ./package/src/hubsante_model/__init__.py
done
- name: Python - Set up
uses: actions/setup-python@v4
with:
python-version: '3.8'
- name: Python - Build package
working-directory: ./generator_python/package/
run: |
pip install --upgrade build
# Set correct version
sed -i "s/version = .*/version = \"${{ env.RELEASE_VERSION }}\"/" pyproject.toml
python -m build
- name: Python - Upload package as artifact
uses: actions/upload-artifact@v3
with:
name: python-package-artifact
path: ./generator_python/package/dist/
# Leverages trusted publisher | Ref.: https://docs.pypi.org/trusted-publishers/adding-a-publisher/
- name: Python - Publish package to pypi
uses: pypa/gh-action-pypi-publish@release/v1
if: github.event_name != 'pull_request'
with:
packages-dir: ./generator_python/package/dist/
- 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;
# Add key gem files
cp hubsante_model.rb gem/lib/hubsanteModel/
cp hubsante_model.gemspec gem/
- name: Ruby - Set up
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
bundler-cache: true
- name: Ruby - Build Gem
working-directory: ./generator_ruby/gem/
run: |
GEM_VERSION=${{ env.RELEASE_VERSION }}
export GEM_VERSION=${GEM_VERSION//+/.}
gem build hubsante_model.gemspec
- name: Ruby - Upload Gem as artifact
uses: actions/upload-artifact@v3
with:
name: ruby-gem-artifact
path: ./generator_ruby/gem/hubsante_model-*.gem
- name: Ruby - Push Gem to GitHub Packages
working-directory: ./generator_ruby/gem/
run: gem push --key github --host https://rubygems.pkg.github.com/ansforge ./hubsante_model-*.gem
env:
GEM_HOST_API_KEY: ${{ secrets.GITHUB_TOKEN }} # GitHub token used to authenticate
- 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: C# - Prepare package files
# working-directory: ./generator_csharp/
# run: |
# # Add key package files
# cp HubsanteModel.csproj ./package/src/
# cp README.md ./package/
# - name: C# - Set up .NET
# uses: actions/setup-dotnet@v3
# with:
# dotnet-version: '6.0.x'
# - name: C# - Build package
# working-directory: ./generator_csharp/package/src/
# run: |
# dotnet restore
# dotnet build --configuration Release
# dotnet pack --configuration Release -p:Version=${{ env.RELEASE_VERSION }}
# - name: C# - Upload package as artifact
# uses: actions/upload-artifact@v3
# with:
# name: csharp-package-artifact
# path: ./generator_csharp/package/src/bin/Release/*.nupkg
# - name: C# - Push package to GitHub Packages
# working-directory: ./generator_csharp/package/src/
# run: |
# dotnet nuget push bin/Release/*.nupkg --source https://nuget.pkg.github.com/ansforge/index.json --api-key ${{ secrets.GITHUB_TOKEN }}
- name: Commit and push changes
if: ${{ !env.ACT }}
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: ⚙️ Auto-génération des librairies