Skip to content

Commit 2f47980

Browse files
committed
refactor: Move abstract unlink and link behind single function
1 parent 48d6009 commit 2f47980

10 files changed

+347
-365
lines changed

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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,8 @@
11
# shellcheck shell=bash
22

33
do-plumbing-link-bins() {
4-
local package="$1"
5-
ensure.non_zero 'package' "$package"
6-
ensure.package_exists "$package"
4+
local pkg="$1"
5+
ensure.non_zero 'pkg' "$pkg"
76

8-
log.info "Linking bin files for '$package'"
9-
10-
local remove_extensions=
11-
local -a bins=()
12-
13-
local bpm_toml_file="$BPM_PACKAGES_PATH/$package/bpm.toml"
14-
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
15-
16-
if [ -f "$bpm_toml_file" ]; then
17-
if util.get_toml_string "$bpm_toml_file" 'binRemoveExtensions'; then
18-
if [ "$REPLY" = 'yes' ]; then
19-
remove_extensions='yes'
20-
fi
21-
fi
22-
23-
if util.get_toml_array "$bpm_toml_file" 'binDirs'; then
24-
for dir in "${REPLIES[@]}"; do
25-
for file in "$BPM_PACKAGES_PATH/$package/$dir"/*; do
26-
symlink_binfile "$file" "$remove_extensions"
27-
done
28-
done
29-
30-
return
31-
fi
32-
33-
fallback_symlink_bins "$package" "$remove_extensions"
34-
elif [ -f "$package_sh_file" ]; then
35-
if util.extract_shell_variable "$package_sh_file" 'REMOVE_EXTENSION'; then
36-
remove_extensions="$REPLY"
37-
fi
38-
39-
if util.extract_shell_variable "$package_sh_file" 'BINS'; then
40-
IFS=':' read -ra bins <<< "$REPLY"
41-
42-
for file in "${bins[@]}"; do
43-
symlink_binfile "$BPM_PACKAGES_PATH/$package/$file" "$remove_extensions"
44-
done
45-
else
46-
fallback_symlink_bins "$package" "$remove_extensions"
47-
fi
48-
else
49-
fallback_symlink_bins "$package" "$remove_extensions"
50-
fi
51-
}
52-
53-
# @description Use heuristics to locate and symlink the bin files. This is ran when
54-
# the user does not supply any bin files/dirs with any config
55-
# @arg $1 package
56-
# @arg $2 Whether to remove extensions
57-
fallback_symlink_bins() {
58-
local package="$1"
59-
local remove_extensions="$2"
60-
61-
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
62-
for file in "$BPM_PACKAGES_PATH/$package"/bin/*; do
63-
symlink_binfile "$file" "$remove_extensions"
64-
done
65-
else
66-
for file in "$BPM_PACKAGES_PATH/$package"/*; do
67-
if [ -x "$file" ]; then
68-
symlink_binfile "$file" "$remove_extensions"
69-
fi
70-
done
71-
fi
72-
}
73-
74-
# @description Symlink the bin file to the correct location
75-
# @arg $1 The full path of the executable
76-
# @arg $2 Whether to remove extensions
77-
symlink_binfile() {
78-
local fullBinFile="$1"
79-
local remove_extensions="$2"
80-
81-
local binName="${fullBinFile##*/}"
82-
83-
if [[ "${remove_extensions:-no}" == @(yes|true) ]]; then
84-
binName="${binName%%.*}"
85-
fi
86-
87-
mkdir -p "$BPM_INSTALL_BIN"
88-
ln -sf "$fullBinFile" "$BPM_INSTALL_BIN/$binName"
89-
chmod +x "$BPM_INSTALL_BIN/$binName"
7+
abstract.bins 'link' "$pkg"
908
}
Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,8 @@
11
# shellcheck shell=bash
22

33
do-plumbing-link-completions() {
4-
local package="$1"
5-
ensure.non_zero 'package' "$package"
6-
ensure.package_exists "$package"
4+
local pkg="$1"
5+
ensure.non_zero 'pkg' "$pkg"
76

8-
log.info "Linking completion files for '$package'"
9-
10-
local bpm_toml_file="$BPM_PACKAGES_PATH/$package/bpm.toml"
11-
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
12-
13-
# Get completion directories
14-
if [ -f "$bpm_toml_file" ]; then
15-
if util.get_toml_array "$bpm_toml_file" 'completionDirs'; then
16-
for dir in "${REPLIES[@]}"; do
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
26-
done
27-
else
28-
fallback_symlink_completions "$package" 'all'
29-
fi
30-
elif [ -f "$package_sh_file" ]; then
31-
local -a bash_completion_files=() zsh_completion_files=()
32-
33-
if util.extract_shell_variable "$package_sh_file" 'BASH_COMPLETIONS'; then
34-
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
39-
else
40-
fallback_symlink_completions "$package" 'bash'
41-
fi
42-
43-
if util.extract_shell_variable "$package_sh_file" 'ZSH_COMPLETIONS'; then
44-
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
49-
else
50-
fallback_symlink_completions "$package" 'zsh'
51-
fi
52-
else
53-
fallback_symlink_completions "$package" 'all'
54-
fi
55-
}
56-
57-
fallback_symlink_completions() {
58-
local package="$1"
59-
local type="$2"
60-
61-
for completion_dir in completion completions contrib/completion contrib/completions; do
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
70-
done
71-
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-
}
80-
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
7+
abstract.completions 'link' "$pkg"
928
}
Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,8 @@
11
# shellcheck shell=bash
22

