Skip to content

Commit 5872dba

Browse files
committed
refactor: Revamp code checking and setting bpm variable values
1 parent 5b2056b commit 5872dba

File tree

11 files changed

+49
-62
lines changed

11 files changed

+49
-62
lines changed

pkg/lib/cmd/bpm.sh

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ main() {
1515
source "$f"
1616
done
1717

18-
local is_global='no'
18+
BPM_IS_LOCAL='yes'
1919

2020
for arg; do
2121
case "$arg" in
@@ -30,7 +30,7 @@ main() {
3030
exit
3131
;;
3232
--global|-g)
33-
is_global='yes'
33+
BPM_IS_LOCAL='no'
3434
shift
3535
;;
3636
*)
@@ -41,8 +41,6 @@ main() {
4141

4242
case "$1" in
4343
add)
44-
must_reset_bpm_vars "$is_global"
45-
4644
shift
4745
do-add "$@"
4846
;;
@@ -51,8 +49,6 @@ main() {
5149
do-complete "$@"
5250
;;
5351
echo)
54-
may_reset_bpm_vars "$is_global"
55-
5652
shift
5753
do-echo "$@"
5854
;;
@@ -61,38 +57,22 @@ main() {
6157
do-init "$@"
6258
;;
6359
link)
64-
must_reset_bpm_vars "$is_global"
65-
6660
shift
6761
do-link "$@"
6862
;;
6963
list)
70-
must_reset_bpm_vars "$is_global"
71-
7264
shift
7365
do-list "$@"
7466
;;
75-
outdated)
76-
must_reset_bpm_vars "$is_global"
77-
78-
shift
79-
bpm-outdated "$@"
80-
;;
8167
package-path)
82-
must_reset_bpm_vars "$is_global"
83-
8468
shift
8569
bpm-package-path "$@"
8670
;;
8771
remove)
88-
must_reset_bpm_vars "$is_global"
89-
9072
shift
9173
do-remove "$@"
9274
;;
9375
upgrade)
94-
must_reset_bpm_vars "$is_global"
95-
9676
shift
9777
do-upgrade "$@"
9878
;;
@@ -105,39 +85,4 @@ main() {
10585
esac
10686
}
10787

108-
must_reset_bpm_vars() {
109-
local is_global="$1"
110-
111-
# TEST this
112-
if [ "$is_global" = 'no' ]; then
113-
local project_root_dir=
114-
if ! project_root_dir="$(util.get_project_root_dir)"; then
115-
die "No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
116-
fi
117-
118-
do_set_bpm_vars "$project_root_dir"
119-
fi
120-
}
121-
122-
may_reset_bpm_vars() {
123-
local is_global="$1"
124-
125-
local project_root_dir=
126-
if project_root_dir="$(util.get_project_root_dir)"; then
127-
do_set_bpm_vars "$project_root_dir"
128-
fi
129-
}
130-
131-
do_set_bpm_vars() {
132-
local project_root_dir="$1"
133-
ensure.non_zero 'project_root_dir' "$project_root_dir"
134-
135-
BPM_ROOT="$project_root_dir"
136-
BPM_PREFIX="$project_root_dir/bpm_packages"
137-
BPM_PACKAGES_PATH="$BPM_PREFIX/packages"
138-
BPM_INSTALL_BIN="$BPM_PREFIX/bin"
139-
BPM_INSTALL_MAN="$BPM_PREFIX/man"
140-
BPM_INSTALL_COMPLETIONS="$BPM_PREFIX/completions"
141-
}
142-
14388
main "$@"

pkg/lib/commands/do-add.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# shellcheck shell=bash
22

