From 908ef3efa5038b540193da78d96bbef7c739f9e7 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sat, 5 Oct 2019 19:11:26 +0100 Subject: [PATCH] Add autoenv add=py to streamline virtualenvs --- autoenv/_autoenv | 1 + autoenv/autoenv.zsh | 67 +++++++++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/autoenv/_autoenv b/autoenv/_autoenv index 9e3ce83..1323154 100644 --- a/autoenv/_autoenv +++ b/autoenv/_autoenv @@ -16,6 +16,7 @@ _autoenv() { init:'add .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' + add=py:'add Python virtualenv to the autoenv' ) _describe -t commands command commands && ret=0 ;; esac diff --git a/autoenv/autoenv.zsh b/autoenv/autoenv.zsh index 57b9aa0..20337c5 100644 --- a/autoenv/autoenv.zsh +++ b/autoenv/autoenv.zsh @@ -20,21 +20,25 @@ options: commands: init add .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. if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then echo '.enter or .exit already exists'; return 1 fi - # Create then edit .enter and .exit scripts. - touch .enter .exit && autoenv edit + # Create the .enter and .exit scripts. + touch .enter .exit # If enter script exists, authorize it. [ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes # If exit script exists, authorize it. [ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes + # Edit the autoenv. + autoenv edit # Enter the autoenv. - _autoenv_enter $PWD ;; + _autoenv_enter $PWD + ;; edit) # Edit .enter and .exit scripts in current directory. if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then @@ -54,32 +58,47 @@ commands: _autoenv_enter $PWD else echo 'vim not found'; return 1 - fi ;; + fi + ;; deinit) # Remove .enter and .exit scripts in current directory. - if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then - # Prompt user to confirm removal of enter and exit scripts. - while true; do - read "answer?Are you sure [y/N]? " - case "$answer" in - y|Y|yes) - # Exit the autoenv. - _autoenv_exit $PWD - # Remove enter and exit scripts if they exist. - [ -f $PWD/.enter ] && rm $PWD/.enter - [ -f $PWD/.exit ] && rm $PWD/.exit - break ;; - *) break ;; - esac - done - else - echo '.enter and .exit not found'; return 1 - fi ;; + if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then + echo '.enter or .exit not found'; return 1 + fi + # Prompt user to confirm removal of enter and exit scripts. + while true; do + read "answer?Are you sure [y/N]? " + case "$answer" in + y|Y|yes) + # Exit the autoenv. + _autoenv_exit $PWD + # Remove enter and exit scripts if they exist. + [ -f $PWD/.enter ] && rm $PWD/.enter + [ -f $PWD/.exit ] && rm $PWD/.exit + break ;; + *) break ;; + esac + done + ;; + + 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. echo "invalid arguments: $@" autoenv --help - return 1 ;; + return 1 + ;; esac }