Skip to content

Commit b5c3076

Browse files
committed
refactor: Refactor bins and man plumbing to remove array appending
Now, the code executes the linking when it finds a file, rather than appending it to an array to symlink it all at the end
1 parent 64d844d commit b5c3076

File tree

5 files changed

+114
-97
lines changed

5 files changed

+114
-97
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Why not use `bpkg` or `Basher`? Because `bpm`...
4444
- Does not use a `package.json` that clobbers with NPM's `package.json` (bpkg)
4545
- Does not automatically invoke `make` commands on your behalf (bpkg)
4646
- Does not automatically source a `package.sh` for package configuration (basher)
47-
- Is able to install more repositories
47+
- Is able to install more repositories out-of-the-box
4848
- Respects the XDG Base Directory specification (bpkg)
4949
- Is faster (bpm considers exec and subshell creation overhead)
5050
- Has a _much_ improved help output (basher)
Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,28 @@
11
# shellcheck shell=bash
22

3-
# @description Get all executable scripts of repository automatically
4-
# @arg $1 Name of package
5-
auto-collect-bins() {
6-
declare -ga REPLIES=()
7-
8-
local package="$1"
9-
10-
local bins=()
11-
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
12-
bins=("$BPM_PACKAGES_PATH/$package"/bin/*)
13-
bins=("${bins[@]##*/}")
14-
bins=("${bins[@]/#/bin/}")
15-
else
16-
# TODO: ignore 'uninstall.sh', 'install.sh' scripts
17-
readarray -t bins < <(find "$BPM_PACKAGES_PATH/$package" -maxdepth 1 -mindepth 1 -perm -u+x -type f -or -type l)
18-
bins=("${bins[@]##*/}")
19-
fi
20-
21-
REPLIES=("${bins[@]}")
22-
}
23-
243
do-plumbing-link-bins() {
254
local package="$1"
265
ensure.nonZero 'package' "$package"
276
ensure.packageExists "$package"
287

298
log.info "Linking bin files for '$package'"
309

31-
local remove_extension=
10+
# We want this to be visible to the other functions
11+
declare -g remove_extension=
3212
local -a bins=()
3313

3414
local bpmTomlFile="$BPM_PACKAGES_PATH/$package/bpm.toml"
3515
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
3616

37-
# Get bin directories
3817
if [ -f "$bpmTomlFile" ]; then
3918
if util.get_toml_array "$bpmTomlFile" 'binDirs'; then
40-
local -a newBins=()
4119
for dir in "${REPLIES[@]}"; do
42-
newBins=("$BPM_PACKAGES_PATH/$package/$dir"/*)
43-
newBins=("${newBins[@]##*/}")
44-
newBins=("${newBins[@]/#/"$dir"/}")
20+
for file in "$BPM_PACKAGES_PATH/$package/$dir"/*; do
21+
symlink_binfile "$file"
22+
done
4523
done
46-
bins+=("${newBins[@]}")
4724
else
48-
auto-collect-bins "$package"
49-
bins=("${REPLIES[@]}")
25+
fallback_symlink_bins "$package"
5026
fi
5127
elif [ -f "$packageShFile" ]; then
5228
if util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'; then
@@ -55,25 +31,53 @@ do-plumbing-link-bins() {
5531

5632
if util.extract_shell_variable "$packageShFile" 'BINS'; then
5733
IFS=':' read -ra bins <<< "$REPLY"
34+
35+
for file in "${bins[@]}"; do
36+
symlink_binfile "$BPM_PACKAGES_PATH/$package/$file"
37+
done
5838
else
59-
auto-collect-bins "$package"
60-
bins=("${REPLIES[@]}")
39+
fallback_symlink_bins "$package"
6140
fi
6241
else
63-
auto-collect-bins "$package"
64-
bins=("${REPLIES[@]}")
42+
fallback_symlink_bins "$package"
43+
fi
44+
}
45+
46+
# @description Use heuristics to locate and symlink the bin files. This is ran when
47+
# the user does not supply any bin files/dirs with any config
48+
# @arg $1 package
49+
fallback_symlink_bins() {
50+
declare -ga REPLIES=()
51+
52+
local package="$1"
53+
54+
local bins=()
55+
56+
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
57+
for file in "$BPM_PACKAGES_PATH/$package"/bin/*; do
58+
symlink_binfile "$file"
59+
done
60+
else
61+
for file in "$BPM_PACKAGES_PATH/$package"/*; do
62+
if [ -x "$file" ]; then
63+
symlink_binfile "$file"
64+
fi
65+
done
6566
fi
67+
}
6668

67-
# Do linking for each bin file
68-
for bin in "${bins[@]}"; do
69-
local name="${bin##*/}"
69+
# @description Actually symlink the bin file to the correct location
70+
# @arg $1 The full path of the executable
71+
symlink_binfile() {
72+
local fullBinFile="$1"
7073

71-
if [[ "${remove_extension:-no}" == @(yes|true) ]]; then
72-
name="${name%%.*}"
73-
fi
74+
local binName="${fullBinFile##*/}"
75+
76+
if [[ "${remove_extension:-no}" == @(yes|true) ]]; then
77+
binName="${binName%%.*}"
78+
fi
7479

75-
mkdir -p "$BPM_INSTALL_BIN"
76-
ln -sf "$BPM_PACKAGES_PATH/$package/$bin" "$BPM_INSTALL_BIN/$name"
77-
chmod +x "$BPM_INSTALL_BIN/$name"
78-
done
80+
mkdir -p "$BPM_INSTALL_BIN"
81+
ln -sf "$fullBinFile" "$BPM_INSTALL_BIN/$binName"
82+
chmod +x "$BPM_INSTALL_BIN/$binName"
7983
}