33
do-add() {
4-
local with_ssh='no' # TODO: refactor flag_
4+
local flag_ssh='no'
55
local flag_all='no'
66

7+
util.setup_mode
8+
79
local -a pkgs=()
810
for arg; do
911
case "$arg" in
1012
--ssh)
11-
with_ssh='yes'
13+
flag_ssh='yes'
1214
;;
1315
--all)
1416
flag_all='yes'
@@ -48,7 +50,7 @@ do-add() {
4850
die "Identifier '$repoSpec' is a directory, not a package"
4951
fi
5052

51-
util.extract_data_from_input "$repoSpec" "$with_ssh"
53+
util.extract_data_from_input "$repoSpec" "$flag_ssh"
5254
local uri="$REPLY1"
5355
local site="$REPLY2"
5456
local package="$REPLY3"

pkg/lib/commands/do-echo.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# shellcheck shell=bash
22

33
do-echo() {
4+
util.setup_mode
5+
46
eval "printf \"%s\" \$$1"
57
}

pkg/lib/commands/do-link.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
do-link() {
44
local install_deps='yes'
55

6+
util.setup_mode
7+
68
local -a dirs=()
79
for arg; do
810
case "$arg" in

pkg/lib/commands/do-list.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ do-list() {
55
local flag_simple='no'
66
local flag_fetch='no'
77

8+
util.setup_mode
9+
810
for arg; do
911
case "$arg" in
1012
--simple)

pkg/lib/commands/do-package-path.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ bpm-package-path() {
44
local id="$1"
55
ensure.non_zero 'id' "$id"
66

7+
util.setup_mode
8+
79
util.extract_data_from_input "$id"
810
local site="$REPLY2"
911
local package="$REPLY3"

pkg/lib/commands/do-remove.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
do-remove() {
44
local flag_all='no'
55

6+
util.setup_mode
7+
68
local -a pkgs=()
79
for arg; do
810
case "$arg" in

pkg/lib/commands/do-upgrade.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
do-upgrade() {
44
local upgrade_bpm='no'
55

6+
util.setup_mode
7+
68
local -a pkgs=()
79
for arg; do
810
case "$arg" in

pkg/lib/util/ensure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ensure.non_zero() {
3030
# the integrity of the testing suite. Most of the callsites of this
3131
# function are in 'do-plumbing-link' since we expect a package to exist
3232
# before performing operations on it. This contrasts 'do-plumbing-unlink' -
33-
# that is not an expectation
33+
# in which the behavior is not expected
3434
# @arg $1 package
3535
ensure.package_exists() {
3636
local id="$1"

pkg/lib/util/util.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,34 @@ util.get_project_root_dir() {
249249
printf "%s" "$PWD"
250250
}
251251

252+
util.set_bpm_variables() {
253+
local project_root_dir="$1"
254+
ensure.non_zero 'project_root_dir' "$project_root_dir"
255+
256+
BPM_ROOT="$project_root_dir"
257+
BPM_PREFIX="$project_root_dir/bpm_packages"
258+
BPM_PACKAGES_PATH="$BPM_PREFIX/packages"
259+
BPM_INSTALL_BIN="$BPM_PREFIX/bin"
260+
BPM_INSTALL_MAN="$BPM_PREFIX/man"
261+
BPM_INSTALL_COMPLETIONS="$BPM_PREFIX/completions"
262+
}
263+
264+
# @description Sets up the variables for the current mode
265+
util.setup_mode() {
266+
if [ "$BPM_IS_LOCAL" = yes ]; then
267+
local project_root_dir=
268+
if ! project_root_dir="$(util.get_project_root_dir)"; then
269+
die "No 'bpm.toml' file found"
270+
fi
271+
272+
util.set_bpm_variables "$project_root_dir"
273+
else
274+
# If we do not set local mode, the default varaible
275+
# values are already correct
276+
:
277+
fi
278+
}
279+
252280
util.show_help() {
253281
cat <<"EOF"
254282
Usage:

tests/mode-global.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load 'util/init.sh'
44

55
# We only test two of all commands
66
@test "error when not passing --global to add, list, and upgrade" {
7-
local str="No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
7+
local str="No 'bpm.toml' file found"
88

99
run bpm add foo
1010
assert_failure

0 commit comments

Comments
 (0)