From e2c5cd60338bd8d641c15bc65c361afca5e25cc4 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 16 Aug 2016 15:26:50 -0700 Subject: [PATCH] Refactor shellenv/setup for better performance --- dotfiles/lib/setup_functions.sh | 36 ++++++++ dotfiles/lib/shellenv.sh | 140 ++++------------------------- dotfiles/lib/shellenv/functions.sh | 15 +--- dotfiles/lib/shellpath.sh | 96 ++++++++++++++++++++ dotfiles/lib/shellrc.sh | 4 + dotfiles/zshenv | 2 +- 6 files changed, 156 insertions(+), 137 deletions(-) create mode 100644 dotfiles/lib/setup_functions.sh create mode 100644 dotfiles/lib/shellpath.sh diff --git a/dotfiles/lib/setup_functions.sh b/dotfiles/lib/setup_functions.sh new file mode 100644 index 00000000..55ec8840 --- /dev/null +++ b/dotfiles/lib/setup_functions.sh @@ -0,0 +1,36 @@ +_python_command="" + +function _set_python_command { + # See comment in add_to_path about why this is necessary + if hash pyenv 2>/dev/null; + then + _python_command="$(pyenv which python)" + else + _python_command="$(which python)" + fi +} + +_set_python_command + +function add_to_path { + # We need to get a path to the ACTUAL python command because + # pyenv alters PATH before actually executing python, which ends + # up changing PATH in a way that is not desireable. + eval "$($_python_command $HOME/.lib/python/shell_path.py --include-assignment "$@")" +} + +function is_osx() { + case `uname` in + 'Darwin') + return 0 + ;; + *) + return 1; + ;; + esac +} + +function environment_variable_exists { + eval "value=\"\${$1+x}\"" + [ ! -z $value ] +} diff --git a/dotfiles/lib/shellenv.sh b/dotfiles/lib/shellenv.sh index 35403f9d..94ab0bd8 100644 --- a/dotfiles/lib/shellenv.sh +++ b/dotfiles/lib/shellenv.sh @@ -1,125 +1,4 @@ -. "$HOME/.lib/shellenv/functions.sh" -command -v greadlink > /dev/null && alias readlink="greadlink" - -function _source_shellenv_files { - for filename in ~/.lib/shellenv/*; do - source $filename - done -} - -function add_to_path { - local python_command - local result - - # We need to get a path to the ACTUAL python command because - # pyenv alters PATH before actually executing python, which ends - # up changing PATH in a way that is not desireable. - hash pyenv 2>/dev/null && python_command="$(pyenv which python)" || python_command="$(which python)" - - result=$($python_command $HOME/.lib/python/shell_path.py --include-assignment "$@") - eval "$result" -} - -function get_python_scripts_path { - python -c "import sysconfig; print sysconfig.get_path('scripts')" -} - -function _setup_env { - _path_helper - - add_to_path "$HOME/.local/bin" "$HOME/.lib/bin" "$HOME/bin" "/usr/local/bin" --before - is_osx && _osx_path_setup - _java_setup - _go_setup - _rust_setup - _tex_setup - - # Travis completion - # [ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" - - export ENVIRONMENT_SETUP_DONE="$(date)" -} - -function _osx_path_setup { - hash brew 2>/dev/null && add_to_path --before "$(brew --prefix coreutils)/libexec/gnubin" - - # Adds airport utility - add_to_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources" --after - - # I believe that this is here because of some issue with - # python builds in OSX - export CFLAGS=-Qunused-arguments - export CPPFLAGS=-Qunused-arguments -} - -function _python_setup { - add_to_path "$HOME/.lib/python" --after - export PYENV_ROOT="/usr/local/var/pyenv" - - if which pyenv > /dev/null; then - eval "$(pyenv init - --no-rehash)" - else - echo "WARNING: pyenv is not installed on this machine and python will likely not function correctly" - fi - - if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi - - add_to_path "$HOME/.lib/python" --path-var 'PYTHONPATH' -} - -function _node_setup { - # node/nvm - 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/" -} - -function _java_setup { - if is_osx; then - 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_path "$ANDROID_HOME" --after - - # Access gnu man pages. - hash brew 2> /dev/null && export MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH" - else - # This may be ubuntu/debian specific - export JAVA_HOME="$(update-alternatives --config java | get_cols ' -1' | head -n 1)" - is_osx && export VISUAL="which emacsclient -c -n" - fi - - add_to_path "$JAVA_HOME/bin" -} - -function _go_setup { - add_to_path "$HOME/go" --path-var 'GOPATH' - add_to_path "${GOPATH//://bin:}/bin" -} - -function _rust_setup { - add_to_path "$HOME/.cargo/bin" -} - -function _ruby_setup { - export RBENV_ROOT="$(brew --prefix rbenv)" - add_to_path "$RBENV_ROOT/bin" - hash rbenv 2> /dev/null && eval "$(rbenv init - --no-rehash)" -} - -function _tex_setup { - is_osx && add_to_path "/Library/TeX/texbin/" -} - -function _path_helper { - if [ -f /usr/libexec/path_helper ]; - then - export PATH_HELPER_RAN="$(date)" - eval "$(/usr/libexec/path_helper -s)" - fi -} +source "$HOME/.lib/shellpath.sh" environment_variable_exists ENVIRONMENT_SETUP_DONE || _setup_env @@ -127,8 +6,19 @@ environment_variable_exists ENVIRONMENT_SETUP_DONE || _setup_env # version managers work. This could cause problems with the situation # where we want to intentionally override the python/ruby/node # versions in use in a given shell. -_node_setup -_ruby_setup -_python_setup + +# TODO: Ruby and node are disabled to speed up shell startup... +# See https://github.com/creationix/nvm/issues/709 for nvm +# _node_setup + +# XXX: these were moved to _setup_env +# _ruby_setup +# _python_setup + +function _source_shellenv_files { + for filename in ~/.lib/shellenv/*; do + source $filename + done +} _source_shellenv_files diff --git a/dotfiles/lib/shellenv/functions.sh b/dotfiles/lib/shellenv/functions.sh index c977e99a..2e49a621 100644 --- a/dotfiles/lib/shellenv/functions.sh +++ b/dotfiles/lib/shellenv/functions.sh @@ -1,3 +1,7 @@ +function get_python_scripts_path { + python -c "import sysconfig; print sysconfig.get_path('scripts')" +} + function path_lines { local python_command # We need to get a path to the ACTUAL python command because @@ -148,17 +152,6 @@ function is_ssh() { test $SSH_CLIENT } -function is_osx() { - case `uname` in - 'Darwin') - return 0 - ;; - *) - return 1; - ;; - esac -} - # TODO: Remove this. alias clipboard='oscopy' diff --git a/dotfiles/lib/shellpath.sh b/dotfiles/lib/shellpath.sh new file mode 100644 index 00000000..6efd9b16 --- /dev/null +++ b/dotfiles/lib/shellpath.sh @@ -0,0 +1,96 @@ +source "$HOME/.lib/setup_functions.sh" + +function _setup_env { + _path_helper + + add_to_path "$HOME/.local/bin" "$HOME/.lib/bin" "$HOME/bin" "/usr/local/bin" --before + _ruby_setup + _python_setup + is_osx && _osx_path_setup + _java_setup + _go_setup + _rust_setup + _tex_setup + + export ENVIRONMENT_SETUP_DONE="$(date)" +} + +function _osx_path_setup { + hash brew 2>/dev/null && add_to_path --before "$(brew --prefix coreutils)/libexec/gnubin" + + # Adds airport utility + add_to_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources" --after + + # I believe that this is here because of some issue with + # python builds in OSX + export CFLAGS=-Qunused-arguments + export CPPFLAGS=-Qunused-arguments +} + +function _python_setup { + add_to_path "$HOME/.lib/python" --after + export PYENV_ROOT="/usr/local/var/pyenv" + + if which pyenv > /dev/null; then + eval "$(pyenv init - --no-rehash)" + else + echo "WARNING: pyenv is not installed on this machine and python will likely not function correctly" + fi + + # if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi + + add_to_path "$HOME/.lib/python" --path-var 'PYTHONPATH' +} + +function _node_setup { + # node/nvm + 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/" +} + +function _java_setup { + if is_osx; then + 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_path "$ANDROID_HOME" --after + + # Access gnu man pages. + hash brew 2> /dev/null && export MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH" + else + # This may be ubuntu/debian specific + export JAVA_HOME="$(update-alternatives --config java | get_cols ' -1' | head -n 1)" + is_osx && export VISUAL="which emacsclient -c -n" + fi + + add_to_path "$JAVA_HOME/bin" +} + +function _go_setup { + add_to_path "$HOME/go" --path-var 'GOPATH' + add_to_path "${GOPATH//://bin:}/bin" +} + +function _rust_setup { + add_to_path "$HOME/.cargo/bin" +} + +function _ruby_setup { + export RBENV_ROOT="$(brew --prefix rbenv)" + add_to_path "$RBENV_ROOT/bin" + hash rbenv 2> /dev/null && eval "$(rbenv init - --no-rehash)" +} + +function _tex_setup { + is_osx && add_to_path "/Library/TeX/texbin/" +} + +function _path_helper { + if [ -f /usr/libexec/path_helper ]; + then + eval "$(/usr/libexec/path_helper -s)" + fi +} diff --git a/dotfiles/lib/shellrc.sh b/dotfiles/lib/shellrc.sh index 998ecfc1..54c4f864 100644 --- a/dotfiles/lib/shellrc.sh +++ b/dotfiles/lib/shellrc.sh @@ -1,6 +1,10 @@ for filename in ~/.lib/shellrc/*; do source $filename done + [ -s "/usr/local/bin/virtualenvwrapper.sh" ] && . /usr/local/bin/virtualenvwrapper.sh [[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh environment_variable_exists INSIDE_EMACS && inside_emacs_hook + +# travis completion +[ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" diff --git a/dotfiles/zshenv b/dotfiles/zshenv index f45da51a..a0825785 100644 --- a/dotfiles/zshenv +++ b/dotfiles/zshenv @@ -1,2 +1,2 @@ source ~/.lib/shellenv.sh -test -r ~/.customenv.sh && source ~/.customenv.sh \ No newline at end of file +test -r ~/.customenv.sh && source ~/.customenv.sh