-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
83 lines (73 loc) · 2.05 KB
/
common.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
#!/bin/bash
set -eo pipefail
# Get absolute path to repo root
REPO_ROOT=$(git rev-parse --show-toplevel || (cd "$(dirname "${BASH_SOURCE[0]}")" && pwd))
export REPO_ROOT
# Check platform:
# BSD (Mac) and GNU (Linux & Git for Windows) coreutils implementations
# have some differences for example in the available command line options.
case "$(uname -s)" in
"Darwin")
export BASH_PLATFORM="mac"
;;
"MINGW"*)
export BASH_PLATFORM="windows"
;;
*)
export BASH_PLATFORM="linux"
;;
esac
export BASH_PLATFORM
# Print a message with green color
print_green() {
printf "\e[1;49;32m%s\e[0m\n" "$1"
}
# Print a message with magenta color
print_magenta() {
printf "\e[1;49;35m%s\e[0m\n" "$1"
}
# Print a message with red color
print_red() {
printf "\e[1;49;31m%s\e[0m\n" "$1"
}
# Print a message with yellow color
print_yellow() {
printf "\e[1;49;33m%s\e[0m\n" "$1"
}
# Print an error and exit the program
print_error_and_exit() {
print_red "ERROR: $1"
# use exit code if given as second argument, otherwise default to 1
exit "${2:-1}"
}
# Print usage and exit the program. An optional error message can be given as well.
print_usage_and_exit() {
if [ $# -eq 1 ]; then
print_red "ERROR: $1"
fi
if [ -z "$USAGE" ]; then
print_red "No usage text provided in variable USAGE"
else
echo "$USAGE"
fi
# use exit code if given as second argument, otherwise default to 1
exit "${2:-1}"
}
# Get all Rust executable names from Cargo.toml
get_rust_executable_names() {
local executables
executables=$(awk -F'=' '/\[\[bin\]\]/,/name/ {if($1 ~ /name/) print $2}' Cargo.toml | tr -d ' "')
if [ "$BASH_PLATFORM" = "windows" ]; then
executables=$(echo "$executables" | sed 's/$/.exe/')
fi
echo "$executables"
}
# if DRYRUN or DRY_RUN has been set, only print commands instead of running them
run_command() {
if [ "$DRY_RUN" = true ] || [ "$DRYRUN" = true ]; then
echo "DRYRUN: $*"
else
echo "Running: $*"
"$@"
fi
}