Skip to content

Commit e1fd772

Browse files
committed
fix:basalt.package-init now works in more circumstances
1 parent a968a09 commit e1fd772

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

pkg/lib/cmd/basalt-package-init.sh

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,62 @@ basalt-package-init.main() {
1414
local basalt_global_repo="${0%/*}"
1515
basalt_global_repo="${basalt_global_repo%/*}"; basalt_global_repo="${basalt_global_repo%/*}"
1616

17-
cat <<-EOF
17+
cat <<EOF
1818
basalt.package-init() {
19-
# basalt variables
20-
export BASALT_GLOBAL_REPO="$basalt_global_repo"
21-
EOF
19+
# basalt variables
20+
export BASALT_GLOBAL_REPO="$basalt_global_repo"
21+
EOF
22+
cat <<"EOF"
23+
export BASALT_GLOBAL_DATA_DIR="${BASALT_GLOBAL_DATA_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/basalt"}"
2224
23-
cat <<-"EOF"
24-
export BASALT_GLOBAL_DATA_DIR="${BASALT_GLOBAL_DATA_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/basalt"}"
25+
# basalt global and internal functions
26+
source "$BASALT_GLOBAL_REPO/pkg/lib/public/basalt-load.sh"
27+
source "$BASALT_GLOBAL_REPO/pkg/lib/public/basalt-package.sh"
2528
26-
# basalt global and internal functions
27-
source "$BASALT_GLOBAL_REPO/pkg/lib/public/basalt-load.sh"
28-
source "$BASALT_GLOBAL_REPO/pkg/lib/public/basalt-package.sh"
29+
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
30+
local __old_cd="$PWD"
2931
30-
# TODO: this needs to be redone
31-
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
32-
local __old_cd="$PWD"
33-
if [ -L "$0" ]; then
34-
local __file="$(readlink "$0")"
35-
if ! cd "${__file%/*}"; then
36-
printf '%s\n' "Error: basalt-package-init: Could not cd to target of '$0'"
37-
return 1
38-
fi
39-
fi
32+
# Do not use "$0", since it won't work in some environments, such as Bats
33+
local __basalt_file="${BASH_SOURCE[0]}"
34+
if [ -L "$__basalt_file" ]; then
35+
local __basalt_target="$(readlink "$__basalt_file")"
36+
if ! cd "${__basalt_target%/*}"; then
37+
printf '%s\n' "Error: basalt-package-init: Could not cd to '${__basalt_target%/*}'"
38+
return 1
39+
fi
40+
else
41+
if ! cd "${__basalt_file%/*}"; then
42+
printf '%s\n' "Error: basalt-package-init: Could not cd to '${__basalt_file%/*}'"
43+
return 1
44+
fi
45+
fi
4046
41-
if ! BASALT_PACKAGE_DIR="$(
42-
while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
43-
if ! cd ..; then
44-
return 1
45-
fi
46-
done
47+
if ! BASALT_PACKAGE_DIR="$(
48+
while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
49+
if ! cd ..; then
50+
return 1
51+
fi
52+
done
4753
48-
if [ "$PWD" = / ]; then
49-
return 1
50-
fi
54+
if [ "$PWD" = / ]; then
55+
return 1
56+
fi
5157
52-
printf '%s' "$PWD"
53-
)"; then
54-
printf '%s\n' "Error: basalt-package-init: Could not find basalt.toml"
55-
if ! cd "$__old_cd"; then
56-
printf '%s\n' "Error: basalt-package-init: Could not cd back to '$__old_cd'"
57-
return 1
58-
fi
59-
return 1
60-
fi
58+
printf '%s' "$PWD"
59+
)"; then
60+
printf '%s\n' "Error: basalt-package-init: Could not find basalt.toml"
61+
if ! cd "$__old_cd"; then
62+
printf '%s\n' "Error: basalt-package-init: Could not cd back to '$__old_cd'"
63+
return 1
64+
fi
65+
return 1
66+
fi
6167
62-
if ! cd "$__old_cd"; then
63-
printf '%s\n' "Error: basalt-package-init: Could not cd back to '$__old_cd'"
64-
return 1
65-
fi
66-
fi
68+
if ! cd "$__old_cd"; then
69+
printf '%s\n' "Error: basalt-package-init: Could not cd back to '$__old_cd'"
70+
return 1
71+
fi
72+
fi
6773
}
68-
EOF
74+
EOF
6975
}

0 commit comments

Comments
 (0)