forked from KSP-CKAN/NetKAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·340 lines (279 loc) · 8.29 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
#!/bin/bash
set -e
# Default flags.
KSP_VERSION_DEFAULT="1.0.5"
KSP_NAME_DEFAULT="dummy"
# 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_PROVE_STEP=1
EXIT_FAILED_JSON_VALIDATION=2
# 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. Takes version as an argument.
# ------------------------------------------------
create_dummy_ksp () {
KSP_VERSION=$KSP_VERSION_DEFAULT
KSP_NAME=$KSP_NAME_DEFAULT
# Set the version to the requested KSP version if supplied.
if [ $# -eq 2 ]
then
KSP_VERSION=$1
KSP_NAME=$2
fi
# TODO: Manual hack, a better way to handle this kind of identifiers may be needed.
case $KSP_VERSION in
"0.23")
echo "Overidding '0.23' with '0.23.0'"
KSP_VERSION="0.23.0"
;;
"0.25")
echo "Overidding '0.25' with '0.25.0'"
KSP_VERSION="0.25.0"
;;
"0.90")
echo "Overiding '0.90' with '0.90.0'"
KSP_VERSION="0.90.0"
;;
"1.0")
echo "Overidding '1.0' with '$KSP_VERSION_DEFAULT'"
KSP_VERSION=$KSP_VERSION_DEFAULT
;;
"1.0.99")
echo "Overidding '1.0.99' with '$KSP_VERSION_DEFAULT'"
KSP_VERSION=$KSP_VERSION_DEFAULT
;;
"any")
echo "Overridding any with '$KSP_VERSION_DEFAULT'"
KSP_VERSION=$KSP_VERSION_DEFAULT
;;
"null")
echo "Overriding 'null' with '$KSP_VERSION_DEFAULT'"
KSP_VERSION=$KSP_VERSION_DEFAULT
;;
"")
echo "Overriding empty version with '$KSP_VERSION_DEFAULT'"
KSP_VERSION=$KSP_VERSION_DEFAULT
;;
*)
echo "No override, Running with '$KSP_VERSION'"
;;
esac
echo "Creating a dummy KSP '$KSP_VERSION' install"
# Remove any existing KSP dummy install.
if [ -d "dummy_ksp/" ]
then
rm -rf dummy_ksp
fi
# Create a new dummy KSP.
mkdir dummy_ksp
mkdir dummy_ksp/CKAN
mkdir dummy_ksp/GameData
mkdir dummy_ksp/Ships/
mkdir dummy_ksp/Ships/VAB
mkdir dummy_ksp/Ships/SPH
mkdir dummy_ksp/Ships/@thumbs
mkdir dummy_ksp/Ships/@thumbs/VAB
mkdir dummy_ksp/Ships/@thumbs/SPH
echo "Version $KSP_VERSION" > dummy_ksp/readme.txt
# Copy in resources.
cp ckan.exe dummy_ksp/ckan.exe
# 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 $REGISTRY_FILE
fi
fi
# Register the new dummy install.
mono ckan.exe ksp add $KSP_NAME "`pwd`/dummy_ksp"
# Set the instance to default.
mono ckan.exe ksp default $KSP_NAME
# 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
# Link to the downloads cache.
ln -s ../../downloads_cache/ dummy_ksp/CKAN/downloads/
}
# ------------------------------------------------
# Function for injecting metadata into a tar.gz
# archive. Assummes metadata.tar.gz to be present.
# ------------------------------------------------
inject_metadata () {
# TODO: Arrays + Bash Functions aren't fun. This needs
# Improvement but appears to work. The variables are
# available to the called functions.
# Check input, requires at least 1 argument.
if [ $# -ne 1 ]
then
echo "Nothing to inject."
cp 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.
# TODO: Unsure why this suddenly needs [*] declaration
# but it does work
for f in ${OTHER_FILES[*]}
do
echo "Injecting: $f"
cp $f CKAN-meta-master
done
# Recompress the archive.
rm -f master.tar.gz
tar -czf master.tar.gz CKAN-meta-master
}
# ------------------------------------------------
# Main entry point.
# ------------------------------------------------
if [ -n "$ghprbActualCommit" ]
then
echo "Commit hash: $ghprbActualCommit"
export COMMIT_CHANGES="`git diff --diff-filter=AM --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 built
fi
if [ -d "downloads_cache/" ]
then
rm -rf downloads_cache
fi
if [ -e "master.tar.gz" ]
then
rm -f master.tar.gz
fi
if [ -e "metadata.tar.gz" ]
then
rm -f metadata.tar.gz
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" == "build.sh" ]
then
echo "Lets try not to validate our build script 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 ""
# Run basic tests.
echo "Running basic sanity tests on metadata."
echo "If these fail, then fix whatever is causing them first."
if ! prove
then
echo "Prove step failed."
exit $EXIT_FAILED_PROVE_STEP
fi
# Find the changes to test.
echo "Finding changes to test..."
# Print the changes.
echo "Detected file changes:"
for f in $COMMIT_CHANGES
do
echo "$f"
done
echo ""
# Create folders.
mkdir built
mkdir downloads_cache # TODO: Point to cache folder here instead if possible.
# 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" = "build.sh" ]; then
echo "Lets try not to validate our build script with netkan"
continue
fi
echo "Running NetKAN for $f"
mono netkan.exe $f --cachedir="downloads_cache" --outputdir="built" $NETKAN_OPTIONS
done
# 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 | python -m json.tool
echo "----------------------------------------------"
echo ""
# Get a list of all the OTHER files.
OTHER_FILES=()
for o in built/*.ckan
do
OTHER_FILES+=($o)
done
# Inject into metadata.
inject_metadata $OTHER_FILES
# Extract identifier and KSP version.
CURRENT_IDENTIFIER=$($JQ_PATH '.identifier' $ckan)
CURRENT_KSP_VERSION=$($JQ_PATH 'if .ksp_version then .ksp_version else .ksp_version_max end' $ckan)
# Strip "'s.
CURRENT_IDENTIFIER=${CURRENT_IDENTIFIER//'"'}
CURRENT_KSP_VERSION=${CURRENT_KSP_VERSION//'"'}
echo "Extracted $CURRENT_IDENTIFIER as identifier."
echo "Extracted $CURRENT_KSP_VERSION as KSP version."
# Create a dummy KSP install.
create_dummy_ksp $CURRENT_KSP_VERSION $KSP_NAME
echo "Running ckan update"
mono ckan.exe update
echo "Running ckan install -c $ckan"
mono ckan.exe install -c $ckan --headless
# Print list of installed mods.
mono ckan.exe list --porcelain
# Check the installed files for this .ckan file.
mono ckan.exe show $CURRENT_IDENTIFIER
# Cleanup.
mono ckan.exe ksp forget $KSP_NAME
done