Skip to content

Commit 9042411

Browse files
committed
refactor: Clean up global operations
1 parent 7fb4fca commit 9042411

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

pkg/lib/commands/do-add.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ do-add() {
2020
# Package parsing (WET)
2121
for pkg in "${pkgs[@]}"; do
2222
util.get_package_info "$pkg"
23-
local repo_type="$REPLY1"
24-
local url="$REPLY2"
25-
local site="$REPLY3"
26-
local package="$REPLY4"
27-
local version="$REPLY5"
23+
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
2824

2925
if ! util.does_package_exist "$repo_type" "$url"; then
3026
print.die "Package located at '$url' does not exist"

pkg/lib/commands/do-global-remove.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ do-global-remove() {
2323

2424
for pkg in "${pkgs[@]}"; do
2525
util.get_package_info "$pkg"
26-
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
26+
local url="$REPLY2" version="$REPLY5"
2727

2828
if [ -n "$version" ]; then
2929
newindent.die "Must not specify ref when removing packages"
3030
fi
3131

32-
util.get_package_id --allow-empty-version "$repo_type" "$url" "$site" "$package" "$version"
33-
local package_id="$REPLY"
34-
35-
util.text_remove_dependency "$BASALT_GLOBAL_DATA_DIR/global/dependencies" "$url" "$package_id" "$flag_force"
32+
util.text_remove_dependency "$BASALT_GLOBAL_DATA_DIR/global/dependencies" "$url" "$flag_force"
3633
done
3734

3835
pkg.do-global-install

pkg/lib/util/util-temporary.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ util.text_add_dependency() {
192192
mkdir -p "${text_file%/*}"
193193
touch "$text_file"
194194

195+
util.get_package_info "$dependency"
196+
local url2="$REPLY2"
197+
195198
local line=
196199
while IFS= read -r line; do
197200
util.get_package_info "$line"
198201
local url1="$REPLY2"
199202

200-
util.get_package_info "$dependency"
201-
local url2="$REPLY2"
202-
203203
if [ "$url1" = "$url2" ]; then
204204
print.indent-yellow 'Warning' "A version of '${line%@*}' is already installed. Skipping"
205205
fi
@@ -211,19 +211,29 @@ util.text_add_dependency() {
211211
util.text_remove_dependency() {
212212
local text_file="$1"
213213
local dependency="$2"
214-
local package_id="$3"
215-
local flag_force="$4"
214+
local flag_force="$3"
216215

217216
ensure.nonzero 'text_file'
218217
ensure.nonzero 'dependency'
219-
ensure.nonzero 'package_id'
220218
ensure.nonzero 'flag_force'
221219

220+
util.get_package_info "$dependency"
221+
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
222+
223+
util.get_package_id --allow-empty-version "$repo_type" "$url" "$site" "$package" "$version"
224+
local package_id="$REPLY"
225+
222226
local -a arr=()
223227
if util.text_dependency_is_installed "$text_file" "$dependency"; then
224228
local line=
225229
while IFS= read -r line; do
226-
if [ "${dependency%@*}" != "${line%@*}" ]; then
230+
util.get_package_info "$line"
231+
local url1="$REPLY2"
232+
233+
util.get_package_info "$dependency"
234+
local url2="$REPLY2"
235+
236+
if [ "$url1" != "$url2" ]; then
227237
arr+=("$line")
228238
fi
229239
done < "$text_file"

pkg/lib/util/util.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ util.get_tarball_url() {
294294
fi
295295
}
296296

297+
# TODO: remove this
297298
# If any version of a text dependency is installed
298299
util.text_dependency_is_installed() {
299300
local text_file="$1"

tests/basalt-global.bats

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ load './util/init.sh'
1111
assert_dir_exist "$BASALT_GLOBAL_DATA_DIR/store/packages/github.com/hyperupcall/[email protected]"
1212
}
1313

14+
@test "Add works twice" {
15+
basalt global add 'hyperupcall/[email protected]'
16+
17+
run basalt global add 'hyperupcall/[email protected]'
18+
assert_success
19+
assert [ "$(<"$BASALT_GLOBAL_DATA_DIR/global/dependencies")" = $'https://github.com/hyperupcall/[email protected]\nhttps://github.com/hyperupcall/[email protected]' ]
20+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/github.com/hyperupcall/[email protected]"
21+
assert_dir_exist "$BASALT_GLOBAL_DATA_DIR/store/packages/github.com/hyperupcall/[email protected]"
22+
}
23+
1424
@test "Remove works" {
1525
basalt global add 'hyperupcall/[email protected]'
1626

@@ -19,3 +29,23 @@ load './util/init.sh'
1929
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/github.com/hyperupcall/[email protected]"
2030
assert_dir_not_exist "$BASALT_GLOBAL_DATA_DIR/store/packages/github.com/hyperupcall/[email protected]"
2131
}
32+
33+
@test "Remove works with multiple repositories" {
34+
basalt global add 'hyperupcall/[email protected]'
35+
basalt global add 'hyperupcall/[email protected]'
36+
37+
run basalt global remove 'hyperupcall/bash-object'
38+
assert_success
39+
assert [ "$(<"$BASALT_GLOBAL_DATA_DIR/global/dependencies")" = 'https://github.com/hyperupcall/[email protected]' ]
40+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/github.com/hyperupcall/[email protected]"
41+
assert_dir_not_exist "$BASALT_GLOBAL_DATA_DIR/store/packages/github.com/hyperupcall/[email protected]"
42+
}
43+
44+
@test "Remove force works" {
45+
basalt global add 'hyperupcall/[email protected]'
46+
47+
run basalt global remove --force 'hyperupcall/bash-object'
48+
assert_success
49+
assert_file_not_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/github.com/hyperupcall/[email protected]"
50+
assert_dir_not_exist "$BASALT_GLOBAL_DATA_DIR/store/packages/github.com/hyperupcall/[email protected]"
51+
}

0 commit comments

Comments
 (0)