spacemacs/core/libs/forks/load-env-vars.el

107 lines
3.9 KiB
EmacsLisp
Raw Permalink Normal View History

;;; load-env-vars.el --- Load environment variables from files -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Jorge Dias
;; Author: Jorge Dias <jorge@mrdias.com>
;; URL: https://github.com/diasjorge/emacs-load-env-vars
;; Keywords: lisp
;; Version: 0.0.2
;; Package-Requires: ((emacs "24"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package allows you set environment variables loaded from a
;; file with bash style variable declarations.
;; Supported syntax:
;;
;; export KEY=VALUE
;; KEY=VALUE
;; KEY='VALUE'
;; KEY="VALUE"
;; # Comment lines are ignored
;; KEY=VALUE # Inline comments are ignored
;; KEY: VALUE
;;
;; Updates for Spacemacs:
;; - set `exec-path' from PATH
;;; Code:
(defvar load-env-vars-env-var-regexp
(rx
line-start
(0+ space)
(optional "export" (0+ space)) ;; optional export
(group (1+ (in "_" alnum))) ;; key
(or
(and (0+ space) "=" (0+ space))
(and ":" (1+ space))) ;; separator
(or
line-start
(and "'" (group (0+ (or "\\'" (not (any "'"))))) "'") ;; single quoted value
(and ?\" (group (0+ (or "\\\"" (not (any "\""))))) ?\") ;; double quoted value
(group (1+ (not (in "#" "\n")))) ;; unquoted value
)
(0+ space)
(optional "#" (0+ any))
)
"Regexp to match env vars in file."
)
(defun load-env-vars-re-seq (regexp)
"Get a list of all REGEXP matches in a buffer."
(save-excursion
(goto-char (point-min))
(save-match-data
(let (matches)
(while (re-search-forward regexp nil t)
(push (list (match-string-no-properties 1) (or (match-string-no-properties 2) (match-string-no-properties 3) (match-string-no-properties 4))) matches))
matches))))
(defun load-env-vars-extract-env-vars ()
"Extract environment variable name and value from STRING."
(load-env-vars-re-seq load-env-vars-env-var-regexp))
(defun load-env-vars-set-env (env-vars)
[core] Environment variable caching fixes Fix several problems with environment variable caching. First, if a shell printed extra output besides the command output, spacemacs//init-spacemacs-env included the extra output in .spacemacs.env. For example, a login shell could print a motd, which would be erroneously included in .spacemacs.env. To avoid this, spacemacs//init-spacemacs-env now redirects the command output to a temporary file and then reads the file. Second, spacemacs//init-spacemacs-env sorted lines using the entire "name=value" string for each line whereas the correct behavior is to sort only on the "name" part. To fix this, spacemacs//init-spacemacs-env now uses sort-regexp-fields with an appropriate regexp to match the "name" part and ignore the "value" part. Third, although load-env-vars-set-env added all PATH settings in .spacemacs.env to exec-path, it set PATH to the first PATH setting in .spacemacs.env. Now load-env-vars-set-env sets PATH from exec-path so that it will reflect that combined value. Fourth, load-env-vars-set-env set exec-path and PATH without consideration to the operating system's directory separator. Now load-env-vars-set-env converts backslashes to forward slashes when adding path entries to exec-path and converts forward slashes to backslashes when setting PATH on platforms that use backslashes. Fifth, load-env-vars-set-env now normalizes exec-path by deleting trailing slashes from path entries, converting drive letters in path entries to lower-case, and deleting duplicate path entries. Sixth, Spacemacs no longer uses exec-path-from-shell, but a changelog entry said that it did. This commit corrects the changelog entry, deletes another outdated entry, and merges two entries that covered the same functionality. Finally, this commit corrects several typos and tries to improve wording in several docstrings, messages, and comments. * CHANGELOG.develop: Delete mention of exec-path-from-shell, which Spacemacs no longer uses by default. Delete mention of dotspacemacs-import-env-vars-from-shell and dotspacemacs-import-env-vars-shell-file-name, which no longer exist. Merge entries about synchronizing environment variables. * core/core-env.el (spacemacs-ignored-environment-variables): Fix typos and mention that the strings are regexps that are matched against the names of environment variables. (spacemacs//init-spacemacs-env): Use a temporary file rather than standard output in order to avoid getting motd and other noise. Sort lines using only environment variables' names, not their values. Better describe the behavior with respect to spacemacs-ignored-environment-variables, duplicate settings, and PATH. Try to make wording clearer. (spacemacs/edit-env): Fix typos in the docstring. * core/libs/load-env-vars.el (load-env-vars-set-env): Fix typos in the docstring. Delete trailing slashes from path entries in exec-path, convert drive letters to lower-case, delete duplicate path entries, and convert backslashes to forward slashes when setting exec-path. Set PATH from exec-path, converting forward slashes to backslashes on platforms that use backslashes.
2019-06-09 03:12:23 +00:00
"Set environment variables from key value lists from ENV-VARS."
(setq exec-path (cl-remove-duplicates (mapcar #'directory-file-name exec-path)
:test #'string-equal :from-end t))
(let ((convert-to-os-path (if (memq system-type '(windows-nt ms-dos))
[core] Environment variable caching fixes Fix several problems with environment variable caching. First, if a shell printed extra output besides the command output, spacemacs//init-spacemacs-env included the extra output in .spacemacs.env. For example, a login shell could print a motd, which would be erroneously included in .spacemacs.env. To avoid this, spacemacs//init-spacemacs-env now redirects the command output to a temporary file and then reads the file. Second, spacemacs//init-spacemacs-env sorted lines using the entire "name=value" string for each line whereas the correct behavior is to sort only on the "name" part. To fix this, spacemacs//init-spacemacs-env now uses sort-regexp-fields with an appropriate regexp to match the "name" part and ignore the "value" part. Third, although load-env-vars-set-env added all PATH settings in .spacemacs.env to exec-path, it set PATH to the first PATH setting in .spacemacs.env. Now load-env-vars-set-env sets PATH from exec-path so that it will reflect that combined value. Fourth, load-env-vars-set-env set exec-path and PATH without consideration to the operating system's directory separator. Now load-env-vars-set-env converts backslashes to forward slashes when adding path entries to exec-path and converts forward slashes to backslashes when setting PATH on platforms that use backslashes. Fifth, load-env-vars-set-env now normalizes exec-path by deleting trailing slashes from path entries, converting drive letters in path entries to lower-case, and deleting duplicate path entries. Sixth, Spacemacs no longer uses exec-path-from-shell, but a changelog entry said that it did. This commit corrects the changelog entry, deletes another outdated entry, and merges two entries that covered the same functionality. Finally, this commit corrects several typos and tries to improve wording in several docstrings, messages, and comments. * CHANGELOG.develop: Delete mention of exec-path-from-shell, which Spacemacs no longer uses by default. Delete mention of dotspacemacs-import-env-vars-from-shell and dotspacemacs-import-env-vars-shell-file-name, which no longer exist. Merge entries about synchronizing environment variables. * core/core-env.el (spacemacs-ignored-environment-variables): Fix typos and mention that the strings are regexps that are matched against the names of environment variables. (spacemacs//init-spacemacs-env): Use a temporary file rather than standard output in order to avoid getting motd and other noise. Sort lines using only environment variables' names, not their values. Better describe the behavior with respect to spacemacs-ignored-environment-variables, duplicate settings, and PATH. Try to make wording clearer. (spacemacs/edit-env): Fix typos in the docstring. * core/libs/load-env-vars.el (load-env-vars-set-env): Fix typos in the docstring. Delete trailing slashes from path entries in exec-path, convert drive letters to lower-case, delete duplicate path entries, and convert backslashes to forward slashes when setting exec-path. Set PATH from exec-path, converting forward slashes to backslashes on platforms that use backslashes.
2019-06-09 03:12:23 +00:00
(apply-partially #'subst-char-in-string ?/ ?\\)
;; Assume that we start with forward slashes.
#'identity)))
(dolist (element env-vars)
(let ((key (car element)) (value (cadr element)))
(if (string-equal "PATH" key)
(let ((paths (split-string value path-separator)))
(setq exec-path (cl-remove-duplicates
(append (mapcar (lambda (path) (directory-file-name (subst-char-in-string ?\\ ?/ path))) paths) exec-path)
:test #'string-equal :from-end t)
)
(setenv "PATH" (mapconcat convert-to-os-path exec-path path-separator)))
[core] Environment variable caching fixes Fix several problems with environment variable caching. First, if a shell printed extra output besides the command output, spacemacs//init-spacemacs-env included the extra output in .spacemacs.env. For example, a login shell could print a motd, which would be erroneously included in .spacemacs.env. To avoid this, spacemacs//init-spacemacs-env now redirects the command output to a temporary file and then reads the file. Second, spacemacs//init-spacemacs-env sorted lines using the entire "name=value" string for each line whereas the correct behavior is to sort only on the "name" part. To fix this, spacemacs//init-spacemacs-env now uses sort-regexp-fields with an appropriate regexp to match the "name" part and ignore the "value" part. Third, although load-env-vars-set-env added all PATH settings in .spacemacs.env to exec-path, it set PATH to the first PATH setting in .spacemacs.env. Now load-env-vars-set-env sets PATH from exec-path so that it will reflect that combined value. Fourth, load-env-vars-set-env set exec-path and PATH without consideration to the operating system's directory separator. Now load-env-vars-set-env converts backslashes to forward slashes when adding path entries to exec-path and converts forward slashes to backslashes when setting PATH on platforms that use backslashes. Fifth, load-env-vars-set-env now normalizes exec-path by deleting trailing slashes from path entries, converting drive letters in path entries to lower-case, and deleting duplicate path entries. Sixth, Spacemacs no longer uses exec-path-from-shell, but a changelog entry said that it did. This commit corrects the changelog entry, deletes another outdated entry, and merges two entries that covered the same functionality. Finally, this commit corrects several typos and tries to improve wording in several docstrings, messages, and comments. * CHANGELOG.develop: Delete mention of exec-path-from-shell, which Spacemacs no longer uses by default. Delete mention of dotspacemacs-import-env-vars-from-shell and dotspacemacs-import-env-vars-shell-file-name, which no longer exist. Merge entries about synchronizing environment variables. * core/core-env.el (spacemacs-ignored-environment-variables): Fix typos and mention that the strings are regexps that are matched against the names of environment variables. (spacemacs//init-spacemacs-env): Use a temporary file rather than standard output in order to avoid getting motd and other noise. Sort lines using only environment variables' names, not their values. Better describe the behavior with respect to spacemacs-ignored-environment-variables, duplicate settings, and PATH. Try to make wording clearer. (spacemacs/edit-env): Fix typos in the docstring. * core/libs/load-env-vars.el (load-env-vars-set-env): Fix typos in the docstring. Delete trailing slashes from path entries in exec-path, convert drive letters to lower-case, delete duplicate path entries, and convert backslashes to forward slashes when setting exec-path. Set PATH from exec-path, converting forward slashes to backslashes on platforms that use backslashes.
2019-06-09 03:12:23 +00:00
(setenv key value))))))
;;;###autoload
(defun load-env-vars (file-path)
"Load environment variables found in FILE-PATH."
(interactive "fEnvironment variables file: ")
(with-temp-buffer
(insert-file-contents file-path)
(let ((env-vars (load-env-vars-extract-env-vars)))
(load-env-vars-set-env env-vars))))
(provide 'load-env-vars)
;;; load-env-vars.el ends here