From f6b8807e83f4e6058bd2e8935b0c3421121d91be Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 5 Sep 2019 14:20:50 -0400 Subject: [PATCH] Add -o option to 'doom env' Now you can output envvar files where ever you like. --- bin/doom | 43 ++++++++-------- core/cli/env.el | 134 +++++++++++++++++++++++++----------------------- 2 files changed, 90 insertions(+), 87 deletions(-) diff --git a/bin/doom b/bin/doom index c6d0d7ba6..7a33bff35 100755 --- a/bin/doom +++ b/bin/doom @@ -98,25 +98,24 @@ (print! (error "No command detected.\n"))) (usage)) ((require 'core-cli) - (let ((default-directory user-emacs-directory)) - (setq argv nil) - (condition-case e - (doom-dispatch (car args) (cdr args)) - (user-error - (print! (error "%s\n") (error-message-string e)) - (print! (yellow "See 'doom help %s' for documentation on this command.") (car args))) - ((debug error) - (message "--------------------------------------------------\n") - (message "There was an unexpected error:") - (message " %s (%s)" (get (car e) 'error-message) (car e)) - (dolist (item (cdr e)) - (message " %s" item)) - (unless debug-on-error - (message - (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" - "report, please include it!\n\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" - " e.g. doom -d install 2>&1 | clipboard-program")) - (signal 'doom-error e))))))))) + (setq argv nil) + (condition-case e + (doom-dispatch (car args) (cdr args)) + (user-error + (print! (error "%s\n") (error-message-string e)) + (print! (yellow "See 'doom help %s' for documentation on this command.") (car args))) + ((debug error) + (message "--------------------------------------------------\n") + (message "There was an unexpected error:") + (message " %s (%s)" (get (car e) 'error-message) (car e)) + (dolist (item (cdr e)) + (message " %s" item)) + (unless debug-on-error + (message + (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" + "report, please include it!\n\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" + " e.g. doom -d install 2>&1 | clipboard-program")) + (signal 'doom-error e)))))))) diff --git a/core/cli/env.el b/core/cli/env.el index 9a1240bda..04276e0aa 100644 --- a/core/cli/env.el +++ b/core/cli/env.el @@ -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. Use the -c or --clear switch to delete your envvar file." - (let ((default-directory doom-emacs-dir)) - (when (member "clear" args) ; DEPRECATED - (message "'doom env clear' is deprecated. Use 'doom env -c' or 'doom env --clear' instead") - (push "-c" args)) + (when (member "clear" args) ; DEPRECATED + (message "'doom env clear' is deprecated. Use 'doom env -c' or 'doom env --clear' instead") + (push "-c" args)) + (let ((env-file (or (cadr (member "-o" args)) + doom-env-file))) (cond ((or (member "-c" 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" - (relpath doom-env-file))) - (delete-file doom-env-file) + (relpath env-file))) + (delete-file env-file) (print! (success "Successfully deleted %S") - (relpath doom-env-file))) + (relpath env-file))) - ((null args) - (doom-reload-env-file 'force)) + ((or (null args) + (member "-o" args)) + (doom-reload-env-file 'force env-file)) ((user-error "I don't understand 'doom env %s'" (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 `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 env-file) "Generates `doom-env-file', if it doesn't exist (or if FORCE-P). This scrapes the variables from your shell environment by running `doom-env-executable' through `shell-file-name' with `doom-env-switches'. By default, on Linux, this is '$SHELL -ic /usr/bin/env'. Variables in `doom-env-ignored-vars' are removed." - (when (or force-p (not (file-exists-p doom-env-file))) - (with-temp-file doom-env-file - (print! (start "%s envvars file at %S") - (if (file-exists-p doom-env-file) - "Regenerating" - "Generating") - (relpath doom-env-file doom-emacs-dir)) - (let ((process-environment doom--initial-process-environment)) - (let ((shell-command-switch doom-env-switches) - (error-buffer (get-buffer-create "*env errors*"))) - (print! (info "Scraping shell environment with '%s %s %s'") - (filename shell-file-name) - shell-command-switch - (filename doom-env-executable)) - (save-excursion - (shell-command doom-env-executable (current-buffer) error-buffer)) - (print-group! - (let ((errors (with-current-buffer error-buffer (buffer-string)))) - (unless (string-empty-p errors) - (print! (info "Error output:\n\n%s") (indent 4 errors)))) - ;; Remove undesireable variables - (insert - (concat - "# -*- mode: dotenv -*-\n" - (format "# Generated with: %s %s %s\n" - shell-file-name - doom-env-switches - doom-env-executable) - "# ---------------------------------------------------------------------------\n" - "# This file was auto-generated by `doom env'. It contains a list of environment\n" - "# variables scraped from your default shell (excluding variables blacklisted\n" - "# in doom-env-ignored-vars).\n" - "#\n" - "# It is NOT safe to edit this file. Changes will be overwritten next time that\n" - "# `doom refresh` is executed. Alternatively, create your own env file and load\n" - "# it with `(doom-load-envvars-file FILE)` in your private config.el.\n" - "# ---------------------------------------------------------------------------\n\n")) - (goto-char (point-min)) - (while (re-search-forward "\n\\([^= \n]+\\)=" nil t) - (save-excursion - (let* ((valend (or (save-match-data - (when (re-search-forward "^\\([^= ]+\\)=" nil t) - (line-beginning-position))) - (point-max))) - (var (match-string 1))) - (when (cl-loop for regexp in doom-env-ignored-vars - if (string-match-p regexp var) - return t) - (print! (info "Ignoring %s") var) - (delete-region (match-beginning 0) (1- valend))))))) - (print! (success "Successfully generated %S") - (relpath doom-env-file doom-emacs-dir)) - t))))) + (let ((env-file (if env-file + (expand-file-name env-file) + doom-env-file))) + (when (or force-p (not (file-exists-p env-file))) + (with-temp-file env-file + (print! (start "%s envvars file at %S") + (if (file-exists-p env-file) + "Regenerating" + "Generating") + (relpath env-file doom-emacs-dir)) + (let ((process-environment doom--initial-process-environment)) + (let ((shell-command-switch doom-env-switches) + (error-buffer (get-buffer-create "*env errors*"))) + (print! (info "Scraping shell environment with '%s %s %s'") + (filename shell-file-name) + shell-command-switch + (filename doom-env-executable)) + (save-excursion + (shell-command doom-env-executable (current-buffer) error-buffer)) + (print-group! + (let ((errors (with-current-buffer error-buffer (buffer-string)))) + (unless (string-empty-p errors) + (print! (info "Error output:\n\n%s") (indent 4 errors)))) + ;; Remove undesireable variables + (insert + (concat + "# -*- mode: dotenv -*-\n" + (format "# Generated with: %s %s %s\n" + shell-file-name + doom-env-switches + doom-env-executable) + "# ---------------------------------------------------------------------------\n" + "# This file was auto-generated by `doom env'. It contains a list of environment\n" + "# variables scraped from your default shell (excluding variables blacklisted\n" + "# in doom-env-ignored-vars).\n" + "#\n" + "# It is NOT safe to edit this file. Changes will be overwritten next time that\n" + "# `doom refresh` is executed. Alternatively, create your own env file and load\n" + "# it with `(doom-load-envvars-file FILE)` in your private config.el.\n" + "# ---------------------------------------------------------------------------\n\n")) + (goto-char (point-min)) + (while (re-search-forward "\n\\([^= \n]+\\)=" nil t) + (save-excursion + (let* ((valend (or (save-match-data + (when (re-search-forward "^\\([^= ]+\\)=" nil t) + (line-beginning-position))) + (point-max))) + (var (match-string 1))) + (when (cl-loop for regexp in doom-env-ignored-vars + if (string-match-p regexp var) + return t) + (print! (info "Ignoring %s") var) + (delete-region (match-beginning 0) (1- valend))))))) + (print! (success "Successfully generated %S") + (relpath env-file doom-emacs-dir)) + t))))))