d964866e4961854a56951250fd6fa0ae229c8e1c
- Author
- Michael Aquilina <michaelaquilina@gmail.com>
- Committer
- Michael Aquilina <michaelaquilina@gmail.com>
- Date
Message
Add mkvenv and rmvenv helper functions
Diff
1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
2index 95d3fc4832c3b6e28953327f6df1e5709c3670fd..9090fb03b2574373951a29f6905ce4db70f002e1 100644
3--- a/autoswitch_virtualenv.plugin.zsh
4+++ b/autoswitch_virtualenv.plugin.zsh
5@@ -23,11 +23,52 @@ function check_venv()
6 if [ "$PWD" != "$MYOLDPWD" ]; then
7 MYOLDPWD="$PWD"
8 if [[ -f ".venv" ]]; then
9- maybeworkon "$(cat .venv)"
10- elif [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
11- maybeworkon "$AUTOSWITCH_DEFAULTENV"
12- elif [[ -n "$VIRTUAL_ENV" ]]; then
13- deactivate
14+ maybeworkon "$(cat .venv)"
15+ else
16+ default_venv
17 fi
18 fi
19 }
20+
21+# Switch to the default virtual environment
22+function default_venv()
23+{
24+ if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
25+ maybeworkon "$AUTOSWITCH_DEFAULTENV"
26+ elif [[ -n "$VIRTUAL_ENV" ]]; then
27+ deactivate
28+ fi
29+}
30+
31+
32+# remove virutal environment for current directory
33+function rmvenv()
34+{
35+ if [[ -f ".venv" ]]; then
36+ venv_name="$(cat .venv)"
37+ current_venv="$(basename $VIRTUAL_ENV)"
38+ if [[ "$current_venv" = "$venv_name" ]]; then
39+ default_venv
40+ fi
41+ rmvirtualenv "$venv_name"
42+ rm ".venv"
43+ else
44+ echo "No .venv file in the current directory!"
45+ fi
46+}
47+
48+
49+# helper function to create a virtual environment for the current directory
50+function mkvenv()
51+{
52+ venv_name="$(basename $PWD)"
53+ echo "$venv_name" > ".venv"
54+ mkvirtualenv "$venv_name" $@
55+ if [[ -f "requirements.txt" ]]; then
56+ printf "Found a requirements.txt. Install? (y/N): "
57+ read install_prompt
58+ if [[ "$install_prompt" = "y" ]]; then
59+ pip install -r requirements.txt
60+ fi
61+ fi
62+}