-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathcreate_packages.sh
318 lines (249 loc) · 11 KB
/
create_packages.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# ============================================================================
#
# CodeProject.AI Server
#
# Create packages script for Linux / macOS
#
# This script will look for a package.bat script each of the modules directories
# and execute that script. The package.bat script is responsible for packaging
# up everything needed for the module to be ready to install.
# verbosity can be: quiet | info | loud
verbosity="quiet"
# Show output in wild, crazy colours
useColor=true
# Whether or not to use the jq utility for JSON parsing
useJq=true
# Set this to false (or call script with --no-dotnet) to exclude .NET packages
# This saves time to allow for quick packaging of the easier, non-compiled modules
includeDotNet=true
# Width of lines
lineWidth=70
# Set this to true to create packages for modules in the ..\CodeProject.AI-Modules
# folder
createExternalModulePackages=true
# Whether we're creating a package via a github action
githubAction=false
# The current .NET version
dotNetTarget="net9.0"
# Whether we're creating packages for all modules, or just a single module
singleModule=false
# Basic locations
# The name of the dir, within the root directory, where packages will be stored
packageDir='downloads/modules/packages'
# The name of the source directory (in development)
srcDirName='src'
# The name of the dir holing the SDK
sdkDir='SDK'
# The name of the dir holding the downloaded/sideloaded backend analysis services
modulesDir="modules"
# The name of the dir holding the external modules
externalModulesDir="CodeProject.AI-Modules"
# The name of the dir holding the server code itself
serverDir="CodeProject.AI-Server"
# Location of the utils script in the main CodeProject.AI server repo
utilsScriptGitHubUrl='https://raw.githubusercontent.com/codeproject/CodeProject.AI-Server/refs/heads/main/src/scripts'
# The path to the directory containing this script
thisScriptDirPath="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# We're assuming this script lives in /devops/build, but this script could be
# called directly from a module's folder if we're just creating a single package
if [ "$(basename $(cd .. ; pwd))" == "modules" ]; then
# In a /modules/<moduleID> folder
singleModule=true
pushd "${thisScriptDirPath}/../.." >/dev/null
elif [ "$(basename $(cd .. ; pwd))" == "${externalModulesDir}" ]; then
# In /CodeProject.AI-Modules/<moduleID> folder
singleModule=true
pushd "${thisScriptDirPath}/../../${serverDir}" >/dev/null
else
# Hopefully in the /devops/build folder
pushd "${thisScriptDirPath}/../.." >/dev/null
fi
rootDirPath="$(pwd)"
popd >/dev/null
sdkPath="${rootDirPath}/${srcDirName}/${sdkDir}"
utilsScriptsDirPath="${rootDirPath}/src/scripts"
# Override some values via parameters ::::::::::::::::::::::::::::::::::::::::::
while [[ $# -gt 0 ]]; do
param=$(echo $1 | tr '[:upper:]' '[:lower:]')
if [ "$param" = "--github-action" ]; then githubAction=true; fi
if [ "$param" = "--no-dotnet" ]; then includeDotNet=false; fi
if [ "$param" = "--no-color" ]; then useColor=false; fi
if [ "$param" = "--verbosity" ]; then
shift
if [[ $# -gt 0 ]]; then
param_value=$(echo $1 | tr '[:upper:]' '[:lower:]')
if [[ "$param_value" =~ ^(quiet|info|loud)$ ]]; then
# echo "Verbosity is $1 -> ${param_value}"
verbosity="$param_value"
echo "Setting verbosity to ${verbosity}"
else
echo "No Verbosity value provided"
fi
else
echo "Verbosity does not match the expected values quiet|info|loud"
fi
fi
shift
done
# Load vars in .env. This may update things like dotNetTarget
if [ -f ${rootDirPath}/.env ]; then
# Export each line from the .env file
while IFS='=' read -r key value; do
# Ignore lines starting with `#` (comments) and empty lines
if [[ ! "$key" =~ ^# ]] && [[ -n "$key" ]]; then
# Trim any surrounding whitespace
key=$(echo $key | xargs)
value=$(echo $value | xargs)
export "$key=$value"
fi
done < ${rootDirPath}/.env
else
echo "${rootDirPath}/.env file not found"
# exit 1
fi
# Standard output may be used as a return value in the functions. Expose stream
# 3 so we can do 'echo "Hello, World!" >&3' within these functions for debugging
# without interfering with return values.
exec 3>&1
# import the utilities :::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# A necessary evil due to cross platform editors and source control playing
# silly buggers
function correctLineEndings () {
local filePath=$1
# Force correct BOM and CRLF issues in the script. Just in case
if [[ $OSTYPE == 'darwin'* ]]; then # macOS
if [[ ${OSTYPE:6} -ge 13 ]]; then # Monterry is 'darwin21' -> "21"
sed -i'.bak' -e '1s/^\xEF\xBB\xBF//' "${filePath}" # remove BOM
sed -i'.bak' -e 's/\r$//' "${filePath}" # CRLF to LF
rm "${filePath}.bak" # Clean up. macOS requires backups for sed
fi
else # Linux
sed -i '1s/^\xEF\xBB\xBF//' "${filePath}" # remove BOM
sed -i 's/\r$//' "${filePath}" # CRLF to LF
fi
}
if [ "${githubAction}" == true ]; then
curl -sL ${utilsScriptGitHubUrl}/utils.sh -o utils.sh
source utils.sh
rm utils.sh
else
correctLineEndings ${utilsScriptsDirPath}/utils.sh
source ${utilsScriptsDirPath}/utils.sh
fi
# Helper method ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function doModulePackage () {
packageModuleId="$1"
packageModuleDirName="$2"
packageModuleDirPath="$3"
if [ "${packageModuleDirPath: -1}" == "/" ]; then
packageModuleDirPath="${packageModuleDirPath:0:${#packageModuleDirPath}-1}"
fi
# dirname=${moduleDirName,,} # requires bash 4.X, which isn't on macOS by default
dirname=$(echo $packageModuleDirName | tr '[:upper:]' '[:lower:]')
if [ -f "${packageModuleDirPath}/package.sh" ]; then
doPackage=true
# TODO: Sniff the modulesettings.json file to get the runtime, and if it's
# .NET do the test.
if [ "$includeDotNet" = false ]; then
if [ "$packageModuleId" = "ObjectDetectionYOLOv5Net" ]; then doPackage=false; fi
if [ "$packageModuleId" = "PortraitFilter" ]; then doPackage=false; fi
if [ "$packageModuleId" = "SentimentAnalysis" ]; then doPackage=false; fi
fi
if [ "$doPackage" = false ]; then
writeLine "Skipping packaging module ${packageModuleId}..." $color_info
else
pushd "$packageModuleDirPath" >/dev/null
# Read the version from the modulesettings.json file and then pass this
# version to the package.bat file.
packageVersion=$(getValueFromModuleSettings "modulesettings.json" "${packageModuleId}" "Version")
if [ "${githubAction}" != true ]; then
write "Packaging module ${packageModuleId} ${packageVersion}..." "White"
fi
packageFileName="${packageModuleId}-${packageVersion}.zip"
if [ "${githubAction}" != true ]; then
correctLineEndings package.sh
fi
bash package.sh ${packageModuleId} ${packageVersion}
if [ $? -ne 0 ]; then
writeLine "Error in package.sh for ${packageModuleDirName}" "Red"
fi
popd >/dev/null
# Move package into modules download cache
# echo Moving ${packageModuleDirPath}/${packageFileName} to ${packageDirPath}/
mv -f ${packageModuleDirPath}/${packageFileName} ${packageDirPath}/ >/dev/null
if [ $? -ne 0 ]; then
writeLine "Error" "Red"
success="false"
elif [ "${githubAction}" == true ]; then
echo $packageFileName
else
writeLine "done" "DarkGreen"
fi
fi
fi
}
# The location of directories relative to the root of the solution directory
modulesDirPath="${rootDirPath}//${modulesDir}"
externalModulesDirPath="${rootDirPath}/../${externalModulesDir}"
packageDirPath="${rootDirPath}/${packageDir}"
if [ ! -d "${packageDirPath}" ]; then mkdir -p "${packageDirPath}"; fi
# Let's go
if [ "${githubAction}" != true ]; then
scriptTitle=' Creating CodeProject.AI Module Packages'
writeLine
writeLine "$scriptTitle" 'DarkCyan' 'Default' $lineWidth
writeLine
writeLine '======================================================================' 'DarkGreen'
writeLine
writeLine ' CodeProject.AI Packager ' 'DarkGreen'
writeLine
writeLine '======================================================================' 'DarkGreen'
writeLine
fi
if [ "$verbosity" != "quiet" ]; then
writeLine
writeLine "rootDirPath = ${rootDirPath}" $color_mute
writeLine "thisScriptDirPath = ${thisScriptDirPath}" $color_mute
writeLine "utilsScriptsDirPath = ${utilsScriptsDirPath}" $color_mute
writeLine "modulesDirPath = ${modulesDirPath}" $color_mute
writeLine "externalModulesDirPath = ${externalModulesDirPath}" $color_mute
writeLine
fi
# And off we go...
success='true'
if [ "${singleModule}" == true ]; then
packageModuleDirPath=$(pwd)
packageModuleDirName="$(basename $(pwd))"
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
if [ "${packageModuleId}" != "" ]; then
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
fi
else
# Walk through the internal modules directory and call the setup script in each dir
for d in "${modulesDirPath}/"*/ ; do
packageModuleDirPath=$d
packageModuleDirName="$(basename $d)"
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
if [ "${packageModuleId}" != "" ]; then
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
fi
done
if [ "$createExternalModulePackages" == "true" ]; then
# Walk through the external modules directory and call the setup script in each dir
for d in "${externalModulesDirPath}/"*/ ; do
packageModuleDirName="$(basename $d)"
packageModuleDirPath=$d
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
if [ "${packageModuleId}" != "" ]; then
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
fi
done
fi
fi
if [ "${githubAction}" != true ]; then
writeLine
writeLine " Modules packaging Complete" "White" "DarkGreen" $lineWidth
writeLine
fi
if [ "${success}" == "false" ]; then exit 1; fi