Skip to content

Commit 64d844d

Browse files
committed
refactor: Cleanup 'plumbing-link-*' commands
1 parent 3134d9b commit 64d844d

File tree

7 files changed

+159
-105
lines changed

7 files changed

+159
-105
lines changed

pkg/lib/commands/do-plumbing-deps.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ do-plumbing-deps() {
1616
local bpmTomlFile="$BPM_PACKAGES_PATH/$package/bpm.toml"
1717
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1818

19-
if [ -f "$bpmTomlFile" ]; then
19+
if [ -f "$bpmTomlFile" ]; then
2020
if util.get_toml_array "$bpmTomlFile" 'dependencies'; then
2121
deps=("${REPLIES[@]}")
2222
fi
2323
elif [ -f "$packageShFile" ]; then
24-
util.extract_shell_variable "$packageShFile" 'DEPS'
25-
IFS=':' read -ra deps <<< "$REPLY"
24+
if util.extract_shell_variable "$packageShFile" 'DEPS'; then
25+
IFS=':' read -ra deps <<< "$REPLY"
26+
fi
2627
fi
2728

2829
log.info "Installing dependencies for '$package'"

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
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+
324
do-plumbing-link-bins() {
425
local package="$1"
526
ensure.nonZero 'package' "$package"
627
ensure.packageExists "$package"
728

829
log.info "Linking bin files for '$package'"
930

10-
local REMOVE_EXTENSION=
31+
local remove_extension=
1132
local -a bins=()
1233

1334
local bpmTomlFile="$BPM_PACKAGES_PATH/$package/bpm.toml"
1435
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1536

37+
# Get bin directories
1638
if [ -f "$bpmTomlFile" ]; then
1739
if util.get_toml_array "$bpmTomlFile" 'binDirs'; then
1840
local -a newBins=()
@@ -22,36 +44,34 @@ do-plumbing-link-bins() {
2244
newBins=("${newBins[@]/#/"$dir"/}")
2345
done
2446
bins+=("${newBins[@]}")
47+
else
48+
auto-collect-bins "$package"
49+
bins=("${REPLIES[@]}")
2550
fi
2651
elif [ -f "$packageShFile" ]; then
27-
util.extract_shell_variable "$packageShFile" 'BINS'
28-
IFS=':' read -ra bins <<< "$REPLY"
29-
30-
util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'
31-
REMOVE_EXTENSION="$REPLY"
32-
fi
52+
if util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'; then
53+
remove_extension="$REPLY"
54+
fi
3355

34-
# Either get bins from a 'bin' folder, or directly from the repository
35-
if ((${#bins} == 0)); then
36-
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
37-
bins=("$BPM_PACKAGES_PATH/$package"/bin/*)
38-
bins=("${bins[@]##*/}")
39-
bins=("${bins[@]/#/bin/}")
56+
if util.extract_shell_variable "$packageShFile" 'BINS'; then
57+
IFS=':' read -ra bins <<< "$REPLY"
4058
else
41-
readarray -t bins < <(find "$BPM_PACKAGES_PATH/$package" -maxdepth 1 -mindepth 1 -perm -u+x -type f -or -type l)
42-
bins=("${bins[@]##*/}")
59+
auto-collect-bins "$package"
60+
bins=("${REPLIES[@]}")
4361
fi
62+
else
63+
auto-collect-bins "$package"
64+
bins=("${REPLIES[@]}")
4465
fi
4566

46-
67+
# Do linking for each bin file
4768
for bin in "${bins[@]}"; do
4869
local name="${bin##*/}"
4970

50-
if "${REMOVE_EXTENSION:-false}"; then
71+
if [[ "${remove_extension:-no}" == @(yes|true) ]]; then
5172
name="${name%%.*}"
5273
fi
5374

54-
# TODO: test for chmod +x
5575
mkdir -p "$BPM_INSTALL_BIN"
5676
ln -sf "$BPM_PACKAGES_PATH/$package/$bin" "$BPM_INSTALL_BIN/$name"
5777
chmod +x "$BPM_INSTALL_BIN/$name"
Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,94 @@
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+
327
do-plumbing-link-completions() {
428
local package="$1"
529
ensure.nonZero 'package' "$package"
630
ensure.packageExists "$package"
731

832
log.info "Linking completion files for '$package'"
933

10-
local -a completions=()
11-
local -a bash_completions=() zsh_completions=()
34+
local -a bash_completion_files=() zsh_completion_files=()
1235

1336
local bpmTomlFile="$BPM_PACKAGES_PATH/$package/bpm.toml"
1437
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1538

39+
# Get completion directories
1640
if [ -f "$bpmTomlFile" ]; then
1741
if util.get_toml_array "$bpmTomlFile" 'completionDirs'; then
1842
local -a newCompletions=()
43+
1944
for dir in "${REPLIES[@]}"; do
20-
newCompletions=("$BPM_PACKAGES_PATH/$package/$dir"/*)
21-
newCompletions=("${newCompletions[@]##*/}")
22-
newCompletions=("${newCompletions[@]/#/"$dir"/}")
45+
newCompletions+=("$BPM_PACKAGES_PATH/$package/$dir"/*)
46+
newCompletions=("${newCompletions[@]/#"$BPM_PACKAGES_PATH/$package/"}")
2347
done
24-
completions+=("${newCompletions[@]}")
2548

26-
# TODO: quick hack
27-
bash_completions+=("${completions[@]}")
28-
zsh_completions+=("${completions[@]}")
49+
bash_completion_files+=("${newCompletions[@]}")
50+
zsh_completion_files+=("${newCompletions[@]}")
51+
else
52+
auto-collect-completion_files "$package"
53+
REPLIES1=("${REPLIES1[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
54+
REPLIES2=("${REPLIES2[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
55+
56+
bash_completion_files+=("${REPLIES1[@]}")
57+
zsh_completion_files+=("${REPLIES2[@]}")
2958
fi
3059
elif [ -f "$packageShFile" ]; then
31-
util.extract_shell_variable "$packageShFile" 'BASH_COMPLETIONS'
32-
IFS=':' read -ra bash_completions <<< "$REPLY"
60+
if util.extract_shell_variable "$packageShFile" 'BASH_COMPLETIONS'; then
61+
IFS=':' read -ra bash_completion_files <<< "$REPLY"
62+
else
63+
auto-collect-completion_files "$package"
64+
bash_completion_files+=("${REPLIES1[@]}")
65+
fi
3366

34-
util.extract_shell_variable "$packageShFile" 'ZSH_COMPLETIONS'
35-
IFS=':' read -ra zsh_completions <<< "$REPLY"
67+
if util.extract_shell_variable "$packageShFile" 'ZSH_COMPLETIONS'; then
68+
IFS=':' read -ra zsh_completion_files <<< "$REPLY"
69+
else
70+
auto-collect-completion_files "$package"
71+
zsh_completion_files+=("${REPLIES2[@]}")
72+
fi
73+
else
74+
auto-collect-completion_files "$package"
75+
REPLIES1=("${REPLIES1[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
76+
REPLIES2=("${REPLIES2[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
77+
bash_completion_files+=("${REPLIES1[@]}")
78+
zsh_completion_files+=("${REPLIES2[@]}")
3679
fi
3780

38-
for completion in "${bash_completions[@]}"; do
81+
# Do linking of completion files
82+
for completion in "${bash_completion_files[@]}"; do
83+
completion="${completion/#"$BPM_PACKAGES_PATH/$package/"}"
84+
3985
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
4086
ln -sf "$BPM_PACKAGES_PATH/$package/$completion" "$BPM_INSTALL_COMPLETIONS/bash/${completion##*/}"
4187
done
4288

43-
for completion in "${zsh_completions[@]}"; do
89+
for completion in "${zsh_completion_files[@]}"; do
90+
completion="${completion/#"$BPM_PACKAGES_PATH/$package/"}"
91+
4492
local target="$BPM_PACKAGES_PATH/$package/$completion"
4593

4694
if grep -qs "^#compdef" "$target"; then
@@ -51,27 +99,4 @@ do-plumbing-link-completions() {
5199
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${completion##*/}"
52100
fi
53101
done
54-
55-
# TODO: Move this inside -f packageshFile
56-
if [[ "${#bash_completions[@]}" -eq 0 && "${#zsh_completions[@]}" -eq 0 ]]; then
57-
for completionDir in completion completions contrib/completion contrib/completions; do
58-
local completionDir="$BPM_PACKAGES_PATH/$package/$completionDir"
59-
60-
for target in "$completionDir"/?*.{sh,bash}; do
61-
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
62-
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/bash/${completion##*/}"
63-
done
64-
65-
for target in "$completionDir"/?*.zsh; do
66-
if grep -qs "^#compdef" "$target"; then
67-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
68-
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${completion##*/}"
69-
else
70-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compctl"
71-
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${completion##*/}"
72-
fi
73-
done
74-
done
75-
fi
76-
77102
}
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
# shellcheck shell=bash
22

3-
# TODO: cleanup
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+
425
do-plumbing-link-man() {
526
local package="$1"
627
ensure.nonZero 'package' "$package"
728
ensure.packageExists "$package"
829

30+
# TODO: only print when actually linking
931
log.info "Linking man files for '$package'"
1032

11-
local -a mans=()
12-
1333
local bpmTomlFile="$BPM_PACKAGES_PATH/$package/bpm.toml"
1434

1535
if [ -f "$bpmTomlFile" ]; then
1636
if util.get_toml_array "$bpmTomlFile" 'manDirs'; then
17-
local -a newMans=()
1837
for dir in "${REPLIES[@]}"; do
19-
for manFile in "$BPM_PACKAGES_PATH/$package/$dir"/*; do
20-
manFile="${manFile##*/}"
21-
22-
local regex="\.([1-9])\$"
23-
if [[ "$manFile" =~ $regex ]]; then
24-
local n="${BASH_REMATCH[1]}"
25-
mkdir -p "$BPM_INSTALL_MAN/man$n"
26-
ln -sf "$BPM_PACKAGES_PATH/$package/$dir/$manFile" "$BPM_INSTALL_MAN/man$n/$manFile"
38+
local fullDir="$BPM_PACKAGES_PATH/$package/$dir"
39+
40+
# 'file' can be
41+
# 1. A man file
42+
# 2. A directory (man1, man2), that contains man files
43+
for file in "$fullDir"/*; do
44+
if [ -f "$file" ]; then
45+
link-man-n-file "$file"
46+
elif [ -d "$file" ]; then
47+
:
48+
# TODO: Implement 2
2749
fi
2850
done
2951
done
52+
else
53+
auto-symlink-man "$package"
3054
fi
31-
32-
return
55+
else
56+
auto-symlink-man "$package"
3357
fi
34-
35-
local files1=("$BPM_PACKAGES_PATH/$package"/man/*)
36-
files1=("${files1[@]##*/}")
37-
38-
local regex="\.([1-9])\$"
39-
for file in "${files1[@]}"; do
40-
if [[ "$file" =~ $regex ]]; then
41-
local n="${BASH_REMATCH[1]}"
42-
mkdir -p "$BPM_INSTALL_MAN/man$n"
43-
ln -sf "$BPM_PACKAGES_PATH/$package/man/$file" "$BPM_INSTALL_MAN/man$n/$file"
44-
fi
45-
done
46-
47-
local files2=("$BPM_PACKAGES_PATH/$package"/*)
48-
files2=("${files2[@]##*/}")
49-
50-
local regex="\.([1-9])\$"
51-
for file in "${files2[@]}"; do
52-
if [[ "$file" =~ $regex ]]; then
53-
local n="${BASH_REMATCH[1]}"
54-
mkdir -p "$BPM_INSTALL_MAN/man$n"
55-
ln -sf "$BPM_PACKAGES_PATH/$package/$file" "$BPM_INSTALL_MAN/man$n/$file"
56-
fi
57-
done
5858
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ do-plumbing-unlink-bins() {
1111

1212
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1313
if [ -f "$packageShFile" ]; then
14-
util.extract_shell_variable "$packageShFile" 'BINS'
15-
IFS=':' read -ra bins <<< "$REPLY"
14+
if util.extract_shell_variable "$packageShFile" 'BINS'; then
15+
IFS=':' read -ra bins <<< "$REPLY"
16+
fi
1617

17-
util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'
18-
REMOVE_EXTENSION="$REPLY"
18+
if util.extract_shell_variable "$packageShFile" 'REMOVE_EXTENSION'; then
19+
REMOVE_EXTENSION="$REPLY"
20+
fi
1921
fi
2022

2123
if ((${#bins} == 0)); then

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ do-plumbing-unlink-completions() {
1010

1111
local packageShFile="$BPM_PACKAGES_PATH/$package/package.sh"
1212
if [ -f "$packageShFile" ]; then
13-
util.extract_shell_variable "$packageShFile" 'BASH_COMPLETIONS'
14-
IFS=':' read -ra bash_completions <<< "$REPLY"
13+
if util.extract_shell_variable "$packageShFile" 'BASH_COMPLETIONS'; then
14+
IFS=':' read -ra bash_completions <<< "$REPLY"
15+
fi
1516

16-
util.extract_shell_variable "$packageShFile" 'ZSH_COMPLETIONS'
17-
IFS=':' read -ra zsh_completions <<< "$REPLY"
17+
if til.extract_shell_variable "$packageShFile" 'ZSH_COMPLETIONS'; then
18+
IFS=':' read -ra zsh_completions <<< "$REPLY"
19+
fi
1820
fi
1921

2022
for completion in "${bash_completions[@]}"; do

pkg/lib/util/util.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ util.extract_shell_variable() {
217217
REPLY="${REPLY%\'}"
218218
REPLY="${REPLY#\"}"
219219
REPLY="${REPLY%\"}"
220+
221+
return 0
220222
fi
221223
done < "$shellFile"
224+
225+
return 1
222226
}
223227

224228
util.show_help() {

0 commit comments

Comments
 (0)