add nodeenv

This commit is contained in:
Chen Houwu 2014-01-04 18:34:27 +08:00
commit 085d21617d
3 changed files with 49 additions and 0 deletions

47
plugins/nodeenv/README.md Normal file
View file

@ -0,0 +1,47 @@
oh-my-zsh::plugin::nodeenv-prompt
====================================
This is a plugin derived from
[virtualenv prompt](https://github.com/tonyseek/oh-my-zsh-virtualenv-prompt).
It support to customize the
[nodeenv](https://github.com/ekalinin/nodeenv)
prompt in oh-my-zsh themes.
Customize Theme
---------------
There are two constant strings which could be overrided in your custom theme.
- `ZSH_THEME_NODEENV_PROMPT_PREFIX`
- `ZSH_THEME_NODEENV_PROMPT_SUFFIX`
And the function `nodeenv_prompt_info` could be used in the prompt of your
theme.
Example
-------
See the following theme codes for example:
function _virtualenv_prompt_info {
[[ -n $(whence virtualenv_prompt_info) ]] && virtualenv_prompt_info
}
function _nodeenv_prompt_info {
[[ -n $(whence nodeenv_prompt_info) ]] && nodeenv_prompt_info
}
ZSH_THEME_GIT_PROMPT_PREFIX=" %{$fg[green]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}!"
ZSH_THEME_GIT_PROMPT_CLEAN=""
function prompt_char {
if [ $UID -eq 0 ]; then echo "%{$fg[red]%}#%{$reset_color%}"; else echo $; fi
}
PROMPT='%(?, ,%{$fg[red]%}FAIL%{$reset_color%}
)
$(_nodeenv_prompt_info)$(_virtualenv_prompt_info)%{$fg[magenta]%}%n%{$reset_color%}@%{$fg[yellow]%}%m%{$reset_color%}: %{$fg_bold[blue]%}%~%{$reset_color%}$(git_prompt_info)
%_ $(prompt_char) '
RPROMPT='%{$fg[green]%}[%*]%{$reset_color%}'

View file

@ -0,0 +1,18 @@
export NODE_VIRTUAL_ENV_DISABLE_PROMPT=1
ZSH_THEME_NODEENV_PROMPT_PREFIX="("
ZSH_THEME_NODEENV_PROMPT_SUFFIX=")"
function nodeenv_prompt_info() {
if [ -n "$NODE_VIRTUAL_ENV" ]; then
if [ "`basename "$NODE_VIRTUAL_ENV"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
local name="[`basename \`dirname "$NODE_VIRTUAL_ENV"\``]"
else
local name=`basename "$NODE_VIRTUAL_ENV"`
fi
echo "$ZSH_THEME_NODEENV_PROMPT_PREFIX$name$ZSH_THEME_NODEENV_PROMPT_SUFFIX"
fi
}

View file

@ -0,0 +1,49 @@
if [[ ! $DISABLE_NODEENV_CD -eq 1 ]]; then
# Automatically activate Git projects's virtual environments based on the
# directory name of the project. Virtual environment name can be overridden
# by placing a .nodeenv file in the project root with a virtualenv name in it
function nodeenv_cwd {
if [ ! $NODEENV_CWD ]; then
NODEENV_CWD=1
# Check if this is a Git repo
NODEENV_PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
if (( $? != 0 )); then
NODEENV_PROJECT_ROOT="."
fi
# Check for virtualenv name override
if [[ -f "$NODEENV_PROJECT_ROOT/.nodeenv" ]]; then
NODEENV_NAME=`cat "$NODEENV_PROJECT_ROOT/.nodeenv"`
elif [[ -f "$NODEENV_PROJECT_ROOT/.nodeenv/bin/activate" ]];then
NODEENV_NAME="$NODEENV_PROJECT_ROOT/.nodeenv"
elif [[ "$NODEENV_PROJECT_ROOT" != "." ]]; then
NODEENV_NAME=`basename "$NODEENV_PROJECT_ROOT"`
else
NODEENV_NAME=""
fi
source $NODEENV_NAME/bin/activate && export CD_NODEENV="$NODEENV_NAME"
if [[ "$NODEENV_NAME" != "" ]]; then
# Activate the environment only if it is not already active
if [[ "$NODE_VIRTUAL_ENV" != "$WORKON_HOME/$NODEENV_NAME" ]]; then
source $NODEENV_NAME/bin/activate && export CD_NODEENV="$NODEENV_NAME"
fi
elif [ $CD_NODEENV ]; then
# We've just left the repo, deactivate the environment
# Note: this only happens if the virtualenv was activated automatically
deactivate_node && unset CD_NODEENV
fi
unset NODEENV_PROJECT_ROOT
unset NODEENV_CWD
fi
}
# Append workon_cwd to the chpwd_functions array, so it will be called on cd
# http://zsh.sourceforge.net/Doc/Release/Functions.html
# TODO: replace with 'add-zsh-hook chpwd workon_cwd' when oh-my-zsh min version is raised above 4.3.4
if (( ${+chpwd_functions} )); then
if (( $chpwd_functions[(I)nodeenv_cwd] == 0 )); then
set -A chpwd_functions $chpwd_functions nodeenv_cwd
fi
else
set -A chpwd_functions nodeenv_cwd
fi
fi