-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelper.sh
More file actions
executable file
·102 lines (85 loc) · 3.13 KB
/
helper.sh
File metadata and controls
executable file
·102 lines (85 loc) · 3.13 KB
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
#!/usr/bin/env bash
# This script will generate a python library from the openapi definition file.
# Note: if running into this error: library initialization failed - unable to allocate file descriptor table - out of memory
# Edit docker systemd service file and add "--default-ulimit nofile=65536:65536" on the ExecStart line
# then systemctl daemon-reload and systemctl restart docker
# default generator
generator_tool="${GENERATOR_TOOL:-swagger}"
# generator can be 'swagger' or 'openapi'
# the docker image used to generate the client code
if [ "$generator_tool" = "swagger" ]; then
generator_lineage="v3"
generator_version="3.0.68"
docker_image="swaggerapi/swagger-codegen-cli-$generator_lineage:$generator_version"
generator_flag="-l"
else
# releases: https://github.com/OpenAPITools/openapi-generator/releases
generator_version="v7.13.0"
docker_image="openapitools/openapi-generator-cli:$generator_version"
generator_flag="-g"
fi
# folder with the generated python code
lib="generated"
html="html"
# we fetch the openapi.yaml file from release
gh_api_release_url="https://api.github.com/repos/elabftw/elabftw/releases/latest"
browser_url="$(
curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "User-Agent: elabftw-script" \
"$gh_api_release_url" \
| jq -r '.assets[] | select(.name == "openapi.yaml") | .browser_download_url'
)"
if [ -z "$browser_url" ] || [ "$browser_url" = "null" ]; then
echo "openapi.yaml not found in latest release assets" >&2
exit 1
fi
function cleanup {
rm -rfv "$lib"
rm -rfv "$html"
}
# generate the lib from remote spec
function generate {
cleanup
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
}
function generate-html {
cleanup
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" html2 -o /local/"$html" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
}
# don't use user/group ids in GH actions
function generate-ci {
docker run --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
# fix permissions
sudo chown -R "$(id -u)":"$(id -gn)" "$lib"
}
# generate the lib from a local file in current directory
function generate-from-local {
cleanup
docker run --user "$(id -u)":"$(id -g)" --rm -v "${PWD}":/local "$docker_image" generate -i /local/openapi.yaml "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
}
function venv {
rm -rf venv
python -m venv venv
source venv/bin/activate
}
function build {
cd "$lib" || exit 1
pip install uv
uv build
}
function publish {
generate
build
cd "$lib" || exit 1
twine upload dist/*
}
function install-dev {
venv
pip install -e generated
}
function build-ci {
generate-ci
build
}
"$1"