This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtasks.sh
executable file
·443 lines (364 loc) · 8.94 KB
/
tasks.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
#!/bin/bash
set -e
cd ${0%/*}
: '
~~~~~~~~~~~~~~~
| Functions |
~~~~~~~~~~~~~~~
'
function fnApplyVersion() {
: '
Updates the files in the project which reference the project version.
This function expects each file to have a corresponding .template file
which contains the $VERSION* vars to be substituted.
'
local files=(
OpenChart/OpenChart.csproj
OpenChart/installer/win-x64/setup.isl
)
export VERSION VERSION_MAJOR VERSION_MINOR VERSION_PATCH
# Update the version across the project files.
for f in "${files[@]}"; do
if [[ ! -e "$f.template" ]]; then
echo "WARNING: Template file is missing at: $f.template"
else
envsubst <$f.template >$f
echo "Updated $f.template --> $f"
fi
done
}
function fnBuild() {
: '
Builds OpenChart in debug mode.
* Arg 1: The build output path.
'
echo "-> Building OpenChart to $1/"
msbuild -restore -clp:Verbosity=minimal $PROJECT_FILE
if [[ ! -e "$1" ]]; then
mkdir $1
fi
cp OpenChart/bin/Debug/net45/* $1
fnCopyAssets $1
fnCopyLibs $1
fnCopyMisc $1
}
function fnBundle() {
: '
Bundles the published app into an installer.
* Arg 1: The path containing the published app.
* Arg 2: The path to the installer files.
'
if isWindows; then
mkdir $BUNDLE_DIR
cp -R $INSTALLER_DIR/$PLATFORM/* $BUNDLE_DIR
cp -R $PUBLISH_DIR/$PLATFORM $BUNDLE_DIR/$PLATFORM
local setup_path=$(pwd)/$BUNDLE_DIR/setup.isl
# Run Inno setup.
iscc "$setup_path"
else
echo "Bundling is not supported on this OS."
fi
}
function fnClean() {
: '
Removes all directories that are generated from building/publishing.
'
echo "-> Removing build files"
rm -rf $OUTPUT_DIR $PUBLISH_DIR OpenChart/bin/ OpenChart/obj/ OpenChart.Tests/bin/ OpenChart.Tests/obj/
}
function fnCopyAssets() {
: '
Copies assets to the output directory.
* Arg 1: The copy destination path.
'
echo "-> Copying assets to $1/"
cp -r $ASSETS_DIR/* $1
local find_path=find
if [[ $TERM == "cygwin" ]]; then
find_path=/bin/$find_path
fi
$find_path $1 -wholename "$1/**/$ORIGINAL_ASSETS_DIR" -type d |
xargs rm -r
}
function fnCopyLibs() {
: '
Copies libs to the output directory.
* Arg 1: The copy destination path.
'
echo "-> Copying libs to $1/"
local path=$LIB_DIR/$PLATFORM
if [[ -e "$path" && $(ls -A "$path") ]]; then
cp -r $path/* $1
fi
}
function fnCopyMisc() {
: '
Copies all other files to the output directory.
* Arg 1: The copy destination path.
'
echo "-> Copying runtime assets to $1/"
local path=$MISC_DIR/$PLATFORM
if [[ -e "$path" && $(ls -A "$path") ]]; then
cp -r $path/* $1
fi
}
function fnDocker() {
: '
Executes commands in a docker container.
'
case "$1" in
build)
docker build . -t openchart
;;
publish)
docker run \
--rm \
-v $(pwd)/docker-dist/:/app/dist/ \
openchart ./tasks.sh publish
echo
echo "Published to $(pwd)/docker-dist/"
;;
test)
docker run --rm openchart ./tasks.sh test
;;
*)
echo "Usage: $0 docker [command]"
echo
echo "COMMANDS"
echo
echo " build Builds the 'openchart' Docker image"
echo " publish Builds the project for release to docker-dist/"
echo " test Runs the test suite in Docker"
echo
exit
;;
esac
}
function fnPublish() {
: '
Builds OpenChart bundled as a single executable.
'
local out_dir=$PUBLISH_DIR/$PLATFORM
echo "-> Publishing OpenChart to $out_dir/"
rm -rf $out_dir
mkdir -p $out_dir
msbuild -clp:Verbosity=minimal -p:Configuration=Release OpenChart
fnCopyAssets $out_dir
fnCopyLibs $out_dir
fnCopyMisc $out_dir
mv OpenChart/bin/Release/net45/* $out_dir
}
function fnRun() {
: '
Runs OpenChart from the output directory.
'
echo "-> Starting OpenChart"
if isLinux or isMacOS; then
$OUTPUT_DIR/OpenChart.sh
elif isWindows; then
$OUTPUT_DIR/OpenChart.exe
fi
}
function fnTest() {
: '
Runs the test suite.
'
echo "-> Running test suite"
local path=$TEST_DIR/bin/Debug/net45
local nunit_path=$(which nunit3-console.exe)
if [[ -z $nunit_path ]]; then
echo "Error: nunit3-console.exe must be in the PATH"
exit 1
fi
echo "-> Found nunit3-console.exe: $nunit_path"
mkdir -p $path
msbuild -clp:Verbosity=minimal OpenChart.Tests
fnCopyAssets $path
fnCopyLibs $path
fnCopyMisc $path
rm -rf $path/logs
mono $nunit_path OpenChart.Tests/bin/Debug/net45/OpenChart.Tests.dll --noresult $@
}
function fnVersion() {
: '
Prints the current version, or modifies the version if an arg is given.
* Arg 1: (optional) Can be <major|minor|patch>
'
case "$1" in
"")
echo $VERSION
exit
;;
apply)
fnApplyVersion
exit
;;
major)
VERSION_MAJOR=$((VERSION_MAJOR + 1))
VERSION_MINOR=0
VERSION_PATCH=0
;;
minor)
VERSION_MINOR=$((VERSION_MINOR + 1))
VERSION_PATCH=0
;;
patch)
VERSION_PATCH=$((VERSION_PATCH + 1))
;;
*)
echo "Usage: $0 version [command]"
echo
echo "COMMANDS"
echo "If no command is given, prints the current version."
echo
echo " apply Applies the current version to any files which depend on it"
echo " major Increments the major version"
echo " minor Increments the minor version"
echo " patch Increments the patch version"
echo
exit
;;
esac
# Update the version.
export VERSION=$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH
echo $VERSION >VERSION
echo "Incremented version to $VERSION"
fnApplyVersion
}
function fnUsage() {
: '
Prints the usage message and exits with code 1.
'
echo "Usage: $0 [command]"
echo "A task runner script for automating building, publishing, testing, etc."
echo
echo "COMMANDS"
echo "If no command is given, runs 'build' then 'run'."
echo
echo " build Builds the project to $OUTPUT_DIR/"
echo " bundle Bundles a published project to $BUNDLE_DIR/"
echo " clean Cleans all build-related files"
echo " docker Run commands in a Docker container"
echo " help Prints this help message"
echo " publish Builds the project for release"
echo " run Runs OpenChart from $OUTPUT_DIR/"
echo " restore Downloads nuget dependencies"
echo " test Runs the test suite"
echo " version Prints the current version"
echo
echo "EXIT CODE"
echo "Returns 0 if everything is OK."
exit 1
}
: '
~~~~~~~~~~~~~~~~
| OS Helpers |
~~~~~~~~~~~~~~~~
'
function isLinux() {
: '
Returns true if this is running on Linux.
'
[[ $DETECTED_OS == "Linux" ]]
}
function isMacOS() {
: '
Returns true if this is running on MacOS.
'
[[ $DETECTED_OS == "Darwin" ]]
}
function isWindows() {
: '
Returns true if this is running on Windows.
'
[[ $DETECTED_OS == "Windows_NT" ]]
}
function isSupportedOS() {
isLinux || isMacOS || isWindows
}
: '
~~~~~~~~~~~~~~~
| Variables |
~~~~~~~~~~~~~~~
'
BUNDLE_DIR=bundle
OUTPUT_DIR=bin
PUBLISH_DIR=dist
ORIGINAL_ASSETS_DIR=__original__
PROJECT_DIR=OpenChart
PROJECT_FILE=$PROJECT_DIR/OpenChart.csproj
TEST_DIR=OpenChart.Tests
TESTDATA_DIR=$(pwd)/$TEST_DIR/testdata
ASSETS_DIR=$PROJECT_DIR/assets
INSTALLER_DIR=$PROJECT_DIR/installer
LIB_DIR=$PROJECT_DIR/lib
MISC_DIR=$PROJECT_DIR/misc
VERSION=$(cat VERSION)
VERSION_ARRAY=($(echo $VERSION | tr '.' ' '))
VERSION_MAJOR=${VERSION_ARRAY[0]}
VERSION_MINOR=${VERSION_ARRAY[1]}
VERSION_PATCH=${VERSION_ARRAY[2]}
DETECTED_OS=$OS
if [[ -z $DETECTED_OS ]]; then
DETECTED_OS=$(uname)
fi
if isLinux; then
PLATFORM=linux-x64
elif isMacOS; then
PLATFORM=osx-x64
elif isWindows; then
PLATFORM=win-x64
fi
: '
~~~~~~~~~~~~~~~~~~~
| Runner Script |
~~~~~~~~~~~~~~~~~~~
'
if ! isSupportedOS; then
echo "ERROR: This OS is not supported ($DETECTED_OS)."
exit 1
fi
case "$1" in
"")
fnBuild $OUTPUT_DIR
fnRun
;;
build)
fnBuild $OUTPUT_DIR
;;
bundle)
fnBundle "$PUBLISH_DIR/$PLATFORM" "$INSTALLER_DIR/$PLATFORM"
;;
clean)
fnClean
;;
docker)
shift
fnDocker $@
;;
publish)
fnPublish
;;
run)
fnRun
;;
restore)
msbuild -t:Restore OpenChart.sln
;;
test)
shift
fnTest $@
;;
version)
shift
fnVersion $@
;;
help | "-h" | "--help")
fnUsage
;;
*)
echo "ERROR: Unrecognized command: $1"
echo
fnUsage
;;
esac