corrected clone; fixed activate bugs; nice error correction; zsh plugin w/shortcut; added alias option

This commit is contained in:
Wryn Wagner 2021-09-14 20:43:20 -06:00
parent 161e24c523
commit 89137fa65e
7 changed files with 130 additions and 86 deletions

View File

@ -5,8 +5,6 @@
# # # #
##################################################################### #####################################################################
#####################################################################
DEPENDENCIES=( DEPENDENCIES=(
fzf fzf
) )
@ -14,8 +12,8 @@ DEPENDENCIES=(
IMPORTS=( IMPORTS=(
"${0:a:h}/zsh/settings.zsh" "${0:a:h}/zsh/settings.zsh"
"${0:a:h}/zsh/helpers.zsh" "${0:a:h}/zsh/helpers.zsh"
"${0:a:h}/zsh/activate.zsh"
"${0:a:h}/zsh/clone.zsh" "${0:a:h}/zsh/clone.zsh"
"${0:a:h}/zsh/activate.zsh"
) )
##################################################################### #####################################################################
@ -46,10 +44,14 @@ CODE_ACTIVATOR() {
local ERROR=0 local ERROR=0
case $1 in case $1 in
clone ) _CA__CLONE ${@:2} || ERROR=1 ;; deactivate ) _CA__RESTORE_ENVIRONMENT && cd || ERROR=1 ;;
* ) _CA__ACTIVATE ${@:2} || ERROR=2 ;; clone ) _CA__CLONE ${@:2} || ERROR=1 ;;
new ) IS_NEW_PROJECT=1 _CA__CLONE ${@:2} || ERROR=1 ;;
* ) _CA__ACTIVATE ${@:1} || ERROR=42 ;;
esac esac
[[ $ERROR -ne 0 ]] && _CA__ERROR_CLEANUP $ERROR
return $ERROR return $ERROR
} }
@ -63,12 +65,7 @@ _CODE_ACTIVATOR() {
case $state in case $state in
project ) project )
compadd clone compadd $(_CA__GET_COMMANDS_AND_PROJECTS | sed 's/deactivate//')
for dir in $CODE_ACTIVATOR__DIRS
do
compadd "$(basename $dir)/$(basename $(ls -d -- $dir/*))"
done
;; ;;
arguments ) arguments )
case $words[2] in case $words[2] in
@ -78,3 +75,54 @@ _CODE_ACTIVATOR() {
esac esac
} }
compdef _CODE_ACTIVATOR CODE_ACTIVATOR 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'
}

43
zsh/'
View File

@ -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

View File

