v1
This commit is contained in:
commit
0d0991df19
80
activator.plugin.zsh
Normal file
80
activator.plugin.zsh
Normal file
@ -0,0 +1,80 @@
|
||||
#!/bin/zsh
|
||||
#####################################################################
|
||||
# #
|
||||
# CODE_ACTIVATOR : a terminal-based navigation utility #
|
||||
# #
|
||||
#####################################################################
|
||||
|
||||
#####################################################################
|
||||
|
||||
DEPENDENCIES=(
|
||||
fzf
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
#####################################################################
|
||||
|
||||
for DEPENDENCY in $DEPENDENCIES
|
||||
do
|
||||
command -v $DEPENDENCY >/dev/null 2>&1 || {
|
||||
echo "I require '$DEPENDENCY' but it's not installed :c"
|
||||
ERROR_CODE=1
|
||||
}
|
||||
done
|
||||
|
||||
#####################################################################
|
||||
|
||||
for IMPORT in $IMPORTS; do source $IMPORT; done
|
||||
|
||||
[ ! $_CA__ACTIVATE_LOADED ] && ERROR_CODE=2
|
||||
[ ! $_CA__CLONE_LOADED ] && ERROR_CODE=2
|
||||
|
||||
#####################################################################
|
||||
|
||||
[ $ERROR_CODE ] && return $ERROR_CODE
|
||||
|
||||
#####################################################################
|
||||
#####################################################################
|
||||
|
||||
CODE_ACTIVATOR() {
|
||||
local ERROR=0
|
||||
|
||||
case $1 in
|
||||
clone ) _CA__CLONE ${@:2} || ERROR=1 ;;
|
||||
* ) _CA__ACTIVATE ${@:2} || ERROR=2 ;;
|
||||
esac
|
||||
|
||||
return $ERROR
|
||||
}
|
||||
|
||||
_CODE_ACTIVATOR() {
|
||||
local state
|
||||
|
||||
_arguments \
|
||||
'1: :->project' \
|
||||
':: :->arguments' \
|
||||
;
|
||||
|
||||
case $state in
|
||||
project )
|
||||
compadd clone
|
||||
|
||||
for dir in $CODE_ACTIVATOR__DIRS
|
||||
do
|
||||
compadd "$(basename $dir)/$(basename $(ls -d -- $dir/*))"
|
||||
done
|
||||
;;
|
||||
arguments )
|
||||
case $words[2] in
|
||||
clone ) __CA__CLONE ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
}
|
||||
compdef _CODE_ACTIVATOR CODE_ACTIVATOR
|
43
zsh/'
Normal file
43
zsh/'
Normal file
@ -0,0 +1,43 @@
|
||||
#####################################################################
|
||||
|
||||
[ ! $_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
|
56
zsh/activate.zsh
Normal file
56
zsh/activate.zsh
Normal file
@ -0,0 +1,56 @@
|
||||
#####################################################################
|
||||
|
||||
[ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh"
|
||||
[ ! $_CA__HELPERS_LOADED ] && source "${0:a:h}/helpers.zsh"
|
||||
|
||||
#####################################################################
|
||||
|
||||
_CA__ACTIVATE() {
|
||||
_CA__RESTORE_ENVIRONMENT
|
||||
|
||||
local PROJECT="$1"
|
||||
[ ! $PROJECT ] && { cd; return 0; }
|
||||
|
||||
local PROJECT_PATH=$(_CA__GET_FULL_PATH $PROJECT)
|
||||
[ ! -d $PROJECT_PATH ] && { cd; return 0; }
|
||||
|
||||
_CA__ACTIVATE_VIRTUAL_ENV $PROJECT_PATH
|
||||
_CA__ACTIVATE_CUSTOM_ENV $PROJECT_PATH
|
||||
_CA__ACTIVATE_SOURCE_DIR $PROJECT_PATH
|
||||
|
||||
_CA__TMUX_WINDOW_RENAME $PROJECT
|
||||
}
|
||||
|
||||
_CA__ACTIVATE_VIRTUAL_ENV() {
|
||||
local PROJECT_PATH="$1"
|
||||
local ACTIVATE="$PROJECT_PATH/$_CA__VIRTUAL_ENV_NAME/bin/activate"
|
||||
local NO_ENV="$PROJECT_PATH/$_CA__NO_ENV_SENTINEL"
|
||||
|
||||
[ -f $ACTIVATE ] && {
|
||||
source $ACTIVATE
|
||||
} || {
|
||||
[ -f $NO_ENV ] && {
|
||||
echo 'no virtual environment here, boss!'
|
||||
} || {
|
||||
_CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_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'
|
||||
}
|
||||
}
|
||||
|
||||
_CA__ACTIVATE_SOURCE_DIR() {
|
||||
local SOURCE_DIR="$1/$_CA__SOURCE_DIR_NAME"
|
||||
cd $SOURCE_DIR
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
export _CA__ACTIVATE_LOADED=1
|
95
zsh/clone.zsh
Normal file
95
zsh/clone.zsh
Normal file
@ -0,0 +1,95 @@
|
||||
#####################################################################
|
||||
|
||||
[ ! $_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)
|
||||
[ ! $BASE_DIR ] && return 1
|
||||
|
||||
[ ! $REMOTE_TARGET ] && REMOTE_TARGET=$(_CA__GET_REMOTE_TARGET)
|
||||
[ ! $REMOTE_TARGET ] && return 1
|
||||
echo $REMOTE_TARGET
|
||||
[[ $REMOTE_TARGET =~ .git$ ]] || return 1
|
||||
|
||||
[ ! $PROJECT_NAME ] && PROJECT_NAME=$(_CA__GET_PROJECT_NAME $REMOTE_TARGET)
|
||||
[ ! $PROJECT_NAME ] && return 1
|
||||
|
||||
local PROJECT_PATH="$BASE_DIR/$PROJECT_NAME"
|
||||
|
||||
[ -d $PROJECT_PATH ] && {
|
||||
echo "project '$PROJECT_NAME' already exists in '$(basename $BASE_DIR)'"
|
||||
return 1
|
||||
}
|
||||
|
||||
mkdir $PROJECT_PATH
|
||||
_CA__CLONE_SOURCE $REMOTE_TARGET $PROJECT_PATH
|
||||
_CA__INTERACTIVE_ENV_SETUP $PROJECT_PATH
|
||||
_CA__INIT_CUSTOM_ENV $PROJECT_PATH
|
||||
}
|
||||
|
||||
|
||||
__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
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
_CA__CLONE_SOURCE() {
|
||||
local REMOTE_TARGET="$1"
|
||||
local PROJECT_PATH="$2"
|
||||
local SOURCE_DIR="$PROJECT_PATH/$_CA__SOURCE_DIR_NAME"
|
||||
|
||||
echo "trying to clone '$REMOTE_TARGET' to '$PROJECT_PATH'"
|
||||
git clone "$REMOTE_TARGET" "$SOURCE_DIR" >/dev/null 2>&1 || {
|
||||
echo "failed to clone '$REMOTE_TARGET'"
|
||||
|
||||
printf "is this a new project? [y/N]"
|
||||
read -k yn
|
||||
[[ $yn =~ ^[yY] ]] && {
|
||||
printf 'initializing project...'
|
||||
{
|
||||
mkdir $SOURCE_DIR \
|
||||
&& cd $SOURCE_DIR \
|
||||
&& git init \
|
||||
&& touch .gitignore \
|
||||
&& git add .gitignore \
|
||||
&& git commit -m 'project init' \
|
||||
&& git remote add "$_CA__GIT_MAIN_BRANCH" $REMOTE_TARGET \
|
||||
&& cd .. \
|
||||
;
|
||||
} >/dev/null >&1 && echo ' success :)' || echo ' failed :c'
|
||||
} || {
|
||||
echo 'exiting'
|
||||
return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
export _CA__CLONE_LOADED=1
|
3
zsh/custom-env-template.zsh
Normal file
3
zsh/custom-env-template.zsh
Normal file
@ -0,0 +1,3 @@
|
||||
#####################################################################
|
||||
|
||||
#####################################################################
|
144
zsh/helpers.zsh
Normal file
144
zsh/helpers.zsh
Normal file
@ -0,0 +1,144 @@
|
||||
#####################################################################
|
||||
|
||||
[ ! $_CA__SETTINGS_LOADED ] && source "${0:a:h}/settings.zsh"
|
||||
|
||||
#####################################################################
|
||||
|
||||
_CA__RESTORE_ENVIRONMENT() {
|
||||
deactivate >/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
|
||||
|
||||
unset _CA__RESTORE_PATH _CA__RESTORE_ENV
|
||||
}
|
||||
|
||||
_CA__GET_FULL_PATH() {
|
||||
local PROJECT="$1"
|
||||
|
||||
local PROJECT_ROOT_SHORT=$(dirname $PROJECT)
|
||||
local PROJECT_NAME=$(basename $PROJECT)
|
||||
|
||||
local FULL_BASE_DIR=$(echo $CODE_ACTIVATOR__DIRS | grep "^.*/$PROJECT_ROOT_SHORT$")
|
||||
|
||||
echo "$FULL_BASE_DIR/$PROJECT_NAME"
|
||||
}
|
||||
|
||||
_CA__TMUX_WINDOW_RENAME() {
|
||||
local PROJECT="$1"
|
||||
|
||||
[[ $TERM =~ ^tmux- ]] \
|
||||
&& tmux rename-window "$(dirname $PROJECT):$(basename $PROJECT)"
|
||||
}
|
||||
|
||||
_CA__SELECT_BASE_DIR() {
|
||||
local BASE_NAMES=()
|
||||
|
||||
for dir in $CODE_ACTIVATOR__DIRS
|
||||
do
|
||||
BASE_NAMES=($BASE_NAMES $(basename $dir))
|
||||
done
|
||||
|
||||
echo $CODE_ACTIVATOR__DIRS \
|
||||
| grep $(echo $BASE_NAMES | $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 : ' \
|
||||
| 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 == $FIRST_SELECTION ]] && return ''
|
||||
|
||||
echo $REMOTE_TARGET | grep -q '\.git$' || REMOTE_TARGET="$REMOTE_TARGET.git"
|
||||
echo $REMOTE_TARGET
|
||||
}
|
||||
|
||||
_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
|
||||
}
|
||||
|
||||
_CA__INTERACTIVE_ENV_SETUP() {
|
||||
local PROJECT_PATH="$1"
|
||||
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
|
||||
|
||||
case $yn in
|
||||
e ) touch $NO_ENV; return 1 ;;
|
||||
n ) return 1 ;;
|
||||
esac
|
||||
|
||||
local ENV_VERSION=$(_CA__SELECT_VIRTUAL_ENV)
|
||||
_CA__INIT_VIRTUAL_ENV $PROJECT_PATH $ENV_VERSION
|
||||
}
|
||||
|
||||
_CA__SELECT_VIRTUAL_ENV() {
|
||||
local VERISON=$(\
|
||||
echo "$(_CA__GET_PYTHON_IN_PATH)\n$(which node 2>/dev/null)" \
|
||||
| $FZF --prompt 'select a virtual environment : ' \
|
||||
)
|
||||
|
||||
[[ $VERSION =~ node$ ]] && VERSION=$(\
|
||||
nodeenv --list \
|
||||
| sed 's/\s\+/\n/g' \
|
||||
| $FZF --prompt 'select a node version : '\
|
||||
)
|
||||
|
||||
echo $VERSION
|
||||
}
|
||||
|
||||
_CA__GET_PYTHON_IN_PATH() {
|
||||
whence -pm '*' | grep python | grep -v -- '-config$'
|
||||
}
|
||||
|
||||
_CA__INIT_VIRTUAL_ENV() {
|
||||
local PROJECT_PATH="$1"
|
||||
local ENV_VERSION="$2"
|
||||
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() {
|
||||
local ENV_PATH="$1"
|
||||
local ENV_VERSION="$2"
|
||||
|
||||
nodeenv --node=$ENV_VERSION $ENV_PATH
|
||||
}
|
||||
|
||||
_CA__INIT_VIRTUALENV() {
|
||||
local ENV_PATH="$1"
|
||||
local ENV_VERSION="$2"
|
||||
|
||||
virtualenv --python=$ENV_VERSION $ENV_PATH
|
||||
}
|
||||
|
||||
_CA__INIT_CUSTOM_ENV() {
|
||||
local PROJECT_PATH="$1"
|
||||
local CUSTOM_ENV="$PROJECT_PATH/$_CA__CUSTOM_ENV_NAME"
|
||||
local CUSTOM_ENV_TEMPLATE="$_CA__CUSTOM_ENV_TEMPALTE"
|
||||
|
||||
cp $CUSTOM_ENV_TEMPLATE $CUSTOM_ENV
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
export _CA__HELPERS_LOADED=1
|
27
zsh/settings.zsh
Normal file
27
zsh/settings.zsh
Normal file
@ -0,0 +1,27 @@
|
||||
#####################################################################
|
||||
|
||||
[ ! $CODE_ACTIVATOR__DIRS ] && export CODE_ACTIVATOR__DIRS=("$HOME/Code")
|
||||
|
||||
export CODE_ACTIVATOR__KNOWN_TARGETS=(
|
||||
$CODE_ACTIVATOR__KNOWN_TARGETS
|
||||
'https://github.com/'
|
||||
'https://bitbucket.org/'
|
||||
'git@github.com:'
|
||||
'git@bitbucket.org:'
|
||||
)
|
||||
|
||||
#####################################################################
|
||||
|
||||
[ ! $_CA__SOURCE_DIR_NAME ] && export _CA__SOURCE_DIR_NAME='code'
|
||||
[ ! $_CA__VIRTUAL_ENV_NAME ] && export _CA__VIRTUAL_ENV_NAME='env'
|
||||
[ ! $_CA__CUSTOM_ENV_NAME ] && export _CA__CUSTOM_ENV_NAME='custom-env'
|
||||
[ ! $_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"
|
||||
|
||||
FZF=(fzf -i --height=20% --layout=reverse)
|
||||
|
||||
#####################################################################
|
||||
|
||||
export _CA__SETTINGS_LOADED=1
|
Loading…
Reference in New Issue
Block a user