aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/ci.sh
blob: e633875b05e368d77acf1fcf3da073fa96db92ae (plain)
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
#!/bin/bash
# shellcheck disable=SC2001,SC2181,SC1117

################################################ SETUP ################################################
dep_build_core=(
	libasound2-dev
	libevdev-dev
	liblua5.3-dev
	libola-dev
	libjack-jackd2-dev
	python3-dev
	libssl-dev
	build-essential
	pkg-config
	git
)

dep_build_osx=(
	ola
	lua
	openssl@1.1
	jack
	python3
)

dep_build_win=(
	mingw-w64
)

dep_build_debian=(
	git-buildpackage
	debhelper
)

exitcode="0"

############################################## FUNCTIONS ##############################################

ARGS(){
	for i in "$@"; do
		case "$i" in
			--target=*|-t=*)
				TARGETS="${i#*=}"
			;;
			--deploy)
				deploy="1"
			;;
			--deps)
				install_deps="1"
			;;
			-v|--verbose)
				verbose="1"
			;;
			-af|--allow-failure)
				allow_failure="1"
			;;
			-h|--help|*)
				print_help
				exit "0"
			;;
		esac
		shift
	done
	[[ -z $TARGETS ]] && print_help && printf "\nNo target specified!\n" && exit "1"	# If no target(s) are specified exit.
}

print_help() {
	printf "Usage: %s [OPTIONS]\n\n" "$0"
	printf -- "-t=<argument>, <argument>\t--target=<argument>, <argument>\n\n"
	printf -- "--deploy\tPack releases to the ./deployment/\$target directory.\n"
	printf -- "--deps\t\tCheck and install all dependencies needed for the specified target prior to target run.\n"
	printf -- "-af, --allow-failure\tAlways exit with code 0.\n"
	printf -- "-v, --verbose\tEnables detailed log output.\n\n"
	printf "Valid test targets are: \t\"check-spelling\" - \"1\", \"check-codespelling\" - \"2\", \"analyze-complexity\" - \"3\", \"analyze-shellscript\" - \"4\", \"stats\" - \"5\".\n"
	printf "Valid build targets are: \t\"build-linux\" - \"10\", \"build-windows\" - \"11\", \"build-debian\" - \"12\".\n"
	printf "Valid dependency install targets are: \t\"deps-linux\", \"deps-windows\", \"deps-debian\", \"deps-osx\" \"deps-tests\", \"deps-all\".\n\n"
}

