aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md15
-rwxr-xr-xvenvworkon.sh36
2 files changed, 26 insertions, 25 deletions
diff --git a/README.md b/README.md
index 3ddcf66..acbec3b 100644
--- a/README.md
+++ b/README.md
@@ -19,13 +19,20 @@ will be printed instead.
## Where venvs are created
When `workon` is called, venvs are created or loaded from the
-`$HOME/.venvs` folder.
+`$HOME/.venvs` folder by default.
+To change this behavior, set the `WORKON_HOME` environment
+variable before sourcing `venvworkon.sh`.
-## Post-load script
-If the file `$HOME/.venvs/<venv>/postload.sh` exists, it is
-automatically sourced after the venv is loaded. This is useful
+## Post-activate script
+If the file `$HOME/.venvs/<venv>/postactivate.sh` exists, it is
+automatically sourced after the venv is activated. This is useful
to export environment variable such as `DJANGO_SETTINGS_MODULE`.
+## Project directory
+If `PROJECT_HOME` is set (e.g. `$HOME/Projects`) and there is
+a project folder which match the environment name, venvworkon
+change the current working directory to the project directory.
+
## Why bother using this
`virtualenvwrapper` is certainly much more feature-packed and
useful than this. But this wrapper is handy for development and
diff --git a/venvworkon.sh b/venvworkon.sh
index 1da01bd..92890b6 100755
--- a/venvworkon.sh
+++ b/venvworkon.sh
@@ -1,30 +1,24 @@
workon() {
- envroot="$HOME/.venv"
+ # Sets WORKON_HOME to default if not set already
+ test -z "$WORKON_HOME" && WORKON_HOME="$HOME/.venv"
- if [ ! -e "$envroot" ]
- then
- mkdir -p "$envroot"
- fi
+ # Creates WORKON_HOME directory if not exists
+ test ! -e "$WORKON_HOME" && mkdir -p $_
- if [ "$#" -lt "1" ]
- then
- ls "$envroot"
- return
- fi
+ # Quit if there is no arguments, while showing available venvs
+ test "$#" -lt "1" && ls "$WORKON_HOME"; return
- envname=$1
+ envdir="$WORKON_HOME/$1"
- envdir="$envroot/$envname"
-
- if [ ! -e "$envdir" ]
- then
- pyvenv "$envdir"
- fi
+ # Creates virtual env if doesn't exists
+ test ! -e "$envdir" && pyvenv $_ && echo "Virtual env created in $_"
source "$envdir/bin/activate"
+ unset envdir # VIRTUAL_ENV is now set by bin/activate
+
+ # Jumps to project directory if exists and PROJECT_HOME is set
+ test -d "$PROJECT_HOME/$(basename $VIRTUAL_ENV)" && cd $_
- if [ -e "$envdir/postload.sh" ]
- then
- source "$envdir/postload.sh"
- fi
+ # Source postactivate.sh if exists in the virtual env
+ test -e "$VIRTUAL_ENV/postactivate.sh" && source $_
}