Refactor doom env
- Limits process-environment during scraping - Add `doom-env-executable` and `doom-env-switches` variables - Announce what commands were run to produce your env var within env var file header
This commit is contained in:
parent
34e3985409
commit
700cda370b
2 changed files with 66 additions and 39 deletions
100
core/cli/env.el
100
core/cli/env.el
|
@ -3,7 +3,7 @@
|
||||||
(dispatcher! env
|
(dispatcher! env
|
||||||
(let ((env-file (abbreviate-file-name doom-env-file)))
|
(let ((env-file (abbreviate-file-name doom-env-file)))
|
||||||
(pcase (car args)
|
(pcase (car args)
|
||||||
("refresh"
|
((or "refresh" "re")
|
||||||
(doom-reload-env-file 'force))
|
(doom-reload-env-file 'force))
|
||||||
("enable"
|
("enable"
|
||||||
(setenv "DOOMENV" "1")
|
(setenv "DOOMENV" "1")
|
||||||
|
@ -42,7 +42,7 @@ needs to be run once).")
|
||||||
;;
|
;;
|
||||||
;; Helpers
|
;; Helpers
|
||||||
|
|
||||||
(defvar doom-ignored-env-vars
|
(defvar doom-env-ignored-vars
|
||||||
'("DBUS_SESSION_BUS_ADDRESS"
|
'("DBUS_SESSION_BUS_ADDRESS"
|
||||||
"GPG_AGENT_INFO"
|
"GPG_AGENT_INFO"
|
||||||
"SSH_AGENT_PID"
|
"SSH_AGENT_PID"
|
||||||
|
@ -53,9 +53,30 @@ needs to be run once).")
|
||||||
"YES")
|
"YES")
|
||||||
"Environment variables to not save in `doom-env-file'.")
|
"Environment variables to not save in `doom-env-file'.")
|
||||||
|
|
||||||
;; Borrows heavily from Spacemacs'`spacemacs//init-spacemacs-env'.
|
(defvar doom-env-executable
|
||||||
|
(if IS-WINDOWS
|
||||||
|
"set"
|
||||||
|
(executable-find "env"))
|
||||||
|
"The program to use to scrape your shell environment with.
|
||||||
|
It is rare that you'll need to change this.")
|
||||||
|
|
||||||
|
(defvar doom-env-switches
|
||||||
|
(if IS-WINDOWS
|
||||||
|
'("-c")
|
||||||
|
;; Execute twice, once in a non-interactive login shell and once in an
|
||||||
|
;; interactive shell in order to capture all the init files possible.
|
||||||
|
'("-lc" "-ic"))
|
||||||
|
"The `shell-command-switch'es to use on `doom-env-executable'.
|
||||||
|
This is a list of strings. Each entry is run separately and in sequence with
|
||||||
|
`doom-env-executable' to scrape envvars from your shell environment.")
|
||||||
|
|
||||||
|
;; Borrows heavily from Spacemacs' `spacemacs//init-spacemacs-env'.
|
||||||
(defun doom-reload-env-file (&optional force-p)
|
(defun doom-reload-env-file (&optional force-p)
|
||||||
"Generates `doom-env-file', if it doesn't exist (or FORCE-P is non-nil)."
|
"Generates `doom-env-file', if it doesn't exist (or FORCE-P is non-nil).
|
||||||
|
|
||||||
|
Runs `doom-env-executable' X times, where X = length of `doom-env-switches', to
|
||||||
|
scrape the variables from your shell environment. Duplicates are removed. The
|
||||||
|
order of `doom-env-switches' determines priority."
|
||||||
(when (or force-p (not (file-exists-p doom-env-file)))
|
(when (or force-p (not (file-exists-p doom-env-file)))
|
||||||
(with-temp-file doom-env-file
|
(with-temp-file doom-env-file
|
||||||
(message "%s envvars file at %S"
|
(message "%s envvars file at %S"
|
||||||
|
@ -63,37 +84,40 @@ needs to be run once).")
|
||||||
"Regenerating"
|
"Regenerating"
|
||||||
"Generating")
|
"Generating")
|
||||||
(abbreviate-file-name doom-env-file))
|
(abbreviate-file-name doom-env-file))
|
||||||
(insert
|
(let ((process-environment doom-site-process-environment))
|
||||||
(concat
|
(insert
|
||||||
"# -*- mode: dotenv -*-\n"
|
(concat
|
||||||
"# ---------------------------------------------------------------------------\n"
|
"# -*- mode: dotenv -*-\n"
|
||||||
"# This file was auto-generated by Doom. It contains all environment variables\n"
|
"# ---------------------------------------------------------------------------\n"
|
||||||
"# scraped from your default shell (excluding variables blacklisted in\n"
|
"# This file was auto-generated by Doom by running:\n"
|
||||||
"# doom-ignored-env-vars).\n"
|
"#\n"
|
||||||
"#\n"
|
(cl-loop for switch in doom-env-switches
|
||||||
"# It is NOT safe to edit this file. Changes will be overwritten next time\n"
|
concat (format "# %s %s %s\n"
|
||||||
"# that `doom env refresh` is executed. Alternatively, create your own env file\n"
|
shell-file-name
|
||||||
"# in your DOOMDIR and load that with `(load-env-vars FILE)`.\n"
|
switch
|
||||||
"#\n"
|
doom-env-executable))
|
||||||
"# To auto-regenerate this file when `doom reload` is run, use `doom env enable'\n"
|
"#\n"
|
||||||
"# or set DOOMENV=1 in your shell environment/config.\n"
|
"# It contains all environment variables scraped from your default shell\n"
|
||||||
"# ---------------------------------------------------------------------------\n\n"))
|
"# (excluding variables blacklisted in doom-env-ignored-vars).\n"
|
||||||
(let ((env-point (point))
|
"#\n"
|
||||||
(switches
|
"# It is NOT safe to edit this file. Changes will be overwritten next time\n"
|
||||||
(cond (IS-WINDOWS '("-c"))
|
"# that `doom env refresh` is executed. Alternatively, create your own env file\n"
|
||||||
;; Execute twice, once in a non-interactive login shell and
|
"# in your DOOMDIR and load that with `(load-env-vars FILE)`.\n"
|
||||||
;; once in an interactive shell in order to capture all the
|
"#\n"
|
||||||
;; init files possible.
|
"# To auto-regenerate this file when `doom reload` is run, use `doom env enable'\n"
|
||||||
((or IS-MAC IS-LINUX) '("-lc" "-ic"))))
|
"# or set DOOMENV=1 in your shell environment/config.\n"
|
||||||
(executable (if IS-WINDOWS
|
"# ---------------------------------------------------------------------------\n\n"))
|
||||||
"set"
|
(let ((env-point (point)))
|
||||||
(executable-find "env"))))
|
(dolist (shell-command-switch doom-env-switches)
|
||||||
(dolist (shell-command-switch switches)
|
(message "Scraping env from '%s %s %s'"
|
||||||
(insert (shell-command-to-string executable)))
|
shell-file-name
|
||||||
;; sort the environment variables
|
shell-command-switch
|
||||||
(sort-lines nil env-point (point-max))
|
doom-env-executable)
|
||||||
;; remove adjacent duplicated lines
|
(insert (shell-command-to-string doom-env-executable)))
|
||||||
(delete-duplicate-lines env-point (point-max) nil t)
|
;; sort the environment variables
|
||||||
;; remove ignored environment variables
|
(sort-lines nil env-point (point-max))
|
||||||
(dolist (var doom-ignored-env-vars)
|
;; remove adjacent duplicated lines
|
||||||
(flush-lines (concat "^" var "=") env-point (point-max)))))))
|
(delete-duplicate-lines env-point (point-max) nil t)
|
||||||
|
;; remove ignored environment variables
|
||||||
|
(dolist (var doom-env-ignored-vars)
|
||||||
|
(flush-lines (concat "^" var "=") env-point (point-max))))))))
|
||||||
|
|
|
@ -99,7 +99,10 @@ MacOS users).")
|
||||||
Doom was setup, which can cause problems.")
|
Doom was setup, which can cause problems.")
|
||||||
|
|
||||||
(defvar doom-site-load-path load-path
|
(defvar doom-site-load-path load-path
|
||||||
"The starting load-path, before it is altered by `doom-initialize'.")
|
"The starting `load-path', before it is altered by `doom-initialize'.")
|
||||||
|
|
||||||
|
(defvar doom-site-process-environment process-environment
|
||||||
|
"The starting `process-environment', before it is altered by `doom-initialize'.")
|
||||||
|
|
||||||
(defvar doom--last-emacs-file (concat doom-local-dir "emacs-version.el"))
|
(defvar doom--last-emacs-file (concat doom-local-dir "emacs-version.el"))
|
||||||
(defvar doom--last-emacs-version nil)
|
(defvar doom--last-emacs-version nil)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue