-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.sh
85 lines (72 loc) · 1.67 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
84
85
#!/usr/bin/bash
set -eo pipefail
# Common shell functions and definitions
REPO_ROOT=$(git rev-parse --show-toplevel || (cd "$(dirname "${BASH_SOURCE[0]}")" && pwd))
export REPO_ROOT
# Check platform
case "$(uname -s)" in
"Darwin")
export BASH_PLATFORM="mac"
;;
"MINGW"*)
export BASH_PLATFORM="windows"
;;
*)
export BASH_PLATFORM="linux"
;;
esac
# BSD sed on MacOS works differently
if [ "$BASH_PLATFORM" = mac ]; then
export SED_COMMAND=(sed -i '')
else
export SED_COMMAND=(sed -i)
fi
# Print a message with red color
print_red() {
printf "\e[1;49;31m%s\e[0m\n" "$1"
}
# Print a message with green color
print_green() {
printf "\e[1;49;32m%s\e[0m\n" "$1"
}
# Print a message with yellow color
print_yellow() {
printf "\e[1;49;33m%s\e[0m\n" "$1"
}
# Print a message with magenta color
print_magenta() {
printf "\e[1;49;35m%s\e[0m\n" "$1"
}
print_error() {
print_red "ERROR: $1"
}
print_warn() {
print_yellow "WARNING: $1"
}
# Print an error and exit
print_error_and_exit() {
print_red "ERROR: $1"
# use exit code if given as argument, otherwise default to 1
exit "${2:-1}"
}
# Check Python is found on path and set PYTHON variable to it
check_and_set_python() {
if [ -n "$(command -v python3)" ]; then
PYTHON=$(which python3)
elif [ -n "$(command -v python)" ]; then
PYTHON=$(which python)
else
print_error_and_exit "Python not found in path"
fi
echo "$($PYTHON --version) from $PYTHON"
export PYTHON
}
# 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
}