Skip to content

Commit 7fb4fca

Browse files
committed
refactor: Major changes
1 parent 6d9ee18 commit 7fb4fca

14 files changed

+262
-64
lines changed

pkg/bin/basalt-package-init

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ if [ -d "$REPLY/lib/basalt" ]; then
5353
elif [ -d "$REPLY/lib" ]; then
5454
PROGRAM_LIB_DIR="$REPLY/lib"
5555
else
56-
echo "Error: Could not determine \$PROGRAM_LIB_DIR"
56+
# TODOO
57+
printf '%s\n' "Error: Could not determine \$PROGRAM_LIB_DIR"
5758
exit 1
5859
fi
5960

pkg/lib/cmd/basalt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ basalt.main() {
4141
case "$1" in
4242
init) shift; do-global-init "$@" ;;
4343
add) shift; do-global-add "$@" ;;
44+
remove) shift; do-global-remove "$@" ;;
45+
list) shift; do-global-list "$@" ;;
4446
*)
4547
if [ -n "$1" ]; then
4648
print.die "Global subcommand '$1' is not a valid"

pkg/lib/commands/do-add.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ do-add() {
1919

2020
# Package parsing (WET)
2121
for pkg in "${pkgs[@]}"; do
22-
if ! util.get_package_info "$pkg"; then
23-
print.die "String '$pkg' does not look like a package"
24-
fi
22+
util.get_package_info "$pkg"
2523
local repo_type="$REPLY1"
2624
local url="$REPLY2"
2725
local site="$REPLY3"

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,12 @@ do-global-add() {
1414
esac done
1515

1616
if ((${#pkgs[@]} == 0)); then
17-
print.indent-yellow 'Warning' "No packages were specified"
17+
newindent.die "Must specify at least one package"
1818
fi
1919

20-
# Package parsing (WET)
2120
for pkg in "${pkgs[@]}"; do
22-
if ! util.get_package_info "$pkg"; then
23-
print.die "String '$pkg' does not look like a package"
24-
fi
25-
local repo_type="$REPLY1"
26-
local url="$REPLY2"
27-
local site="$REPLY3"
28-
local package="$REPLY4"
29-
local version="$REPLY5"
21+
util.get_package_info "$pkg"
22+
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
3023

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# shellcheck shell=bash
2+
3+
do-global-list() {
4+
util.init_global
5+
6+
if (($# != 0)); then
7+
newindent.die "No arguments or flags must be specified"
8+
fi
9+
10+
printf '%s\n' "$(<"$BASALT_GLOBAL_DATA_DIR/global/dependencies")"
11+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# shellcheck shell=bash
2+
3+
do-global-remove() {
4+
util.init_global
5+
6+
local flag_force='no'
7+
local -a pkgs=()
8+
for arg; do case "$arg" in
9+
--force)
10+
flag_force='yes'
11+
;;
12+
-*)
13+
print.die "Flag '$arg' not recognized"
14+
;;
15+
*)
16+
pkgs+=("$arg")
17+
;;
18+
esac done
19+
20+
if ((${#pkgs[@]} == 0)); then
21+
newindent.die "Must specify at least one package"
22+
fi
23+
24+
for pkg in "${pkgs[@]}"; do
25+
util.get_package_info "$pkg"
26+
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
27+
28+
if [ -n "$version" ]; then
29+
newindent.die "Must not specify ref when removing packages"
30+
fi
31+
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"
36+
done
37+
38+
pkg.do-global-install
39+
}

pkg/lib/util/ensure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ensure.nonzero() {
3232

3333
local -n value="$name"
3434
if [ -z "$value" ]; then
35-
print.internal_die "Argument '$name' for function '${FUNCNAME[1]}' is empty"
35+
print.internal_die "Argument '$name' for function '${FUNCNAME[1]} ${FUNCNAME[2]} ${FUNCNAME[3]}' is empty"
3636
fi
3737
}
3838

pkg/lib/util/pkg.sh

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ pkg.do-global-install() {
88
print.indent-die "Could not remove global '.basalt' directory"
99
fi
1010

11-
local -a dependencies=()
12-
readarray -t dependencies < "$BASALT_GLOBAL_DATA_DIR/global/dependencies"
13-
pkg.install_package "$BASALT_GLOBAL_DATA_DIR/global" 'lenient' "${dependencies[@]}"
11+
local -a deps=()
12+
local dep=
13+
14+
while IFS= read -r dep; do
15+
if [ -z "$dep" ]; then
16+
continue
17+
fi
18+
19+
deps+=("$dep")
20+
done < "$BASALT_GLOBAL_DATA_DIR/global/dependencies"; unset dep
21+
22+
pkg.install_package "$BASALT_GLOBAL_DATA_DIR/global" 'lenient' "${deps[@]}"
1423
}
1524

1625
# @description Installs a pacakge and all its dependencies, relative to a
@@ -25,17 +34,10 @@ pkg.install_package() {
2534
ensure.nonzero 'symlink_mode'
2635

2736
# TODO: save the state and have rollback feature
28-
2937
local pkg=
3038
for pkg; do
31-
if ! util.get_package_info "$pkg"; then
32-
print.die "String '$pkg' does not look like a package"
33-
fi
34-
local repo_type="$REPLY1"
35-
local url="$REPLY2"
36-
local site="$REPLY3"
37-
local package="$REPLY4"
38-
local version="$REPLY5"
39+
util.get_package_info "$pkg"
40+
local repo_type="$REPLY1" url="$REPLY2" site="$REPLY3" package="$REPLY4" version="$REPLY5"
3941

4042
util.get_package_id "$repo_type" "$url" "$site" "$package" "$version"
4143
local package_id="$REPLY"
@@ -53,8 +55,9 @@ pkg.install_package() {
5355

5456
# Only after all the dependencies are installed do we muck with the package
5557
pkg.phase_global_integration "$package_id"
56-
done
57-
unset pkg
58+
done; unset pkg
59+
60+
# TODO: make this happen only once (recursion)
5861

5962
# Only if all the previous modifications to the global package store has been successfull
6063
# do we muck with the current local project
@@ -194,9 +197,7 @@ pkg.phase_local_integration() {
194197

195198
local pkg=
196199
for pkg; do
197-
if ! util.get_package_info "$pkg"; then
198-
print.die "String '$pkg' does not look like a package"
199-
fi
200+
util.get_package_info "$pkg"
200201
local repo_type="$REPLY1"
201202
local url="$REPLY2"
202203
local site="$REPLY3"
@@ -248,28 +249,31 @@ pkg.phase_local_generate-scripts() {
248249

249250
# Create generated files
250251
local content=
251-
if util.get_toml_array "$project_dir/basalt.toml" 'sourceDirs'; then
252-
if ((${#REPLIES[@]} > 0)); then
253-
# TODO output to file descriptor with exec
254-
255-
local source_dir=
256-
for source_dir in "${REPLIES[@]}"; do
257-
# TODO: use BASALT_GLOBAL_DATA_DIR
258-
printf -v content '%s%s\n' "$content" "for __basalt_f in \"$project_dir/$source_dir\"/*; do
259-
if [ -f \"\$__basalt_f\" ]; then
260-
source \"\$__basalt_f\"
261-
fi
252+
if [ -d "$project_dir/basalt.toml" ]; then
253+
if util.get_toml_array "$project_dir/basalt.toml" 'sourceDirs'; then
254+
if ((${#REPLIES[@]} > 0)); then
255+
# TODO output to file descriptor with exec
256+
257+
local source_dir=
258+
for source_dir in "${REPLIES[@]}"; do
259+
# TODO: use BASALT_GLOBAL_DATA_DIR
260+
printf -v content '%s%s\n' "$content" "for __basalt_f in \"$project_dir/$source_dir\"/*; do
261+
if [ -f \"\$__basalt_f\" ]; then
262+
source \"\$__basalt_f\"
263+
fi
262264
done
263265
"
264-
done
265-
unset source_dir
266+
done
267+
unset source_dir
266268

267-
printf -v content '%s%s' "$content" 'unset __basalt_f'
269+
printf -v content '%s%s' "$content" 'unset __basalt_f'
268270

269-
if [ ! -d "$project_dir/.basalt/generated" ]; then
270-
mkdir -p "$project_dir/.basalt/generated"
271+
if [ ! -d "$project_dir/.basalt/generated" ]; then
272+
mkdir -p "$project_dir/.basalt/generated"
273+
fi
274+
cat <<< "$content" > "$project_dir/.basalt/generated/source_package.sh"
271275
fi
272-
cat <<< "$content" > "$project_dir/.basalt/generated/source_package.sh"
273276
fi
274277
fi
278+
275279
}

pkg/lib/util/print.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ print.indent-green() {
7373
fi
7474
}
7575

76-
print.indent-light-cyan() {
76+
newindent.die() {
77+
newindent.error "$1"
78+
exit 1
79+
}
80+
81+
newindent.error() {
7782
if [ -n "${NO_COLOR+x}" ] || [ "$TERM" = dumb ]; then
78-
printf "%11s %s\n" "$1" "$2"
83+
printf "%11s %s\n" "Error" "$1"
7984
else
80-
printf "\033[0;36m%11s\033[0m %s\n" "$1" "$2"
85+
printf "\033[0;31m%11s\033[0m %s\n" 'Error' "$1"
8186
fi
8287
}

pkg/lib/util/util-temporary.sh

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,49 @@ util.text_add_dependency() {
194194

195195
local line=
196196
while IFS= read -r line; do
197-
if [ "${line%@*}" = "${dependency%@*}" ]; then
197+
util.get_package_info "$line"
198+
local url1="$REPLY2"
199+
200+
util.get_package_info "$dependency"
201+
local url2="$REPLY2"
202+
203+
if [ "$url1" = "$url2" ]; then
198204
print.indent-yellow 'Warning' "A version of '${line%@*}' is already installed. Skipping"
199-
return
200205
fi
201-
done < "$text_file"
202-
unset line
206+
done < "$text_file"; unset line
203207

204208
printf '%s\n' "$dependency" >> "$text_file"
205209
}
210+
211+
util.text_remove_dependency() {
212+
local text_file="$1"
213+
local dependency="$2"
214+
local package_id="$3"
215+
local flag_force="$4"
216+
217+
ensure.nonzero 'text_file'
218+
ensure.nonzero 'dependency'
219+
ensure.nonzero 'package_id'
220+
ensure.nonzero 'flag_force'
221+
222+
local -a arr=()
223+
if util.text_dependency_is_installed "$text_file" "$dependency"; then
224+
local line=
225+
while IFS= read -r line; do
226+
if [ "${dependency%@*}" != "${line%@*}" ]; then
227+
arr+=("$line")
228+
fi
229+
done < "$text_file"
230+
231+
printf "%s\n" "${arr[@]}" > "$text_file"
232+
else
233+
if [ "$flag_force" = 'no' ]; then
234+
newindent.die "Dependency '${dependency%@*}' is not installed. Skipping"
235+
fi
236+
fi
237+
238+
rm -rf "$BASALT_GLOBAL_DATA_DIR/store/packages/$package_id"*
239+
if [ "$flag_force" = 'yes' ]; then
240+
rm -rf "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id"*
241+
fi
242+
}

0 commit comments

Comments
 (0)