From 89137fa65e52be4dfe265f4f48b2f573c660ffcb Mon Sep 17 00:00:00 2001 From: Wryn Wagner Date: Tue, 14 Sep 2021 20:43:20 -0600 Subject: [PATCH] corrected clone; fixed activate bugs; nice error correction; zsh plugin w/shortcut; added alias option --- activator.plugin.zsh | 70 +++++++++++++++++++++++++++++++------ zsh/' | 43 ----------------------- zsh/activate.zsh | 24 +++++++------ zsh/clone.zsh | 9 +++-- zsh/custom-env-template.zsh | 11 ++++++ zsh/helpers.zsh | 45 +++++++++++++++--------- zsh/settings.zsh | 14 ++++++-- 7 files changed, 130 insertions(+), 86 deletions(-) delete mode 100644 zsh/' diff --git a/activator.plugin.zsh b/activator.plugin.zsh index 61a4185..e166a9f 100644 --- a/activator.plugin.zsh +++ b/activator.plugin.zsh @@ -5,8 +5,6 @@ # # ##################################################################### -##################################################################### - DEPENDENCIES=( fzf ) @@ -14,8 +12,8 @@ DEPENDENCIES=( IMPORTS=( "${0:a:h}/zsh/settings.zsh" "${0:a:h}/zsh/helpers.zsh" - "${0:a:h}/zsh/activate.zsh" "${0:a:h}/zsh/clone.zsh" + "${0:a:h}/zsh/activate.zsh" ) ##################################################################### @@ -46,10 +44,14 @@ CODE_ACTIVATOR() { local ERROR=0 case $1 in - clone ) _CA__CLONE ${@:2} || ERROR=1 ;; - * ) _CA__ACTIVATE ${@:2} || ERROR=2 ;; + deactivate ) _CA__RESTORE_ENVIRONMENT && cd || ERROR=1 ;; + clone ) _CA__CLONE ${@:2} || ERROR=1 ;; + new ) IS_NEW_PROJECT=1 _CA__CLONE ${@:2} || ERROR=1 ;; + * ) _CA__ACTIVATE ${@:1} || ERROR=42 ;; esac + [[ $ERROR -ne 0 ]] && _CA__ERROR_CLEANUP $ERROR + return $ERROR } @@ -63,12 +65,7 @@ _CODE_ACTIVATOR() { case $state in project ) - compadd clone - - for dir in $CODE_ACTIVATOR__DIRS - do - compadd "$(basename $dir)/$(basename $(ls -d -- $dir/*))" - done + compadd $(_CA__GET_COMMANDS_AND_PROJECTS | sed 's/deactivate//') ;; arguments ) case $words[2] in @@ -78,3 +75,54 @@ _CODE_ACTIVATOR() { esac } compdef _CODE_ACTIVATOR CODE_ACTIVATOR + +##################################################################### + +_CA__GET_COMMANDS_AND_PROJECTS() { + local COMMANDS=(deactivate clone new) + local PROJECTS=() + + for base_dir in $CODE_ACTIVATOR__DIRS + do + for project_dir in $(ls -d -- $base_dir/*) + do + PROJECTS+=("$(basename $base_dir)/$(basename $project_dir)") + done + done + + echo $COMMANDS $PROJECTS +} + +_CA__ERROR_CLEANUP() { + local ERROR="$1" + case $ERROR in + 42 ) + echo 'failed to activate environment; aborting' >&2 + ;; + esac +} + +##################################################################### + +[[ $CODE_ACTIVATOR__DISABLE_SHORTCUT -eq 0 ]] && { + _CA__ZSH_SHORTCUT_PLUGIN() { + local OPTIONS=$(_CA__GET_COMMANDS_AND_PROJECTS | sed 's/\s\+/\n/g') + + local ARGUMENT=$(\ + _CA__GET_COMMANDS_AND_PROJECTS \ + | sed 's/\s\+/\n/g' \ + | $_CA__FZF --prompt 'select a project: ' \ + ) + + _CA__IN_ZSH_PLUGIN=1 CODE_ACTIVATOR $ARGUMENT + + echo + zle reset-prompt + } + zle -N codeactivator _CA__ZSH_SHORTCUT_PLUGIN + bindkey $CODE_ACTIVATOR__SHORTCUT codeactivator +} + +[[ $CODE_ACTIVATOR__DISABLE_ALIAS -eq 0 ]] && { + alias $CODE_ACTIVATOR__ALIAS='CODE_ACTIVATOR' +} diff --git a/zsh/' b/zsh/' deleted file mode 100644 index 331e084..0000000 --- a/zsh/' +++ /dev/null @@ -1,43 +0,0 @@ -##################################################################### - -[ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh" -[ ! $_CA__HELPERS_LOADED ] && source "${0:a:h}/helpers.zsh" - -##################################################################### - -_CA__CLONE() { - local BASE_DIR="$1" - local REMOTE_TARGET="$2" - local PROJECT_NAME="$3" - - [ ! $BASE_DIR ] && BASE_DIR=$(_CA_SELECT_BASE_DIR) - [ ! $REMOTE_TARGET ] && REMOTE_TARGET=$(_CA_GET_REMOTE_TARGET) - [ ! $PROJECT_NAME ] && PROJECT_NAME=$(_CA_GET_PROJECT_NAME) -} - - -__CA__CLONE() { - local state - - _arguments \ - '1: :->base_dir' \ - '2: :->project_name' \ - '3: :->remote_target' \ - ':: :->arguments' \ - ; - - case $state in - base_dir ) - for dir in $CODE_ACTIVATOR__DIRS; do compadd $dir; done - ;; - project_name ) - ;; - remote_target ) - ;; - arguments ) ;; - esac -} - -##################################################################### - -export _CA__CLONE_LOADED=1 diff --git a/zsh/activate.zsh b/zsh/activate.zsh index 6c852c8..8769ab2 100644 --- a/zsh/activate.zsh +++ b/zsh/activate.zsh @@ -9,14 +9,20 @@ _CA__ACTIVATE() { _CA__RESTORE_ENVIRONMENT local PROJECT="$1" - [ ! $PROJECT ] && { cd; return 0; } + [ ! $PROJECT ] && { + [ ! $_CA__IN_ZSH_PLUGIN ] && cd + return 0 + } local PROJECT_PATH=$(_CA__GET_FULL_PATH $PROJECT) - [ ! -d $PROJECT_PATH ] && { cd; return 0; } + [ ! -d $PROJECT_PATH ] && return 1 + + local SOURCE_PATH="$PROJECT_PATH/$_CA__SOURCE_DIR_NAME" + [ ! -d $SOURCE_PATH ] && return 1 _CA__ACTIVATE_VIRTUAL_ENV $PROJECT_PATH _CA__ACTIVATE_CUSTOM_ENV $PROJECT_PATH - _CA__ACTIVATE_SOURCE_DIR $PROJECT_PATH + _CA__ACTIVATE_SOURCE_PATH $SOURCE_PATH _CA__TMUX_WINDOW_RENAME $PROJECT } @@ -32,7 +38,7 @@ _CA__ACTIVATE_VIRTUAL_ENV() { [ -f $NO_ENV ] && { echo 'no virtual environment here, boss!' } || { - _CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH + _CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH && source $ACTIVATE } } } @@ -41,14 +47,12 @@ _CA__ACTIVATE_CUSTOM_ENV() { local PROJECT_PATH="$1" local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME" - [ ! -f $CUSTOM_ENV ] || { - source $CUSTOM_ENV && echo 'custom environment active' - } + [ -f $CUSTOM_ENV ] && source $CUSTOM_ENV } -_CA__ACTIVATE_SOURCE_DIR() { - local SOURCE_DIR="$1/$_CA__SOURCE_DIR_NAME" - cd $SOURCE_DIR +_CA__ACTIVATE_SOURCE_PATH() { + local SOURCE_PATH="$1" + cd $SOURCE_PATH } ##################################################################### diff --git a/zsh/clone.zsh b/zsh/clone.zsh index eb49c10..2036450 100644 --- a/zsh/clone.zsh +++ b/zsh/clone.zsh @@ -2,6 +2,7 @@ [ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh" [ ! $_CA__HELPERS_LOADED ] && source "${0:a:h}/helpers.zsh" +[ ! $_CA__ACTIVATE_LOADED ] && source "${0:a:h}/activate.zsh" ##################################################################### @@ -29,9 +30,10 @@ _CA__CLONE() { } mkdir $PROJECT_PATH - _CA__CLONE_SOURCE $REMOTE_TARGET $PROJECT_PATH - _CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH + [ ! $IS_NEW_PROJECT ] && _CA__CLONE_SOURCE $REMOTE_TARGET $PROJECT_PATH _CA__INIT_CUSTOM_ENV $PROJECT_PATH + + _CA__ACTIVATE "$(basename $BASE_DIR)/$PROJECT_NAME" } @@ -69,7 +71,7 @@ _CA__CLONE_SOURCE() { echo "failed to clone '$REMOTE_TARGET'" printf "is this a new project? [y/N]" - read -k yn + _CA__READ_K yn [[ $yn =~ ^[yY] ]] && { printf 'initializing project...' { @@ -84,6 +86,7 @@ _CA__CLONE_SOURCE() { ; } >/dev/null >&1 && echo ' success :)' || echo ' failed :c' } || { + rm -rf -- $PROJECT_PATH echo 'exiting' return 1 } diff --git a/zsh/custom-env-template.zsh b/zsh/custom-env-template.zsh index 8059e9d..483a346 100644 --- a/zsh/custom-env-template.zsh +++ b/zsh/custom-env-template.zsh @@ -1,3 +1,14 @@ ##################################################################### +# Store initial PATH; allows for safe PATH manipulation for a particular project +export RESTORE_PATH="$PATH" + +# Store a list of project-specific environment variables; unset on env exit +export RESTORE_ENV=( + CUSTOM_ENV_VAR + ) + ##################################################################### + +# example env var +export CUSTOM_ENV_VAR=69 diff --git a/zsh/helpers.zsh b/zsh/helpers.zsh index 9a086b3..132e073 100644 --- a/zsh/helpers.zsh +++ b/zsh/helpers.zsh @@ -6,7 +6,7 @@ _CA__RESTORE_ENVIRONMENT() { deactivate >/dev/null 2>&1 - deactivate_node >dev/null 2>&1 + deactivate_node >/dev/null 2>&1 [ $_CA__RESTORE_PATH ] && export PATH="$_CA__RESTORE_PATH" [ $_CA__RESTORE_ENV ] && for var in $_CA__RESTORE_ENV; do unset $var; done @@ -41,21 +41,21 @@ _CA__SELECT_BASE_DIR() { done echo $CODE_ACTIVATOR__DIRS \ - | grep $(echo $BASE_NAMES | $FZF --prompt 'select base directory : ') + | grep $(echo $BASE_NAMES | $_CA__FZF --prompt 'select base directory : ') } _CA__GET_REMOTE_TARGET() { local REMOTE_TARGET=$(\ echo $CODE_ACTIVATOR__KNOWN_TARGETS \ | sed 's/\s\+/\n/g' \ - | $FZF --print-query --prompt 'set a remote target : ' \ + | $_CA__FZF --print-query --prompt 'set a remote target : ' \ | tail -1 \ ) local FIRST_SELECTION="$REMOTE_TARGET" echo $CODE_ACTIVATOR__KNOWN_TARGETS | grep -q "$REMOTE_TARGET" && { - REMOTE_TARGET="$REMOTE_TARGET$(echo | $FZF --print-query --prompt "$REMOTE_TARGET" | tail -1)" + REMOTE_TARGET="$REMOTE_TARGET$(echo | $_CA__FZF --print-query --prompt "$REMOTE_TARGET" | tail -1)" } [[ $REMOTE_TARGET == $FIRST_SELECTION ]] && return '' @@ -68,7 +68,7 @@ _CA__GET_PROJECT_NAME() { local REMOTE_TARGET="$1" local DEFAULT_NAME=$(basename $REMOTE_TARGET | sed 's/\.git$//') - echo $DEFAULT_NAME | $FZF --print-query --prompt 'set local project name : ' | tail -1 + echo $DEFAULT_NAME | $_CA__FZF --print-query --prompt 'set local project name : ' | tail -1 } _CA__INTERACTIVE_ENV_SETUP() { @@ -76,7 +76,7 @@ _CA__INTERACTIVE_ENV_SETUP() { local NO_ENV="$PROJECT_PATH/$_CA__NO_ENV_SENTINEL" printf 'set up a virtual environment now? [(Y)es / (n)o / n(e)ver] ' - read -k yn + _CA__READ_K yn case $yn in e ) touch $NO_ENV; return 1 ;; @@ -84,26 +84,34 @@ _CA__INTERACTIVE_ENV_SETUP() { esac local ENV_VERSION=$(_CA__SELECT_VIRTUAL_ENV) + [ ! $ENV_VERSION ] && return 1 + _CA__INIT_VIRTUAL_ENV $PROJECT_PATH $ENV_VERSION } _CA__SELECT_VIRTUAL_ENV() { - local VERISON=$(\ + local VERSION=$(\ echo "$(_CA__GET_PYTHON_IN_PATH)\n$(which node 2>/dev/null)" \ - | $FZF --prompt 'select a virtual environment : ' \ + | $_CA__FZF --prompt 'select a virtual environment : ' \ ) - [[ $VERSION =~ node$ ]] && VERSION=$(\ - nodeenv --list \ - | sed 's/\s\+/\n/g' \ - | $FZF --prompt 'select a node version : '\ - ) + [[ $VERSION =~ node$ ]] && { + FIRST_PICK=$VERSION + + VERSION=$(\ + nodeenv --list 2>&1 \ + | sed 's/\s\+/\n/g' \ + | $_CA__FZF --prompt 'select a node version : '\ + ) + + [[ $VERSION == $FIRST_PICK ]] && return + } echo $VERSION } _CA__GET_PYTHON_IN_PATH() { - whence -pm '*' | grep python | grep -v -- '-config$' + whence -pm '*' | grep python | grep -v -- '-config$\|m$' } _CA__INIT_VIRTUAL_ENV() { @@ -112,8 +120,8 @@ _CA__INIT_VIRTUAL_ENV() { local ENV_PATH="$PROJECT_PATH/$_CA__VIRTUAL_ENV_NAME" [[ $ENV_VERSION =~ ^[0-9] ]]\ - && _CA__INIT_NODE_ENV $PROJECT_PATH $ENV_VERSION \ - || _CA__INIT_VIRTUALENV $PROJECT_PATH $ENV_VERSION \ + && _CA__INIT_NODE_ENV $ENV_PATH $ENV_VERSION \ + || _CA__INIT_VIRTUALENV $ENV_PATH $ENV_VERSION \ ; } @@ -121,6 +129,7 @@ _CA__INIT_NODE_ENV() { local ENV_PATH="$1" local ENV_VERSION="$2" + "setting up node env ($ENV_VERSION)" nodeenv --node=$ENV_VERSION $ENV_PATH } @@ -128,13 +137,15 @@ _CA__INIT_VIRTUALENV() { local ENV_PATH="$1" local ENV_VERSION="$2" + "setting up virtualenv ($ENV_VERSION)" virtualenv --python=$ENV_VERSION $ENV_PATH } _CA__INIT_CUSTOM_ENV() { local PROJECT_PATH="$1" + + local CUSTOM_ENV_TEMPLATE="$_CA__CUSTOM_ENV_TEMPLATE" local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME" - local CUSTOM_ENV_TEMPLATE="$_CA__CUSTOM_ENV_TEMPALTE" cp $CUSTOM_ENV_TEMPLATE $CUSTOM_ENV } diff --git a/zsh/settings.zsh b/zsh/settings.zsh index c17b886..45db808 100644 --- a/zsh/settings.zsh +++ b/zsh/settings.zsh @@ -10,6 +10,12 @@ export CODE_ACTIVATOR__KNOWN_TARGETS=( 'git@bitbucket.org:' ) +[ ! $CODE_ACTIVATOR__SHORTCUT ] && export CODE_ACTIVATOR__SHORTCUT='' +[ ! $CODE_ACTIVATOR__ALIAS ] && export CODE_ACTIVATOR__ALIAS='lkj' + +[ ! $CODE_ACTIVATOR__DISABLE_SHORTCUT ] && export CODE_ACTIVATOR__DISABLE_SHORTCUT=0 +[ ! $CODE_ACTIVATOR__DISABLE_ALIAS ] && export CODE_ACTIVATOR__DISABLE_ALIAS=0 + ##################################################################### [ ! $_CA__SOURCE_DIR_NAME ] && export _CA__SOURCE_DIR_NAME='code' @@ -18,9 +24,13 @@ export CODE_ACTIVATOR__KNOWN_TARGETS=( [ ! $_CA__NO_ENV_SENTINEL ] && export _CA__NO_ENV_SENTINEL='.no-env' [ ! $_CA__GIT_MAIN_BRANCH ] && export _CA__GIT_MAIN_BRANCH='master' -[ ! $_CA__CUSTOM_ENV_TEMPLATE ] && export _CA__CUSTOM_ENV_TEMPLATE="${0:a:h}/custome-env-template.zsh" +[ ! $_CA__CUSTOM_ENV_TEMPLATE ] && export _CA__CUSTOM_ENV_TEMPLATE="${0:a:h}/custom-env-template.zsh" -FZF=(fzf -i --height=20% --layout=reverse) + +##################################################################### + +_CA__FZF=(fzf -i --height=20% --layout=reverse) +_CA__READ_K() { read -k $1; echo; } #####################################################################