Skip to content

Commit 563e132

Browse files
dongcarljanus
authored andcommitted
guix: Add source-able bash prelude and utils
1 parent 1a5b21e commit 563e132

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Diff for: contrib/guix/libexec/prelude.bash

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
export LC_ALL=C
3+
set -e -o pipefail
4+
5+
# shellcheck source=../../shell/realpath.bash
6+
source contrib/shell/realpath.bash
7+
8+
# shellcheck source=../../shell/git-utils.bash
9+
source contrib/shell/git-utils.bash
10+
11+
################
12+
# Required non-builtin commands should be invokable
13+
################
14+
15+
check_tools() {
16+
for cmd in "$@"; do
17+
if ! command -v "$cmd" > /dev/null 2>&1; then
18+
echo "ERR: This script requires that '$cmd' is installed and available in your \$PATH"
19+
exit 1
20+
fi
21+
done
22+
}
23+
24+
check_tools cat env readlink dirname basename git
25+
26+
################
27+
# We should be at the top directory of the repository
28+
################
29+
30+
same_dir() {
31+
local resolved1 resolved2
32+
resolved1="$(bash_realpath "${1}")"
33+
resolved2="$(bash_realpath "${2}")"
34+
[ "$resolved1" = "$resolved2" ]
35+
}
36+
37+
if ! same_dir "${PWD}" "$(git_root)"; then
38+
cat << EOF
39+
ERR: This script must be invoked from the top level of the git repository
40+
41+
Hint: This may look something like:
42+
env FOO=BAR ./contrib/guix/guix-<blah>
43+
44+
EOF
45+
exit 1
46+
fi
47+
48+
################
49+
# Set common variables
50+
################
51+
52+
VERSION="${VERSION:-$(git_head_version)}"
53+
DISTNAME="${DISTNAME:-bitcoin-${VERSION}}"
54+
55+
version_base_prefix="${PWD}/guix-build-"
56+
VERSION_BASE="${version_base_prefix}${VERSION}" # TOP
57+
58+
DISTSRC_BASE="${DISTSRC_BASE:-${VERSION_BASE}}"
59+
60+
OUTDIR_BASE="${OUTDIR_BASE:-${VERSION_BASE}/output}"

Diff for: contrib/shell/git-utils.bash

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
git_root() {
4+
git rev-parse --show-toplevel 2> /dev/null
5+
}
6+
7+
git_head_version() {
8+
local recent_tag
9+
if recent_tag="$(git describe --exact-match HEAD 2> /dev/null)"; then
10+
echo "${recent_tag#v}"
11+
else
12+
git rev-parse --short=12 HEAD
13+
fi
14+
}

Diff for: contrib/shell/realpath.bash

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
3+
# Based on realpath.sh written by Michael Kropat
4+
# Found at: https://github.com/mkropat/sh-realpath/blob/65512368b8155b176b67122aa395ac580d9acc5b/realpath.sh
5+
6+
bash_realpath() {
7+
canonicalize_path "$(resolve_symlinks "$1")"
8+
}
9+
10+
resolve_symlinks() {
11+
_resolve_symlinks "$1"
12+
}
13+
14+
_resolve_symlinks() {
15+
_assert_no_path_cycles "$@" || return
16+
17+
local dir_context path
18+
if path=$(readlink -- "$1"); then
19+
dir_context=$(dirname -- "$1")
20+
_resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@"
21+
else
22+
printf '%s\n' "$1"
23+
fi
24+
}
25+
26+
_prepend_dir_context_if_necessary() {
27+
if [ "$1" = . ]; then
28+
printf '%s\n' "$2"
29+
else
30+
_prepend_path_if_relative "$1" "$2"
31+
fi
32+
}
33+
34+
_prepend_path_if_relative() {
35+
case "$2" in
36+
/* ) printf '%s\n' "$2" ;;
37+
* ) printf '%s\n' "$1/$2" ;;
38+
esac
39+
}
40+
41+
_assert_no_path_cycles() {
42+
local target path
43+
44+
target=$1
45+
shift
46+
47+
for path in "$@"; do
48+
if [ "$path" = "$target" ]; then
49+
return 1
50+
fi
51+
done
52+
}
53+
54+
canonicalize_path() {
55+
if [ -d "$1" ]; then
56+
_canonicalize_dir_path "$1"
57+
else
58+
_canonicalize_file_path "$1"
59+
fi
60+
}
61+
62+
_canonicalize_dir_path() {
63+
(cd "$1" 2>/dev/null && pwd -P)
64+
}
65+
66+
_canonicalize_file_path() {
67+
local dir file
68+
dir=$(dirname -- "$1")
69+
file=$(basename -- "$1")
70+
(cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
71+
}

0 commit comments

Comments
 (0)