pkg/lib/commands/do-plumbing-link-completions.sh

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
# shellcheck shell=bash
22

3-
auto-collect-completion_files() {
4-
declare -ga REPLIES=()
5-
6-
local package="$1"
7-
8-
local -a bash_completion_files=() zsh_completion_files=()
9-
10-
for completionDir in completion completions contrib/completion contrib/completions; do
11-
local completionDir="$BPM_PACKAGES_PATH/$package/$completionDir"
12-
13-
# TODO: optimize
14-
for target in "$completionDir"/?*.{sh,bash}; do
15-
bash_completion_files+=("$target")
16-
done
17-
18-
for target in "$completionDir"/?*.zsh; do
19-
zsh_completion_files+=("$target")
20-
done
21-
done
22-
23-
REPLIES1=("${bash_completion_files[@]}")
24-
REPLIES2=("${zsh_completion_files[@]}")
25-
}
26-
273
do-plumbing-link-completions() {
284
local package="$1"
295
ensure.nonZero 'package' "$package"
@@ -100,3 +76,27 @@ do-plumbing-link-completions() {
10076
fi
10177
done
10278
}
79+
80+
auto-collect-completion_files() {
81+
declare -ga REPLIES=()
82+
83+
local package="$1"
84+
85+
local -a bash_completion_files=() zsh_completion_files=()
86+
87+
for completionDir in completion completions contrib/completion contrib/completions; do
88+
local completionDir="$BPM_PACKAGES_PATH/$package/$completionDir"
89+
90+
# TODO: optimize
91+
for target in "$completionDir"/?*.{sh,bash}; do
92+
bash_completion_files+=("$target")
93+
done
94+
95+
for target in "$completionDir"/?*.zsh; do
96+
zsh_completion_files+=("$target")
97+
done
98+
done
99+
100+
REPLIES1=("${bash_completion_files[@]}")
101+
REPLIES2=("${zsh_completion_files[@]}")
102+
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
# shellcheck shell=bash
22

3-
# @arg $1 The man file to symlink
4-
link-man-n-file() {
5-
local fullManFile="$1"
6-
7-
local manFile="${fullManFile##*/}"
8-
9-
local regex="\.([1-9])\$"
10-
if [[ "$fullManFile" =~ $regex ]]; then
11-
local n="${BASH_REMATCH[1]}"
12-
mkdir -p "$BPM_INSTALL_MAN/man$n"
13-
ln -sf "$fullManFile" "$BPM_INSTALL_MAN/man$n/$manFile"
14-
fi
15-
}
16-
17-
# @description Automatically locate man files in the project and symlink them.
18-
# This is used when no directories are given in any config files
19-
auto-symlink-man() {
20-
for file in "$BPM_PACKAGES_PATH/$package"/{,man/}*; do
21-
link-man-n-file "$file"
22-
done
23-
}
24-
253
do-plumbing-link-man() {
264
local package="$1"
275
ensure.nonZero 'package' "$package"
@@ -42,17 +20,39 @@ do-plumbing-link-man() {
4220
# 2. A directory (man1, man2), that contains man files
4321
for file in "$fullDir"/*; do
4422
if [ -f "$file" ]; then
45-
link-man-n-file "$file"
23+
symlink-manfile "$file"
4624
elif [ -d "$file" ]; then
4725
:
4826
# TODO: Implement 2
4927
fi
5028
done
5129
done
5230
else
53-
auto-symlink-man "$package"
31+
fallback_symlink_mans "$package"
5432
fi
5533
else
56-
auto-symlink-man "$package"
34+
fallback_symlink_mans "$package"
35+
fi
36+
}
37+
38+
# @description Use heuristics to locate and symlink man files. This is ran when
39+
# the user does not supply any man files/dirs with any config
40+
fallback_symlink_mans() {
41+
for file in "$BPM_PACKAGES_PATH/$package"/{,man/}*; do
42+
symlink-manfile "$file"
43+
done
44+
}
45+
46+
# @arg $1 The man file to symlink
47+
symlink-manfile() {
48+
local fullManFile="$1"
49+
50+
local manFile="${fullManFile##*/}"
51+
52+
local regex="\.([1-9])\$"
53+
if [[ "$fullManFile" =~ $regex ]]; then
54+
local n="${BASH_REMATCH[1]}"
55+
mkdir -p "$BPM_INSTALL_MAN/man$n"
56+
ln -sf "$fullManFile" "$BPM_INSTALL_MAN/man$n/$manFile"
5757
fi
5858
}

pkg/lib/commands/do-plumbing-unlink-bins.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do-plumbing-unlink-bins() {
77
log.info "Unlinking bin files for '$package'"
88

99
local -a bins=()
10-
local REMOVE_EXTENSION=
10+
local remove_extension=
1111

1212
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1313
if [ -f "$packageShFile" ]; then
@@ -16,7 +16,7 @@ do-plumbing-unlink-bins() {
1616
fi
1717

1818
if util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'; then
19-
REMOVE_EXTENSION="$REPLY"
19+
remove_extension="$REPLY"
2020
fi
2121
fi
2222

@@ -26,6 +26,18 @@ do-plumbing-unlink-bins() {
2626
bins=("${bins[@]##*/}")
2727
bins=("${bins[@]/#/bin/}")
2828
else
29+
for file in "$BPM_PACKAGES_PATH/$package"/*; do
30+
if [ -x "$file" ]; then
31+
local name="${file##*/}"
32+
33+
if "${remove_extension:-false}"; then
34+
name="${name%%.*}"
35+
fi
36+
37+
rm -f "$BPM_INSTALL_BIN/$name"
38+
fi
39+
done
40+
2941
readarray -t bins < <(find "$BPM_PACKAGES_PATH/$package" -maxdepth 1 -perm -u+x -type f -or -type l)
3042
bins=("${bins[@]##*/}")
3143
fi
@@ -34,10 +46,11 @@ do-plumbing-unlink-bins() {
3446
for bin in "${bins[@]}"; do
3547
local name="${bin##*/}"
3648

37-
if "${REMOVE_EXTENSION:-false}"; then
49+
if "${remove_extension:-false}"; then
3850
name="${name%%.*}"
3951
fi
4052

53+
# TODO: unlink?
4154
rm -f "$BPM_INSTALL_BIN/$name"
4255
done
4356
}

0 commit comments

Comments
 (0)