forked from mbautin/brew-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linuxbrew-common.sh
executable file
·71 lines (62 loc) · 2.64 KB
/
linuxbrew-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
#@IgnoreInspection BashAddShebang
# Copyright (c) YugaByte, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations
# under the License.
#
set -euo pipefail
if [[ $BASH_SOURCE == $0 ]]; then
echo "$BASH_SOURCE must be sourced, not executed" >&2
exit 1
fi
declare -i -r ABS_PATH_LIMIT=85
get_brew_link() {
local brew_dirname="linuxbrew-$(date +%Y%m%dT%H%M%S)"
local brew_link="$(realpath .)/$brew_dirname"
local len=${#brew_link}
if [[ $len -gt $ABS_PATH_LIMIT ]]; then
echo "Linuxbrew link absolute path should be no more than $ABS_PATH_LIMIT bytes, but actual" \
"length is $len bytes: $brew_link" >&2
exit 1
fi
echo "$brew_link"
}
readonly YB_LINUXBREW_BUILD_ROOT=$( cd "${BASH_SOURCE%/*}" && pwd )
# Prepend 'x' symbols to source path up to specified length.
# Parameters:
# path - source path.
# len - required output path length. Optional parameter, if absent uses $ABS_PATH_LIMIT.
get_fixed_length_path() {
local path="$1"
local len="${2:-$ABS_PATH_LIMIT}"
# Generate a path of a fixed length (up to a certain maximum length).
local p="abcdefghijklmnopqrstuvwxyz0123456789"
echo "$path-$(echo "$p$p$p$p$p" )" | cut -c-$len
}
# Escape special characters in source string, so it can be used with sed as simple string pattern.
# https://stackoverflow.com/a/28783790/461529
# https://stackoverflow.com/a/29613573/461529
get_escaped_sed_re() {
# Every character except ^ is placed in its own character set [...] expression to treat it as a
# literal. Then, ^ characters are escaped as \^. Note, that []] also works, i.e. matches ]
# correctly.
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<$1
}
# Escape special characters in source string, so it can be used with sed as a replacement string.
# https://stackoverflow.com/a/28783790/461529
# https://stackoverflow.com/a/29613573/461529
get_escaped_sed_replacement_str() {
# The replacement string in a sed s/// command is not a regex, but it recognizes placeholders
# that refer to either the entire string matched by the regex (&) or specific capture-group
# results by index (\1, \2, ...), so these must be escaped, along with the (customary) regex
# delimiter, /.
local delim=${2:-/}
sed "s/[$delim&\]/\\\\&/g" <<<$1
}