-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
630 lines (532 loc) · 16.7 KB
/
run
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/bin/bash
#
# License: https://wiimag.com/LICENSE
# Copyright 2023 Wiimag inc. All rights reserved.
#
# Run script to execute most common operations used when working on the application framework.
#
# Set the directory containing additional scripts
SCRIPT_DIR=$(cd $(dirname $0)/scripts; pwd)
# Set the directory where the build configuration files are located.
CONFIG_DIR=$(cd $(dirname $0)/config; pwd)
# Load the scripts/common.sh file
source $SCRIPT_DIR/common.sh
# Set the default values for the parameters
APP_NAME=$(build_setting "APP_NAME")
SHORT_NAME=$(build_setting "PROJECT_ID")
SOLUTION_DIR=projects/.build
PROGRAM_NAME=$(basename $0)
# Precompute some paths
FULL_SOLUTION_DIR=$(convert_path_to_platform $(pwd)/$SOLUTION_DIR)
#
# Parse common command line arguments
#
RUN=0 # Indicate if the default build should be run at the end
BUILD=()
BUILD_DEBUG=0
BUILD_RELEASE=1
BUILD_DEPLOY=0
TESTS=()
OPEN=0
OPEN_WORKSPACE=0
GENERATE=()
PACKAGE=()
START=()
DIFF=()
PRINT=()
PRINT_LARGEFILES=0
HELP=0
VERBOSE=0
NO_COLOR=0
NO=0
YES=1
COMMAND_COUNTER=0
COMMAND_NAME_ARGS="START"
# Check if project/.build is present, if not, then we need to generate the project
# Also check if the project/.build/CMakeCache.txt is present, if not, then we need to generate the project
if [ ! -d "$SOLUTION_DIR" ] || [ ! -f "$SOLUTION_DIR/CMakeCache.txt" ]; then
BUILD=($YES)
GENERATE=($YES)
fi
while [[ $# -gt 0 ]]; do
#echo "\$1:\"$1\" \$2:\"$2\""
case $1 in
-h|--help)
HELP=1
shift # past argument
;;
--verbose)
VERBOSE=1
shift # past argument
;;
--no-color)
NO_COLOR=1
shift # past argument
;;
b|build)
# Update the command counter
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
BUILD=($YES)
COMMAND_NAME_ARGS="BUILD"
shift # past argument
# The release argument is the default, but remove it if it was specified
if [ $# -ne 0 ] && [ $1 = "release" ]; then
BUILD_RELEASE=1
shift
fi
if [ $# -ne 0 ] && [ $1 = "debug" ]; then
BUILD_DEBUG=1
shift
fi
if [ $# -ne 0 ] && [ $1 = "deploy" ]; then
BUILD_DEPLOY=1
shift
fi
;;
p|print)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
PRINT=($YES)
COMMAND_NAME_ARGS="PRINT"
shift # past argument
# The release argument is the default, but remove it if it was specified
if [ $# -ne 0 ] && [ $1 = "largefiles" ]; then
PRINT_LARGEFILES=1
shift
fi
;;
g|generate)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
GENERATE=($YES)
COMMAND_NAME_ARGS="GENERATE"
shift # past argument
;;
p|package)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
PACKAGE=($YES)
COMMAND_NAME_ARGS="PACKAGE"
shift # past argument
;;
o|open)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
OPEN=$COMMAND_COUNTER
shift # past argument
# Check if we open the workspace or the cmake generated solution
if [ $# -ne 0 ] && ([ $1 == "workspace" ] || [ $1 == "w" ]); then
# Always generate the project if we are opening the workspace
if [ ${#GENERATE[@]} -eq 0 ]; then
GENERATE=($YES)
fi
OPEN_WORKSPACE=1
shift
fi
;;
t|tests)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
TESTS=($YES)
COMMAND_NAME_ARGS="TESTS"
shift # past argument
;;
d|diff)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
DIFF=($YES)
COMMAND_NAME_ARGS="DIFF"
shift # past argument
;;
s|start)
COMMAND_COUNTER=$((COMMAND_COUNTER+1))
START=($YES)
COMMAND_NAME_ARGS="START"
RUN=1
shift # past argument
;;
*)
if [ $COMMAND_NAME_ARGS == "GENERATE" ]; then
GENERATE+=("$1")
elif [ $COMMAND_NAME_ARGS == "PACKAGE" ]; then
PACKAGE+=("$1")
elif [ $COMMAND_NAME_ARGS == "BUILD" ]; then
BUILD+=("$1")
elif [ $COMMAND_NAME_ARGS == "PRINT" ]; then
PRINT+=("$1")
elif [ $COMMAND_NAME_ARGS == "TESTS" ]; then
TESTS+=("$1")
elif [ $COMMAND_NAME_ARGS == "DIFF" ]; then
DIFF+=("$1")
elif [ $COMMAND_NAME_ARGS == "START" ]; then
if [ ${#START[@]} -eq 0 ]; then
START=($YES)
fi
START+=("$1")
elif [ ${#START[@]} -eq 0 ]; then
START=($YES $1)
else
START+=("$1") # Assume this is a positional argument for the start command
fi
shift # past argument
;;
esac
done
# If there is no arguments to ./run, then lets force start to be the default command
if [ $COMMAND_COUNTER -eq 0 ]; then
if [ ${#START[@]} -eq 0 ]; then
START=($YES)
fi
fi
# Define a set of colors if NO_COLOR is not set
if [ $NO_COLOR -eq 0 ]; then
bold=$(tput bold)
normal=$(tput sgr0)
green=$(tput setaf 2)
dark_gray='\033[1;30m'
light_gray='\033[0;37m'
red='\033[0;31m'
yellow='\033[1;33m'
blue='\033[0;34m'
purple='\033[0;35m'
cyan='\033[0;36m'
white='\033[1;37m'
nc='\033[0m' # No Color
italic=$(tput sitm)
underline=$(tput smul)
reset=$(tput sgr0)
else
bold=""
normal=""
green=''
dark_gray=''
light_gray=''
red=''
yellow=''
blue=''
purple=''
cyan=''
white=''
nc='' # No Color
italic=''
underline=''
reset=''
fi
#
# Check if help was requested
#
if [ $HELP -ge 1 ]; then
echo " Usage: $PROGRAM_NAME [${bold}b${normal}uild [debug|release|deploy]] "
echo " [${bold}g${normal}enerate] "
echo " [${bold}t${normal}ests] "
echo " [${bold}o${normal}pen [${bold}w${normal}orkspace]] "
echo " [${bold}p${normal}rint [largefiles] "
echo " [${bold}d${normal}iff [main|release/1.0.0|...] "
echo " [${bold}s${normal}tart] [--option=<value>] ...additional arguments passed to last command"
echo ""
echo " Commands:"
echo " ${bold}b${normal}uild [debug|release] Build the solution in debug or release (release if used by default)"
echo " ${bold}g${normal}enerate Generate the solution (i.e. using CMake)"
echo " ${bold}t${normal}ests Run application tests (i.e. using --run-tests)"
echo " ${bold}o${normal}pen [workspace] Open the application solution workspace (i.e. Visual Studio or Xcode IDE)"
echo " ${bold}p${normal}rint [largefiles|...] Print various solution information or stats"
echo " ${bold}d${normal}iff [...] Diff the current branch with the specified remote branch (i.e. main, release/1.0.0, etc.)"
echo " ${bold}s${normal}tart Launch the application after running other commands "
echo ""
echo " If no commands are specified, we start the application by default, i.e. './run')"
echo ""
echo " Commands can be chained one after the other, i.e. './run build tests --success=true --verbose'"
echo ""
echo " Optional arguments:"
echo " -h, --help Show this help message and exit"
echo " --verbose Increase the verbosity of the bash script"
echo " --no-color Disable color output"
echo ""
echo " Tests arguments:"
echo " --success=<true|false> Print test success as well as failures"
echo " --test-suite=<name> Run only tests for the suite with the specified name"
echo " --test-case=<name> Run only the test case with the specified name"
echo ""
echo " Examples:"
echo " ./run build Build the solution"
echo " ./run build tests start Build then run tests and finally lanch the application"
echo " ./run build tests --success=true Build then run tests passing the --success=true argument to test framework"
echo " ./run print largefiles 50 Print top 50 largest files in the repo."
echo " ./run diff Diff the current branch with the main branch (default)"
echo " ./run diff release/1.0 Diff the current branch with the remote release 1.0 branch"
echo " ./run generate open cmake Generate and open the generated cmake solution"
echo ""
exit 0
fi
if [ $VERBOSE -ge 1 ]; then
# Invoke print_global_variables
print_global_variables
# Print all set variables
echo "APP_NAME: $APP_NAME"
echo "SHORT_NAME: $SHORT_NAME"
echo "BUILD: ${BUILD[@]}"
echo " BUILD_DEBUG: $BUILD_DEBUG"
echo " BUILD_RELEASE: $BUILD_RELEASE"
echo " BUILD_DEPLOY: $BUILD_DEPLOY"
echo "OPEN: $OPEN"
echo " OPEN_WORKSPACE: $OPEN_WORKSPACE"
echo "GENERATE: ${GENERATE[@]}"
echo "PRINT: ${PRINT[@]}"
echo " PRINT_LARGEFILES: $PRINT_LARGEFILES"
echo "TESTS: ${TESTS[@]}"
echo "DIFF: ${DIFF[@]}"
echo "START: ${START[@]}"
echo "HELP: $HELP"
echo "VERBOSE: $VERBOSE"
fi
#
# Generate the CMAKE solution
#
if [ ${#GENERATE[@]} -ge 1 ] && [ ${GENERATE[0]} -ge 1 ]; then
echo
echo -e "${green}Generating ${APP_NAME} solution...${normal}"
echo
# Create the build directory if it doesn't exist and ignore any errors
mkdir -p $SOLUTION_DIR 2>/dev/null
cd $SOLUTION_DIR
# Copy second to last parameters to CMAKE_ARGS
CMAKE_ARGS=("${GENERATE[@]:1}")
# Read build configs and add all the name=value pairs to the CMAKE_ARGS array as -Dname=value
while IFS='=' read -r name value; do
# Skip comments
[[ $name == \#* ]] && continue
# Skip empty lines
[[ -z $name ]] && continue
# Skip lines that do not start with BUILD_
[[ $name != BUILD_* ]] && continue
# Skip if -D$name* is already POSTIONAL_ARGS
[[ " ${GENERATE[@]} " =~ " -D$name" ]] && continue
CMAKE_ARGS+=("-D$name=$value")
done < $CONFIG_DIR/build.config
# Write to cmake.args file if verbose is enabled all the cmake arguments one per line
echo "CMAKE_ARGS:" > cmake.args
for arg in "${CMAKE_ARGS[@]}"; do
echo " $arg" >> cmake.args
done
# Print cmake.args if CMAKE_ARGS non empty
if [ ${#CMAKE_ARGS[@]} -gt 0 ]; then
echo -ne "${dark_gray}"
cat cmake.args
echo -e "${normal}"
fi
if [ $machine == "MinGw" ]; then
cmake -G "Visual Studio 17 2022" -A x64 ../.. ${CMAKE_ARGS[@]}
elif [ ${machine} == "Mac" ]; then
cmake -G "Xcode" ../.. ${CMAKE_ARGS[@]}
fi
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===GENERATE FAILED ($retval)===${normal}"
echo
exit $retval
fi
# Keep track of it the solution was generated or not.
# We will use this to determine if we need to build the solution or not.
cd ../../
# Print the path of the generated solution
echo
echo -e "${dark_gray}Solution generated at: ${normal}$(convert_path_to_file_link $FULL_SOLUTION_DIR)"
# Skip running the applicatin by default since we just generated the solution
RUN=0
fi
#
# Build solution
#
if [ ${#BUILD[@]} -ge 1 ] && [ ${BUILD[0]} -ge 1 ]; then
# Print building message in green
echo
echo -e "${green}Building ${APP_NAME} solution...${normal}"
echo
cd $SOLUTION_DIR
CMAKE_ARGS=()
if [ $machine == "MinGw" ]; then
# Append /verbosity and /filelogger to output build results to ../../artifacts/cmake-build.log
if [ $VERBOSE -eq 1 ]; then
CMAKE_ARGS+=("/verbosity:detailed")
fi
CMAKE_ARGS+=("-fileLogger")
CMAKE_ARGS+=("-fileLoggerParameters:logfile=cmake-build.log;verbosity=Normal")
elif [ ${machine} == "Mac" ]; then
# Use -quiet if verbose is not enabled
if [ $VERBOSE -eq 0 ]; then
CMAKE_ARGS+=("-quiet")
fi
fi
# Copy second to last parameters to CMAKE_ARGS
ADDITIONAL_ARGS=("${BUILD[@]:1}")
if [ $BUILD_DEBUG -eq 1 ]; then
cmake --build . --parallel 7 --config Debug --target ${SHORT_NAME} ${ADDITIONAL_ARGS[@]} -- ${CMAKE_ARGS[@]}
elif [ $BUILD_DEPLOY -eq 1 ]; then
cmake --build . --parallel 7 --config Deploy --target ${SHORT_NAME} ${ADDITIONAL_ARGS[@]} -- ${CMAKE_ARGS[@]}
else
cmake --build . --parallel 7 --config Release --target ${SHORT_NAME} ${ADDITIONAL_ARGS[@]} -- ${CMAKE_ARGS[@]}
fi
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===BUILD FAILED ($retval)===${normal}"
echo
exit $retval
fi
BUILD_LOG_PATH=$(convert_path_to_platform $FULL_SOLUTION_DIR/cmake-build.log)
echo
echo -e "${dark_gray}Build log: ${normal}$(convert_path_to_file_link $BUILD_LOG_PATH)"
cd ../../
RUN=0
fi
#
# Build package
#
if [ ${#PACKAGE[@]} -ge 1 ] && [ ${PACKAGE[0]} -ge 1 ]; then
# Run script to build the package
echo
echo -e "${green}Building ${APP_NAME} package...${normal}"
echo
# Capture all additional arguments
ADDITIONAL_ARGS=("${PACKAGE[@]:1}")
# Run the packaging script
./scripts/build-package.sh --build ${ADDITIONAL_ARGS[@]}
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===PACKAGE FAILED ($retval)===${normal}"
echo
exit $retval
fi
RUN=0
fi
#
# GIT DIFF
#
if [ ${#DIFF[@]} -ge 1 ] && [ ${DIFF[0]} -ge 1 ]; then
# Capture all additional arguments
ADDITIONAL_ARGS=("${DIFF[@]:1}")
# Check if we have a remote branch to diff against and make sure the argument doesn't start with -- or -
if [ ${#ADDITIONAL_ARGS[@]} -eq 0 ] || [[ ${ADDITIONAL_ARGS[0]} == --* ]] || [[ ${ADDITIONAL_ARGS[0]} == -* ]]; then
# Insert main at position 0 and push other arguments
ADDITIONAL_ARGS=("${ADDITIONAL_ARGS[@]:0:0}" "main" "${ADDITIONAL_ARGS[@]:0}")
fi
# Diff the current branch with the specified remote branch
git difftool -d "origin/${ADDITIONAL_ARGS[0]}" ${ADDITIONAL_ARGS[@]:1}
# Check return value
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===DIFF FAILED ($retval)===${normal}"
echo
exit $retval
fi
RUN=0
fi
#
# Open IDE
#
if [ $OPEN -ge 1 ]; then
echo
echo -e "${green}Opening ${SHORT_NAME} in IDE...${normal}"
echo
if [ $OPEN_WORKSPACE -eq 1 ]; then
if [ $machine == "MinGw" ]; then
start $SOLUTION_DIR/${SHORT_NAME}.sln
elif [ ${machine} == "Mac" ]; then
open $SOLUTION_DIR/${SHORT_NAME}.xcodeproj
fi
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===OPEN WORKSPACE FAILED ($retval)===${normal}"
echo
exit $retval
fi
RUN=0
fi
fi
#
# Print various solution information
#
if [ ${#PRINT[@]} -ge 1 ] && [ ${PRINT[0]} -ge 1 ]; then
# Capture all additional arguments
ADDITIONAL_ARGS=("${PRINT[@]:1}")
if [ $PRINT_LARGEFILES -eq 1 ]; then
TOP_COUNT=20
if is_number ${ADDITIONAL_ARGS[0]}; then
TOP_COUNT=${ADDITIONAL_ARGS[0]}
fi
while read -r largefile; do
echo $largefile | awk '{printf "%s %s ", $1, $3 ; system("git rev-list --all --objects | grep " $1 " | cut -d \" \" -f 2-")}'
done <<< "$(git rev-list --all --objects | awk '{print $1}' | git cat-file --batch-check | sort -k3nr | head -n $TOP_COUNT)"
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===PRINT LARGEFILES FAILED ($retval)===${normal}"
echo
exit $retval
fi
RUN=0
fi
fi
#
# Run tests
#
if [ ${#TESTS[@]} -ge 1 ] && [ ${TESTS[0]} -ge 1 ]; then
# Capture all additional arguments
ADDITIONAL_ARGS=("${TESTS[@]:1}")
# If VERBOSE is defined, add it to the arguments
if [ $VERBOSE -eq 1 ]; then
ADDITIONAL_ARGS=("${ADDITIONAL_ARGS[@]:0:0}" "--verbose" "${ADDITIONAL_ARGS[@]:0}")
fi
echo
echo -e "${green}${italic}Running ${APP_NAME} tests with arguments: ${ADDITIONAL_ARGS[@]}${normal}"
echo
# Delete old test log
rm -f artifacts/tests.log 2> /dev/null
if [ ${machine} == "MinGw" ]; then
./build/${SHORT_NAME}.exe --run-tests ${ADDITIONAL_ARGS[@]}
elif [ ${machine} == "Mac" ]; then
./build/${APP_NAME}.app/Contents/MacOS/${APP_NAME} --run-tests ${ADDITIONAL_ARGS[@]}
fi
retval=$?
if [ $retval -ne 0 ]; then
echo
echo -e "${red}===TESTS FAILED ($retval)===${normal}"
echo
cat artifacts/tests.log 2> /dev/null
exit $retval
fi
# Print building message in green
echo
echo -e "${green}===TESTS SUCCESSED===${normal}"
echo
# Print test results
cat artifacts/tests.log
RUN=0
fi
# Check if we have an explicit command to run/start the application
if [ ${#START[@]} -ge 1 ] && [ ${START[0]} -ge 1 ]; then
RUN=1
fi
# Finally run the application if requested.
if [ $RUN -eq 1 ]; then
ADDITIONAL_ARGS=("${START[@]:1}")
# If VERBOSE is defined, add it to ADDITIONAL_ARGS
if [ $VERBOSE -eq 1 ]; then
ADDITIONAL_ARGS=("${ADDITIONAL_ARGS[@]:0:0}" "--verbose" "${ADDITIONAL_ARGS[@]:0}")
fi
echo
echo -e "${green}${italic}Running ${APP_NAME} with arguments: ${ADDITIONAL_ARGS[@]}${normal}"
echo
if [ ${machine} == "MinGw" ]; then
./build/${SHORT_NAME}.exe ${ADDITIONAL_ARGS[@]}
elif [ ${machine} == "Mac" ]; then
./build/${APP_NAME}.app/Contents/MacOS/${APP_NAME} ${ADDITIONAL_ARGS[@]}
fi
retval=$?
if [ $retval -ne 0 ]; then
echo
echo "${red}===RUN FAILED ($retval)===${normal}"
echo
exit $retval
fi
fi
echo
exit $?