-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbuild-oas.sh
More file actions
executable file
·108 lines (87 loc) · 3.06 KB
/
build-oas.sh
File metadata and controls
executable file
·108 lines (87 loc) · 3.06 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
103
104
105
106
107
108
#!/bin/bash
set -eux -o pipefail
version=$1 # e.g. 2024-07
modules=("db_control" "db_data" "inference" "admin")
destination="src/main/java/org/openapitools"
build_dir="gen"
update_apis_repo() {
echo "Updating apis repo"
pushd codegen/apis
git fetch
git checkout main
git pull
just build
popd
}
verify_spec_version() {
local version=$1
echo "Verifying spec version $version exists in apis repo"
if [ -z "$version" ]; then
echo "Version is required"
exit 1
fi
verify_directory_exists "codegen/apis/_build/${version}"
}
verify_file_exists() {
local filename=$1
if [ ! -f "$filename" ]; then
echo "File does not exist at $filename"
exit 1
fi
}
verify_directory_exists() {
local directory=$1
if [ ! -d "$directory" ]; then
echo "Directory does not exist at $directory"
exit 1
fi
}
generate_client() {
local module_name=$1
oas_file="codegen/apis/_build/${version}/${module_name}_${version}.oas.yaml"
verify_file_exists $oas_file
# Cleanup previous build files
echo "Cleaning up previous build files"
rm -rf "${build_dir}"
# Generate client module
docker run --rm -v $(pwd):/workspace openapitools/openapi-generator-cli:v7.0.1 generate \
--input-spec "/workspace/$oas_file" \
--generator-name java \
--additional-properties=dateLibrary='java8',disallowAdditionalPropertiesIfNotPresent=false \
--output "/workspace/${build_dir}"
# Copy the generated module to the correct location
rm -rf "${destination}/${module_name}"
mkdir -p "${destination}/${module_name}"
path_to_copy="${build_dir}/src/main/java/org/openapitools/client"
cp -r $path_to_copy "${destination}/${module_name}"
# Adjust package names in generated file
find "${destination}/${module_name}" -name "*.java" | while IFS= read -r file; do
sed -i '' "s/org\.openapitools\.client/org\.openapitools\.${module_name}\.client/g" "$file"
done
# Add NDJSON block to ApiClient.java in the db_data module
if [ "$module_name" == "db_data" ]; then
echo "Adding NDJSON handler block to ApiClient.java"
# Use sed to insert the NDJSON block into ApiClient.java
sed -i '' '/return RequestBody.create((File) obj, MediaType.parse(contentType));/a \
} else if ("application/x-ndjson".equals(contentType)) { \
// Handle NDJSON (Newline Delimited JSON) \
if (obj instanceof Iterable) { \
StringBuilder ndjsonContent = new StringBuilder(); \
for (Object item : (Iterable<?>) obj) { \
String json = JSON.serialize(item); \
ndjsonContent.append(json).append("\\n"); \
} \
return RequestBody.create(ndjsonContent.toString(), MediaType.parse(contentType)); \
} else { \
throw new ApiException("NDJSON content requires a collection of objects."); \
} \
' src/main/java/org/openapitools/db_data/client/ApiClient.java
fi
}
update_apis_repo
verify_spec_version $version
rm -rf "${destination}"
mkdir -p "${destination}"
for module in "${modules[@]}"; do
generate_client $module
done