-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathupdate-apps-api
180 lines (146 loc) · 4.92 KB
/
update-apps-api
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
#!/bin/bash
# NOTE: This is a Bash version of the equivalent PowerShell script. If you make modifications to this file,
# you MUST make similar modifications to the PowerShell equivalent.
set -e
# This is the checkout which we build against for the swagger-codegen project. This can be a tag or a commit (or technically a branch)
# but is meant to be relatively stable, and regardless only intentionally updated, so that builds are generally speaking reproducable.
CheckoutId="129235049ab062492c75cc193ecfada84633cc56"
SkipBuild=0
SkipUpdate=0
while test $# -gt 0
do
case "$1" in
--SkipBuild)
SkipBuild=1
;;
--SkipUpdate)
SkipUpdate=1
;;
esac
shift
done
echo "SkipBuild=$SkipBuild"
function Status {
echo -e "\e[32m$1\e[0m"
}
function ErrorAndQuit {
echo -e "\e[31m$1\e[0m"
exit 1
}
function TestSoftware {
softwares=("java" "git" "mvn" "npm" "tsp")
notFound=()
for s in "${softwares[@]}"
do
if ! [[ $(command -v $s) ]]; then
# not found
notFound+=("$s")
fi
done
if ! [[ "${#notFound[@]}" -eq 0 ]]; then
echo "One or more required software packages are not installed, and must be installed and available on your path before continuing."
echo "Please install the following software before trying again:"
for software in "${notFound[@]}"
do
echo "$software"
done
exit
fi
}
function TestProjectFile {
Path="$1"
if ! [[ -d "$1" ]]; then
read -p "Creating $Path, continue? [y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
mkdir -p "$Path"
else
exit
fi
else
echo "Using $Path"
fi
}
function CloneOrCheckout {
repo="$1"
localPath="$2"
branch="$3"
if [[ -z "$(ls -A $localPath)" ]]; then
#Clone
Status "Cloning $repo into $localPath"
git clone "$repo" "$localPath" --branch "$branch"
else
Status "Updating $localPath"
#Pull
status=$(git status --porcelain)
if [[ -z "$status" ]]; then
# :thumbsup:
git fetch
git checkout "$branch"
else
# :thumbsdown:
ErrorAndQuit "$localPath has local changes, refusing to continue. Please stash or otherwise revert your changes before continuing."
fi
fi
}
function CheckFileOrExit {
Path="$1"
if [[ -d "$Path" ]]; then
ErrorAndQuit "Could not find $Path, exiting"
fi
}
function StartMain {
SwaggerGenerator="$1"
JavaRepo="$2"
NodeRepo="$3"
InputSpec="$4"
TemplateDir="$5"
Status "Ensuring prerequisite software is installed"
TestSoftware
Status "Creating directories if needed"
TestProjectFile "$SwaggerGenerator"
TestProjectFile "$JavaRepo"
TestProjectFile "$NodeRepo"
SwaggerGenerator=$(realpath "$SwaggerGenerator")
JavaRepo=$(realpath "$JavaRepo")
NodeRepo=$(realpath "$NodeRepo")
MethodScriptDirectory=$(realpath $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../../)
cd "$SwaggerGenerator"
if [[ "$SkipUpdate" -eq 0 ]]; then
Status "Getting Swagger Codegen"
# Checkout SwaggerGenerator
CloneOrCheckout "https://github.com/swagger-api/swagger-codegen" "$SwaggerGenerator" "$CheckoutId"
fi
if [[ "$SkipBuild" -eq 0 ]]; then
Status "Building Swagger Codegen"
mvn -Dmaven.test.skip=true clean package
fi
GeneratorJar=$(realpath modules/swagger-codegen-cli/target/swagger-codegen-cli.jar)
CheckFileOrExit "$GeneratorJar"
Status "Generating OpenAPI spec from $InputSpec"
cd "$InputSpec"
npm install
tsp compile main.tsp --emit "@typespec/openapi3"
InputSpec="$InputSpec/tsp-output/@typespec/openapi3/openapi.yaml"
Status "Using $InputSpec as the OpenAPI input"
Status "Generating Java Client"
java -jar "$GeneratorJar" generate -i "$InputSpec" -l java -o "$JavaRepo" -DhideGenerationTimestamp=true --template-dir "$MethodScriptDirectory/mustacheTemplates"
# Copy in src/main/java
Status "Moving API files into MethodScript"
cd "$MethodScriptDirectory"
mkdir -p "$MethodScriptDirectory/src/main/java/io/swagger/client"
rm -rf "$MethodScriptDirectory/src/main/java/io/swagger/client"
mkdir "$MethodScriptDirectory/src/main/java/io/swagger/client"
cp -R "$JavaRepo/src/main/java/io/swagger/client/" "$MethodScriptDirectory/src/main/java/io/swagger/"
Status "Generating Node Server"
java -jar "$GeneratorJar" generate -i "$InputSpec" -l nodejs-server -o "$NodeRepo" --disable-examples --template-dir "$TemplateDir"
npx swagger-typescript-api -p "$InputSpec" -o "$NodeRepo\models" --clean-output --no-client
Status "Done!"
}
SwaggerGeneratorDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../../../SwaggerGenerator"
JavaRepoDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../../../MethodScriptAppsJavaApi"
NodeRepoDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../../../apps.methodscript.com"
InputSpec="$(realpath $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../../src/main/resources/apps.methodscript.com)"
TemplateDir="$NodeRepoDirectory/mustacheTemplates"
StartMain "$SwaggerGeneratorDirectory" "$JavaRepoDirectory" "$NodeRepoDirectory" "$InputSpec" "$TemplateDir"
exit 0