-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
337 lines (297 loc) · 8.98 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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2024 Stéphane Micheloud
#
# Licensed under the MIT License.
#
##############################################################################
## Subroutines
getHome() {
local source="${BASH_SOURCE[0]}"
while [[ -h "$source" ]]; do
local linked="$(readlink "$source")"
local dir="$( cd -P $(dirname "$source") && cd -P $(dirname "$linked") && pwd )"
source="$dir/$(basename "$linked")"
done
( cd -P "$(dirname "$source")" && pwd )
}
debug() {
local DEBUG_LABEL="[46m[DEBUG][0m"
[[ $DEBUG -eq 1 ]] && echo "$DEBUG_LABEL $1" 1>&2
}
warning() {
local WARNING_LABEL="[46m[WARNING][0m"
echo "$WARNING_LABEL $1" 1>&2
}
error() {
local ERROR_LABEL="[91mError:[0m"
echo "$ERROR_LABEL $1" 1>&2
}
# use variables EXITCODE, TIMER_START
cleanup() {
[[ $1 =~ ^[0-1]$ ]] && EXITCODE=$1
if [[ $TIMER -eq 1 ]]; then
local TIMER_END=$(date +'%s')
local duration=$((TIMER_END - TIMER_START))
echo "Total execution time: $(date -d @$duration +'%H:%M:%S')" 1>&2
fi
debug "EXITCODE=$EXITCODE"
exit $EXITCODE
}
args() {
[[ $# -eq 0 ]] && HELP=1 && return 1
for arg in "$@"; do
case "$arg" in
## options
-debug) DEBUG=1 ;;
-help) HELP=1 ;;
-timer) TIMER=1 ;;
-verbose) VERBOSE=1 ;;
-*)
error "Unknown option \"$arg\""
EXITCODE=1 && return 0
;;
## subcommands
clean) CLEAN=1 ;;
compile) COMPILE=1 ;;
doc) DOC=1 ;;
help) HELP=1 ;;
lint) LINT=1 ;;
run) COMPILE=1 && RUN=1 ;;
*)
error "Unknown subcommand \"$arg\""
EXITCODE=1 && return 0
;;
esac
done
debug "Options : TIMER=$TIMER VERBOSE=$VERBOSE"
debug "Subcommands: CLEAN=$CLEAN COMPILE=$COMPILE DOC=$DOC HELP=$HELP LINT=$LINT RUN=$RUN"
debug "Variables : DART_HOME=$DART_HOME"
debug "Variables : GIT_HOME=$GIT_HOME"
# See http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
[[ $TIMER -eq 1 ]] && TIMER_START=$(date +"%s")
}
help() {
cat << EOS
Usage: $BASENAME { <option> | <subcommand> }
Options:
-debug print commands executed by this script
-timer print total execution time
-verbose print progress messages
Subcommands:
clean delete generated files
compile compile Dart source files
doc generate HTML documentation
help print this help message
lint analyze Dart source files with dart analyze
run execute the generated program "${TARGET_FILE/$ROOT_DIR\//}"
EOS
}
clean() {
if [[ -d "$TARGET_DIR" ]]; then
if [[ $DEBUG -eq 1 ]]; then
debug "Delete directory \"$TARGET_DIR\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Delete directory \"${TARGET_DIR/$ROOT_DIR\//}\"" 1>&2
fi
rm -rf "$TARGET_DIR"
[[ $? -eq 0 ]] || ( EXITCODE=1 && return 0 )
fi
}
lint() {
local dart_opts=
[[ $DEBUG -eq 1 ]] && dart_opts="--verbose $dart_opts"
if [[ $DEBUG -eq 1 ]]; then
debug "\"$DART_CMD\" analyze \"$(mixed_path $SOURCE_DIR)/main/dart/\" $dart_opts"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Analyze Dart source files in directory \"${mixed_path $SOURCE_DIR}\"" 1>&2
fi
eval "\"$DART_CMD\" analyze \"$(mixed_path $SOURCE_DIR)/main/dart\" $dart_opts"
if [[ $? -ne 0 ]]; then
error "Failed to analyze Dart source files in directory \"${SOURCE_DIR/$ROOT_DIR\//}"
cleanup 1
fi
}
compile() {
[[ -d "$TARGET_DIR" ]] || mkdir -p "$TARGET_DIR"
local is_required="$(action_required "$TARGET_FILE" "$SOURCE_DIR/main/dart/" "*.dart")"
[[ $is_required -eq 0 ]] && return 1
local source_files=
local n=0
for f in $(find "$SOURCE_DIR/main/dart/main.dart" -type f -name "*.dart" 2>/dev/null); do
source_files="$source_files\"$f\" "
n=$((n + 1))
done
if [[ $n -eq 0 ]]; then
warning "No Dart source file found"
return 1
fi
local s=; [[ $n -gt 1 ]] && s="s"
local n_files="$n Dart source file$s"
local dart_opts="--enable-experiment=macros --output \"$TARGET_FILE\""
if [[ $DEBUG -eq 1 ]]; then
debug "$DART_CMD pub upgrade"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Resolve project dependencies" 1>&2
fi
eval "\"$DART_CMD\" pub upgrade"
if [[ $? -ne 0 ]]; then
error "Failed to resolve project dependencies"
cleanup 1
fi
[[ $DEBUG -eq 1 ]] && dart_opts="--verbose $dart_opts"
if [[ $DEBUG -eq 1 ]]; then
debug "$DART_CMD compile exe $dart_opts $source_files"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Compile $n_files to directory \"${TARGET_DIR/$ROOT_DIR\//}\"" 1>&2
fi
eval "\"$DART_CMD\" compile exe $dart_opts $source_files"
if [[ $? -ne 0 ]]; then
error "Failed to compile $n_files to directory \"${TARGET_DIR/$ROOT_DIR\//}\""
cleanup 1
fi
}
action_required() {
local timestamp_file=$1
local search_path=$2
local search_pattern=$3
local latest=
for f in $(find $search_path -name $search_pattern 2>/dev/null); do
[[ $f -nt $latest ]] && latest=$f
done
if [[ -z "$latest" ]]; then
## Do not compile if no source file
echo 0
elif [[ ! -f "$timestamp_file" ]]; then
## Do compile if timestamp file doesn't exist
echo 1
else
## Do compile if timestamp file is older than most recent source file
local timestamp=$(stat -c %Y $timestamp_file)
[[ $timestamp_file -nt $latest ]] && echo 1 || echo 0
fi
}
mixed_path() {
if [[ -x "$CYGPATH_CMD" ]]; then
$CYGPATH_CMD -am $1
elif [[ $(($mingw + $msys)) -gt 0 ]]; then
echo $1 | sed 's|/|\\\\|g'
else
echo $1
fi
}
## See https://github.com/dart-lang/dartdoc#dartdoc_optionsyaml
doc() {
[[ -d "$TARGET_DOCS_DIR" ]] || mkdir -p "$TARGET_DOCS_DIR"
local dartdoc_opts="--output=\"$TARGET_DOCS_DIR\""
[[ $DEBUG -eq 1 ]] && dartdoc_opts="--verbose $dartdoc_opts"
if [[ $DEBUG -eq 1 ]]; then
debug "\"$DART_CMD\" doc $dartdoc_opts \"$SOURCE_MAIN_DIR/\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Generate documentation into directory \"${TARGET_DOCS_DIR/$ROOT_DIR\//}\"" 1>&2
fi
eval "\"$DART_CMD\" doc $dartdoc_opts \"$SOURCE_MAIN_DIR/\""
if [[ $? -ne 0 ]]; then
error "Failed to generate documentation into directory \"${TARGET_DOCS_DIR/$ROOT_DIR\//}\""
cleanup 1
fi
}
run() {
if [[ ! -f "$TARGET_FILE" ]]; then
error "Main program \"${TARGET_FILE/$ROOT_DIR\//}\" not found"
cleanup 1
fi
if [[ $DEBUG -eq 1 ]]; then
debug "Execute program \"$TARGET_FILE\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Execute program \"${TARGET_FILE/$ROOT_DIR\//}\"" 1>&2
fi
eval "$TARGET_FILE"
if [[ $? -ne 0 ]]; then
error "Failed to execute program \"${TARGET_FILE/$ROOT_DIR\//}\""
cleanup 1
fi
}
run_tests() {
echo "tests"
}
##############################################################################
## Environment setup
BASENAME=$(basename "${BASH_SOURCE[0]}")
EXITCODE=0
ROOT_DIR="$(getHome)"
SOURCE_DIR="$ROOT_DIR/src"
SOURCE_MAIN_DIR="$SOURCE_DIR/main/dart"
TARGET_DIR="$ROOT_DIR/target"
TARGET_DOCS_DIR="$TARGET_DIR/docs"
## We refrain from using `true` and `false` which are Bash commands
## (see https://man7.org/linux/man-pages/man1/false.1.html)
CLEAN=0
COMPILE=0
DEBUG=0
DOC=0
HELP=0
LINT=0
RUN=0
TEST=0
TIMER=0
VERBOSE=0
COLOR_START="[32m"
COLOR_END="[0m"
cygwin=0
mingw=0
msys=0
darwin=0
case "$(uname -s)" in
CYGWIN*) cygwin=1 ;;
MINGW*) mingw=1 ;;
MSYS*) msys=1 ;;
Darwin*) darwin=1
esac
unset CYGPATH_CMD
PSEP=":"
if [[ $(($cygwin + $mingw + $msys)) -gt 0 ]]; then
CYGPATH_CMD="$(which cygpath 2>/dev/null)"
PSEP=";"
[[ -n "$DART_HOME" ]] && DART_HOME="$(mixed_path $DART_HOME)"
[[ -n "$GIT_HOME" ]] && GIT_HOME="$(mixed_path $GIT_HOME)"
DIFF_CMD="$GIT_HOME/usr/bin/diff.exe"
LOCAL_REPO="$(mixed_path $USERPROFILE)/.m2/repository"
else
DIFF_CMD="$(which diff)"
LOCAL_REPO="$HOME/.m2/repository"
fi
if [[ ! -x "$DART_HOME/bin/dart" ]]; then
error "Dart installation not found"
cleanup 1
fi
DART_CMD="$DART_HOME/bin/dart"
PROJECT_NAME="$(basename $ROOT_DIR)"
PROJECT_URL="github.com/$USER/dart-examples"
PROJECT_VERSION="1.0-SNAPSHOT"
APP_NAME="userJson"
TARGET_FILE="$TARGET_DIR/$APP_NAME.exe"
args "$@"
[[ $EXITCODE -eq 0 ]] || cleanup 1
##############################################################################
## Main
[[ $HELP -eq 1 ]] && help && cleanup
if [[ $CLEAN -eq 1 ]]; then
clean || cleanup 1
fi
if [[ $LINT -eq 1 ]]; then
lint || cleanup 1
fi
if [[ $COMPILE -eq 1 ]]; then
compile || cleanup 1
fi
if [[ $DOC -eq 1 ]]; then
doc || cleanup 1
fi
if [[ $RUN -eq 1 ]]; then
run || cleanup 1
fi
if [[ $TEST -eq 1 ]]; then
run_tests || cleanup 1
fi
cleanup