-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·244 lines (214 loc) · 6.66 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
#!/usr/bin/env bash
show_usage () {
cat << EOF
usage: $(basename "$0") [ -h | --long-options]
--help shows this message
build modes:
--native use local build toolchain
--docker use docker as the build toolchain [default]
build options:
--clear removes build artifacts
components:
--all builds all courses
umb-cs622-2015f builds Theory of Formal Languages
umb-cs624-2015s builds Analysis of Algorithms
umb-cs630-2014f builds Database Management Systems
umb-cs637-2015s builds Database Backed Websites
umb-cs638-2016s builds Applied Machine Learning
umb-cs671-2015s builds Machine Learning
umb-cs680-2015f builds Object-Oriented Design and Programming
umb-cs681-2016s builds Object Oriented Software Development
EOF
}
# configure bash environment
set -o errexit -o pipefail -o noclobber -o nounset
# declare project structure
ARG_VERBOSE=0
DIR_PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# initialize logging
__log () {
if [ $# -lt 3 ]; then return 1; fi
printf "\e[1;$2m%-10s\e[m %s\\n" "$1" "${@:3}"
}
log_debug () { [[ "${ARG_VERBOSE}" -ne 1 ]] || __log 'debug' '0' "$@"; }
log_info () { __log 'info' '34' "$@"; }
log_warning () { __log 'warn' '33' "$@"; }
log_error () { __log 'error' '31' "$@"; return 1; }
# this script expects bash v4.0 or higher
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
log_warning "you are using bash version ${BASH_VERSION}"
log_error "this script requires bash version 4.0 or higher"
fi
# helper functions
count_keys_if_set () {
if [ $# -ne 1 ]; then return 1; fi
local sum=0
declare -A arr=${1#*=}
for k in "${!arr[@]}"; do
if [ "${arr[$k]}" -eq 1 ]; then
sum=$((sum + 1))
fi
done
echo $sum
}
is_command_installed () {
if [ $# -eq 0 ]; then return 1; fi
for cmd in "$@"; do
if ! hash "$cmd" 2>/dev/null; then
log_warning "command $cmd is not installed"
return 1
fi
done
return 0
}
docker_image_exists () {
local out
out=$(docker image inspect "$1" >/dev/null 2>&1 && echo "0" || echo "1")
[ "$out" == "0" ]
}
remove_docker_image_if_exists () {
if [ $# -ne 1 ]; then return 1; fi
if docker_image_exists "$1"; then
log_info "removing docker image $1"
docker rmi "$1" > /dev/null
log_info "removed docker image $1"
fi
}
# more specific helper functions
validate_arguments () {
if [ $# -ne 3 ]; then return 1; fi
declare -A modes=${1#*=}
declare -A components=${3#*=}
local count_modes
local count_components
count_modes="$(count_keys_if_set "$(declare -p modes)")"
count_components="$(count_keys_if_set "$(declare -p components)")"
if [ "$count_modes" -lt 1 ]; then
log_error "specify a build mode"
fi
if [ "$count_modes" -gt 1 ]; then
log_error "only one build mode can be specified"
fi
if [ "$count_components" -eq 0 ]; then
log_error "specify at least one component"
fi
}
build_components () {
if [ $# -ne 3 ]; then return 1; fi
declare -A modes=${1#*=}
declare -A opts=${2#*=}
declare -A components=${3#*=}
if [ "${#components[@]}" -ne 0 ]; then
log_debug "components:"
for J in "${!components[@]}"; do
if [ "${components[$J]}" -eq 1 ]; then
log_debug " - $J"
fi
done
fi
if [ "${modes["native"]}" -eq 1 ]; then
if [ "${opts["clear"]}" -eq 1 ]; then
log_info "removing local artifacts"
for K in "${!components[@]}"; do
if [ "${components[$K]}" -eq 0 ]; then continue; fi
make -C "${DIR_PROJECT_ROOT}/${K}" clean
done
log_info "removed local artifacts"
else
log_info "using local build toolchain"
for K in "${!components[@]}"; do
if [ "${components[$K]}" -eq 0 ]; then continue; fi
log_info "building ${K}"
make -C "${DIR_PROJECT_ROOT}/${K}"
log_debug "built ${K}"
done
fi
elif [ "${modes["docker"]}" -eq 1 ]; then
if ! is_command_installed "docker"; then
log_error "docker is not installed"
fi
if [ "${opts["clear"]}" -eq 1 ]; then
for K in "${!components[@]}"; do
if [ "${components[$K]}" -eq 0 ]; then continue; fi
remove_docker_image_if_exists "ghorbanzade/${K}"
done
log_info "removed docker artifacts"
else
log_info "using docker build toolchain"
for K in "${!components[@]}"; do
if [ "${components[$K]}" -eq 0 ]; then continue; fi
local container="container-${K}"
local image="ghorbanzade/${K}"
log_info "building ${K}"
docker build -t "${image}" -f "${K}/Dockerfile" "${K}"
docker create --name "$container" "$image"
docker cp "${container}:/opt/build/${K}" "${DIR_PROJECT_ROOT}/build/${K}"
docker stop "${container}"
docker rm "${container}"
log_debug "built ${K}"
done
if [ "${opts["ci"]}" -eq 1 ]; then
docker image prune --force --filter label=stage=intermediate
fi
fi
fi
}
# start build process
ARG_HELP=0
declare -A COMPONENTS=(
["umb-cs622-2015f"]=0
["umb-cs624-2015s"]=0
["umb-cs630-2014f"]=0
["umb-cs637-2015s"]=0
["umb-cs638-2016s"]=0
["umb-cs671-2015s"]=0
["umb-cs680-2015f"]=0
["umb-cs681-2016s"]=0
)
declare -A BUILD_MODES=(
["docker"]=0
["native"]=0
)
declare -A BUILD_OPTS=(
["ci"]=0
["clear"]=0
)
for arg in "$@"; do
case $arg in
"-h" | "help" | "--help")
ARG_HELP=1
;;
"--all")
for K in "${!COMPONENTS[@]}"; do COMPONENTS[$K]=1; done
;;
"--ci")
BUILD_OPTS["ci"]=1
;;
"--clear")
BUILD_OPTS["clear"]=1
;;
"--docker")
BUILD_MODES["docker"]=1
;;
"--native")
BUILD_MODES["native"]=1
;;
esac
for K in "${!COMPONENTS[@]}"; do
if [ "$arg" == "$K" ]; then
COMPONENTS["$arg"]=1;
fi
done
done
if [[ ${ARG_HELP} -eq 1 ]]; then
show_usage
exit
fi
validate_arguments \
"$(declare -p BUILD_MODES)" \
"$(declare -p BUILD_OPTS)" \
"$(declare -p COMPONENTS)"
build_components \
"$(declare -p BUILD_MODES)" \
"$(declare -p BUILD_OPTS)" \
"$(declare -p COMPONENTS)"