-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·466 lines (398 loc) · 12.8 KB
/
build.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/bin/bash
set -e
# We want our globs to be null
shopt -s nullglob
# Locations of CKAN and NetKAN.
LATEST_CKAN_URL="http://ckan-travis.s3.amazonaws.com/ckan.exe"
LATEST_NETKAN_URL="http://ckan-travis.s3.amazonaws.com/netkan.exe"
LATEST_CKAN_META="https://github.com/KSP-CKAN/CKAN-meta/archive/master.tar.gz"
# Third party utilities.
JQ_PATH="jq"
# Return codes.
EXIT_OK=0
EXIT_FAILED_JSON_VALIDATION=2
EXIT_FAILED_ROOT_NETKANS=3
EXIT_FAILED_DUPLICATE_IDENTIFIERS=4
EXIT_FAILED_NO_GAME_VERSION=5
# Allow us to specify a commit id as the first argument
if [ -n "$1" ]
then
echo "Using CLI argument of $1"
ghprbActualCommit="$1"
fi
# ------------------------------------------------
# Function for creating dummy KSP directories to
# test on.
# Usage: create_dummy_ksp name main_ver ver2 ver3 ...
# ------------------------------------------------
create_dummy_ksp() {
KSP_NAME="$1"
shift
KSP_VERSION="$1"
shift
COMPAT_VERSIONS=("$@")
echo "Creating a dummy KSP '$KSP_VERSION' install"
# Remove any existing KSP dummy install.
if [[ -d dummy_ksp/ ]]
then
rm -rf dummy_ksp
fi
# Reset the Mono registry.
if [[ "$USER" = "jenkins" ]]
then
REGISTRY_FILE="$HOME"/.mono/registry/CurrentUser/software/ckan/values.xml
if [[ -r "$REGISTRY_FILE" ]]
then
rm -f --verbose "$REGISTRY_FILE"
fi
fi
# Create dummy install.
# The DLCs are simulated depending on the version.
if versions_less_or_equal "1.7.1" "$KSP_VERSION"
then
mono ckan.exe ksp fake --set-default --headless "$KSP_NAME" dummy_ksp "$KSP_VERSION" --MakingHistory 1.1.0 --BreakingGround 1.0.0
elif versions_less_or_equal "1.4.1" "$KSP_VERSION"
then
mono ckan.exe ksp fake --set-default --headless "$KSP_NAME" dummy_ksp "$KSP_VERSION" --MakingHistory 1.1.0
else
mono ckan.exe ksp fake --set-default --headless "$KSP_NAME" dummy_ksp "$KSP_VERSION"
fi
# Add other compatible versions.
for compVer in "${COMPAT_VERSIONS[@]}"
do
mono ckan.exe compat add "$compVer"
done
# Link to the netkan downloads cache as a legacy cache.
ln -s --verbose ../../downloads_cache/ dummy_ksp/CKAN/downloads
# Copy in resources.
cp --verbose ckan.exe dummy_ksp/ckan.exe
# Point to the local metadata instead of GitHub.
mono ckan.exe repo add local "file://`pwd`/master.tar.gz"
mono ckan.exe repo remove default
}
# ------------------------------------------------
# Function for injecting metadata into a tar.gz
# archive. Assumes metadata.tar.gz to be present.
# ------------------------------------------------
inject_metadata() {
# Check input, requires at least 1 argument.
if (( $# < 1 ))
then
echo "Nothing to inject."
cp --verbose metadata.tar.gz master.tar.gz
return 0
fi
echo "Injecting into metadata."
# Extract the metadata into a new folder.
rm -rf CKAN-meta-master
tar -xzf metadata.tar.gz
# Copy in the files to inject.
for f in "$@"
do
echo "Injecting: $f"
# Find proper destination path
ID=$($JQ_PATH --raw-output '.identifier' "$f")
DEST="CKAN-meta-master/$ID/$(basename $f)"
# Print a diff if the generated file already exists
if [[ -e $DEST ]]
then
echo "Changes:"
diff -su --label Current "$DEST" --label New "$f" || true
echo
fi
mkdir -p --verbose $(dirname "$DEST")
cp --verbose "$f" "$DEST"
done
# Recompress the archive.
rm -f --verbose master.tar.gz
tar -czf master.tar.gz CKAN-meta-master
}
# ------------------------------------------------
# Print the list of game versions we know about.
# ------------------------------------------------
get_versions() {
# Usage: VERSIONS=( $(get_versions) )
# Get our official list of releases
BUILDS_JSON=$(wget -q -O - https://raw.githubusercontent.com/KSP-CKAN/CKAN/master/Core/builds.json)
# Get just the MAJOR.MINOR.PATCH strings
echo $BUILDS_JSON | "$JQ_PATH" --raw-output '.builds[]' \
| sed -e 's/\.[0-9]\+$//' \
| uniq \
| tac
}
# ------------------------------------------------
# Compare two game versions.
# Returns true if first <= second, false otherwise.
# ------------------------------------------------
versions_less_or_equal() {
# Usage: versions_less_or_equal major1.minor1.patch1 major2.minor2.patch2
# Returns: 0=true, 1=false, 2=error
VER1="$1"
VER2="$2"
if [[ -z $VER1 || -z $VER2 ]]
then
# Null means unbounded, so always match
return 0
elif [[ $VER1 =~ ^([0-9]+)\.([0-9]+) ]]
then
MAJOR1=${BASH_REMATCH[1]}
MINOR1=${BASH_REMATCH[2]}
if [[ $VER2 =~ ^([0-9]+)\.([0-9]+) ]]
then
MAJOR2=${BASH_REMATCH[1]}
MINOR2=${BASH_REMATCH[2]}
if (( $MAJOR1 < $MAJOR2 )); then return 0
elif (( $MAJOR1 > $MAJOR2 )); then return 1
elif (( $MINOR1 < $MINOR2 )); then return 0
elif (( $MINOR1 > $MINOR2 )); then return 1
else
# First two numbers match, check for a third
if [[ $VER1 =~ ^[0-9]+\.[0-9]+\.([0-9]+) ]]
then
PATCH1=${BASH_REMATCH[1]}
if [[ $VER2 =~ ^[0-9]+\.[0-9]+\.([0-9]+) ]]
then
PATCH2=${BASH_REMATCH[1]}
if (( $PATCH1 < $PATCH2 )); then return 0
elif (( $PATCH1 > $PATCH2 )); then return 1
else
# All are equal
return 0
fi
else
# No third digit, accept it
return 0
fi
else
# No third digit, accept it
return 0
fi
fi
else
# Second version not valid
return 2
fi
else
# First version not valid
return 2
fi
}
# ------------------------------------------------
# Print versions that match the given min and max.
# ------------------------------------------------
matching_versions() {
# ASSUMES: We have done VERSIONS=( $(get_versions) ) globally
# Usage: matching_versions ksp_version_min ksp_version_max
MIN="$1"
MAX="$2"
if [[ ( -z "$MIN" && -z "$MAX" ) || ( "$MIN" = any && "$MAX" = any ) ]]
then
echo "${VERSIONS[@]}"
else
declare -a MATCHES
MATCHES=()
for VER in "${VERSIONS[@]}"
do
if versions_less_or_equal "$MIN" "$VER" && versions_less_or_equal "$VER" "$MAX"
then
MATCHES+=($VER)
fi
done
echo "${MATCHES[@]}"
fi
}
# ------------------------------------------------
# Print versions that match the given .ckan file.
# ------------------------------------------------
ckan_matching_versions() {
# Usage: ckan_matching_versions modname-version.ckan
CKAN="$1"
# Get min and max versions
MIN=$("$JQ_PATH" --raw-output 'if .ksp_version then .ksp_version elif .ksp_version_min then .ksp_version_min else "" end' "$CKAN")
MAX=$("$JQ_PATH" --raw-output 'if .ksp_version then .ksp_version elif .ksp_version_max then .ksp_version_max else "" end' "$CKAN")
matching_versions "$MIN" "$MAX"
}
# ------------------------------------------------
# Print versions indicated in pull request body text.
# Input comes from the Jenkins ghprbPullLongDescription environment variable.
# We look for a string that looks like:
# ckan compat add 1.4 1.5 1.6
# And we print out the version numbers that we find.
# ------------------------------------------------
versions_from_description() {
if [[ $ghprbPullLongDescription =~ 'ckan compat add'((' '[0-9.]+)+) ]]
then
# Unquoted so each version is a separate string
echo ${BASH_REMATCH[1]}
fi
}
declare -a VERSIONS
VERSIONS=( $(get_versions) )
# ------------------------------------------------
# Main entry point.
# ------------------------------------------------
if [ -n "$ghprbActualCommit" ]
then
echo "Commit hash: $ghprbActualCommit"
export COMMIT_CHANGES="`git diff --diff-filter=AMR --name-only --stat origin/master...HEAD`"
else
echo "No commit provided, skipping further tests."
exit "$EXIT_OK"
fi
# Make sure we start from a clean slate.
if [ -d "built/" ]
then
rm -rf --verbose built
fi
if [ -d "downloads_cache/" ]
then
rm -rf --verbose downloads_cache
fi
if [ -e "master.tar.gz" ]
then
rm -f --verbose master.tar.gz
fi
if [ -e "metadata.tar.gz" ]
then
rm -f --verbose metadata.tar.gz
fi
# Check our new NetKAN is not in the root of our repo
root_netkans=( *.netkan )
if (( ${#root_netkans[@]} > 0 ))
then
echo NetKAN file found in root of repository, please move it into NetKAN/
exit "$EXIT_FAILED_ROOT_NETKANS"
fi
# Check JSON.
echo "Running jsonlint on the changed files"
echo "If you get an error below you should look for syntax errors in the metadata"
for f in $COMMIT_CHANGES
do
if ! [[ "$f" =~ ^NetKAN/ ]]
then
echo "Skipping file '$f': Not in the NetKAN directory."
continue
elif [[ "$f" =~ .frozen$ ]]
then
echo "Lets try not to validate '$f' with jsonlint"
continue
fi
echo "Validating $f..."
jsonlint -s -v "$f"
if [ $? -ne 0 ]
then
echo "Failed to validate $f"
exit "$EXIT_FAILED_JSON_VALIDATION"
fi
done
echo ""
# Print the changes.
echo "Detected file changes:"
for f in $COMMIT_CHANGES
do
echo "$f"
done
echo ""
# Create folders.
mkdir --verbose built
# Point to cache folder here
mkdir --verbose downloads_cache
# Fetch latest ckan and netkan executable.
echo "Fetching latest ckan.exe"
wget --quiet "$LATEST_CKAN_URL" -O ckan.exe
mono ckan.exe version
echo "Fetching latest netkan.exe"
wget --quiet "$LATEST_NETKAN_URL" -O netkan.exe
mono netkan.exe --version
# Fetch the latest metadata.
echo "Fetching latest metadata"
wget --quiet "$LATEST_CKAN_META" -O metadata.tar.gz
# Determine KSP dummy name.
if [ -z $ghprbActualCommit ]
then
KSP_NAME=dummy
else
KSP_NAME="$ghprbActualCommit"
fi
# Build all the passed .netkan files.
# Note: Additional NETKAN_OPTIONS may be set on jenkins jobs
for f in $COMMIT_CHANGES
do
if [[ "$f" =~ \.netkan$ ]]
then
basename=$(basename "$f" .netkan)
frozen_files=( "NetKAN/$basename.frozen"* )
if [ ${#frozen_files[@]} -gt 0 ]
then
echo "'$basename' matches an existing frozen identifier: ${frozen_files[@]}"
exit "$EXIT_FAILED_DUPLICATE_IDENTIFIERS"
fi
echo "Running NetKAN for $f"
mono netkan.exe "$f" --cachedir="downloads_cache" --outputdir="built" $NETKAN_OPTIONS
else
echo "Let's try not to build '$f' with netkan"
fi
done
# Get array of all the files
OTHER_FILES=(built/*.ckan)
# Check if we found any
if (( ${#OTHER_FILES[@]} > 0 ))
then
# Inject into metadata
inject_metadata "${OTHER_FILES[@]}"
fi
# Test all the built files.
for ckan in built/*.ckan
do
if [ ! -e "$ckan" ]
then
echo "No ckan files to test"
continue
fi
echo "Checking $ckan"
echo ""
echo "----------------------------------------------"
cat "$ckan"
echo "----------------------------------------------"
echo ""
# Extract identifier and KSP version.
CURRENT_IDENTIFIER=$($JQ_PATH --raw-output '.identifier' "$ckan")
KSP_VERSIONS=( $(ckan_matching_versions "$ckan") $(versions_from_description) )
if (( ${#KSP_VERSIONS[@]} < 1 ))
then
echo "$ckan doesn't match any valid game version"
exit "$EXIT_FAILED_NO_GAME_VERSION"
fi
echo "Extracted $CURRENT_IDENTIFIER as identifier."
echo "Extracted ${KSP_VERSIONS[*]} as KSP versions."
# Create a dummy KSP install.
create_dummy_ksp "$KSP_NAME" "${KSP_VERSIONS[@]}"
# Get or restore fresh registry
if [[ ! -e registry.json ]]
then
echo "Running ckan update"
mono ckan.exe update
echo "Saving fresh registry file"
cp --verbose dummy_ksp/CKAN/registry.json .
else
echo "Restoring saved registry file"
cp --verbose registry.json dummy_ksp/CKAN
fi
echo "Running ckan install -c $ckan"
mono ckan.exe prompt --headless <<EOCKAN
install -c $ckan --headless
list --porcelain
show $CURRENT_IDENTIFIER
ksp forget $KSP_NAME
EOCKAN
# Check for Installations that have gone wrong.
gamedata=($(find dummy_ksp/GameData/. -name GameData -exec sh -c 'if test -d "{}"; then echo "{}";fi' \;))
if (( ${#gamedata[@]} > 0 ))
then
echo "GameData directory found within GameData"
printf '%s\n' "Path: ${gamedata[@]}"
exit 1;
fi
# Blank line between files
echo
done