Skip to content

Commit 646f644

Browse files
committed
refactor: plumbing completion
This is the last major refactor that needs to be done before adding future features
1 parent 9e589e5 commit 646f644

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

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

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,96 +7,86 @@ do-plumbing-link-completions() {
77

88
log.info "Linking completion files for '$package'"
99

10-
local -a bash_completion_files=() zsh_completion_files=()
11-
1210
local bpm_toml_file="$BPM_PACKAGES_PATH/$package/bpm.toml"
1311
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
1412

1513
# Get completion directories
1614
if [ -f "$bpm_toml_file" ]; then
1715
if util.get_toml_array "$bpm_toml_file" 'completionDirs'; then
18-
local -a newCompletions=()
19-
2016
for dir in "${REPLIES[@]}"; do
21-
newCompletions+=("$BPM_PACKAGES_PATH/$package/$dir"/*)
22-
newCompletions=("${newCompletions[@]/#"$BPM_PACKAGES_PATH/$package/"}")
17+
for file in "$BPM_PACKAGES_PATH/$package/$dir"/*; do
18+
local fileName="${file##*/}"
19+
20+
if [[ $fileName == *.@(sh|bash) ]]; then
21+
symlink_bash_completion_file "$file"
22+
elif [[ $fileName == *.zsh ]]; then
23+
symlink_zsh_completion_file "$file"
24+
fi
25+
done
2326
done
24-
25-
bash_completion_files+=("${newCompletions[@]}")
26-
zsh_completion_files+=("${newCompletions[@]}")
2727
else
28-
auto_collect_completion_files "$package"
29-
REPLIES1=("${REPLIES1[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
30-
REPLIES2=("${REPLIES2[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
31-
32-
bash_completion_files+=("${REPLIES1[@]}")
33-
zsh_completion_files+=("${REPLIES2[@]}")
28+
fallback_symlink_completions "$package" 'all'
3429
fi
3530
elif [ -f "$package_sh_file" ]; then
31+
local -a bash_completion_files=() zsh_completion_files=()
32+
3633
if util.extract_shell_variable "$package_sh_file" 'BASH_COMPLETIONS'; then
3734
IFS=':' read -ra bash_completion_files <<< "$REPLY"
35+
36+
for file in "${bash_completion_files[@]}"; do
37+
symlink_bash_completion_file "$BPM_PACKAGES_PATH/$package/$file"
38+
done
3839
else
39-
auto_collect_completion_files "$package"
40-
bash_completion_files+=("${REPLIES1[@]}")
40+
fallback_symlink_completions "$package" 'bash'
4141
fi
4242

4343
if util.extract_shell_variable "$package_sh_file" 'ZSH_COMPLETIONS'; then
4444
IFS=':' read -ra zsh_completion_files <<< "$REPLY"
45+
46+
for file in "${zsh_completion_files[@]}"; do
47+
symlink_zsh_completion_file "$BPM_PACKAGES_PATH/$package/$file"
48+
done
4549
else
46-
auto_collect_completion_files "$package"
47-
zsh_completion_files+=("${REPLIES2[@]}")
50+
fallback_symlink_completions "$package" 'zsh'
4851
fi
4952
else
50-
auto_collect_completion_files "$package"
51-
REPLIES1=("${REPLIES1[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
52-
REPLIES2=("${REPLIES2[@]/#/"$BPM_PACKAGES_PATH/$package/"}")
53-
bash_completion_files+=("${REPLIES1[@]}")
54-
zsh_completion_files+=("${REPLIES2[@]}")
53+
fallback_symlink_completions "$package" 'all'
5554
fi
56-
57-
# Do linking of completion files
58-
for completion in "${bash_completion_files[@]}"; do
59-
completion="${completion/#"$BPM_PACKAGES_PATH/$package/"}"
60-
61-
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
62-
ln -sf "$BPM_PACKAGES_PATH/$package/$completion" "$BPM_INSTALL_COMPLETIONS/bash/${completion##*/}"
63-
done
64-
65-
for completion in "${zsh_completion_files[@]}"; do
66-
completion="${completion/#"$BPM_PACKAGES_PATH/$package/"}"
67-
68-
local target="$BPM_PACKAGES_PATH/$package/$completion"
69-
70-
if grep -qs "^#compdef" "$target"; then
71-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
72-
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${completion##*/}"
73-
else
74-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compctl"
75-
ln -sf "$target" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${completion##*/}"
76-
fi
77-
done
7855
}
7956

80-
auto_collect_completion_files() {
81-
declare -ga REPLIES=()
82-
57+
fallback_symlink_completions() {
8358
local package="$1"
84-
85-
local -a bash_completion_files=() zsh_completion_files=()
59+
local type="$2"
8660

8761
for completion_dir in completion completions contrib/completion contrib/completions; do
88-
local completion_dir="$BPM_PACKAGES_PATH/$package/$completion_dir"
89-
90-
# TODO: optimize
91-
for target in "$completion_dir"/?*.{sh,bash}; do
92-
bash_completion_files+=("$target")
93-
done
94-
95-
for target in "$completion_dir"/?*.zsh; do
96-
zsh_completion_files+=("$target")
62+
for file in "$BPM_PACKAGES_PATH/$package/$completion_dir"/*; do
63+
local fileName="${file##*/}"
64+
65+
if [[ $fileName == *.@(sh|bash) ]] && [[ $type == all || $type == bash ]]; then
66+
symlink_bash_completion_file "$file"
67+
elif [[ $fileName == *.zsh ]] && [[ $type == all || $type == zsh ]]; then
68+
symlink_zsh_completion_file "$file"
69+
fi
9770
done
9871
done
72+
}
73+
74+
symlink_bash_completion_file() {
75+
local file="$1"
76+
77+
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
78+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/bash/${file##*/}"
79+
}
9980

100-
REPLIES1=("${bash_completion_files[@]}")
101-
REPLIES2=("${zsh_completion_files[@]}")
81+
symlink_zsh_completion_file() {
82+
local file="$1"
83+
84+
if grep -qs "^#compdef" "$file"; then
85+
# TODO: run mkdir outside of loop
86+
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
87+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
88+
else
89+
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compctl"
90+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
91+
fi
10292
}

0 commit comments

Comments
 (0)