Skip to content

Commit 1695d12

Browse files
committed
refactor: Move package printing to own function
1 parent d0b015b commit 1695d12

File tree

2 files changed

+156
-66
lines changed

2 files changed

+156
-66
lines changed

pkg/src/bin/basalt-package-init.sh

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
# since these functions must only be called in a fresh Bash context
1010

1111
main.basalt-package-init() {
12+
# All files are already sourced when testing. This ensures stubs are not overriden
13+
if [ "$BASALT_IS_TESTING" != 'yes' ]; then
14+
if [ -z "$__basalt_dirname" ]; then
15+
printf '%s\n' "Fatal: main.basalt: Variable '__basalt_dirname' is empty"
16+
exit 1
17+
fi
18+
source "$__basalt_dirname/pkg/src/util/util-init.sh"
19+
fi
20+
1221
# Set main variables (WET)
1322
local basalt_global_repo=
1423
if [ -L "$0" ]; then # Only subshell when necessary
@@ -22,79 +31,80 @@ main.basalt-package-init() {
2231
fi
2332
basalt_global_repo=${basalt_global_repo%/*}; basalt_global_repo=${basalt_global_repo%/*}
2433

25-
cat <<EOF
26-
basalt.package-init() {
27-
# basalt variables
28-
export BASALT_GLOBAL_REPO="$basalt_global_repo"
29-
EOF
30-
cat <<"EOF"
31-
export BASALT_GLOBAL_DATA_DIR="${BASALT_GLOBAL_DATA_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/basalt"}"
34+
util.init_print_package_init "$basalt_global_repo"
35+
# cat <<EOF
36+
# basalt.package-init() {
37+
# # basalt variables
38+
# export BASALT_GLOBAL_REPO="$basalt_global_repo"
39+
# EOF
40+
# cat <<"EOF"
41+
# export BASALT_GLOBAL_DATA_DIR="${BASALT_GLOBAL_DATA_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/basalt"}"
3242

33-
if [ -z "$BASALT_GLOBAL_DATA_DIR" ]; then
34-
printf '%s\n' "Error: basalt.package-init: Variable '\$BASALT_GLOBAL_DATA_DIR' is empty" >&2
35-
exit 1
36-
fi
43+
# if [ -z "$BASALT_GLOBAL_DATA_DIR" ]; then
44+
# printf '%s\n' "Error: basalt.package-init: Variable '\$BASALT_GLOBAL_DATA_DIR' is empty" >&2
45+
# exit 1
46+
# fi
3747

38-
# basalt global and internal functions
39-
if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh" ]; then
40-
printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-global.sh' in '\$BASALT_GLOBAL_REPO'" >&2
41-
exit 1
42-
fi
43-
source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh"
48+
# # basalt global and internal functions
49+
# if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh" ]; then
50+
# printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-global.sh' in '\$BASALT_GLOBAL_REPO'" >&2
51+
# exit 1
52+
# fi
53+
# source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh"
4454

45-
if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh" ]; then
46-
printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-package.sh' in '\$BASALT_GLOBAL_REPO'" >&2
47-
exit 1
48-
fi
49-
source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh"
55+
# if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh" ]; then
56+
# printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-package.sh' in '\$BASALT_GLOBAL_REPO'" >&2
57+
# exit 1
58+
# fi
59+
# source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh"
5060

51-
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
52-
local __old_cd="$PWD"
61+
# if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
62+
# local __old_cd="$PWD"
5363

54-
# Do not use "$0", since it won't work in some environments, such as Bats
55-
local __basalt_file="${BASH_SOURCE[0]}"
56-
if [ -L "$__basalt_file" ]; then
57-
local __basalt_target="$(readlink "$__basalt_file")"
58-
if ! cd "${__basalt_target%/*}"; then
59-
printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_target%/*}'" >&2
60-
exit 1
61-
fi
62-
else
63-
if ! cd "${__basalt_file%/*}"; then
64-
printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_file%/*}'" >&2
65-
exit 1
66-
fi
67-
fi
64+
# # Do not use "$0", since it won't work in some environments, such as Bats
65+
# local __basalt_file="${BASH_SOURCE[0]}"
66+
# if [ -L "$__basalt_file" ]; then
67+
# local __basalt_target="$(readlink "$__basalt_file")"
68+
# if ! cd "${__basalt_target%/*}"; then
69+
# printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_target%/*}'" >&2
70+
# exit 1
71+
# fi
72+
# else
73+
# if ! cd "${__basalt_file%/*}"; then
74+
# printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_file%/*}'" >&2
75+
# exit 1
76+
# fi
77+
# fi
6878

69-
# Note that this variable should not be exported. It can cause weird things to occur. For example,
70-
# if a Basalt local package called a command from a global package, things won't work since
71-
# 'BASALT_PACKAGE_DIR' would already be defined and won't be properly set for the global package
72-
if ! BASALT_PACKAGE_DIR="$(
73-
while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
74-
if ! cd ..; then
75-
exit 1
76-
fi
77-
done
79+
# # Note that this variable should not be exported. It can cause weird things to occur. For example,
80+
# # if a Basalt local package called a command from a global package, things won't work since
81+
# # 'BASALT_PACKAGE_DIR' would already be defined and won't be properly set for the global package
82+
# if ! BASALT_PACKAGE_DIR="$(
83+
# while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
84+
# if ! cd ..; then
85+
# exit 1
86+
# fi
87+
# done
7888

79-
if [ "$PWD" = / ]; then
80-
exit 1
81-
fi
89+
# if [ "$PWD" = / ]; then
90+
# exit 1
91+
# fi
8292

83-
printf '%s' "$PWD"
84-
)"; then
85-
printf '%s\n' "Error: basalt.package-init: Could not find basalt.toml" >&2
86-
if ! cd "$__old_cd"; then
87-
printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
88-
exit 1
89-
fi
90-
exit 1
91-
fi
93+
# printf '%s' "$PWD"
94+
# )"; then
95+
# printf '%s\n' "Error: basalt.package-init: Could not find basalt.toml" >&2
96+
# if ! cd "$__old_cd"; then
97+
# printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
98+
# exit 1
99+
# fi
100+
# exit 1
101+
# fi
92102

93-
if ! cd "$__old_cd"; then
94-
printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
95-
exit 1
96-
fi
97-
fi
98-
}
99-
EOF
103+
# if ! cd "$__old_cd"; then
104+
# printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
105+
# exit 1
106+
# fi
107+
# fi
108+
# }
109+
# EOF
100110
}

pkg/src/util/util-init.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,83 @@ util.init_lock() {
9191
util.init_deinit() {
9292
rm -rf "$___basalt_lock_dir"
9393
}
94+
95+
util.init_print_package_init() {
96+
local basalt_global_repo="$1"
97+
98+
cat <<EOF
99+
basalt.package-init() {
100+
# basalt variables
101+
export BASALT_GLOBAL_REPO="$basalt_global_repo"
102+
EOF
103+
cat <<"EOF"
104+
export BASALT_GLOBAL_DATA_DIR="${BASALT_GLOBAL_DATA_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/basalt"}"
105+
106+
if [ -z "$BASALT_GLOBAL_DATA_DIR" ]; then
107+
printf '%s\n' "Error: basalt.package-init: Variable '\$BASALT_GLOBAL_DATA_DIR' is empty" >&2
108+
exit 1
109+
fi
110+
111+
# basalt global and internal functions
112+
if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh" ]; then
113+
printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-global.sh' in '\$BASALT_GLOBAL_REPO'" >&2
114+
exit 1
115+
fi
116+
source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-global.sh"
117+
118+
if [ ! -f "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh" ]; then
119+
printf '%s\n' "Error: basalt.package-init: Failed to find file 'basalt-package.sh' in '\$BASALT_GLOBAL_REPO'" >&2
120+
exit 1
121+
fi
122+
source "$BASALT_GLOBAL_REPO/pkg/src/public/basalt-package.sh"
123+
124+
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
125+
local __old_cd="$PWD"
126+
127+
# Do not use "$0", since it won't work in some environments, such as Bats
128+
local __basalt_file="${BASH_SOURCE[0]}"
129+
if [ -L "$__basalt_file" ]; then
130+
local __basalt_target="$(readlink "$__basalt_file")"
131+
if ! cd "${__basalt_target%/*}"; then
132+
printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_target%/*}'" >&2
133+
exit 1
134+
fi
135+
else
136+
if ! cd "${__basalt_file%/*}"; then
137+
printf '%s\n' "Error: basalt.package-init: Could not cd to '${__basalt_file%/*}'" >&2
138+
exit 1
139+
fi
140+
fi
141+
142+
# Note that this variable should not be exported. It can cause weird things to occur. For example,
143+
# if a Basalt local package called a command from a global package, things won't work since
144+
# 'BASALT_PACKAGE_DIR' would already be defined and won't be properly set for the global package
145+
if ! BASALT_PACKAGE_DIR="$(
146+
while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
147+
if ! cd ..; then
148+
exit 1
149+
fi
150+
done
151+
152+
if [ "$PWD" = / ]; then
153+
exit 1
154+
fi
155+
156+
printf '%s' "$PWD"
157+
)"; then
158+
printf '%s\n' "Error: basalt.package-init: Could not find basalt.toml" >&2
159+
if ! cd "$__old_cd"; then
160+
printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
161+
exit 1
162+
fi
163+
exit 1
164+
fi
165+
166+
if ! cd "$__old_cd"; then
167+
printf '%s\n' "Error: basalt.package-init: Could not cd back to '$__old_cd'" >&2
168+
exit 1
169+
fi
170+
fi
171+
}
172+
EOF
173+
}

0 commit comments

Comments
 (0)