Add -o option to 'doom env'

Now you can output envvar files where ever you like.
This commit is contained in:
Henrik Lissner 2019-09-05 14:20:50 -04:00
parent 9f08d11908
commit f6b8807e83
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 90 additions and 87 deletions

View file

@ -98,25 +98,24 @@
(print! (error "No command detected.\n"))) (print! (error "No command detected.\n")))
(usage)) (usage))
((require 'core-cli) ((require 'core-cli)
(let ((default-directory user-emacs-directory)) (setq argv nil)
(setq argv nil) (condition-case e
(condition-case e (doom-dispatch (car args) (cdr args))
(doom-dispatch (car args) (cdr args)) (user-error
(user-error (print! (error "%s\n") (error-message-string e))
(print! (error "%s\n") (error-message-string e)) (print! (yellow "See 'doom help %s' for documentation on this command.") (car args)))
(print! (yellow "See 'doom help %s' for documentation on this command.") (car args))) ((debug error)
((debug error) (message "--------------------------------------------------\n")
(message "--------------------------------------------------\n") (message "There was an unexpected error:")
(message "There was an unexpected error:") (message " %s (%s)" (get (car e) 'error-message) (car e))
(message " %s (%s)" (get (car e) 'error-message) (car e)) (dolist (item (cdr e))
(dolist (item (cdr e)) (message " %s" item))
(message " %s" item)) (unless debug-on-error
(unless debug-on-error (message
(message (concat "\nRun the command again with the -d (or --debug) option to enable debug\n"
(concat "\nRun the command again with the -d (or --debug) option to enable debug\n" "mode and, hopefully, generate a stack trace. If you decide to file a bug\n"
"mode and, hopefully, generate a stack trace. If you decide to file a bug\n" "report, please include it!\n\n"
"report, please include it!\n\n" "Emacs outputs to standard error, so you'll need to redirect stderr to\n"
"Emacs outputs to standard error, so you'll need to redirect stderr to\n" "stdout to pipe this to a file or clipboard!\n\n"
"stdout to pipe this to a file or clipboard!\n\n" " e.g. doom -d install 2>&1 | clipboard-program"))
" e.g. doom -d install 2>&1 | clipboard-program")) (signal 'doom-error e))))))))
(signal 'doom-error e)))))))))

View file

@ -22,22 +22,24 @@ This file is automatically regenerated when you run this command or 'doom
refresh'. However, 'doom refresh' will only regenerate this file if it exists. refresh'. However, 'doom refresh' will only regenerate this file if it exists.
Use the -c or --clear switch to delete your envvar file." Use the -c or --clear switch to delete your envvar file."
(let ((default-directory doom-emacs-dir)) (when (member "clear" args) ; DEPRECATED
(when (member "clear" args) ; DEPRECATED (message "'doom env clear' is deprecated. Use 'doom env -c' or 'doom env --clear' instead")
(message "'doom env clear' is deprecated. Use 'doom env -c' or 'doom env --clear' instead") (push "-c" args))
(push "-c" args))
(let ((env-file (or (cadr (member "-o" args))
doom-env-file)))
(cond ((or (member "-c" args) (cond ((or (member "-c" args)
(member "--clear" args)) (member "--clear" args))
(unless (file-exists-p doom-env-file) (unless (file-exists-p env-file)
(user-error! "%S does not exist to be cleared" (user-error! "%S does not exist to be cleared"
(relpath doom-env-file))) (relpath env-file)))
(delete-file doom-env-file) (delete-file env-file)
(print! (success "Successfully deleted %S") (print! (success "Successfully deleted %S")
(relpath doom-env-file))) (relpath env-file)))
((null args) ((or (null args)
(doom-reload-env-file 'force)) (member "-o" args))
(doom-reload-env-file 'force env-file))
((user-error "I don't understand 'doom env %s'" ((user-error "I don't understand 'doom env %s'"
(string-join args " ")))))) (string-join args " "))))))
@ -79,64 +81,66 @@ It is rare that you'll need to change this.")
This is a list of strings. Each entry is run separately and in sequence with 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.") `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 env-file)
(defun doom-reload-env-file (&optional force-p)
"Generates `doom-env-file', if it doesn't exist (or if FORCE-P). "Generates `doom-env-file', if it doesn't exist (or if FORCE-P).
This scrapes the variables from your shell environment by running This scrapes the variables from your shell environment by running
`doom-env-executable' through `shell-file-name' with `doom-env-switches'. By `doom-env-executable' through `shell-file-name' with `doom-env-switches'. By
default, on Linux, this is '$SHELL -ic /usr/bin/env'. Variables in default, on Linux, this is '$SHELL -ic /usr/bin/env'. Variables in
`doom-env-ignored-vars' are removed." `doom-env-ignored-vars' are removed."
(when (or force-p (not (file-exists-p doom-env-file))) (let ((env-file (if env-file
(with-temp-file doom-env-file (expand-file-name env-file)
(print! (start "%s envvars file at %S") doom-env-file)))
(if (file-exists-p doom-env-file) (when (or force-p (not (file-exists-p env-file)))
"Regenerating" (with-temp-file env-file
"Generating") (print! (start "%s envvars file at %S")
(relpath doom-env-file doom-emacs-dir)) (if (file-exists-p env-file)
(let ((process-environment doom--initial-process-environment)) "Regenerating"
(let ((shell-command-switch doom-env-switches) "Generating")
(error-buffer (get-buffer-create "*env errors*"))) (relpath env-file doom-emacs-dir))
(print! (info "Scraping shell environment with '%s %s %s'") (let ((process-environment doom--initial-process-environment))
(filename shell-file-name) (let ((shell-command-switch doom-env-switches)
shell-command-switch (error-buffer (get-buffer-create "*env errors*")))
(filename doom-env-executable)) (print! (info "Scraping shell environment with '%s %s %s'")
(save-excursion (filename shell-file-name)
(shell-command doom-env-executable (current-buffer) error-buffer)) shell-command-switch
(print-group! (filename doom-env-executable))
(let ((errors (with-current-buffer error-buffer (buffer-string)))) (save-excursion
(unless (string-empty-p errors) (shell-command doom-env-executable (current-buffer) error-buffer))
(print! (info "Error output:\n\n%s") (indent 4 errors)))) (print-group!
;; Remove undesireable variables (let ((errors (with-current-buffer error-buffer (buffer-string))))
(insert (unless (string-empty-p errors)
(concat (print! (info "Error output:\n\n%s") (indent 4 errors))))
"# -*- mode: dotenv -*-\n" ;; Remove undesireable variables
(format "# Generated with: %s %s %s\n" (insert
shell-file-name (concat
doom-env-switches "# -*- mode: dotenv -*-\n"
doom-env-executable) (format "# Generated with: %s %s %s\n"
"# ---------------------------------------------------------------------------\n" shell-file-name
"# This file was auto-generated by `doom env'. It contains a list of environment\n" doom-env-switches
"# variables scraped from your default shell (excluding variables blacklisted\n" doom-env-executable)
"# in doom-env-ignored-vars).\n" "# ---------------------------------------------------------------------------\n"
"#\n" "# This file was auto-generated by `doom env'. It contains a list of environment\n"
"# It is NOT safe to edit this file. Changes will be overwritten next time that\n" "# variables scraped from your default shell (excluding variables blacklisted\n"
"# `doom refresh` is executed. Alternatively, create your own env file and load\n" "# in doom-env-ignored-vars).\n"
"# it with `(doom-load-envvars-file FILE)` in your private config.el.\n" "#\n"
"# ---------------------------------------------------------------------------\n\n")) "# It is NOT safe to edit this file. Changes will be overwritten next time that\n"
(goto-char (point-min)) "# `doom refresh` is executed. Alternatively, create your own env file and load\n"
(while (re-search-forward "\n\\([^= \n]+\\)=" nil t) "# it with `(doom-load-envvars-file FILE)` in your private config.el.\n"
(save-excursion "# ---------------------------------------------------------------------------\n\n"))
(let* ((valend (or (save-match-data (goto-char (point-min))
(when (re-search-forward "^\\([^= ]+\\)=" nil t) (while (re-search-forward "\n\\([^= \n]+\\)=" nil t)
(line-beginning-position))) (save-excursion
(point-max))) (let* ((valend (or (save-match-data
(var (match-string 1))) (when (re-search-forward "^\\([^= ]+\\)=" nil t)
(when (cl-loop for regexp in doom-env-ignored-vars (line-beginning-position)))
if (string-match-p regexp var) (point-max)))
return t) (var (match-string 1)))
(print! (info "Ignoring %s") var) (when (cl-loop for regexp in doom-env-ignored-vars
(delete-region (match-beginning 0) (1- valend))))))) if (string-match-p regexp var)
(print! (success "Successfully generated %S") return t)
(relpath doom-env-file doom-emacs-dir)) (print! (info "Ignoring %s") var)
t))))) (delete-region (match-beginning 0) (1- valend)))))))
(print! (success "Successfully generated %S")
(relpath env-file doom-emacs-dir))
t))))))