Skip to content

Commit a968a09

Browse files
committed
refactor: Rename BASALT_PACKAGE_PATH to BASALT_PACKAGE_DIR
This also changes the code behind generated/source_package.sh
1 parent bd8b96f commit a968a09

File tree

8 files changed

+88
-27
lines changed

8 files changed

+88
-27
lines changed

docs/reference/environment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ The directory Basalt stores nearly all data. This includes downloaded tarballs,
1616

1717
Local environment variables are only valid within a Bash package (after `eval "$(basalt-package-init)"; basalt.package-init`)
1818

19-
### `BASALT_PACKAGE_PATH`
19+
### `BASALT_PACKAGE_DIR`
2020

21-
The full path to the current project. It is calculated by walking up the file tree from `$PWD`, only stopping after detecting a `./basalt.toml`. The directory that was stopped at is the new value of `BASALT_PACKAGE_PATH`
21+
The full path to the current project. It is calculated by walking up the file tree from `$PWD`, only stopping after detecting a `./basalt.toml`. The directory that was stopped at is the new value of `BASALT_PACKAGE_DIR`

pkg/bin/basalt-package-init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Do NOT edit!
44

55
# https://github.com/bashup/realpaths
6+
# TODO: get rid of this, and for 'basalt'
67
realpath.location() {
78
realpath.follow "$1"
89
realpath.absolute "$REPLY" ".."

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ basalt.package-init() {
2828
source "$BASALT_GLOBAL_REPO/pkg/lib/public/basalt-package.sh"
2929
3030
# TODO: this needs to be redone
31-
if [ -z "$BASALT_PACKAGE_PATH" ]; then
32-
local __old_cd="$PWD"
31+
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
32+
local __old_cd="$PWD"
3333
if [ -L "$0" ]; then
3434
local __file="$(readlink "$0")"
3535
if ! cd "${__file%/*}"; then
@@ -38,7 +38,7 @@ basalt.package-init() {
3838
fi
3939
fi
4040
41-
if ! BASALT_PACKAGE_PATH="$(
41+
if ! BASALT_PACKAGE_DIR="$(
4242
while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
4343
if ! cd ..; then
4444
return 1

pkg/lib/commands/do-init.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ do-init() {
6767
eval "$(basalt-package-init)"; basalt.package-init
6868
basalt.package-load
6969
70-
source "$BASALT_PACKAGE_PATH/pkg/lib/cmd/file.sh"
70+
source "$BASALT_PACKAGE_DIR/pkg/lib/cmd/file.sh"
7171
file.main "$@"
7272
EOF
7373
bprint.die "Could not write to $file2"
@@ -78,6 +78,8 @@ do-init() {
7878
mkdir -p 'pkg/lib/cmd'
7979
local file3="./pkg/lib/cmd/file.sh"
8080
if ! cat >| "$file3" <<"EOF"; then
81+
# shellcheck shell=bash
82+
8183
file.main() {
8284
printf '%s\n' "Woof!"
8385
}
@@ -86,6 +88,40 @@ EOF
8688
fi
8789
bprint.info "Created $file3"
8890

91+
92+
mkdir -p 'tests/util'
93+
local file4='./tests/util/init.sh'
94+
if ! cat >| "$file4" <<"EOF"; then
95+
# shellcheck shell=bash
96+
97+
eval "$(basalt-package-init)"; basalt.package-init
98+
basalt.package-load
99+
# basalt.load 'github.com/hyperupcall/bats-common-utils' 'load.bash'
100+
101+
load './util/test_util.sh'
102+
103+
export NO_COLOR=
104+
105+
setup() {
106+
ensure.cd "$BATS_TEST_TMPDIR"
107+
}
108+
109+
teardown() {
110+
ensure.cd "$BATS_SUITE_TMPDIR"
111+
}
112+
EOF
113+
bprint.die "Could not write to $file4"
114+
fi
115+
bprint.info "Created $file4"
116+
117+
mkdir -p 'tests/util'
118+
local file5='./tests/util/test_util.sh'
119+
if ! cat >| "$file5" <<"EOF"; then
120+
# shellcheck shell=bash
121+
EOF
122+
bprint.die "Could not write to $file5"
123+
fi
124+
bprint.info "Created $file5"
89125
;;
90126
full)
91127
if ! git clone -q 'https://github.com/hyperupcall/template-bash' .; then

pkg/lib/public/basalt-load.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ basalt.load() {
8787
source "$__basalt_pkg_path/load.bash"
8888

8989
if declare -f basalt_load &>/dev/null; then
90-
BASALT_PACKAGE_PATH="$__basalt_pkg_path" basalt_load
90+
BASALT_PACKAGE_DIR="$__basalt_pkg_path" basalt_load
9191
unset basalt_load
9292
fi
9393
fi
@@ -98,7 +98,7 @@ basalt.load() {
9898
# Assume can only have one version of a particular package for direct dependencies
9999
local __basalt_load_package_exists='no' __basalt_did_run_source='no'
100100
local __basalt_actual_pkg_path=
101-
for __basalt_actual_pkg_path in "$BASALT_PACKAGE_PATH/.basalt/packages/$__basalt_pkg_path"*; do
101+
for __basalt_actual_pkg_path in "$BASALT_PACKAGE_DIR/.basalt/packages/$__basalt_pkg_path"*; do
102102
if [ "$__basalt_load_package_exists" = yes ]; then
103103
printf '%s\n' "Error: basalt.load There are multiple direct dependencies for package '$__basalt_pkg_path'. This should not happen"
104104
return 1
@@ -108,7 +108,7 @@ basalt.load() {
108108

109109
if [ -n "$__basalt_file" ]; then
110110
if [ -f "$__basalt_actual_pkg_path/$__basalt_file" ]; then
111-
BASALT_PACKAGE_PATH="$__basalt_actual_pkg_path" source "$__basalt_actual_pkg_path/$__basalt_file"
111+
BASALT_PACKAGE_DIR="$__basalt_actual_pkg_path" source "$__basalt_actual_pkg_path/$__basalt_file"
112112
__basalt_did_run_source='yes'
113113
else
114114
printf '%s\n' "Error: basalt.load File '$__basalt_file' not found in package '$__basalt_pkg_path'"
@@ -120,7 +120,7 @@ basalt.load() {
120120
source "$__basalt_actual_pkg_path/load.bash"
121121

122122
if declare -f basalt_load &>/dev/null; then
123-
BASALT_PACKAGE_PATH="$__basalt_actual_pkg_path" basalt_load
123+
BASALT_PACKAGE_DIR="$__basalt_actual_pkg_path" basalt_load
124124
unset basalt_load
125125
fi
126126

pkg/lib/public/basalt-package.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
basalt.package-load() {
66
local __basalt_shopt_nullglob=
77

8-
if [ -z "${BASALT_PACKAGE_PATH:-}" ]; then
9-
printf '%s\n' "Error: basalt.package-load: Variable 'BASALT_PACKAGE_PATH' is empty"
8+
if [ -z "${BASALT_PACKAGE_DIR:-}" ]; then
9+
printf '%s\n' "Error: basalt.package-load: Variable 'BASALT_PACKAGE_DIR' is empty"
1010
return 1
1111
fi
1212

@@ -17,9 +17,10 @@ basalt.package-load() {
1717
fi
1818
shopt -s nullglob
1919

20+
# TODO: Use array expand glob so no need to shopt -s nullglob at end to make this cleaner
2021
local __basalt_site= __basalt_repository_owner= __basalt_package=
21-
if [ -d "$BASALT_PACKAGE_PATH"/.basalt/packages ]; then
22-
for __basalt_site in "$BASALT_PACKAGE_PATH"/.basalt/packages/*/; do
22+
if [ -d "$BASALT_PACKAGE_DIR"/.basalt/packages ]; then
23+
for __basalt_site in "$BASALT_PACKAGE_DIR"/.basalt/packages/*/; do
2324
for __basalt_repository_owner in "$__basalt_site"/*/; do
2425
for __basalt_package in "$__basalt_repository_owner"/*/; do
2526
if [ "$__basalt_shopt_nullglob" = 'yes' ]; then
@@ -38,8 +39,8 @@ basalt.package-load() {
3839
done; unset __basalt_site
3940
fi
4041

41-
if [ -f "$BASALT_PACKAGE_PATH/.basalt/generated/source_package.sh" ]; then
42-
source "$BASALT_PACKAGE_PATH/.basalt/generated/source_package.sh"
42+
if [ -f "$BASALT_PACKAGE_DIR/.basalt/generated/source_package.sh" ]; then
43+
source "$BASALT_PACKAGE_DIR/.basalt/generated/source_package.sh"
4344
fi
4445

4546
if [ "$__basalt_shopt_nullglob" = 'yes' ]; then

pkg/lib/util/pkg.sh

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,43 @@ pkg.phase_local_integration_nonrecursive() {
215215
if [ -f "$project_dir/basalt.toml" ]; then
216216
if util.get_toml_array "$project_dir/basalt.toml" 'sourceDirs'; then
217217
if ((${#REPLIES[@]} > 0)); then
218-
# TODO output to file descriptor with exec
218+
# Convert the full '$project_dir' path into something that uses the environment variables
219+
local project_dir_short=
220+
if [ "$BASALT_LOCAL_PROJECT_DIR" = "${project_dir::${#BASALT_LOCAL_PROJECT_DIR}}" ]; then
221+
project_dir_short='$BASALT_PACKAGE_DIR'
222+
elif [ "$BASALT_GLOBAL_DATA_DIR" = "${project_dir::${#BASALT_GLOBAL_DATA_DIR}}" ]; then
223+
project_dir_short="\$BASALT_GLOBAL_DATA_DIR${project_dir:${#BASALT_GLOBAL_DATA_DIR}}"
224+
else
225+
bprint.fatal "Unexpected path to project directory '$project_dir'"
226+
fi
227+
228+
# shellcheck disable=SC2016
229+
printf -v content '%s%s\n' "$content" '# shellcheck shell=bash
230+
231+
if [ -z "$BASALT_PACKAGE_DIR" ]; then
232+
printf "%s\n" "Fatal: source_package.sh: $BASALT_PACKAGE_DIR is empty, but must exist"
233+
exit 1
234+
fi
235+
236+
if [ -z "$BASALT_GLOBAL_DATA_DIR" ]; then
237+
printf "%s\n" "Fatal: source_package.sh: $BASALT_GLOBAL_DATA_DIR is empty, but must exist"
238+
exit 1
239+
fi'
219240

220-
local source_dir=
221241
for source_dir in "${REPLIES[@]}"; do
222-
# TODO: use BASALT_GLOBAL_DATA_DIR
223-
printf -v content '%s%s\n' "$content" "for __basalt_f in \"$project_dir/$source_dir\"/*; do
224-
if [ -f \"\$__basalt_f\" ]; then
225-
source \"\$__basalt_f\"
226-
fi
227-
done
228-
"
242+
printf -v content '%s%s\n' "$content" "
243+
# Silently skip if directory doesn't exist since a corresponding warning will print during package installation
244+
if [ -d \"$project_dir_short/$source_dir\" ]; then
245+
# Works if nullglob is unset, given that there is no file called '*'
246+
for __basalt_f in \"$project_dir_short/$source_dir\"/*; do
247+
if [ -f \"\$__basalt_f\" ]; then
248+
# shellcheck disable=SC1090
249+
source \"\$__basalt_f\"
250+
fi
251+
done; unset __basalt_f
252+
fi"
229253
done; unset source_dir
230254

231-
printf -v content '%s%s' "$content" 'unset __basalt_f'
232-
233255
if [ ! -d "$project_dir/.basalt/generated" ]; then
234256
mkdir -p "$project_dir/.basalt/generated"
235257
fi

pkg/lib/util/util.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ util.get_latest_package_version() {
220220
}
221221

222222
util.get_package_info() {
223+
unset REPLY{1,2,3,4,5}
223224
REPLY1=; REPLY2=; REPLY3=; REPLY4=; REPLY5=
224225
local input="$1"
225226
ensure.nonzero 'input'

0 commit comments

Comments
 (0)