Skip to content

Commit 5d35e80

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 fa49954 commit 5d35e80

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
@@ -330,10 +330,11 @@ instead:
330330
(projectile-find-file)))
331331
```
332332

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

338339
```lisp
339340
(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
@@ -210,3 +210,21 @@
210210
(venv-deactivate)
211211
(venv-projectile-auto-workon)
212212
(assert-venv-activated)))))
213+
214+
(ert-deftest venv-projectile-auto-workon-works-with-text-file ()
215+
(with-temp-env
216+
venv-tmp-env
217+
;; the reason for setting a bogus venv-location here is that the
218+
;; venv-location shouldn't matter, projectile-auto-workon should happen
219+
;; indepedent of it's being set or not
220+
(let* ((venv-location "bogus")
221+
;; Create a file in the projectile-project-root with
222+
;; the text content of the venv to be activated
223+
(venv-tmp-text-file (make-temp-file "venv" nil nil venv-tmp-env))
224+
(venv-tmp-text-name (file-name-nondirectory venv-tmp-text-file)))
225+
(noflet ((projectile-project-root () temporary-file-directory))
226+
(setq venv-dirlookup-names (list venv-tmp-text-name))
227+
(venv-deactivate)
228+
(venv-projectile-auto-workon)
229+
(assert-venv-activated)
230+
(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)