Created
March 4, 2014 12:25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun source (filename) | |
"Update environment variables from a shell source file." | |
(interactive "fSource file: ") | |
(message "Sourcing environment from `%s'..." filename) | |
(with-temp-buffer | |
(shell-command (format "diff -u <(true; export) <(source %s; export)" filename) '(4)) | |
(let ((envvar-re "declare -x \\([^=]+\\)=\\(.*\\)$")) | |
;; Remove environment variables | |
(while (search-forward-regexp (concat "^-" envvar-re) nil t) | |
(let ((var (match-string 1))) | |
(message "%s" (prin1-to-string `(setenv ,var nil))) | |
(setenv var nil))) | |
;; Update environment variables | |
(goto-char (point-min)) | |
(while (search-forward-regexp (concat "^+" envvar-re) nil t) | |
(let ((var (match-string 1)) | |
(value (read (match-string 2)))) | |
(message "%s" (prin1-to-string `(setenv ,var ,value))) | |
(setenv var value))))) | |
(message "Sourcing environment from `%s'... done." filename)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Exactly what I was looking for!