Skip to content

Commit 052e3fb

Browse files
committed
feat: Support binRemoveExtension key in 'bpm.toml'. Closes #32
This has the same behavior as the one in `package.sh`. Right now, the value is a string type, but later when the toml parser matures, it should, of course, be a boolean
1 parent 646f644 commit 052e3fb

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

docs/reference.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ Set the installation and package checkout prefix (default is `$BPM_ROOT/cellar`)
1616

1717
## `bpm.toml`
1818

19-
Use `bpm.toml` for more fine grained control of where `bpm` searches for binaries, completions, and man pages
19+
Use `bpm.toml` for more fine grained control of where `bpm` searches for binaries, completions, and man pages. _Note_ that arrays _must only_ span a single line (the line it was defined on) due to limitations with the toml parser. This should be lifted in the future
2020

2121
### `dependencies`
2222

2323
### `binDirs`
2424

25+
### `binRemoveExtensions`
26+
27+
TODO: make boolean option
28+
29+
Set to the string `yes` to remove extensions when linking bins
30+
31+
For example if a file in a repository was at `./bin/git-list-all-aliases.sh`, it would be linked and you would call it as `git-list-all-aliases`
32+
2533
### `completionDirs`
2634

2735
### `manDirs`

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,80 @@ do-plumbing-link-bins() {
77

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

10-
# We want this to be visible to the other functions
11-
declare -g remove_extension=
10+
local remove_extensions=
1211
local -a bins=()
1312

1413
local bpm_toml_file="$BPM_PACKAGES_PATH/$package/bpm.toml"
1514
local package_sh_file="$BPM_PACKAGES_PATH/$package/package.sh"
1615

1716
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+
1823
if util.get_toml_array "$bpm_toml_file" 'binDirs'; then
1924
for dir in "${REPLIES[@]}"; do
2025
for file in "$BPM_PACKAGES_PATH/$package/$dir"/*; do
21-
symlink_binfile "$file"
26+
symlink_binfile "$file" "$remove_extensions"
2227
done
2328
done
24-
else
25-
fallback_symlink_bins "$package"
29+
30+
return
2631
fi
32+
33+
fallback_symlink_bins "$package" "$remove_extensions"
2734
elif [ -f "$package_sh_file" ]; then
2835
if util.extract_shell_variable "$package_sh_file" 'REMOVE_EXTENSION'; then
29-
remove_extension="$REPLY"
36+
remove_extensions="$REPLY"
3037
fi
3138

3239
if util.extract_shell_variable "$package_sh_file" 'BINS'; then
3340
IFS=':' read -ra bins <<< "$REPLY"
3441

3542
for file in "${bins[@]}"; do
36-
symlink_binfile "$BPM_PACKAGES_PATH/$package/$file"
43+
symlink_binfile "$BPM_PACKAGES_PATH/$package/$file" "$remove_extensions"
3744
done
3845
else
39-
fallback_symlink_bins "$package"
46+
fallback_symlink_bins "$package" "$remove_extensions"
4047
fi
4148
else
42-
fallback_symlink_bins "$package"
49+
fallback_symlink_bins "$package" "$remove_extensions"
4350
fi
4451
}
4552

4653
# @description Use heuristics to locate and symlink the bin files. This is ran when
4754
# the user does not supply any bin files/dirs with any config
4855
# @arg $1 package
56+
# @arg $2 Whether to remove extensions
4957
fallback_symlink_bins() {
50-
declare -ga REPLIES=()
51-
5258
local package="$1"
53-
54-
local bins=()
59+
local remove_extensions="$2"
5560

5661
if [ -d "$BPM_PACKAGES_PATH/$package/bin" ]; then
5762
for file in "$BPM_PACKAGES_PATH/$package"/bin/*; do
58-
symlink_binfile "$file"
63+
symlink_binfile "$file" "$remove_extensions"
5964
done
6065
else
6166
for file in "$BPM_PACKAGES_PATH/$package"/*; do
6267
if [ -x "$file" ]; then
63-
symlink_binfile "$file"
68+
symlink_binfile "$file" "$remove_extensions"
6469
fi
6570
done
6671
fi
6772
}
6873

69-
# @description Actually symlink the bin file to the correct location
74+
# @description Symlink the bin file to the correct location
7075
# @arg $1 The full path of the executable
76+
# @arg $2 Whether to remove extensions
7177
symlink_binfile() {
7278
local fullBinFile="$1"
79+
local remove_extensions="$2"
7380

7481
local binName="${fullBinFile##*/}"
7582

76-
if [[ "${remove_extension:-no}" == @(yes|true) ]]; then
83+
if [[ "${remove_extensions:-no}" == @(yes|true) ]]; then
7784
binName="${binName%%.*}"
7885
fi
7986

tests/do-plumbing-link-bins.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ load 'util/init.sh'
131131
assert [ "$(readlink $BPM_INSTALL_BIN/exec2)" = "$BPM_PACKAGES_PATH/username/package/bin/exec2.sh" ]
132132
}
133133

134+
@test "remove extension if binRemoveExtensions is true in bpm.toml" {
135+
local package="username/package"
136+
137+
test_util.setup_pkg "$package"; {
138+
echo 'binRemoveExtensions = "yes"' > 'bpm.toml'
139+
mkdir bin
140+
touch 'bin/exec1'
141+
touch 'bin/exec2.sh'
142+
}; test_util.finish_pkg
143+
test_util.fake_clone "$package"
144+
145+
run do-plumbing-link-bins username/package
146+
147+
assert_success
148+
assert [ "$(readlink $BPM_INSTALL_BIN/exec1)" = "$BPM_PACKAGES_PATH/username/package/bin/exec1" ]
149+
assert [ "$(readlink $BPM_INSTALL_BIN/exec2)" = "$BPM_PACKAGES_PATH/username/package/bin/exec2.sh" ]
150+
}
151+
152+
134153
@test "does not remove extension if REMOVE_EXTENSION is false" {
135154
local package="username/package"
136155

tests/util/test_util.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# shellcheck shell=bash
2+
# shellcheck disable=SC2164
23

34
# @description This mocks a command by creating a function for it, which
45
# prints all the arguments to the command, in addition to the command name
56
test_util.mock_command() {
6-
# This creates a function with the first argument (the function to
7-
# mock). When called, it prints the command name, along with the arguments
7+
# This creates a function with a name of the first argument. When called, it prints the command name, along with the arguments
88
# it was called with
99
eval "$1() { echo \"$1 \$*\"; }"
1010
}
@@ -35,3 +35,19 @@ test_util.readlink() {
3535
readlink -f "$1"
3636
fi
3737
}
38+
39+
# @description Creates a 'bpm package', and cd's into it
40+
test_util.setup_pkg() {
41+
local package="$1"
42+
43+
create_package "$package"
44+
cd "$BPM_ORIGIN_DIR/$package"
45+
}
46+
47+
# @description Commits changes and cd's out of the package directory
48+
# into the regular testing directory
49+
test_util.finish_pkg() {
50+
git add .
51+
git commit -m "commit"
52+
cd "$BPM_CWD"
53+
}

0 commit comments

Comments
 (0)