diff options
author | 2025-02-13 14:13:49 +0530 | |
---|---|---|
committer | 2025-02-13 14:13:49 +0530 | |
commit | 8a2e1006b3b272126332aa064f3ad95387129544 (patch) | |
tree | 944c80ac612a65980d94a54ba11b6c7102037ecf /.config/zsh/oh-my-zsh/plugins/autoenv | |
parent | dcbb16d8b08ff5956abef5e6478b59df2e93ad35 (diff) | |
download | dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.tar.gz dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.tar.bz2 dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.zip |
Diffstat (limited to '.config/zsh/oh-my-zsh/plugins/autoenv')
-rw-r--r-- | .config/zsh/oh-my-zsh/plugins/autoenv/README.md | 20 | ||||
-rw-r--r-- | .config/zsh/oh-my-zsh/plugins/autoenv/autoenv.plugin.zsh | 72 |
2 files changed, 92 insertions, 0 deletions
diff --git a/.config/zsh/oh-my-zsh/plugins/autoenv/README.md b/.config/zsh/oh-my-zsh/plugins/autoenv/README.md new file mode 100644 index 0000000..5dfb5fb --- /dev/null +++ b/.config/zsh/oh-my-zsh/plugins/autoenv/README.md @@ -0,0 +1,20 @@ +# Autoenv plugin + +This plugin loads the [Autoenv](https://github.com/inishchith/autoenv). + +To use it, add `autoenv` to the plugins array in your zshrc file: + +```zsh +plugins=(... autoenv) +``` + +## Functions + +* `use_env()`: creates and/or activates a virtualenv. For use in `.env` files. + See the source code for details. + +## Requirements + +In order to make this work, you will need to have the autoenv installed. + +More info on the usage and install at [the project's homepage](https://github.com/inishchith/autoenv). diff --git a/.config/zsh/oh-my-zsh/plugins/autoenv/autoenv.plugin.zsh b/.config/zsh/oh-my-zsh/plugins/autoenv/autoenv.plugin.zsh new file mode 100644 index 0000000..229a8a8 --- /dev/null +++ b/.config/zsh/oh-my-zsh/plugins/autoenv/autoenv.plugin.zsh @@ -0,0 +1,72 @@ +# Initialization: activate autoenv or report its absence +() { +local d autoenv_dir install_locations +if ! type autoenv_init >/dev/null; then + # Check if activate.sh is in $PATH + if (( $+commands[activate.sh] )); then + autoenv_dir="${commands[activate.sh]:h}" + fi + + # Locate autoenv installation + if [[ -z $autoenv_dir ]]; then + install_locations=( + ~/.autoenv + ~/.local/bin + /usr/local/opt/autoenv + /opt/homebrew/opt/autoenv + /usr/local/bin + /usr/share/autoenv-git + ~/Library/Python/bin + ) + for d ( $install_locations ); do + if [[ -e $d/activate.sh ]]; then + autoenv_dir=$d + break + fi + done + fi + + # Look for Homebrew path as a last resort + if [[ -z "$autoenv_dir" ]] && (( $+commands[brew] )); then + d=$(brew --prefix)/opt/autoenv + if [[ -e $d/activate.sh ]]; then + autoenv_dir=$d + fi + fi + + # Complain if autoenv is not installed + if [[ -z $autoenv_dir ]]; then + cat <<END >&2 +-------- AUTOENV --------- +Could not locate autoenv installation. +Please check if autoenv is correctly installed. +In the meantime the autoenv plugin is DISABLED. +-------------------------- +END + return 1 + fi + # Load autoenv + source $autoenv_dir/activate.sh +fi +} +[[ $? != 0 ]] && return $? + +# The use_env call below is a reusable command to activate/create a new Python +# virtualenv, requiring only a single declarative line of code in your .env files. +# It only performs an action if the requested virtualenv is not the current one. + +use_env() { + local venv + venv="$1" + if [[ "${VIRTUAL_ENV:t}" != "$venv" ]]; then + if workon | grep -q "$venv"; then + workon "$venv" + else + echo -n "Create virtualenv $venv now? (Yn) " + read answer + if [[ "$answer" == "Y" ]]; then + mkvirtualenv "$venv" + fi + fi + fi +} |