diff --git a/dotfiles/lib/shellenv.sh b/dotfiles/lib/shellenv.sh index 9ff8a5b0..de7e2be0 100644 --- a/dotfiles/lib/shellenv.sh +++ b/dotfiles/lib/shellenv.sh @@ -1,58 +1,73 @@ 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" -add_to_back_of_path "$HOME/.rvm/bin" -add_to_front_of_path "$HOME/bin" -hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin" -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 && ""$@" +function _source_shellenv_files { + for filename in ~/.lib/shellenv/*; do + source $filename + done } -# Travis completion -[ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" +function _setup_env { + 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" -[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm -export NODE_PATH="/usr/local/lib/node_modules/" + export NVM_DIR="/Users/imalison/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm + 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 -add_to_front_of_path "$HOME/.rbenv/bin" -hash rbenv 2> /dev/null && eval "$(rbenv init -)" -hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin" + export RBENV_ROOT=/usr/local/var/rbenv + idem_add_to_front_of_path "$HOME/.rbenv/bin" + hash rbenv 2> /dev/null && eval "$(rbenv init -)" + 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 diff --git a/dotfiles/lib/shellenv/functions.sh b/dotfiles/lib/shellenv/functions.sh index 30187fee..bf3e29c7 100644 --- a/dotfiles/lib/shellenv/functions.sh +++ b/dotfiles/lib/shellenv/functions.sh @@ -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)" } +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 () { local string IFS @@ -347,6 +373,7 @@ function parse_timestamp { } function refresh_config { + unset ENVIRONMENT_SETUP_DONE source ~/.zshenv source ~/.zshrc } diff --git a/dotfiles/lib/shellenv/uber.sh b/dotfiles/lib/shellenv/uber.sh index 264181ce..a5bf4650 100644 --- a/dotfiles/lib/shellenv/uber.sh +++ b/dotfiles/lib/shellenv/uber.sh @@ -4,7 +4,6 @@ export UBER_OWNER="imalison@uber.com" [ -z "$GIT_SSH" ] || export GIT_SSH="$HOME/.lib/git-ssh.sh" export VAGRANT_DEFAULT_PROVIDER=aws [ -s "/usr/local/bin/virtualenvwrapper.sh" ] && . /usr/local/bin/virtualenvwrapper.sh -[ -s "$HOME/.nvm/nvm.sh" ] && . $HOME/.nvm/nvm.sh cdsync () { cd $(boxer sync_dir $@) diff --git a/dotfiles/zshenv b/dotfiles/zshenv index 21fe1f91..d7accd59 100644 --- a/dotfiles/zshenv +++ b/dotfiles/zshenv @@ -1 +1,11 @@ -source ~/.lib/shellenv.sh \ No newline at end of file +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