-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgen-client.sh
executable file
·164 lines (144 loc) · 4.68 KB
/
gen-client.sh
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
#!/bin/bash
set -eu
if [ $# -eq 0 ]
then
echo "No arguments provided"
echo "Usage: $0 <api_spec> <component> [<language> [<package>]]"
exit 1
fi
normalize_version ()
{
python3 -c '
import sys
from packaging.version import Version
print(Version(sys.stdin.read()))
'
}
generator_version ()
{
python3 -c '
import sys
from packaging.version import Version
language = sys.argv[1]
core_version = Version(sys.stdin.read())
if language.lower() == "python":
if core_version >= Version("3.70.dev"):
print("v7.10.0")
else:
print("v4.3.1")
elif language.lower() == "ruby":
if core_version >= Version("3.70.dev"):
print("v7.10.0")
else:
print("v4.3.1")
elif language.lower() == "typescript":
print("v5.2.1")
else:
exit(1)
' "$@"
}
API_SPEC="$1"
COMPONENT="$2"
LANGUAGE="${3:-python}"
PACKAGE="${4:-pulp_${COMPONENT//-/_}}"
DOMAIN_ENABLED="$(jq -r '.info."x-pulp-domain-enabled" // false' < "${API_SPEC}")"
VERSION="$(jq -r --arg component "${COMPONENT}" '.info."x-pulp-app-versions"[$component] // error("No version found.")' < "${API_SPEC}" | normalize_version)"
CORE_VERSION="$(jq -r '.info."x-pulp-app-versions".core // "0.0.0"' < "${API_SPEC}" | normalize_version)"
GENERATOR_VERSION="$(generator_version "${LANGUAGE}" <<<"${CORE_VERSION}")"
IMAGE_OVERRIDE_VAR="OPENAPI_${LANGUAGE^^}_IMAGE"
OPENAPI_IMAGE="${!IMAGE_OVERRIDE_VAR:-docker.io/openapitools/openapi-generator-cli:${GENERATOR_VERSION}}"
IMAGE_TAG="${OPENAPI_IMAGE#*:}"
echo "${COMPONENT}: ${VERSION} core: ${CORE_VERSION} domains: ${DOMAIN_ENABLED} generator: ${GENERATOR_VERSION}"
echo "Using: ${OPENAPI_IMAGE}"
if command -v podman > /dev/null
then
CONTAINER_EXEC=podman
if [[ -n "${PULP_MCS_LABEL:-}" ]]
then
USER_COMMAND=("--userns=keep-id" "--security-opt" "label=level:${PULP_MCS_LABEL}")
else
USER_COMMAND=("--userns=keep-id")
fi
ULIMIT_COMMAND=()
else
CONTAINER_EXEC=docker
if [[ -n "${PULP_MCS_LABEL:-}" ]]
then
USER_COMMAND=("-u" "$(id -u)" "--security-opt" "label=level:${PULP_MCS_LABEL}")
else
USER_COMMAND=("-u" "$(id -u)")
fi
ULIMIT_COMMAND=("--ulimit" "nofile=122880:122880")
fi
# Mount volumes from parent container with `--volumes-from` option if `PARENT_CONTAINER_ID` is set.
if [ -z "${PARENT_CONTAINER_ID:-}" ]
then
if command -v getenforce > /dev/null && [ "$(getenforce)" == "Enforcing" ]
then
VOLUME_NAME="/local:Z"
else
VOLUME_NAME="/local"
fi
VOLUME_OPTION=("--volume" "${PWD}:${VOLUME_NAME}")
VOLUME_DIR="/local"
else
VOLUME_OPTION=("--volumes-from" "${PARENT_CONTAINER_ID}:rw")
VOLUME_DIR="${PWD}"
fi
REMOVE_COOKIE_AUTH_FILTER='del(.paths[][].security|select(.)[]|select(.cookieAuth))|del(.components.securitySchemes.cookieAuth)'
if [ "$LANGUAGE" = "python" ]
then
cat "${API_SPEC}" | jq "." > patched-api.json
$CONTAINER_EXEC run \
"${ULIMIT_COMMAND[@]}" \
"${USER_COMMAND[@]}" \
--rm \
"${VOLUME_OPTION[@]}" \
"${OPENAPI_IMAGE}" generate \
-i "${VOLUME_DIR}/patched-api.json" \
-g python \
-o "${VOLUME_DIR}/${PACKAGE}-client" \
"--additional-properties=packageName=pulpcore.client.${PACKAGE},projectName=${PACKAGE}-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED}" \
-t "${VOLUME_DIR}/templates/python/${IMAGE_TAG}" \
--skip-validate-spec \
--strict-spec=false
cp python/__init__.py "${PACKAGE}-client/pulpcore/"
cp python/__init__.py "${PACKAGE}-client/pulpcore/client/"
fi
if [ "$LANGUAGE" = "ruby" ]
then
# https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-skip-certain-files-during-code-generation
mkdir -p "${PACKAGE}-client"
echo git_push.sh > "${PACKAGE}-client/.openapi-generator-ignore"
cat "${API_SPEC}" | jq "${REMOVE_COOKIE_AUTH_FILTER}" > patched-api.json
$CONTAINER_EXEC run \
"${ULIMIT_COMMAND[@]}" \
"${USER_COMMAND[@]}" \
--rm \
"${VOLUME_OPTION[@]}" \
"${OPENAPI_IMAGE}" generate \
-i "${VOLUME_DIR}/patched-api.json" \
-g ruby \
-o "${VOLUME_DIR}/${PACKAGE}-client" \
"--additional-properties=gemName=${PACKAGE}_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/${PACKAGE}" \
--library=faraday \
-t "${VOLUME_DIR}/templates/ruby/${IMAGE_TAG}" \
--skip-validate-spec \
--strict-spec=false
fi
if [ "$LANGUAGE" = "typescript" ]
then
cat "${API_SPEC}" | jq "." > patched-api.json
$CONTAINER_EXEC run \
"${ULIMIT_COMMAND[@]}" \
"${USER_COMMAND[@]}" \
--rm \
"${VOLUME_OPTION[@]}" \
"${OPENAPI_TYPESCRIPT_IMAGE}" generate \
-i "${VOLUME_DIR}/patched-api.json" \
-g typescript-axios \
-o "${VOLUME_DIR}/${PACKAGE}-client" \
-t "${VOLUME_DIR}/templates/typescript-axios" \
--skip-validate-spec \
--strict-spec=false
fi