-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
107 lines (93 loc) · 2.54 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/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 in bold
print_bold() {
printf "\e[1;49;37m%s\e[0m\n" "$1"
}
# Format text with green color
green() {
printf "\e[1;49;32m%s\e[0m" "$1"
}
# Print a message with green color
print_green() {
printf "\e[1;49;32m%s\e[0m\n" "$1"
}
# Print a message with cyan color
print_cyan() {
printf "\e[1;49;36m%s\e[0m\n" "$1"
}
# Print a message with magenta color
print_magenta() {
printf "\e[1;49;35m%s\e[0m\n" "$1"
}
# Format text with red color
red() {
printf "\e[1;49;31m%s\e[0m" "$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 the Rust executable name from Cargo.toml
get_rust_executable_name() {
local executable
executable=$(awk -F'=' '/\[\[bin\]\]/,/name/ {if($1 ~ /name/) print $2}' Cargo.toml | tr -d ' "')
# If no name under [[bin]], get the package name
if [ -z "$executable" ]; then
executable=$(awk -F'=' '/\[package\]/,/name/ {if($1 ~ /name/) print $2}' Cargo.toml | tr -d ' "')
fi
if [ "$BASH_PLATFORM" = windows ]; then
executable="${executable}.exe"
fi
echo "$executable"
}
# 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
}