Skip to content

Commit 4608e43

Browse files
committed
refactor: Cleanup program entrypoints
1 parent 7565e59 commit 4608e43

File tree

6 files changed

+124
-183
lines changed

6 files changed

+124
-183
lines changed

pkg/bin/basalt

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,47 @@
11
#!/usr/bin/env bash
2-
# AUTOGENERATED by the 'util-Bash-generate-bins.sh' Glue action!
3-
# Do NOT edit!
42

5-
# https://github.com/bashup/realpaths
6-
realpath.location() {
7-
realpath.follow "$1"
8-
realpath.absolute "$REPLY" ".."
9-
}
3+
# https://github.com/ko1nksm/readlinkf
4+
readlinkf_posix() {
5+
[ "${1:-}" ] || return 1
6+
max_symlinks=40
7+
CDPATH='' # to avoid changing to an unexpected directory
108

11-
realpath.dirname() {
12-
REPLY=.
13-
! [[ $1 =~ /+[^/]+/*$|^//$ ]] || REPLY="${1%${BASH_REMATCH[0]}}"
14-
REPLY=${REPLY:-/}
15-
}
9+
target=$1
10+
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
11+
[ -d "${target:-/}" ] && target="$target/"
1612

17-
realpath.follow() {
18-
local target
19-
while [[ -L "$1" ]] && target=$(readlink -- "$1"); do
20-
realpath.dirname "$1"
21-
# Resolve relative to symlink's directory
22-
[[ $REPLY != . && $target != /* ]] && REPLY=$REPLY/$target || REPLY=$target
23-
# Break out if we found a symlink loop
24-
for target; do [[ $REPLY == "$target" ]] && break 2; done
25-
# Add to the loop-detect list and tail-recurse
26-
set -- "$REPLY" "$@"
27-
done
28-
REPLY="$1"
29-
}
13+
cd -P . 2>/dev/null || return 1
14+
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
15+
if [ ! "$target" = "${target%/*}" ]; then
16+
case $target in
17+
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
18+
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
19+
esac
20+
target=${target##*/}
21+
fi
3022

31-
realpath.absolute() {
32-
REPLY=$PWD; local eg=extglob; ! shopt -q $eg || eg=; ${eg:+shopt -s $eg}
33-
while (($#)); do case $1 in
34-
//|//[^/]*) REPLY=//; set -- "${1:2}" "${@:2}" ;;
35-
/*) REPLY=/; set -- "${1##+(/)}" "${@:2}" ;;
36-
*/*) set -- "${1%%/*}" "${1##${1%%/*}+(/)}" "${@:2}" ;;
37-
''|.) shift ;;
38-
..) realpath.dirname "$REPLY"; shift ;;
39-
*) REPLY="${REPLY%/}/$1"; shift ;;
40-
esac; done; ${eg:+shopt -u $eg}
41-
}
23+
if [ ! -L "$target" ]; then
24+
target="${PWD%/}${target:+/}${target}"
25+
printf '%s\n' "${target:-/}"
26+
return 0
27+
fi
4228

29+
link=$(ls -dl -- "$target" 2>/dev/null) || break
30+
target=${link#*" $target -> "}
31+
done
32+
return 1
33+
}
4334

44-
# Get the path to the 'lib' directory, which could be different depending on
45-
# the method of installation. If the user installed the program through their
46-
# distribution's package manager, the 'lib' files are in an extra subfolder
47-
# compared to an installation through Git (ex. Basher)
48-
realpath.location "${BASH_SOURCE[0]}"
49-
REPLY="${REPLY/%\/}"
50-
REPLY="${REPLY%/*}"
51-
if [ -d "$REPLY/lib/basalt" ]; then
52-
PROGRAM_LIB_DIR="$REPLY/lib/basalt"
53-
elif [ -d "$REPLY/lib" ]; then
54-
PROGRAM_LIB_DIR="$REPLY/lib"
55-
else
56-
printf '%s\n' "Error: Could not determine \$PROGRAM_LIB_DIR"
57-
exit 1
35+
if [ "$0" != "${BASH_SOURCE[0]}" ]; then
36+
printf '%s\n' "Error: basalt-package-init: Script must not be sourced"
37+
return 1
5838
fi
5939

40+
__basalt_dirname="$(readlinkf_posix "${BASH_SOURCE[0]}")"
41+
__basalt_dirname="${__basalt_dirname%/*}"
42+
__basalt_dirname="${__basalt_dirname%/*}"
43+
__basalt_dirname="${__basalt_dirname%/*}"
6044
# shellcheck disable=SC1091
61-
source "$PROGRAM_LIB_DIR/cmd/basalt.sh"
62-
basalt.main "$@"
45+
source "$__basalt_dirname/pkg/lib/cmd/basalt.sh"
46+
unset __basalt_dirname
47+
main.basalt "$@"

pkg/bin/basalt-package-init

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,47 @@
11
#!/usr/bin/env bash
2-
# AUTOGENERATED by the 'util-Bash-generate-bins.sh' Glue action!
3-
# Do NOT edit!
42

5-
# https://github.com/bashup/realpaths
6-
# TODO: get rid of this, and for 'basalt'
7-
realpath.location() {
8-
realpath.follow "$1"
9-
realpath.absolute "$REPLY" ".."
10-
}
3+
# https://github.com/ko1nksm/readlinkf
4+
readlinkf_posix() {
5+
[ "${1:-}" ] || return 1
6+
max_symlinks=40
7+
CDPATH='' # to avoid changing to an unexpected directory
118

12-
realpath.dirname() {
13-
REPLY=.
14-
! [[ $1 =~ /+[^/]+/*$|^//$ ]] || REPLY="${1%${BASH_REMATCH[0]}}"
15-
REPLY=${REPLY:-/}
16-
}
9+
target=$1
10+
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
11+
[ -d "${target:-/}" ] && target="$target/"
1712

18-
realpath.follow() {
19-
local target
20-
while [[ -L "$1" ]] && target=$(readlink -- "$1"); do
21-
realpath.dirname "$1"
22-
# Resolve relative to symlink's directory
23-
[[ $REPLY != . && $target != /* ]] && REPLY=$REPLY/$target || REPLY=$target
24-
# Break out if we found a symlink loop
25-
for target; do [[ $REPLY == "$target" ]] && break 2; done
26-
# Add to the loop-detect list and tail-recurse
27-
set -- "$REPLY" "$@"
28-
done
29-
REPLY="$1"
30-
}
13+
cd -P . 2>/dev/null || return 1
14+
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
15+
if [ ! "$target" = "${target%/*}" ]; then
16+
case $target in
17+
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
18+
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
19+
esac
20+
target=${target##*/}
21+
fi
3122

32-
realpath.absolute() {
33-
REPLY=$PWD; local eg=extglob; ! shopt -q $eg || eg=; ${eg:+shopt -s $eg}
34-
while (($#)); do case $1 in
35-
//|//[^/]*) REPLY=//; set -- "${1:2}" "${@:2}" ;;
36-
/*) REPLY=/; set -- "${1##+(/)}" "${@:2}" ;;
37-
*/*) set -- "${1%%/*}" "${1##${1%%/*}+(/)}" "${@:2}" ;;
38-
''|.) shift ;;
39-
..) realpath.dirname "$REPLY"; shift ;;
40-
*) REPLY="${REPLY%/}/$1"; shift ;;
41-
esac; done; ${eg:+shopt -u $eg}
42-
}
23+
if [ ! -L "$target" ]; then
24+
target="${PWD%/}${target:+/}${target}"
25+
printf '%s\n' "${target:-/}"
26+
return 0
27+
fi
4328

29+
link=$(ls -dl -- "$target" 2>/dev/null) || break
30+
target=${link#*" $target -> "}
31+
done
32+
return 1
33+
}
4434

45-
# Get the path to the 'lib' directory, which could be different depending on
46-
# the method of installation. If the user installed the program through their
47-
# distribution's package manager, the 'lib' files are in an extra subfolder
48-
# compared to an installation through Git (ex. Basher)
49-
realpath.location "${BASH_SOURCE[0]}"
50-
REPLY="${REPLY/%\/}"
51-
REPLY="${REPLY%/*}"
52-
if [ -d "$REPLY/lib/basalt" ]; then
53-
PROGRAM_LIB_DIR="$REPLY/lib/basalt"
54-
elif [ -d "$REPLY/lib" ]; then
55-
PROGRAM_LIB_DIR="$REPLY/lib"
56-
else
57-
# TODOO
58-
printf '%s\n' "Error: Could not determine \$PROGRAM_LIB_DIR"
59-
exit 1
35+
if [ "$0" != "${BASH_SOURCE[0]}" ]; then
36+
printf '%s\n' "Error: basalt-package-init: Script must not be sourced"
37+
return 1
6038
fi
6139

40+
__basalt_dirname="$(readlinkf_posix "${BASH_SOURCE[0]}")"
41+
__basalt_dirname="${__basalt_dirname%/*}"
42+
__basalt_dirname="${__basalt_dirname%/*}"
43+
__basalt_dirname="${__basalt_dirname%/*}"
6244
# shellcheck disable=SC1091
63-
source "$PROGRAM_LIB_DIR/cmd/basalt-package-init.sh"
64-
basalt-package-init.main "$@"
45+
source "$__basalt_dirname/pkg/lib/cmd/basalt-package-init.sh"
46+
unset __basalt_dirname
47+
main.basalt-package-init "$@"

pkg/lib/cmd/basalt-package-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# entry for the directory containing this file, which we execute to load the
1010
# aforementioned Basalt utility functions
1111

12-
basalt-package-init.main() {
12+
main.basalt-package-init() {
1313
# Set main variables (WET)
1414
local basalt_global_repo="${0%/*}"
1515
basalt_global_repo="${basalt_global_repo%/*}"; basalt_global_repo="${basalt_global_repo%/*}"

pkg/lib/cmd/basalt.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ shopt -s nullglob extglob
55
export LANG="C" LANGUAGE="C" LC_ALL="C"
66
export GIT_TERMINAL_PROMPT=0
77

8-
for f in "$PROGRAM_LIB_DIR"/{commands,plumbing,util}/?*.sh; do
8+
# shellcheck disable=SC2154
9+
for f in "$__basalt_dirname"/pkg/lib/{commands,plumbing,util}/?*.sh; do
910
source "$f"
1011
done
1112

12-
basalt.main() {
13+
main.basalt() {
1314
util.init_always
1415

1516
for arg; do case "$arg" in

pkg/lib/public/basalt-global.sh

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
# @brief Contains functions to be used anywhere (shell initialization, Basalt
55
# packages, etc.)
66

7-
__basalt_load_dosource() {
8-
:
9-
}
10-
117
basalt.load() {
128
local __basalt_flag_global='no'
139
local __basalt_flag_dry='no'
@@ -18,7 +14,6 @@ basalt.load() {
1814
shift
1915
;;
2016
--dry)
21-
# TODO: implement dry
2217
__basalt_flag_dry='yes'
2318
shift
2419
;;
@@ -41,7 +36,8 @@ basalt.load() {
4136
4237
Example:
4338
basalt-load --global 'github.com/rupa/z' 'z.sh'
44-
basalt-load 'github.com/ztombol/bats-assert'
39+
basalt-load --dry 'github.com/hyperupcall/bats-common-utils' 'load.bash'
40+
basalt-load 'github.com/bats-core/bats-assert' 'load.bash'
4541
EOF
4642
return
4743
;;
@@ -59,79 +55,56 @@ basalt.load() {
5955
return 1
6056
fi
6157

58+
local __basalt_is_nullglob=
59+
if shopt -q nullglob; then
60+
__basalt_is_nullglob='yes'
61+
else
62+
__basalt_is_nullglob='no'
63+
fi
64+
shopt -s nullglob
65+
66+
local -a __basalt_pkg_path_full_array=()
6267
if [ "$__basalt_flag_global" = 'yes' ]; then
63-
# TODO: Possible bug if nullglob is not set
64-
# TODO: should check to ensure first (zeroith) element is not empty
65-
local -a __basalt_pkg_path_full_array=("$BASALT_GLOBAL_DATA_DIR/global/.basalt/packages/$__basalt_pkg_path"@*)
66-
local __basalt_pkg_path_full="${__basalt_pkg_path_full_array[0]}"
67-
unset __basalt_pkg_path_full_array
68-
69-
if [ ! -d "$__basalt_pkg_path_full" ]; then
70-
printf '%s\n' "Error: basalt.load: Package '$__basalt_pkg_path' not installed globally"
71-
return 1
72-
fi
68+
__basalt_pkg_path_full_array=("$BASALT_GLOBAL_DATA_DIR/global/.basalt/packages/$__basalt_pkg_path@"*)
69+
else
70+
__basalt_pkg_path_full_array=("$BASALT_PACKAGE_DIR/.basalt/packages/$__basalt_pkg_path@"*)
71+
fi
7372

74-
if [ ! -f "$__basalt_pkg_path_full/$__basalt_file" ]; then
75-
printf '%s\n' "Error: basalt.load: File '$__basalt_file' not found in package '$__basalt_pkg_path'"
76-
return 1
77-
fi
73+
if ((${#__basalt_pkg_path_full_array[@]} > 1)); then
74+
printf '%s\n' "Error: basalt.load: Multiple versions of the package '$__basalt_pkg_path' exists"
75+
return 1
76+
fi
7877

79-
source "$__basalt_pkg_path_full/$__basalt_file"
78+
if [ "$__basalt_is_nullglob" = 'yes' ]; then
79+
shopt -s nullglob
8080
else
81-
# TODO: this should be removable
82-
# If 'package' is an absoluate path, we can skip to executing the file
83-
if [ "${__basalt_pkg_path::1}" = / ]; then
84-
if [ -f "$__basalt_pkg_path/load.bash" ]; then
85-
# Load package (WET)
86-
unset basalt_load
87-
source "$__basalt_pkg_path/load.bash"
88-
89-
if declare -f basalt_load &>/dev/null; then
90-
BASALT_PACKAGE_DIR="$__basalt_pkg_path" basalt_load
91-
unset basalt_load
92-
fi
93-
fi
94-
95-
return
96-
fi
81+
shopt -u nullglob
82+
fi
83+
84+
local __basalt_pkg_path_full="${__basalt_pkg_path_full_array[0]}"
9785

98-
# Assume can only have one version of a particular package for direct dependencies
99-
local __basalt_load_package_exists='no' __basalt_did_run_source='no'
100-
local __basalt_actual_pkg_path=
101-
for __basalt_actual_pkg_path in "$BASALT_PACKAGE_DIR/.basalt/packages/$__basalt_pkg_path"*; do
102-
if [ "$__basalt_load_package_exists" = yes ]; then
103-
printf '%s\n' "Error: basalt.load There are multiple direct dependencies for package '$__basalt_pkg_path'. This should not happen"
104-
return 1
105-
else
106-
__basalt_load_package_exists='yes'
107-
fi
108-
109-
if [ -n "$__basalt_file" ]; then
110-
if [ -f "$__basalt_actual_pkg_path/$__basalt_file" ]; then
111-
BASALT_PACKAGE_DIR="$__basalt_actual_pkg_path" source "$__basalt_actual_pkg_path/$__basalt_file"
112-
__basalt_did_run_source='yes'
113-
else
114-
printf '%s\n' "Error: basalt.load File '$__basalt_file' not found in package '$__basalt_pkg_path'"
115-
return 1
116-
fi
117-
elif [ -f "$__basalt_actual_pkg_path/load.bash" ]; then
118-
# Load package (WET)
119-
unset basalt_load
120-
source "$__basalt_actual_pkg_path/load.bash"
121-
122-
if declare -f basalt_load &>/dev/null; then
123-
BASALT_PACKAGE_DIR="$__basalt_actual_pkg_path" basalt_load
124-
unset basalt_load
125-
fi
126-
127-
__basalt_did_run_source='yes'
128-
fi
129-
done
130-
131-
if [ "$__basalt_did_run_source" = 'no' ]; then
132-
printf '%s\n' "Warning: basalt.load Nothing was sourced when calling 'basalt-load $*'. Does the package or file actually exist?"
86+
if [ -z "$__basalt_pkg_path_full" ] || [ ! -d "$__basalt_pkg_path_full" ]; then
87+
local __basalt_str='locally'
88+
if [ "$__basalt_flag_global" = 'yes' ]; then
89+
__basalt_str=" globally"
13390
fi
91+
printf '%s\n' "Error: basalt.load: Package '$__basalt_pkg_path' is not installed$__basalt_str"
92+
93+
return 1
94+
fi
13495

135-
unset __basalt_actual_pkg_path
96+
if [ ! -f "$__basalt_pkg_path_full/$__basalt_file" ]; then
97+
printf '%s\n' "Error: basalt.load: File '$__basalt_file' not found in package '$__basalt_pkg_path'"
98+
return 1
99+
fi
100+
101+
if [ "$__basalt_flag_dry" = 'yes' ]; then
102+
printf '%s\n' "Would have sourced file '$__basalt_pkg_path_full/$__basalt_file'"
103+
else
104+
if source "$__basalt_pkg_path_full/$__basalt_file"; then
105+
:
106+
else
107+
return $?
108+
fi
136109
fi
137110
}

pkg/lib/util/pkg.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pkg.install_packages() {
1111
ensure.nonzero 'project_dir'
1212
ensure.nonzero 'symlink_mode'
1313

14-
# TODO: save the state and have rollback feature
1514
local pkg=
1615
for pkg; do
1716
util.get_package_info "$pkg"

0 commit comments

Comments
 (0)