basherpm/basher

Set BASHER_PACKAGES_PATH with eval $(basher init -)

Closed this issue · 2 comments

Hi @juanibiapina 😸!

Sometimes in my scripts I need to get the path to some file in a basher-installed package.

I myself export the BASHER_PACKAGES_PATH variable, so I can reliably use ${BASHER_PACKAGES_PATH}/namespace/package/file in my own scripts.

But I noticed that eval "$(basher init -)" does not export this variable for users that do not set it themselves. It should not be a problem in most cases, because we still have access to the basher package-path command.

Now I also noticed that using basher package-path impacts performance. Obviously, running all the following machinery

basher
search for command basher, unless already cached
spawn basher process
set many variables, resolving paths, etc
set PATH to add basher's libexec
search for command basher-package-path
exec it
get result

...is much slower that just using the environment variable BASHER_PACKAGES_PATH, especially if the script is called in a loop!

So what I'm requesting here is simply to add this variable in basher-init, for it to be set when eval "$(basher init -)" is ran. This way I can reliably use it in scripts that are destined for others, not just for me.

I couldn't think of any problem that could arise if we indeed set BASHER_PACKAGES_PATH with basher-init, but maybe you can?

If you can, then it's not a problem either because there is still a simple workaround to get the performance of using the environment variable while still falling back to the command if it's not set. Example:

# (I do this in shellm/core package itself, so I know it exists)
if [ -d "${BASHER_PACKAGES_PATH}" ]; then
  PATH="${BASHER_PACKAGES_PATH}/shellm/core/libexec:${PATH}"
else
  PATH="$(basher package-path shellm/core/libexec):${PATH}"
fi

If this request gets implemented, I could simply drop the fallback:

PATH="${BASHER_PACKAGES_PATH}/shellm/core/libexec:${PATH}"

And there would be no performance impact, even for users who do not set BASHER_PACKAGES_PATH variable themselves (and I would not need to tell them to do so to improve performance).

The implementation should hold on two lines:

diff --git a/libexec/basher-init b/libexec/basher-init
index 6cdd4f0..22f57c9 100755
--- a/libexec/basher-init
+++ b/libexec/basher-init
@@ -16,6 +16,7 @@ print_fish_commands() {
   echo "set -gx BASHER_SHELL $shell"
   echo "set -gx BASHER_ROOT $BASHER_ROOT"
   echo "set -gx BASHER_PREFIX $BASHER_PREFIX"
+  echo "set -gx BASHER_PACKAGES_PATH $BASHER_PACKAGES_PATH"
 
   echo 'if not contains $BASHER_ROOT/cellar/bin $PATH'
   echo 'set -gx PATH $BASHER_ROOT/cellar/bin $PATH'
@@ -26,6 +27,7 @@ print_sh_commands(){
   echo "export BASHER_SHELL=$shell"
   echo "export BASHER_ROOT=$BASHER_ROOT"
   echo "export BASHER_PREFIX=$BASHER_PREFIX"
+  echo "export BASHER_PACKAGES_PATH=$BASHER_PACKAGES_PATH"
 
   echo 'export PATH="$BASHER_ROOT/cellar/bin:$PATH"'
 }

I don't see any problems with this as well. You can open a PR.

I believe I can close this.