Skip to content

Commit 5818097

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 c7e8450 commit 5818097

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,11 @@ instead:
332332
(projectile-find-file)))
333333
```
334334

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

340341
```lisp
341342
(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
@@ -245,3 +245,21 @@
245245
(s-concat (venv-name-to-dir venv-tmp-env) ".project"))
246246
(venv-workon venv-tmp-env)
247247
(should (not (equal default-directory temp-dir)))))))
248+
249+
(ert-deftest venv-projectile-auto-workon-works-with-text-file ()
250+
(with-temp-env
251+
venv-tmp-env
252+
;; the reason for setting a bogus venv-location here is that the
253+
;; venv-location shouldn't matter, projectile-auto-workon should happen
254+
;; indepedent of it's being set or not
255+
(let* ((venv-location "bogus")
256+
;; Create a file in the projectile-project-root with
257+
;; the text content of the venv to be activated
258+
(venv-tmp-text-file (make-temp-file "venv" nil nil venv-tmp-env))
259+
(venv-tmp-text-name (file-name-nondirectory venv-tmp-text-file)))
260+
(noflet ((projectile-project-root () temporary-file-directory))
261+
(setq venv-dirlookup-names (list venv-tmp-text-name))
262+
(venv-deactivate)
263+
(venv-projectile-auto-workon)
264+
(assert-venv-activated)
265+
(delete-file venv-tmp-text-file)))))

virtualenvwrapper.el

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ Set your common venvs names in `venv-dirlookup-names'"
114114
(--map (concat (projectile-project-root) it)
115115
venv-dirlookup-names))))
116116
(when path
117-
(setq venv-current-name path) ;; there's really nothing that feels good to do here ;_;
118-
(venv--activate-dir path))))
117+
;; If the PATH is a regular and readable file, read the first
118+
;; string in this file and use it to active the virtualenv.
119+
(if (and path (file-regular-p path) (file-readable-p path))
120+
(with-temp-buffer
121+
(insert-file-contents path)
122+
(venv-workon (car (split-string (buffer-string) "\n" t))))
123+
;; PATH is not a file, assume it's a virtualenv directory and activate it
124+
(setq venv-current-name path) ;; there's really nothing that feels good to do here ;_;
125+
(venv--activate-dir path)))))
119126

120127

121128
;; internal utility functions

0 commit comments

Comments
 (0)