@ -9,14 +9,20 @@ _CA__ACTIVATE() {
_CA__RESTORE_ENVIRONMENT _CA__RESTORE_ENVIRONMENT
local PROJECT="$1" local PROJECT="$1"
[ ! $PROJECT ] && { cd; return 0; } [ ! $PROJECT ] && {
[ ! $_CA__IN_ZSH_PLUGIN ] && cd
return 0
}
local PROJECT_PATH=$(_CA__GET_FULL_PATH $PROJECT) 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_VIRTUAL_ENV $PROJECT_PATH
_CA__ACTIVATE_CUSTOM_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 _CA__TMUX_WINDOW_RENAME $PROJECT
} }
@ -32,7 +38,7 @@ _CA__ACTIVATE_VIRTUAL_ENV() {
[ -f $NO_ENV ] && { [ -f $NO_ENV ] && {
echo 'no virtual environment here, boss!' 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 PROJECT_PATH="$1"
local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME" local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME"
[ ! -f $CUSTOM_ENV ] || { [ -f $CUSTOM_ENV ] && source $CUSTOM_ENV
source $CUSTOM_ENV && echo 'custom environment active'
}
} }
_CA__ACTIVATE_SOURCE_DIR() { _CA__ACTIVATE_SOURCE_PATH() {
local SOURCE_DIR="$1/$_CA__SOURCE_DIR_NAME" local SOURCE_PATH="$1"
cd $SOURCE_DIR cd $SOURCE_PATH
} }
##################################################################### #####################################################################

View File

@ -2,6 +2,7 @@
[ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh" [ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh"
[ ! $_CA__HELPERS_LOADED ] && source "${0:a:h}/helpers.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 mkdir $PROJECT_PATH
_CA__CLONE_SOURCE $REMOTE_TARGET $PROJECT_PATH [ ! $IS_NEW_PROJECT ] && _CA__CLONE_SOURCE $REMOTE_TARGET $PROJECT_PATH
_CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH
_CA__INIT_CUSTOM_ENV $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'" echo "failed to clone '$REMOTE_TARGET'"
printf "is this a new project? [y/N]" printf "is this a new project? [y/N]"
read -k yn _CA__READ_K yn
[[ $yn =~ ^[yY] ]] && { [[ $yn =~ ^[yY] ]] && {
printf 'initializing project...' printf 'initializing project...'
{ {
@ -84,6 +86,7 @@ _CA__CLONE_SOURCE() {
; ;
} >/dev/null >&1 && echo ' success :)' || echo ' failed :c' } >/dev/null >&1 && echo ' success :)' || echo ' failed :c'
} || { } || {
rm -rf -- $PROJECT_PATH
echo 'exiting' echo 'exiting'
return 1 return 1
} }

View File

@ -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

View File

@ -6,7 +6,7 @@
_CA__RESTORE_ENVIRONMENT() { _CA__RESTORE_ENVIRONMENT() {
deactivate >/dev/null 2>&1 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_PATH ] && export PATH="$_CA__RESTORE_PATH"
[ $_CA__RESTORE_ENV ] && for var in $_CA__RESTORE_ENV; do unset $var; done [ $_CA__RESTORE_ENV ] && for var in $_CA__RESTORE_ENV; do unset $var; done
@ -41,21 +41,21 @@ _CA__SELECT_BASE_DIR() {
done done
echo $CODE_ACTIVATOR__DIRS \ 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() { _CA__GET_REMOTE_TARGET() {
local REMOTE_TARGET=$(\ local REMOTE_TARGET=$(\
echo $CODE_ACTIVATOR__KNOWN_TARGETS \ echo $CODE_ACTIVATOR__KNOWN_TARGETS \
| sed 's/\s\+/\n/g' \ | sed 's/\s\+/\n/g' \
| $FZF --print-query --prompt 'set a remote target : ' \ | $_CA__FZF --print-query --prompt 'set a remote target : ' \
| tail -1 \ | tail -1 \
) )
local FIRST_SELECTION="$REMOTE_TARGET" local FIRST_SELECTION="$REMOTE_TARGET"
echo $CODE_ACTIVATOR__KNOWN_TARGETS | grep -q "$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 '' [[ $REMOTE_TARGET == $FIRST_SELECTION ]] && return ''
@ -68,7 +68,7 @@ _CA__GET_PROJECT_NAME() {
local REMOTE_TARGET="$1" local REMOTE_TARGET="$1"
local DEFAULT_NAME=$(basename $REMOTE_TARGET | sed 's/\.git$//') 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() { _CA__INTERACTIVE_ENV_SETUP() {
@ -76,7 +76,7 @@ _CA__INTERACTIVE_ENV_SETUP() {
local NO_ENV="$PROJECT_PATH/$_CA__NO_ENV_SENTINEL" local NO_ENV="$PROJECT_PATH/$_CA__NO_ENV_SENTINEL"
printf 'set up a virtual environment now? [(Y)es / (n)o / n(e)ver] ' printf 'set up a virtual environment now? [(Y)es / (n)o / n(e)ver] '
read -k yn _CA__READ_K yn
case $yn in case $yn in
e ) touch $NO_ENV; return 1 ;; e ) touch $NO_ENV; return 1 ;;
@ -84,26 +84,34 @@ _CA__INTERACTIVE_ENV_SETUP() {
esac esac
local ENV_VERSION=$(_CA__SELECT_VIRTUAL_ENV) local ENV_VERSION=$(_CA__SELECT_VIRTUAL_ENV)
[ ! $ENV_VERSION ] && return 1
_CA__INIT_VIRTUAL_ENV $PROJECT_PATH $ENV_VERSION _CA__INIT_VIRTUAL_ENV $PROJECT_PATH $ENV_VERSION
} }
_CA__SELECT_VIRTUAL_ENV() { _CA__SELECT_VIRTUAL_ENV() {
local VERISON=$(\ local VERSION=$(\
echo "$(_CA__GET_PYTHON_IN_PATH)\n$(which node 2>/dev/null)" \ 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=$(\ [[ $VERSION =~ node$ ]] && {
nodeenv --list \ FIRST_PICK=$VERSION
| sed 's/\s\+/\n/g' \
| $FZF --prompt 'select a node version : '\ VERSION=$(\
) nodeenv --list 2>&1 \
| sed 's/\s\+/\n/g' \
| $_CA__FZF --prompt 'select a node version : '\
)
[[ $VERSION == $FIRST_PICK ]] && return
}
echo $VERSION echo $VERSION
} }
_CA__GET_PYTHON_IN_PATH() { _CA__GET_PYTHON_IN_PATH() {
whence -pm '*' | grep python | grep -v -- '-config$' whence -pm '*' | grep python | grep -v -- '-config$\|m$'
} }
_CA__INIT_VIRTUAL_ENV() { _CA__INIT_VIRTUAL_ENV() {
@ -112,8 +120,8 @@ _CA__INIT_VIRTUAL_ENV() {
local ENV_PATH="$PROJECT_PATH/$_CA__VIRTUAL_ENV_NAME" local ENV_PATH="$PROJECT_PATH/$_CA__VIRTUAL_ENV_NAME"
[[ $ENV_VERSION =~ ^[0-9] ]]\ [[ $ENV_VERSION =~ ^[0-9] ]]\
&& _CA__INIT_NODE_ENV $PROJECT_PATH $ENV_VERSION \ && _CA__INIT_NODE_ENV $ENV_PATH $ENV_VERSION \
|| _CA__INIT_VIRTUALENV $PROJECT_PATH $ENV_VERSION \ || _CA__INIT_VIRTUALENV $ENV_PATH $ENV_VERSION \
; ;
} }
@ -121,6 +129,7 @@ _CA__INIT_NODE_ENV() {
local ENV_PATH="$1" local ENV_PATH="$1"
local ENV_VERSION="$2" local ENV_VERSION="$2"
"setting up node env ($ENV_VERSION)"
nodeenv --node=$ENV_VERSION $ENV_PATH nodeenv --node=$ENV_VERSION $ENV_PATH
} }
@ -128,13 +137,15 @@ _CA__INIT_VIRTUALENV() {
local ENV_PATH="$1" local ENV_PATH="$1"
local ENV_VERSION="$2" local ENV_VERSION="$2"
"setting up virtualenv ($ENV_VERSION)"
virtualenv --python=$ENV_VERSION $ENV_PATH virtualenv --python=$ENV_VERSION $ENV_PATH
} }
_CA__INIT_CUSTOM_ENV() { _CA__INIT_CUSTOM_ENV() {
local PROJECT_PATH="$1" local PROJECT_PATH="$1"
local CUSTOM_ENV_TEMPLATE="$_CA__CUSTOM_ENV_TEMPLATE"
local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME" local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME"
local CUSTOM_ENV_TEMPLATE="$_CA__CUSTOM_ENV_TEMPALTE"
cp $CUSTOM_ENV_TEMPLATE $CUSTOM_ENV cp $CUSTOM_ENV_TEMPLATE $CUSTOM_ENV
} }

View File

@ -10,6 +10,12 @@ export CODE_ACTIVATOR__KNOWN_TARGETS=(
'git@bitbucket.org:' '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' [ ! $_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__NO_ENV_SENTINEL ] && export _CA__NO_ENV_SENTINEL='.no-env'
[ ! $_CA__GIT_MAIN_BRANCH ] && export _CA__GIT_MAIN_BRANCH='master' [ ! $_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; }
##################################################################### #####################################################################