Refactor zsh_env startup so that it is possible to override PATH

variable once it has already been configured.
This commit is contained in:
Ivan Malison 2015-08-10 23:25:34 -07:00
parent f76b446ccf
commit b83433a807
4 changed files with 102 additions and 51 deletions

View File

@ -1,58 +1,73 @@
source ~/.lib/shellenv/functions.sh source ~/.lib/shellenv/functions.sh
command -v greadlink > /dev/null && alias readlink="greadlink"
add_to_back_of_path "$HOME/.local/lib/python2.6/site-packages" function _source_shellenv_files {
add_to_back_of_path "$HOME/.rvm/bin" for filename in ~/.lib/shellenv/*; do
add_to_front_of_path "$HOME/bin" source $filename
hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin" done
add_to_front_of_path "/usr/local/bin"
if is_osx; then
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
add_to_back_of_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources"
local JDK_LOCATION="$(find /Library/Java/JavaVirtualMachines -depth 1 | head -n 1)"
export JAVA_HOME="$JDK_LOCATION/Contents/Home"
export STUDIO_JDK=$JDK_LOCATION
export GRADLE_HOME="$(brew --prefix gradle)"
export ANDROID_HOME="$(brew --prefix android-sdk)"
add_to_back_of_path "$ANDROID_HOME"
# Access gnu man pages.
hash brew 2> /dev/null && export MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH"
else
export JAVA_HOME="$(update-alternatives --config java | get_cols ' -1' | head -n 1)"
is_osx && export VISUAL="which emacsclient -c -n"
fi
add_to_front_of_path "$JAVA_HOME/bin"
add_to_back_of_path "$(dotfiles_directory)/resources/python"
add_to_back_of_path "/usr/local/sbin"
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
for filename in ~/.lib/shellenv/*; do
source $filename
done
function with_shellrc {
zsh -c "source ~/.zshrc && ""$@"
} }
# Travis completion function _setup_env {
[ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" idem_add_to_back_of_path "$HOME/.local/lib/python2.6/site-packages"
idem_add_to_back_of_path "$HOME/.rvm/bin"
idem_add_to_front_of_path "$HOME/bin"
hash brew 2>/dev/null && idem_add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin"
idem_add_to_front_of_path "/usr/local/bin"
test -e /usr/libexec/path_helper && eval `/usr/libexec/path_helper -s` if is_osx; then
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
idem_add_to_back_of_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources"
local JDK_LOCATION="$(find /Library/Java/JavaVirtualMachines -depth 1 | head -n 1)"
export JAVA_HOME="$JDK_LOCATION/Contents/Home"
export STUDIO_JDK=$JDK_LOCATION
export GRADLE_HOME="$(brew --prefix gradle)"
export ANDROID_HOME="$(brew --prefix android-sdk)"
idem_add_to_back_of_path "$ANDROID_HOME"
# Access gnu man pages.
hash brew 2> /dev/null && export MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH"
else
export JAVA_HOME="$(update-alternatives --config java | get_cols ' -1' | head -n 1)"
is_osx && export VISUAL="which emacsclient -c -n"
fi
idem_add_to_front_of_path "$JAVA_HOME/bin"
idem_add_to_back_of_path "$(dotfiles_directory)/resources/python"
idem_add_to_back_of_path "/usr/local/sbin"
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
_source_shellenv_files
function with_shellrc {
zsh -c "source ~/.zshrc && ""$@"
}
# Travis completion
[ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh"
test -e /usr/libexec/path_helper && eval `/usr/libexec/path_helper -s`
export NVM_DIR="/Users/imalison/.nvm" export NVM_DIR="/Users/imalison/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
export NODE_PATH="/usr/local/lib/node_modules/" export NODE_PATH="/usr/local/lib/node_modules/"
add_to_front_of_path "$HOME/.lib/python" 'PYTHONPATH' idem_add_to_front_of_path "$HOME/.local/bin"
idem_add_to_front_of_path "$HOME/.lib/python" 'PYTHONPATH'
export RBENV_ROOT=/usr/local/var/rbenv export RBENV_ROOT=/usr/local/var/rbenv
add_to_front_of_path "$HOME/.rbenv/bin" idem_add_to_front_of_path "$HOME/.rbenv/bin"
hash rbenv 2> /dev/null && eval "$(rbenv init -)" hash rbenv 2> /dev/null && eval "$(rbenv init -)"
hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin" hash brew 2>/dev/null && idem_add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin"
export ENVIRONMENT_SETUP_DONE="$(date)"
}
if [ -z ${ENVIRONMENT_SETUP_DONE+x} ]; then
_setup_env
else
_source_shellenv_files
fi

View File

@ -33,6 +33,32 @@ function add_to_back_of_path {
export $target="$(_add_to_back_of_path_lines $1 $target | tr '\n' ':' | remove_trailing_colon)" export $target="$(_add_to_back_of_path_lines $1 $target | tr '\n' ':' | remove_trailing_colon)"
} }
function idem_add_to_front_of_path {
target=${2-PATH}
exists_in_path_var $1 $target || add_to_front_of_path $1 $target
}
function idem_add_to_back_of_path {
target=${2-PATH}
exists_in_path_var $1 $target || add_to_back_of_path $1 $target
}
function indirect_expand {
eval "value=\"\${$1}\""
echo $value
}
function environment_variable_exists {
eval "value=\"\${$1+x}\""
[ ! -z $value ]
}
function exists_in_path_var {
target=${2-PATH}
local path_contents="$(indirect_expand $target)"
[[ ":$path_contents:" == *":$1:"* ]]
}
function split_into_vars () { function split_into_vars () {
local string IFS local string IFS
@ -347,6 +373,7 @@ function parse_timestamp {
} }
function refresh_config { function refresh_config {
unset ENVIRONMENT_SETUP_DONE
source ~/.zshenv source ~/.zshenv
source ~/.zshrc source ~/.zshrc
} }

View File

@ -4,7 +4,6 @@ export UBER_OWNER="imalison@uber.com"
[ -z "$GIT_SSH" ] || export GIT_SSH="$HOME/.lib/git-ssh.sh" [ -z "$GIT_SSH" ] || export GIT_SSH="$HOME/.lib/git-ssh.sh"
export VAGRANT_DEFAULT_PROVIDER=aws export VAGRANT_DEFAULT_PROVIDER=aws
[ -s "/usr/local/bin/virtualenvwrapper.sh" ] && . /usr/local/bin/virtualenvwrapper.sh [ -s "/usr/local/bin/virtualenvwrapper.sh" ] && . /usr/local/bin/virtualenvwrapper.sh
[ -s "$HOME/.nvm/nvm.sh" ] && . $HOME/.nvm/nvm.sh
cdsync () { cdsync () {
cd $(boxer sync_dir $@) cd $(boxer sync_dir $@)

View File

@ -1 +1,11 @@
function init_profile {
zmodload zsh/datetime
setopt promptsubst
PS4='+$EPOCHREALTIME %N:%i> '
# save file stderr to file descriptor 3 and redirect stderr (including trace
# output) to a file with the script's PID as an extension
exec 3>&2 2>/tmp/startlog.$$
# set options to turn on tracing and expansion of commands contained in the prompt
setopt xtrace prompt_subst
}
source ~/.lib/shellenv.sh source ~/.lib/shellenv.sh