-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlib.sh
217 lines (192 loc) · 4.07 KB
/
lib.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
#!/usr/bin/env bash
# Support library for tools
## realpath [path]
##
## Poor man's substitute for the real realpath
if ! command -v realpath >/dev/null 2>&1; then
realpath() {
python -c "import os; import sys; print(os.path.realpath(sys.argv[1]))" "$1"
}
fi
## is-var <varname>
##
## Check if a variable is set
is-var() {
test -n "${!1+z}"
}
## error [message]..
##
## Pretty print error messages
error() {
echo $'\e[31merror\e[m:' "${BASH_SOURCE[1]}(${BASH_LINENO[1]}) ${FUNCNAME[1]}:" "$@" >&2
}
## os
##
## Print the OS name in lower case.
os() {
if [[ $OS == Windows_NT ]]; then
echo windows
else
uname | tr '[:upper:]' '[:lower:]'
fi
}
## arch_from_triple <triple>
##
## Print architecture from a target triplet.
arch_from_triple() {
local triple=$1
local arch=${triple%%-*}
case "$arch" in
mingw32)
arch=i386
;;
esac
echo "$arch"
}
## ncpu
##
## Print number of logical CPUs, 1 is printed if couldn't be found
ncpu() {
local ncpu=
case "$(os)" in
windows)
ncpu=$NUMBER_OF_PROCESSORS
;;
darwin)
ncpu=$(sysctl -n hw.ncpu)
;;
linux)
ncpu=$(nproc)
;;
esac
if [[ $ncpu -le 0 ]]; then
ncpu=1
fi
echo "$ncpu"
}
## nativepath <path>
##
## Translate the given unix path to a native path.
nativepath() {
if [[ $# -eq 0 ]]; then
error "a path must be given"
return 1
fi
if [[ -z "$1" ]]; then
error "path must not be empty"
return 1
fi
case "$(os)" in
windows)
sed -e 's|^/\(.\)|\1:|' -e 's|/|\\\\|g' <<< "$1"
;;
*)
echo "$1"
;;
esac
}
## pushenv [-n] (<variable name> [<value>])..
##
## Push the specified environment variable to be used with the next step in a
## job pipeline.
##
## If -n is passed, the value will be left as-is and won't be quoted.
pushenv() {
if [[ $# -eq 0 ]]; then
error "a variable name must be passed"
return 1
fi
local quote=true
OPTIND=1
while getopts 'n' curopt; do
case "$curopt" in
'n')
quote=false
;;
*)
return 1
;;
esac
done
shift $((OPTIND - 1))
local printNotice=true
local envfile=$PWD/environment
local formatStr='export %s=%s\n'
if $quote; then
formatStr='export %s=%q\n'
fi
while [[ $# -gt 0 ]]; do
local name=$1
local value=$2
if [[ $value == *$'\n'* ]]; then
error "variable value must not contain newline"
return 1
fi
# The format string changes depending on the options passed.
# shellcheck disable=SC2059
if ! printf "$formatStr" "$name" "$value" >> "$envfile"; then
echo "Could not write environmental settings to $envfile"
return 1
fi
if $printNotice; then
echo "Environmental settings are appended to $envfile"
printNotice=false
fi
shift 2 || shift $#
done
}
## pushpath <path>..
##
## prepend the given values to PATH for the next step in a job pipeline.
pushpath() {
if [[ $# -eq 0 ]]; then
error "a path must be passed"
return 1
fi
declare -a _pushpath_path
local snippet=false
while [[ $# -gt 0 ]]; do
if [[ -z "$1" ]]; then
error "path must not be empty"
return 1
fi
local path
path=$(realpath "$1")
if [[ $1 == *$'\n'* || $1 == *':'* ]]; then
error "path must not contain newline or ':' (colon)"
return 1
fi
snippet=true
_pushpath_path=( "$path" "${_pushpath_path[@]}" )
shift 1
done
if $snippet; then
# This is intentional, we want the PATH to be expanded at source time.
# shellcheck disable=SC2016
pushenv -n PATH "$(IFS=': '; printf '%q${PATH:+:$PATH}' "${_pushpath_path[*]}")"
fi
}
## fold [desc]..
##
## Start output fold with description `desc`
fold() {
if is-var GITHUB_ACTIONS; then
echo "::group::$*"
fi
}
## endfold
##
## End the last output fold
endfold() {
if is-var GITHUB_ACTIONS; then
echo "::endgroup::"
fi
}
## tolowercase <string>
##
## Convert string to lower case because CI bash
## might not support the built-in case changing
## parameter expansion
tolowercase() {
echo "$(tr [A-Z] [a-z] <<< "${1?}")"
}