33
do-plumbing-link-man() {
4-
local package="$1"
5-
ensure.non_zero 'package' "$package"
6-
ensure.package_exists "$package"
4+
local pkg="$1"
5+
ensure.non_zero 'pkg' "$pkg"
76

8-
# TODO: only print when actually linking
9-
log.info "Linking man files for '$package'"
10-
11-
local bpm_toml_file="$BPM_PACKAGES_PATH/$package/bpm.toml"
12-
13-
if [ -f "$bpm_toml_file" ]; then
14-
if util.get_toml_array "$bpm_toml_file" 'manDirs'; then
15-
for dir in "${REPLIES[@]}"; do
16-
local full_dir="$BPM_PACKAGES_PATH/$package/$dir"
17-
18-
# 'file' can be
19-
# 1. A man file
20-
# 2. A directory (man1, man2), that contains man files
21-
for file in "$full_dir"/*; do
22-
if [ -f "$file" ]; then
23-
symlink_manfile "$file"
24-
elif [ -d "$file" ]; then
25-
for actualFile in "$file"/*; do
26-
if [ -f "$actualFile" ]; then
27-
symlink_manfile "$actualFile"
28-
fi
29-
done
30-
fi
31-
done
32-
done
33-
else
34-
fallback_symlink_mans "$package"
35-
fi
36-
else
37-
fallback_symlink_mans "$package"
38-
fi
39-
}
40-
41-
# @description Use heuristics to locate and symlink man files. This is ran when
42-
# the user does not supply any man files/dirs with any config
43-
fallback_symlink_mans() {
44-
for file in "$BPM_PACKAGES_PATH/$package"/{,man/}*; do
45-
if [ -f "$file" ]; then
46-
symlink_manfile "$file"
47-
elif [ -d "$file" ]; then
48-
for actualFile in "$file"/*; do
49-
if [ -f "$actualFile" ]; then
50-
symlink_manfile "$actualFile"
51-
fi
52-
done
53-
fi
54-
done
55-
}
56-
57-
# @arg $1 The man file to symlink
58-
symlink_manfile() {
59-
local full_man_file="$1"
60-
61-
local manFile="${full_man_file##*/}"
62-
63-
local regex="\.([1-9])\$"
64-
if [[ "$full_man_file" =~ $regex ]]; then
65-
local n="${BASH_REMATCH[1]}"
66-
mkdir -p "$BPM_INSTALL_MAN/man$n"
67-
ln -sf "$full_man_file" "$BPM_INSTALL_MAN/man$n/$manFile"
68-
fi
7+
abstract.mans 'link' "$pkg"
698
}
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,8 @@
11
# shellcheck shell=bash
22

33
do-plumbing-unlink-bins() {
4-
local package="$1"
5-
ensure.non_zero 'package' "$package"
4+
local pkg="$1"
5+
ensure.non_zero 'pkg' "$pkg"
66

7-
log.info "Unlinking bin files for '$package'"
8-
9-
local -a bins=()
10-
local remove_extension=
11-
12-
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
13-
if [ -f "$package_sh_file" ]; then
14-
if util.extract_shell_variable "$package_sh_file" 'BINS'; then
15-
IFS=':' read -ra bins <<< "$REPLY"
16-
fi
17-
18-
if util.extract_shell_variable "$package_sh_file" 'REMOVE_EXTENSION'; then
19-
remove_extension="$REPLY"
20-
fi
21-
fi
22-
23-
if ((${#bins} == 0)); then
24-
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
25-
bins=("$BPM_PACKAGES_PATH/$package"/bin/*)
26-
bins=("${bins[@]##*/}")
27-
bins=("${bins[@]/#/bin/}")
28-
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-
41-
readarray -t bins < <(find "$BPM_PACKAGES_PATH/$package" -maxdepth 1 -perm -u+x -type f -or -type l)
42-
bins=("${bins[@]##*/}")
43-
fi
44-
fi
45-
46-
for bin in "${bins[@]}"; do
47-
local name="${bin##*/}"
48-
49-
if "${remove_extension:-false}"; then
50-
name="${name%%.*}"
51-
fi
52-
53-
# TODO: unlink?
54-
rm -f "$BPM_INSTALL_BIN/$name"
55-
done
7+
abstract.bins 'unlink' "$pkg"
568
}
Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
11
# shellcheck shell=bash
22

33
do-plumbing-unlink-completions() {
4-
local package="$1"
5-
ensure.non_zero 'package' "$package"
4+
local pkg="$1"
5+
ensure.non_zero 'pkg' "$pkg"
66

7-
log.info "Unlinking completion files for '$package'"
8-
9-
local -a bash_completions=() zsh_completions=()
10-
11-
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
12-
if [ -f "$package_sh_file" ]; then
13-
if util.extract_shell_variable "$package_sh_file" 'BASH_COMPLETIONS'; then
14-
IFS=':' read -ra bash_completions <<< "$REPLY"
15-
fi
16-
17-
if util.extract_shell_variable "$package_sh_file" 'ZSH_COMPLETIONS'; then
18-
IFS=':' read -ra zsh_completions <<< "$REPLY"
19-
fi
20-
fi
21-
22-
for completion in "${bash_completions[@]}"; do
23-
rm -f "$BPM_INSTALL_COMPLETIONS/bash/${completion##*/}"
24-
done
25-
26-
for completion in "${zsh_completions[@]}"; do
27-
local target="$BPM_PACKAGES_PATH/$package/$completion"
28-
29-
if grep -sq "#compdef" "$target"; then
30-
rm -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${completion##*/}"
31-
else
32-
rm -f "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${completion##*/}"
33-
fi
34-
done
7+
abstract.completions 'unlink' "$pkg"
358
}

0 commit comments

Comments
 (0)