forked from syl20bnr/spacemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: rework environment variables and PATH management
See updated DOCUMENTATION.org and FAQ.org for more info. * add core-env.el * add library load-env-vars.el * add bootstrap package dotenv-mode.el * remove spacemacs-environment from bootstrap layer * remove dotspacemacs variable dotspacemacs-import-env-vars-from-shell * remove dotspacemacs variable dotspacemacs-improt-env-vars-shell-file-name * add new key binding SPC f e e to open spacemacs.env file * add new key binding SPC f e E to reload environment variable from env file * add new key binding SPC f e C-e to re-initialize the env file from shell.
- Loading branch information
Showing
16 changed files
with
237 additions
and
120 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
;;; core-env.el --- Spacemacs Core File | ||
;; | ||
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors | ||
;; | ||
;; Author: Sylvain Benner <[email protected]> | ||
;; URL: https://github.com/syl20bnr/spacemacs | ||
;; | ||
;; This file is not part of GNU Emacs. | ||
;; | ||
;;; License: GPLv3 | ||
|
||
(require 'core-dotspacemacs) | ||
(require 'load-env-vars) | ||
|
||
(defvar spacemacs-env-vars-file | ||
(concat (or dotspacemacs-directory spacemacs-private-directory) "spacemacs.env") | ||
"Absolute path to the env file where environment variables are set.") | ||
|
||
(defun spacemacs/init-env (&optional force) | ||
"Attempt to fetch the environment variables from the users shell. | ||
This solution is far from perfect and we should not rely on this function | ||
a lot. We use it only to initialize the env file when it does not exist | ||
yet. | ||
If FORCE is non-nil then force the initialization of the file, note that the | ||
current contents of the file will be overwritten." | ||
(when (or force (not (file-exists-p spacemacs-env-vars-file))) | ||
(with-temp-file spacemacs-env-vars-file | ||
(let ((shell-command-switch "-ic")) | ||
(insert (shell-command-to-string "env")))) | ||
(spacemacs-buffer/warning | ||
(concat "Spacemacs tried to import your environment variables from " | ||
"your shell and saved them to `%s'. " | ||
"Please check that the values are correct by calling " | ||
"`spacemacs/edit-env' function and update the file if needed. " | ||
"If you later need to add environment variables add them to this " | ||
"file.") | ||
spacemacs-env-vars-file))) | ||
|
||
(defun spacemacs/force-init-env () | ||
"Forces a reinitialization of environment variables." | ||
(interactive) | ||
(spacemacs/init-env t)) | ||
|
||
(defun spacemacs/edit-env () | ||
"Open the env file for edition." | ||
(interactive) | ||
(if (file-exists-p spacemacs-env-vars-file) | ||
(progn | ||
(find-file spacemacs-env-vars-file) | ||
(when (fboundp 'dotenv-mode) | ||
(dotenv-mode))) | ||
(message "Cannot file env file `%s'" spacemacs-env-vars-file))) | ||
|
||
(defun spacemacs/load-env (&optional force) | ||
"Load the environment variables from the env file. | ||
If FORCE is non-nil then force the loading of environment variables from env | ||
file." | ||
(interactive "P") | ||
(when (or force (and (display-graphic-p) | ||
(or (eq system-type 'darwin) | ||
(eq system-type 'gnu/linux) | ||
(eq window-system 'x))))) | ||
(if (file-exists-p spacemacs-env-vars-file) | ||
(progn | ||
(load-env-vars spacemacs-env-vars-file) | ||
(message "Environment variables loaded.")) | ||
(message "Cannot file env file `%s'" spacemacs-env-vars-file))) | ||
|
||
(provide 'core-env) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
;;; load-env-vars.el --- Load environment variables from files -*- lexical-binding: t; -*- | ||
|
||
;; Copyright (C) 2018 Jorge Dias | ||
|
||
;; Author: Jorge Dias <[email protected]> | ||
;; 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 | ||
(and "'" (group (0+ (or "\\'" (not (any "'"))))) "'") ;; single quoted value | ||
(and ?\" (group (0+ (or "\\\"" (not (any "\""))))) ?\") ;; double quoted value | ||
(group (1+ (not (in "#" "\n" space)))) ;; 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) | ||
"Set envariable variables from key value lists from ENV-VARS." | ||
(dolist (element env-vars) | ||
(let ((key (car element)) (value (cadr element))) | ||
(when (string-equal "PATH" key) | ||
(let ((paths (split-string value path-separator))) | ||
(dolist (p paths) | ||
(add-to-list 'exec-path p)))) | ||
(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 |
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
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
Oops, something went wrong.