2014-12-04 08:58:45 +02:00
|
|
|
[![Build Status](https://travis-ci.org/Tarrasch/zsh-autoenv.svg?branch=master)](https://travis-ci.org/Tarrasch/zsh-autoenv)
|
2013-09-08 19:50:20 +03:00
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
# Autoenv for Zsh
|
2013-09-08 18:14:28 +03:00
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
zsh-autoenv automatically sources (known/whitelisted) `.autoenv.zsh` files,
|
|
|
|
typically used in project root directories.
|
2013-09-08 18:14:28 +03:00
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
It handles "enter" and leave" events, nesting, and stashing of
|
|
|
|
variables (overwriting and restoring).
|
|
|
|
|
2017-12-16 00:34:53 +02:00
|
|
|
## Requirements
|
|
|
|
|
|
|
|
- Zsh version 4.3.10 or later.
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
## Features
|
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
- Support for enter and leave events, which can use the same file.
|
|
|
|
By default it uses `.autoenv.zsh` for entering, and `.autoenv_leave.zsh`
|
|
|
|
for leaving.
|
|
|
|
- Interactively asks for confirmation / authentication before sourcing an
|
|
|
|
unknown `.autoenv.zsh` file, and remembers whitelisted files by their
|
|
|
|
hashed content.
|
|
|
|
- Test suite.
|
2017-12-16 00:34:53 +02:00
|
|
|
- Written in/for Zsh.
|
2015-05-06 21:42:42 +03:00
|
|
|
|
|
|
|
### Variable stashing
|
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
You can use `autostash` in your `.autoenv.zsh` files to overwrite some
|
|
|
|
variable, e.g. `$PATH`. When leaving the directory, it will be automatically
|
|
|
|
restored.
|
2015-05-06 21:42:42 +03:00
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
% echo 'echo ENTERED; autostash FOO=changed' > project/.autoenv.zsh
|
2015-05-06 21:42:42 +03:00
|
|
|
% FOO=orig
|
|
|
|
% cd project
|
|
|
|
Attempting to load unauthorized env file!
|
2015-05-22 00:58:26 +03:00
|
|
|
-rw-rw-r-- 1 user user 36 Mai 6 20:38 /tmp/project/.autoenv.zsh
|
2015-05-06 21:42:42 +03:00
|
|
|
|
|
|
|
**********************************************
|
|
|
|
|
|
|
|
echo ENTERED; autostash FOO=changed
|
|
|
|
|
|
|
|
**********************************************
|
|
|
|
|
|
|
|
Would you like to authorize it? (type 'yes') yes
|
|
|
|
ENTERED
|
|
|
|
project % echo $FOO
|
|
|
|
changed
|
|
|
|
% cd ..
|
|
|
|
% echo $FOO
|
|
|
|
orig
|
|
|
|
|
|
|
|
There is also `stash`, `unstash` and `autounstash`, in case you want to
|
|
|
|
have more control.
|
|
|
|
|
|
|
|
The varstash library has been taken from smartcd, and was optimized for Zsh.
|
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
## Writing your .autoenv.zsh file
|
2015-05-06 21:42:42 +03:00
|
|
|
|
|
|
|
### `autoenv_source_parent()`
|
|
|
|
|
2015-10-09 23:13:36 +03:00
|
|
|
zsh-autoenv will stop looking for `.autoenv.zsh` files upwards after the first
|
|
|
|
one has been found, but you can use the function `autoenv_source_parent` to
|
|
|
|
source the next `.autoenv.zsh` file upwards the directory tree from there.
|
2013-09-08 18:14:28 +03:00
|
|
|
|
2015-10-09 23:13:36 +03:00
|
|
|
The function accepts an optional argument, which allows to stop looking before
|
|
|
|
the file system root is reached:
|
|
|
|
|
|
|
|
```zsh
|
|
|
|
autoenv_source_parent ../..
|
|
|
|
```
|
2013-09-08 18:14:28 +03:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
Clone the repository and source it from your `~/.zshrc` file:
|
|
|
|
|
|
|
|
% git clone https://github.com/Tarrasch/zsh-autoenv ~/.dotfiles/lib/zsh-autoenv
|
|
|
|
% echo 'source ~/.dotfiles/lib/zsh-autoenv/autoenv.zsh' >> ~/.zshrc
|
|
|
|
|
2014-12-11 17:31:41 +02:00
|
|
|
### Using [antigen](https://github.com/zsh-users/antigen)
|
2013-09-08 18:14:28 +03:00
|
|
|
|
|
|
|
antigen-bundle Tarrasch/zsh-autoenv
|
|
|
|
|
2015-02-18 17:30:43 +02:00
|
|
|
### Using [zgen](https://github.com/tarjoilija/zgen)
|
|
|
|
|
|
|
|
Add the following to your `.zshrc` where you are loading your plugins:
|
|
|
|
|
|
|
|
zgen load Tarrasch/zsh-autoenv
|
|
|
|
|
2016-05-27 05:33:06 +03:00
|
|
|
### Using [zplug](https://github.com/zplug/zplug)
|
|
|
|
|
|
|
|
Add the following to your `.zshrc` where you are loading your plugins:
|
|
|
|
|
|
|
|
zplug "Tarrasch/zsh-autoenv"
|
2014-12-11 17:31:41 +02:00
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
## Configuration
|
|
|
|
|
|
|
|
You can use the following variables to control zsh-autoenv's behavior.
|
|
|
|
Add them to your `~/.zshrc` file, before sourcing/loading zsh-autoenv.
|
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
### AUTOENV_FILE_ENTER
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
Name of the file to look for when entering directories.
|
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
Default: `.autoenv.zsh`
|
2015-05-06 21:42:42 +03:00
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
### AUTOENV_FILE_LEAVE
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
Name of the file to look for when leaving directories.
|
|
|
|
Requires `AUTOENV_HANDLE_LEAVE=1`.
|
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
Default: `.autoenv_leave.zsh`
|
2015-05-06 21:42:42 +03:00
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
### AUTOENV_LOOK_UPWARDS
|
|
|
|
|
2015-05-22 00:58:26 +03:00
|
|
|
Look for zsh-autoenv "enter" files in parent dirs?
|
2015-05-06 21:42:42 +03:00
|
|
|
|
|
|
|
Default: `1`
|
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
### AUTOENV_HANDLE_LEAVE
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
Handle leave events when changing away from a subtree, where an "enter"
|
|
|
|
event was handled?
|
|
|
|
|
|
|
|
Default: `1`
|
|
|
|
|
2017-12-14 02:18:44 +02:00
|
|
|
### AUTOENV_DISABLED
|
2017-10-18 01:29:43 +03:00
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
(Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.
|
|
|
|
|
|
|
|
Default: 0
|
|
|
|
|
2017-12-14 02:18:44 +02:00
|
|
|
### AUTOENV_DEBUG
|
2017-10-18 01:29:43 +03:00
|
|
|
|
|
|
|
Set debug level. If enabled (> 0) it will print information to stderr.
|
2015-05-06 21:42:42 +03:00
|
|
|
|
2017-04-30 15:14:25 +03:00
|
|
|
- 0: no debug messages
|
|
|
|
- 1: generic debug logging
|
|
|
|
- 2: more verbose messages
|
|
|
|
- messages about adding/removing files on the internal stack
|
|
|
|
- 3: everything
|
|
|
|
- sets xtrace option (`set -x`) while sourcing env files
|
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
Default: `0`
|
|
|
|
|
2017-08-11 15:15:46 +03:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
zsh-autoenv works automatically once installed.
|
|
|
|
|
|
|
|
You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
|
|
|
|
It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
|
|
|
|
|
2018-09-14 04:46:43 +03:00
|
|
|
## Helper functions
|
|
|
|
|
|
|
|
The following helper functions are available:
|
|
|
|
|
|
|
|
### autoenv_append_path
|
|
|
|
|
|
|
|
Appends path(s) to `$path` (`$PATH`), if they are not in there already.
|
|
|
|
|
|
|
|
### autoenv_prepend_path
|
|
|
|
|
|
|
|
Prepends path(s) to `$path` (`$PATH`), if they are not in there already.
|
|
|
|
|
|
|
|
### autoenv_remove_path
|
|
|
|
|
|
|
|
Removes path(s) from `$path` (`$PATH`).
|
|
|
|
|
|
|
|
Returns 0 in case `$path` has changed, 1 otherwise.
|
|
|
|
|
2017-04-24 01:41:27 +03:00
|
|
|
## Recipes
|
|
|
|
|
|
|
|
### Automatically activate Python virtualenvs
|
|
|
|
|
|
|
|
Given `AUTOENV_FILE_ENTER=.autoenv.zsh`, `AUTOENV_FILE_LEAVE=.autoenv.zsh` and
|
|
|
|
`AUTOENV_HANDLE_LEAVE=1` the following script will activate Python virtualenvs
|
|
|
|
automatically in all subdirectories (`.venv` directories get used by
|
|
|
|
[pipenv](https://github.com/kennethreitz/pipenv) with
|
|
|
|
`PIPENV_VENV_IN_PROJECT=1`):
|
|
|
|
|
|
|
|
```zsh
|
|
|
|
# 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
|
|
|
|
|
2017-04-27 23:39:52 +03:00
|
|
|
setopt localoptions extendedglob
|
2017-04-24 01:41:27 +03:00
|
|
|
local -a venv
|
2018-09-14 05:01:21 +03:00
|
|
|
venv=(./(../)#.venv(NY1:A))
|
2017-04-24 01:41:27 +03:00
|
|
|
|
2017-05-24 19:02:47 +03:00
|
|
|
if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
|
2017-04-24 01:41:27 +03:00
|
|
|
if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
|
|
|
|
unset _ZSH_ACTIVATED_VIRTUALENV
|
2017-05-24 19:02:47 +03:00
|
|
|
echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
|
2018-09-14 05:01:21 +03:00
|
|
|
|
|
|
|
# Simulate "deactivate", but handle $PATH better (remove VIRTUAL_ENV).
|
|
|
|
if ! autoenv_remove_path $VIRTUAL_ENV/bin; then
|
|
|
|
echo "warning: ${VIRTUAL_ENV}/bin not found in \$PATH" >&2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# NOTE: does not handle PYTHONHOME/_OLD_VIRTUAL_PYTHONHOME
|
|
|
|
unset _OLD_VIRTUAL_PYTHONHOME
|
|
|
|
# NOTE: does not handle PS1/_OLD_VIRTUAL_PS1
|
|
|
|
unset _OLD_VIRTUAL_PS1
|
|
|
|
unset VIRTUAL_ENV
|
2017-04-24 01:41:27 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$VIRTUAL_ENV" ]]; then
|
|
|
|
if (( $#venv )); then
|
2017-05-24 19:02:47 +03:00
|
|
|
echo "Activating virtualenv: ${(D)venv}" >&2
|
2018-09-14 05:01:21 +03:00
|
|
|
export VIRTUAL_ENV=$venv[1]
|
|
|
|
autoenv_prepend_path $VIRTUAL_ENV/bin
|
2017-04-24 01:41:27 +03:00
|
|
|
_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
|
|
|
|
```
|
2015-05-06 21:42:42 +03:00
|
|
|
|
|
|
|
## Related projects
|
|
|
|
|
2017-10-18 01:29:43 +03:00
|
|
|
- <https://github.com/direnv/direnv>
|
|
|
|
- <https://github.com/cxreg/smartcd>
|
|
|
|
- <https://github.com/kennethreitz/autoenv>
|
2014-12-11 17:31:41 +02:00
|
|
|
|
2015-05-06 21:42:42 +03:00
|
|
|
## History
|
2014-12-11 17:31:41 +02:00
|
|
|
|
2015-05-06 22:33:40 +03:00
|
|
|
This started as an optimized version of the bash plugin
|
|
|
|
[autoenv](https://github.com/kennethreitz/autoenv) but for Zsh, and grew a lot
|
|
|
|
of functionality on top of it (inspired by [smartcd]).
|
|
|
|
|
|
|
|
The code was initially based on
|
|
|
|
[@joshuaclayton](https://github.com/joshuaclayton)'s dotfiles.
|
|
|
|
In September 2013 [@Tarrasch](https://github.com/Tarrasch) packaged it into a
|
|
|
|
nice [antigen]-compatible unit with integration tests. Since November 2014,
|
2017-10-18 01:29:43 +03:00
|
|
|
[@blueyed](https://github.com/blueyed) took over and added many nice
|
2015-05-06 22:33:40 +03:00
|
|
|
features, mainly inspired by [smartcd].
|
|
|
|
|
|
|
|
[antigen]: https://github.com/Tarrasch/antigen-hs
|
|
|
|
[smartcd]: https://github.com/cxreg/smartcd
|