Tarrasch / zsh-autoenv

Autoenv for zsh
693 stars 30 forks source link

Manual trigger for autoenv hook #56

Closed mafrosis closed 5 months ago

mafrosis commented 7 years ago

A small patch to add an autoenv shell command which triggers _autoenv_chpwd_handler. I had to add a variable to that function to ignore that we have already entered the directory where it's triggered.

I took a look at the tests, but I couldn't see how to run them. What is cram?

blueyed commented 7 years ago

Thanks. What is your use case? Doesn't e.g. cd . work for you to reload a env file?

See https://bitheap.org/cram/ for Cram.

mafrosis commented 7 years ago

cd . works for other autoenv-like tools - but not here as the current directory stored in the stack.

blueyed commented 7 years ago

@mafrosis It should however reload the env file, if you changed it. I still don't see why you need to manually trigger it.

mafrosis commented 7 years ago

Since the content of the sourced .autoenv.sh can be anything you like, there's all kinds of use cases.

My specific file contains this:

export DOCKER_MACHINE_NAME=example
eval $(docker-machine env ${DOCKER_MACHINE_NAME})
echo "Using docker-machine ${DOCKER_MACHINE_NAME} ($(docker-machine ip ${DOCKER_MACHINE_NAME}))"

If the docker-machine isn't running when I cd into the directory, the script fails. I start the docker-machine, and then I need to cd out and back in to trigger the script (or type source .autoenv.sh)

blueyed commented 7 years ago

So the problem here really is that an .autoenv.zsh file gets not re-sourced when it is in the stack already, right? (Already in stack: /home/user/.autoenv.zsh)

Just noticed this with the following in ~/.autoenv.zsh, which was meant to be triggered on every directory change:

# Environment file for all projects.
#  - (de)activates Python virtualenvs (.venv) from pipenv

if [[ $autoenv_event == 'enter' ]]; then
  autoenv_source_parent

  if [[ -z "$VIRTUAL_ENV" ]]; then
    venv=(./(../)#.venv(NY1:A))

    if (( $#venv )); then
      echo "Activating virtualenv: $venv" >&2
      source $venv[1]/bin/activate
      _ZSH_ACTIVATED_VIRTUALENV=$VIRTUAL_ENV
    fi
  fi

else
  if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" ]] && \
      [[ "$_ZSH_ACTIVATED_VIRTUALENV" == "$VIRTUAL_ENV" ]]; then
    echo "De-activating virtualenv: $VIRTUAL_ENV" >&2
    deactivate

    unset _ZSH_ACTIVATED_VIRTUALENV
  fi
fi

I think we could set a flag from the .autoenv.zsh file to re-source it on every directory change for that use case.. but then it's also possible to add (and remove) a custom chpwd hook:

# Environment file for all projects.
#  - (de)activates Python virtualenvs (.venv) from pipenv

if [[ $autoenv_event == 'enter' ]]; then
  autoenv_source_parent

  _my_autoenv_venv_chpwd() {
    if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
      return
    fi

    local -a venv
    venv=(./(../)#.venv(NY1:A))

    if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" ]]; then
      if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
        echo "De-activating virtualenv: $VIRTUAL_ENV" >&2
        deactivate
        unset _ZSH_ACTIVATED_VIRTUALENV
      fi
    fi

    if [[ -z "$VIRTUAL_ENV" ]]; then
      if (( $#venv )); then
        echo "Activating virtualenv: $venv" >&2
        source $venv[1]/bin/activate
        _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
      fi
    fi
  }
  autoload -U add-zsh-hook
  add-zsh-hook chpwd _my_autoenv_venv_chpwd
  _my_autoenv_venv_chpwd

else
  add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
fi

(added to README in https://github.com/Tarrasch/zsh-autoenv/pull/58)

blueyed commented 7 years ago

I think your PR makes sense, and will merge it after you or me has added a test for it.

blueyed commented 6 years ago

@mafrosis ping. (just visitied this repo again myself)

mafrosis commented 6 years ago

I'm no longer using zsh-autoenv. Please do with this as you will!