From 1e6ef4d6e4ec168235988d574f7043d13bc329a6 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Fri, 13 Dec 2019 15:23:23 -0500 Subject: [PATCH] Rewrite interactive CLI commands - Replace doom//upgrade with doom/upgrade - Replace doom//autoloads with doom/reload-autoloads - Replace doom//refresh with doom/reload - Remove doom//install; there should be no workflow for this command - Remove doom//autoremove; autoremove was replaced with purge. Maybe I'll write a doom//purge analogue. Not sure yet. - Use compile instead of hacky wrapper around core-cli API Rewrite interactive CLI commands - Rewrite doom//upgrade & doom//autoloads - Remove doom//install; there really should be no use-case for it - Remove doom//autoremove; autoremove was replaced with purge. I'll get around to writing a doom//purge eventually. fixup! Rewrite interactive CLI commands --- bin/doom | 6 +-- core/autoload/cli.el | 81 ----------------------------------------- core/autoload/config.el | 81 +++++++++++++++++++++++++++-------------- 3 files changed, 57 insertions(+), 111 deletions(-) diff --git a/bin/doom b/bin/doom index ddb82518f..47dc9b7cd 100755 --- a/bin/doom +++ b/bin/doom @@ -75,7 +75,8 @@ with a different private module." start-time))))) (user-error (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)) + (error "")) ; Ensure non-zero exit code ((debug error) (print! (error "There was an unexpected error:")) (print-group! @@ -99,8 +100,7 @@ with a different private module." (string-join (append (list (file-name-nondirectory load-file-name) "-d" command) args) " ")) - ;; Ensure the process returns non-zero - (error ""))))))) + (error ""))))))) ; Ensure non-zero exit code (doom-cli-execute :main (cdr (member "--" argv))) (setq argv nil)) diff --git a/core/autoload/cli.el b/core/autoload/cli.el index e7f45c810..f0582ef1c 100644 --- a/core/autoload/cli.el +++ b/core/autoload/cli.el @@ -1,86 +1,5 @@ ;;; core/autoload/cli.el -*- lexical-binding: t; -*- -;; Externs -(defvar evil-collection-mode-list) - -;;;###autoload -(defun doom--cli-run (command &rest _args) - (require 'core-cli) - (require 'core-packages) - (require 'straight) - (when (featurep 'general) - (general-auto-unbind-keys)) - (let* ((evil-collection-mode-list nil) - (default-directory doom-emacs-dir) - (buf (get-buffer-create " *bin/doom*")) - (doom-format-backend 'ansi) - (ignore-window-parameters t) - (noninteractive t) - (standard-output - (lambda (char) - (with-current-buffer buf - (insert char) - (when (memq char '(?\n ?\r)) - (ansi-color-apply-on-region (line-beginning-position -1) (line-end-position)) - (redisplay)))))) - (setq doom-modules (doom-modules)) - (with-current-buffer (switch-to-buffer buf) - (erase-buffer) - (require 'package) - (redisplay) - (doom-cli-execute command nil) - (print! (green "\nDone!")))) - (when (featurep 'general) - (general-auto-unbind-keys 'undo)) - (message (format! (green "Done!")))) - - -;;;###autoload -(defun doom//autoloads (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "autoloads"))) - -;;;###autoload -(defun doom//update (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "update"))) - -;;;###autoload -(defun doom//upgrade (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "upgrade")) - (when (y-or-n-p "You must restart Emacs for the upgrade to take effect. Restart?") - (doom/restart-and-restore))) - -;;;###autoload -(defun doom//install (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "install"))) - -;;;###autoload -(defun doom//autoremove (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "autoremove"))) - -;;;###autoload -(defun doom//refresh (&optional yes) - "TODO" - (interactive "P") - (let ((doom-auto-accept yes)) - (doom--cli-run "refresh"))) - - - ;; ;;; Library diff --git a/core/autoload/config.el b/core/autoload/config.el index 579d0f447..4159a48b0 100644 --- a/core/autoload/config.el +++ b/core/autoload/config.el @@ -1,5 +1,8 @@ ;;; core/autoload/config.el -*- lexical-binding: t; -*- +(defvar doom-bin-dir (concat doom-emacs-dir "bin/")) +(defvar doom-bin (concat doom-bin-dir "doom")) + ;;;###autoload (defvar doom-reload-hook nil "A list of hooks to run when `doom/reload' is called.") @@ -45,6 +48,21 @@ (interactive) (find-file (expand-file-name "packages.el" doom-private-dir))) + +;; +;;; Managements + +(cl-defmacro doom--compile (command &key on-success on-failure) + (declare (indent defun)) + `(with-current-buffer (compile ,command) + (add-hook + 'compilation-finish-functions + (lambda (_buf status) + (if (equal status "finished\n") + ,on-success + ,on-failure)) + nil 'local))) + ;;;###autoload (defun doom/reload () "Reloads your private config. @@ -55,25 +73,20 @@ reloads your package list, and lastly, reloads your private config.el. Runs `doom-reload-hook' afterwards." (interactive) - (or (y-or-n-p - (concat "You are about to reload your Doom config from within Emacs. This " - "is highly experimental and may cause issues. It is recommended you " - "use 'bin/doom refresh' on the command line instead.\n\n" - "Reload anyway?")) - (user-error "Aborted")) (require 'core-cli) - (let ((doom-reloading-p t)) - (compile (format "%s/bin/doom refresh" doom-emacs-dir)) - (while compilation-in-progress - (sit-for 1)) - (doom-initialize 'force) - (with-demoted-errors "PRIVATE CONFIG ERROR: %s" - (general-auto-unbind-keys) - (unwind-protect - (doom-initialize-modules 'force) - (general-auto-unbind-keys t))) - (run-hook-wrapped 'doom-reload-hook #'doom-try-run-hook)) - (message "Finished!")) + (doom--compile (format "%s refresh" doom-bin) + :on-success + (let ((doom-reloading-p t)) + (doom-initialize 'force) + (with-demoted-errors "PRIVATE CONFIG ERROR: %s" + (general-auto-unbind-keys) + (unwind-protect + (doom-initialize-modules 'force) + (general-auto-unbind-keys t))) + (run-hook-wrapped 'doom-reload-hook #'doom-try-run-hook) + (print! (success "Config successfully reloaded!"))) + :on-failure + (user-error "Failed to reload your config"))) ;;;###autoload (defun doom/reload-autoloads () @@ -92,14 +105,28 @@ line." (doom-cli-reload-autoloads nil 'force)) ;;;###autoload -(defun doom/reload-env () - "Regenerates and reloads your shell environment. +(defun doom/reload-env (&optional arg) + "Regenerates and/or reloads your envvar file. -Uses the same mechanism as 'bin/doom env reload'." +If passed the prefix ARG, clear the envvar file. Uses the same mechanism as +'bin/doom env'. + +An envvar file contains a snapshot of your shell environment, which can be +imported into Emacs." + (interactive "P") + (doom--compile (format "%s env%s" doom-bin (if arg " -c" "")) + :on-success + (let ((doom-reloading-p t)) + (unless arg + (doom-load-envvars-file doom-env-file))) + :on-failure + (error "Failed to generate env file"))) + +;;;###autoload +(defun doom/upgrade () + "Run 'doom upgrade' then prompt to restart Emacs." (interactive) - (compile (format "%s env" (expand-file-name "bin/doom" doom-emacs-dir))) - (while compilation-in-progress - (sit-for 1)) - (unless (file-readable-p doom-env-file) - (error "Failed to generate env file")) - (doom-load-envvars-file doom-env-file)) + (doom--compile (format "%s upgrade" doom-bin) + :on-success + (when (y-or-n-p "You must restart Emacs for the upgrade to take effect.\n\nRestart Emacs?") + (doom/restart-and-restore))))