Skip to content

Commit c17ebdd

Browse files
committed
feat: Add 'prune' subcommand
1 parent 1f22611 commit c17ebdd

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

completions/bpm.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
_bpm() {
22
local -ra listPreSubcommandOptions=(--help --version --global)
3-
local -ra listSubcommands=(add echo init link list package-path remove upgrade)
3+
local -ra listSubcommands=(add echo init link list package-path prune remove upgrade)
44

55
local -r currentWord="${COMP_WORDS[COMP_CWORD]}"
66

completions/bpm.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set cmd bpm
2-
set -l listSubcommands add echo init link list package-path remove upgrade
2+
set -l listSubcommands add echo init link list package-path prune remove upgrade
33

44
# Not only does this prevent appending completion properties to $cmd (we
55
# want to start from a completely new definition), it also removes incorrect

pkg/lib/cmd/bpm.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ main() {
6868
shift
6969
bpm-package-path "$@"
7070
;;
71+
prune)
72+
shift
73+
do-prune "$@"
74+
;;
7175
remove)
7276
shift
7377
do-remove "$@"

pkg/lib/commands/do-prune.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# shellcheck shell=bash
2+
3+
do-prune() {
4+
util.setup_mode
5+
6+
log.info "Pruning packages"
7+
8+
for file in "$BPM_INSTALL_BIN"/* "$BPM_INSTALL_MAN"/*/* "$BPM_INSTALL_COMPLETIONS"/{bash,zsh/{compsys,compctl},fish}/*; do
9+
local real_file=
10+
real_file="$(readlink "$file")"
11+
12+
if [[ "${real_file:0:1}" == / && -e "$real_file" ]]; then
13+
# The only valid symlinks 'bpm' creates are absolute paths
14+
# to an existing file
15+
continue
16+
fi
17+
18+
printf ' -> %s\n' "Unsymlinking broken symlink '$file'"
19+
unlink "$file"
20+
done
21+
}

pkg/lib/util/util.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ Subcommands:
310310
Installs a package from a local directory. These have a
311311
namespace of 'local'
312312
313+
prune
314+
Removes broken symlinks in the bins, completions, and man
315+
directories. This is usually only required if a package is
316+
force-removed
317+
313318
list [--simple] [--fetch] [package...]
314319
List installed packages
315320

tests/do-prune.bats

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bats
2+
3+
load 'util/init.sh'
4+
5+
@test "prune works for bin files" {
6+
local file="$BPM_INSTALL_BIN/file"
7+
8+
mkdir -p "$BPM_INSTALL_BIN"
9+
ln -s "/invalid" "$file"
10+
11+
assert [ -L "$file" ]
12+
13+
run do-prune
14+
15+
assert_success
16+
assert [ ! -L "$file" ]
17+
assert [ ! -e "$file" ]
18+
}
19+
20+
@test "prune works for man files" {
21+
local file="$BPM_INSTALL_MAN/man1/something.1"
22+
23+
mkdir -p "$BPM_INSTALL_MAN/man1"
24+
ln -s "/invalid" "$file"
25+
26+
assert [ -L "$file" ]
27+
28+
run do-prune
29+
30+
assert_success
31+
assert [ ! -L "$file" ]
32+
assert [ ! -e "$file" ]
33+
}
34+
35+
@test "prune works for completion files" {
36+
local file1="$BPM_INSTALL_COMPLETIONS/bash/something.bash"
37+
local file2="$BPM_INSTALL_COMPLETIONS/zsh/compsys/_something.zsh"
38+
39+
mkdir -p "$BPM_INSTALL_COMPLETIONS"/{bash,zsh/compsys}
40+
ln -s "/invalid" "$file1"
41+
ln -s "/invalid" "$file2"
42+
43+
assert [ -L "$file1" ]
44+
assert [ -L "$file2" ]
45+
46+
run do-prune
47+
48+
assert_success
49+
assert [ ! -L "$file1" ]
50+
assert [ ! -e "$file1" ]
51+
assert [ ! -L "$file2" ]
52+
assert [ ! -e "$file2" ]
53+
}
54+
55+
@test "prune works for relative files" {
56+
local file="$BPM_INSTALL_BIN/file"
57+
58+
mkdir -p "$BPM_INSTALL_BIN"
59+
ln -s "invalid" "$file"
60+
61+
assert [ -L "$file" ]
62+
63+
run do-prune
64+
65+
assert_success
66+
assert [ ! -L "$file" ]
67+
assert [ ! -e "$file" ]
68+
}

0 commit comments

Comments
 (0)