Skip to content

Commit df295fa

Browse files
committed
Make auto-workon work with text files containing virtualenv name
This way you can keep all your virtualenvs in one place and can reuse the same virtualenv for multiple projects. Virtualfish (virtualenvwrapper for fish shell) does this as well, see: http://virtualfish.readthedocs.io/en/latest/plugins.html#auto-activation-auto-activation
1 parent 5649028 commit df295fa

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,11 @@ instead:
324324
(projectile-find-file)))
325325
```
326326

327-
As long as a virtualenv is found in the `projectile-project-root` and
328-
whose name is in the list `venv-dirlookup-names` it will be
329-
automatically activated. By default, it's value is `'(".venv", "venv")'`,
330-
but you can set if however you like to match your naming conventions:
327+
As long as a virtualenv or a text file with the name of the virtualenv is
328+
found in the `projectile-project-root` and whose name is in the list
329+
`venv-dirlookup-names` it will be automatically activated. By default, it's
330+
value is `'(".venv", "venv")'`, but you can set if however you like to match
331+
your naming conventions:
331332

332333
```lisp
333334
(setq venv-dirlookup-names '(".venv" "pyenv" ".virtual"))

test/virtualenvwrapper-test.el

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,21 @@
155155
(venv-deactivate)
156156
(venv-projectile-auto-workon)
157157
(assert-venv-activated)))))
158+
159+
(ert-deftest venv-projectile-auto-workon-works-with-text-file ()
160+
(with-temp-env
161+
venv-tmp-env
162+
;; the reason for setting a bogus venv-location here is that the
163+
;; venv-location shouldn't matter, projectile-auto-workon should happen
164+
;; indepedent of it's being set or not
165+
(let* ((venv-location "bogus")
166+
;; Create a file in the projectile-project-root with
167+
;; the text content of the venv to be activated
168+
(venv-tmp-text-file (make-temp-file "venv" nil nil venv-tmp-env))
169+
(venv-tmp-text-name (file-name-nondirectory venv-tmp-text-file)))
170+
(noflet ((projectile-project-root () temporary-file-directory))
171+
(setq venv-dirlookup-names (list venv-tmp-text-name))
172+
(venv-deactivate)
173+
(venv-projectile-auto-workon)
174+
(assert-venv-activated)
175+
(delete-file venv-tmp-text-file)))))

virtualenvwrapper.el

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,19 @@ to activate when one of them is found."
9494
"If a venv in the projetile root exists, activates it.
9595
Set your common venvs names in `venv-dirlookup-names'"
9696
(let ((path (--first
97-
(file-exists-p it)
98-
(--map (concat (projectile-project-root) it)
99-
venv-dirlookup-names))))
97+
(file-exists-p it)
98+
(--map (concat (projectile-project-root) it)
99+
venv-dirlookup-names))))
100100
(when path
101-
(setq venv-current-name path) ;; there's really nothing that feels good to do here ;_;
102-
(venv--activate-dir path))))
101+
;; If the PATH is a regular and readable file, read the first
102+
;; string in this file and use it to active the virtualenv.
103+
(if (and path (file-regular-p path) (file-readable-p path))
104+
(with-temp-buffer
105+
(insert-file-contents path)
106+
(venv-workon (car (split-string (buffer-string) "\n" t))))
107+
;; PATH is not a file, assume it's a virtualenv directory and activate it
108+
(setq venv-current-name path) ;; there's really nothing that feels good to do here ;_;
109+
(venv--activate-dir path)))))
103110

104111

105112
;; internal utility functions

0 commit comments

Comments
 (0)