deps_apt(){
	start_apt update -y -qq > /dev/null || error_handler "There was an error doing apt update."
	for dependency in "$@"; do
		if [ "$(dpkg-query -W -f='${Status}' "$dependency" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
			deps+=("$dependency")   # Add not installed dependency to the "to be installed array".
		else
			[[ -n $verbose ]] && printf "%s already installed!\n" "$dependency"   # If the dependency is already installed print it.
		fi
	done

	if [ ! "${#deps[@]}" -ge "1" ]; then	# If nothing needs to get installed don't start apt.
		[[ -n $verbose ]] && echo "All dependencies are fulfilled."	# Dependency array empty! Not running apt!
	else
		[[ -z $verbose ]] && echo "Starting dependency installation."
		[[ -n $verbose ]] && echo "Then following dependencies are going to be installed:"	# Dependency array contains items. Running apt.
		[[ -n $verbose ]] && echo "${deps[@]}" | sed 's/ /, /g'
		start_apt install -y -qq --no-install-suggests --no-install-recommends "${deps[@]}" > /dev/null || error_handler "There was an error doing dependency installation!"
	fi
	[[ -n $verbose ]] && printf "\n"
}

start_apt(){
	i="0"
	if command -v fuser &> /dev/null; then
		while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do
			[ "$i" -eq "0" ] && printf "\nWaiting for other software managers to finish"
			[ "$i" -le "16" ] && printf "." # Print a max of 16 dots if waiting.
			((i=i+1))
			sleep "1s"
		done
		[ "$i" -ge "1" ] && printf "ready!\n"
	fi
	DEBIAN_FRONTEND=noninteractive apt-get "$@"
}

deps_brew(){
	# 'brew install' sometimes returns non-zero for some arcane reason. 
	for dependency in "$@"; do
		brew install "$dependency"
	done
	brew link --overwrite python
}

# Build targets and corresponding deployment.

build-linux(){
	[[ -n $install_deps ]] && deps_apt "${dep_build_core[@]}"
	make full
}

build-osx(){
	# OpenSSL is not a proper install due to some Apple bull, so provide additional locations via the environment...
	# Additionally, newer versions of this "recipe" seem to use the name 'openssl@1.1' instead of plain 'openssl' and there seems to be
	# no way to programmatically get the link and include paths. Genius! Hardcoding the new version for the time being...
	export CFLAGS="$CFLAGS -I/usr/local/opt/openssl@1.1/include"
	export LDFLAGS="$LDFLAGS -L/usr/local/opt/openssl@1.1/lib"
	make full
}

build-windows(){
	[[ -n $install_deps ]] && deps_apt "${dep_build_core[@]}" "${dep_build_win[@]}"
	# Download libraries to link with for Windows
	wget "https://downloads.sourceforge.net/project/luabinaries/5.3.5/Windows%20Libraries/Dynamic/lua-5.3.5_Win64_dllw6_lib.zip" -O lua53.zip
	unzip lua53.zip lua53.dll
	make windows
	make -C backends lua.dll
}

build-debian(){
	[[ -n $install_deps ]] && deps_apt "${dep_build_core[@]}" "${dep_build_debian[@]}"
	git checkout debian/master
	gbp buildpackage
}

build-linux-deploy(){
	#printf "\nLinux Deployment started..\n"
	mkdir -p ./deployment/linux/backends
	mkdir -p ./deployment/linux/docs
	cp ./midimonster ./deployment/linux/
	cp ./backends/*.so ./deployment/linux/backends/
	cp ./monster.cfg ./deployment/linux/monster.cfg
	cp ./backends/*.md ./deployment/linux/docs/
	cp -r ./configs ./deployment/linux/
	cd ./deployment/linux || error_handler "Error doing cd to ./deployment"
	filename="midimonster-$(git describe)-$OS.tgz"
	touch "$filename" && tar --exclude=*.tgz -czf "$filename" "./"
	find . ! -iname "*.zip" ! -iname "*.tgz" -delete
}

build-windows-deploy(){
	#printf "\nWindows Deployment started..\n"
	mkdir -p ./deployment/windows/backends
	mkdir -p ./deployment/windows/docs
	strip midimonster.exe backends/*.dll	# Strip the Windows binaries as they become huge quickly.
	cp ./midimonster.exe ./deployment/windows/
	cp ./backends/*.dll ./deployment/windows/backends/
	cp ./backends/*.dll.disabled ./deployment/windows/backends/
	cp ./monster.cfg ./deployment/windows/monster.cfg
	cp ./backends/*.md ./deployment/windows/docs/
	cp -r ./configs ./deployment/windows/
	cd ./deployment/windows || error_handler "Error doing cd to ./deployment/windows"
	zip -r "./midimonster-$(git describe)-windows.zip" "./"
	find . ! -iname "*.zip" ! -iname "*.tgz" -delete
}

build-debian-deploy(){
	#printf "\nDebian Package Deployment started..\n"
	mkdir -p ./deployment/debian/
	cp ./*.deb ./deployment/debian/
}

# Tests

ckeck-spelling(){		# Check spelling.
	[[ -n $install_deps ]] && deps_apt "lintian"
	spellcheck_files=$(find . -type f | grep -v ".git/")	# Create list of files to be spellchecked.
	sl_results=$(xargs spellintian 2>&1 <<< "$spellcheck_files")	# Run spellintian to find spelling errors
	sl_errors=$(wc -l <<< "$sl_results")
	sl_errors_dups=$( (grep -c "\(duplicate word\)") <<< "$sl_results")
	sl_errors_nodups=$( (grep -cv "\(duplicate word\)") <<< "$sl_results")

	if [ "$sl_errors" -gt "1" ]; then
		printf "Spellintian found %s errors (%s spelling, %s duplicate words):\n\n" "$sl_errors" "$sl_errors_nodups" "$sl_errors_dups"
		printf "%s\n\n" "$sl_results"
		exitcode=1
	else
		printf "Spellintian reports no errors\n"
	fi
}

check-codespelling(){	# Check code for common misspellings.
	[[ -n $install_deps ]] && deps_apt "codespell"
	spellcheck_files=$(find . -type f | grep -v ".git/")	# Create list of files to be spellchecked.
	cs_results=$(xargs codespell --quiet 2 <<< "$spellcheck_files" 2>&1)
	cs_errors=$(wc -l <<< "$cs_results")
	if [ "$cs_errors" -gt "1" ]; then
		printf "Codespell found %s errors:\n\n" "$cs_errors"
		printf "%s\n\n" "$cs_results"
		exitcode=1
	else
		printf "Codespell reports no errors\n"
	fi
}

analyze-complexity(){	# code complexity analyser.
	[[ -n $install_deps ]] && deps_apt "python3" "python3-pip"
	if [ -z "$(which ~/.local/bin/lizard)" ]; then
		printf "Installing lizard...\n"
		pip3 install lizard >/dev/null
	fi
	printf "Running lizard for code complexity analysis\n"
	~/.local/bin/lizard ./
	if [ "$?" -ne "0" ]; then
		exitcode=1
	fi
}

analyze-shellscript(){	# Shellscript analysis tool.
	[[ -n $install_deps ]] && deps_apt "shellcheck"
	printf "Running shellcheck:\n"
	shell_files="$(find . -type f -iname \*.sh)"
	xargs shellcheck -Cnever -s bash <<< "$shell_files"
	if [ "$?" -ne "0" ]; then
		exitcode=1
	fi
}

stats(){				# Code statistics.
	[[ -n $install_deps ]] && deps_apt "cloc"
	printf "Code statistics:\n"
	cloc ./
}

target_queue(){
	printf "\n"
	IFS=',|.' read -ra Queue <<< "$TARGETS"
	for i in "${Queue[@]}"; do
		case "$i" in
			check-spelling|1)
				ckeck-spelling
			;;
			check-codespelling|2)
				check-codespelling
			;;
			analyze-complexity|3)
				analyze-complexity
			;;
			analyze-shellscript|4)
				analyze-shellscript
			;;
			stats|5)
				stats
			;;
			build-linux|10)
				OS="linux"
				build-linux
				[[ -n $deploy ]] && build-linux-deploy
			;;
			build-windows|build-win|11)
				build-windows
				[[ -n $deploy ]] && build-windows-deploy
			;;
			build-debian|build-deb|12)
				build-debian
				[[ -n $deploy ]] && build-debian-deploy
			;;
			build-osx|13)
				OS="osx"
				build-osx
				[[ -n $deploy ]] && build-linux-deploy
			;;
			deps-linux)
				# Target to install all needed dependencies for linux builds.
				deps_apt "${dep_build_core[@]}"
			;;
			deps-windows|deps-win)
				# Target to install all needed dependencies for windows builds.
				deps_apt "${dep_build_core[@]}" "${dep_build_win[@]}"
			;;
			deps-debian|deps-deb)
				# Target to install all needed dependencies for debian packaging.
				deps_apt "${dep_build_core[@]}" "${dep_build_debian[@]}"
			;;
			deps-osx)
				# Target to install all needed dependencies for osx.
				printf "\nNot implemented yet!\n"
				deps_brew "${dep_build_osx[@]}"
			;;
			deps-tests)
				deps_apt "lintian" "codespell" "python3" "python3-pip" "shellcheck" "cloc"
				# Install lizard if not found.
				if [ -z "$(which ~/.local/bin/lizard)" ]; then
					pip3 install lizard >/dev/null
				fi
			;;
			deps-all)
				# Target to install all needed dependencies for this ci script.
				deps_apt "${dep_build_core[@]}" "${dep_build_win[@]}" "${dep_build_debian[@]}" "lintian" "codespell" "python3" "python3-pip" "shellcheck" "cloc"
			;;
			*)
				printf "Target '%s' not valid!\n" "$i"
			;;
		esac
		printf "\n"
	done
}

error_handler(){
	[[ -n $1 ]] && printf "\n%s\n" "$1"
	printf "\nAborting"
	for i in {1..3}; do sleep 0.3s && printf "." && sleep 0.2s; done
	printf "\n"
	exit "1"
}

################################################ Main #################################################
trap error_handler SIGINT SIGTERM

ARGS "$@"	# Parse arguments.
target_queue	# Start requestet targets.

# Allow failure handler.
[[ -z $allow_failure ]] && exit "$exitcode"
exit "0"