Add autoenv add=py to streamline virtualenvs

This commit is contained in:
Kenneth Benzie 2019-10-05 19:11:26 +01:00
parent 4252d06e65
commit 908ef3efa5
2 changed files with 44 additions and 24 deletions

View File

@ -16,6 +16,7 @@ _autoenv() {
init:'add .enter and .exit scripts in current directory' init:'add .enter and .exit scripts in current directory'
edit:'edit .enter and .exit scripts in current directory' edit:'edit .enter and .exit scripts in current directory'
deinit:'remove .enter and .exit scripts in current directory' deinit:'remove .enter and .exit scripts in current directory'
add=py:'add Python virtualenv to the autoenv'
) )
_describe -t commands command commands && ret=0 ;; _describe -t commands command commands && ret=0 ;;
esac esac

View File

@ -20,21 +20,25 @@ options:
commands: commands:
init add .enter and .exit scripts in current directory init add .enter and .exit scripts in current directory
edit edit .enter and .exit scripts in current directory edit edit .enter and .exit scripts in current directory
deinit remove .enter and .exit scripts in current directory" deinit remove .enter and .exit scripts in current directory
add=py add Python virtualenv to the autoenv"
;; ;;
init) # Create .enter and .exit scripts in current directory. init) # Create .enter and .exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
echo '.enter or .exit already exists'; return 1 echo '.enter or .exit already exists'; return 1
fi fi
# Create then edit .enter and .exit scripts. # Create the .enter and .exit scripts.
touch .enter .exit && autoenv edit touch .enter .exit
# If enter script exists, authorize it. # If enter script exists, authorize it.
[ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes [ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes
# If exit script exists, authorize it. # If exit script exists, authorize it.
[ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes [ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes
# Edit the autoenv.
autoenv edit
# Enter the autoenv. # Enter the autoenv.
_autoenv_enter $PWD ;; _autoenv_enter $PWD
;;
edit) # Edit .enter and .exit scripts in current directory. edit) # Edit .enter and .exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
@ -54,32 +58,47 @@ commands:
_autoenv_enter $PWD _autoenv_enter $PWD
else else
echo 'vim not found'; return 1 echo 'vim not found'; return 1
fi ;; fi
;;
deinit) # Remove .enter and .exit scripts in current directory. deinit) # Remove .enter and .exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
# Prompt user to confirm removal of enter and exit scripts. echo '.enter or .exit not found'; return 1
while true; do fi
read "answer?Are you sure [y/N]? " # Prompt user to confirm removal of enter and exit scripts.
case "$answer" in while true; do
y|Y|yes) read "answer?Are you sure [y/N]? "
# Exit the autoenv. case "$answer" in
_autoenv_exit $PWD y|Y|yes)
# Remove enter and exit scripts if they exist. # Exit the autoenv.
[ -f $PWD/.enter ] && rm $PWD/.enter _autoenv_exit $PWD
[ -f $PWD/.exit ] && rm $PWD/.exit # Remove enter and exit scripts if they exist.
break ;; [ -f $PWD/.enter ] && rm $PWD/.enter
*) break ;; [ -f $PWD/.exit ] && rm $PWD/.exit
esac break ;;
done *) break ;;
else esac
echo '.enter and .exit not found'; return 1 done
fi ;; ;;
add=py) # Add Python virtualenv to the sandbox
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1
fi
_autoenv_exit $PWD
virtualenv .local
echo 'source .local/bin/activate' >> .enter
echo 'deactivate' >> .exit
_autoenv_authorized $PWD/.enter yes
_autoenv_authorized $PWD/.exit yes
_autoenv_enter $PWD
;;
*) # Invalid arguments, show help then error. *) # Invalid arguments, show help then error.
echo "invalid arguments: $@" echo "invalid arguments: $@"
autoenv --help autoenv --help
return 1 ;; return 1
;;
esac esac
} }