From 8417d06bbefd25edfe58bed5e28a5fd441794fe5 Mon Sep 17 00:00:00 2001 From: Etenil Date: Mon, 9 Oct 2017 10:50:15 +0100 Subject: Added dotenv support. --- README.md | 10 ++++++++++ venvworkon.sh | 26 +++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5feedb6..c08a075 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,16 @@ 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. +## DotEnv support +If the script is able to find the path to your project, and +a `.env` file exists at the root of your project, then it +will be sourced automatically when activating the environment. + +For the _venvworkon_ to find the path of the project, it +either needs to be created through the shorthand `.` name, or +you need to have `PROJECT_HOME` set and your environment +name must match your project directory's name. + ## 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 ad0090e..07fcaf4 100755 --- a/venvworkon.sh +++ b/venvworkon.sh @@ -20,10 +20,12 @@ workon() { envdir="$WORKON_HOME/$1" python_version="3" + projectdir="" if [ "$1" == "." ]; then target=$(basename `pwd`) envdir="$WORKON_HOME/$target" + projectdir=$(pwd) fi if [ "$#" -gt "1" ]; then @@ -35,19 +37,33 @@ workon() { # Creates virtual env if doesn't exists if [ ! -e "$envdir" ]; then $python_exec -m venv $envdir + + if [ "$projectdir" != "" ]; then + echo "projectdir=$projectdir" > "$envdir/postactivate.sh" + fi + echo "Virtual env created in $envdir" fi 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 - if [ -d "$PROJECT_HOME/$(basename $VIRTUAL_ENV)" ]; then - cd "$PROJECT_HOME/$(basename $VIRTUAL_ENV)" - fi - # Source postactivate.sh if exists in the virtual env if [ -e "$VIRTUAL_ENV/postactivate.sh" ]; then source "$VIRTUAL_ENV/postactivate.sh" fi + + if [ -e "$projectdir/.env" ]; then + . "$projectdir/.env" + fi + + # Jumps to project directory if exists and PROJECT_HOME is set + projecthome="$PROJECT_HOME/$(basename $VIRTUAL_ENV)" + if [ -d "$projecthome" ]; then + cd "$projecthome" + + if [ -e "$projecthome/.env" ]; then + . "$projecthome/.env" + fi + fi } -- cgit v1.2.3