doom-emacs/bin/doom
Henrik Lissner 6c0b7e1530
refactor!(cli): rewrite CLI framework libraries
BREAKING CHANGE: this changes Doom's CLI framework in subtle ways, which
is listed in greater detail below. If you've never extended Doom's CLI,
then this won't affect you, but otherwise it'd be recommended you read
on below.

This commit focuses on the CLI framework itself and backports some
foundational changes to its DSL and how it resolves command line
arguments to CLIs, validates input, displays documentation, and persists
state across sessions -- and more. This is done in preparation for the
final stretch towarding completing the CLI rewrite (see #4273).

This is also an effort to generalize Doom's CLI (both its framework and
bin/doom), to increase it versatility and make it a viable dev tool for
other Doom projects (on our Github org) and beyond.

However, there is a *lot* to cover so I'll try to be brief:

- Refactor: generalize Doom's CLI framework by moving all bin/doom
  specific configuration/commands out of core-cli into bin/doom. This
  makes it easier to use bin/doom as a project-agnostic development
  tool (or for users to write their own).
- Refactor: change the namespace for CLI variables/functions from
  doom-cli-X to doom-X.
- Fix: subcommands being mistaken as arguments. "doom make index" will
  resolve to (defcli! (doom make index)) if it exists,
  otherwise (defcli! (doom make)) with "index" as an argument. Before
  this, it would resolve to the latter no matter what. &rest can
  override this; with (defcli! (doom make) (&rest args)), (defcli! (doom
  make index)) will never be invoked.
- Refactor!: redesign our output library (was core/autoload/output.el,
  is now core/autoload/print.el), and how our CLI framework buffers and
  logs output, and now merges logs across (exit! ...) restarts.
- Feat: add support for :before and :after pseudo commands. E.g.

    (defcli! (:before doom help) () ...)
    (defcli! (:after doom sync) () ...)

  Caveat: unlike advice, only one of each can be defined per-command.
- Feat: option arguments now have rudimentary type validation (see
  `doom-cli-option-arg-types`). E.g.

    (defcli! (doom foo) ((foo ("--foo" num))) ...)

  If NUM is not a numeric, it will throw a validation error.

  Any type that isn't in `doom-cli-option-arg-types` will be treated as a
  wildcard string type. `num` can also be replaced with a specification,
  e.g. "HOST[:PORT]", and can be formatted by using symbol quotes:
  "`HOST'[:`PORT']".
- Feat: it is no longer required that options *immediately* follow the command
  that defines them (but it must be somewhere after it, not before). E.g.
    With:
      (defcli! (:before doom foo) ((foo ("--foo"))) ...)
      (defcli! (doom foo baz) () ...)
    Before:
      FAIL: doom --foo foo baz
      GOOD: doom foo --foo baz
      FAIL: doom foo baz --foo
    After:
      FAIL: doom --foo foo baz
      GOOD: doom foo --foo baz
      GOOD: doom foo baz --foo
- Refactor: CLI session state is now kept in a doom-cli-context struct (which
  can be bound to a CLI-local variable with &context in the arglist):

    (defcli! (doom sync) (&context context)
      (print! "Command: " (doom-cli-context-command context)))

  These contexts are persisted across sessions (when restarted). This is
  necessary to support seamless script restarting (i.e. execve
  emulation) in post-3.0.
- Feat: Doom's CLI framework now understands "--". Everything after it will be
  treated as regular arguments, instead of sub-commands or options.
- Refactor!: the semantics of &rest for CLIs has changed. It used to be "all
  extra literal, non-option arguments". It now means *all* unprocessed
  arguments, and its use will suppress "unrecognized option" errors, and
  tells the framework not to process any further subcommands. Use &args
  if you just want "all literal arguments following this command".
- Feat: add new auxiliary keywords for CLI arglists: &context, &multiple,
  &flags, &args, &stdin, &whole, and &cli.
  - &context SYM: binds the currently running context to SYM (a
    `doom-cli-context` struct). Helpful for introspection or passing
    along state when calling subcommands by hand (with `call!`).
  - &stdin SYM: SYM will be bound to a string containing any input piped
    into the running script, or nil if none. Use
    `doom-cli-context-pipe-p` to detect whether the script has been
    piped into or out of.
  - &multiple OPTIONS...: allows all following OPTIONS to be repeated. E.g. "foo
    -x a -x b -x c" will pass (list ("-x" . "a") ("-x" . "b") ("-x" .
    "c")) as -x's value.
  - &flags OPTIONS...: All options after "&flags" get an implicit --no-* switch
    and cannot accept arguments. Will be set to :yes or :no depending on which flag is
    provided, and nil if the flag isn't provided. Otherwise, a default
    value can be specified in that options' arglist. E.g.

      (defcli! (doom foo) (&flags (foo ("--foo" :no))) ...)

    When called, this command sets FOO to :yes if --foo, :no if --no-foo, and
    defaults to :no otherwise.
  - &args SYM: this replaces what &rest used to be; it binds to SYM a
    list of all unprocessed (non-option) arguments.
  - &rest SYM: now binds SYM to a list of all unprocessed arguments, including
    options. This also suppresses "unrecognized option" errors, but will render
    any sub-commands inaccessible. E.g.

      (defcli! (doom make) (&rest rest) ...)
      ;; These are now inaccessible!
      (defcli! (doom make foo) (&rest rest) ...)
      (defcli! (doom make bar) (&rest rest) ...)
  - &cli SYM: binds SYM to the currently running `doom-cli` struct. Can also be
    obtained via `(doom-cli-get (doom-cli-context-command context))`. Possibly
    useful for introspection.
- feat: add defobsolete! macro for quickly defining obsolete commands.
- feat: add defalias! macro for quickly defining alias commands.
- feat: add defautoload! macro for defining an autoloaded command (won't
  be loaded until it is called for).
- refactor!: rename defcligroup! to defgroup! for consistency.
- fix: CLIs will now recursively inherit plist properties from parent
  defcli-group!'s (but will stack :prefix).
- refactor!: remove obsolete 'doom update':
- refactor!: further generalize 'doom ci'
  - In an effort to generalize 'doom ci' (so other Doom--or
    non-doom--projects can use it), all its subcommands have been
    changed to operate on the current working directory's repo instead
    of $EMACSDIR.
  - Doom-specific CI configuration was moved to .github/ci.el.
  - All 'doom ci' commands will now preload one of \$CURRENT_REPO_ROOT/ci.el or
    \$DOOMDIR/ci.el before executing.
- refactor!: changed 'doom env'
  - 'doom env {-c,--clear}' is now 'doom env {clear,c}'
  - -r/--reject and -a/--allow may now be specified multiple times
- refactor!: rewrote CLI help framework and error handling to be more
  sophisticated and detailed.
- feat: can now initiate $PAGER on output with (exit! :pager) (or use
  :pager? to only invoke pager is output is longer than the terminal is
  tall).
- refactor!: changed semantics+conventions for global bin/doom options
  - Single-character global options are now uppercased, to distinguish them from
    local options:
    - -d (for debug mode) is now -D
    - -y (to suppress prompts) is now -!
    - -l (to load elisp) is now -L
    - -h (short for --help) is now -?
  - Replace --yes/-y switches with --force/-!
  - -L/--load FILE: now silently ignores file errors.
  - Add --strict-load FILE: does the same as -L/--load, but throws an error if
    FILE does not exist/is unreadable.
  - Add -E/--eval FORM: evaluates arbitrary lisp before commands are processed.
  - -L/--load, --strict-load, and -E/--eval can now be used multiple times in
    one command.
  - Add --pager COMMAND to specify an explicit pager. Will also obey
    $DOOMPAGER envvar. Does not obey $PAGER.
- Fix #3746: which was likely caused by the generated post-script overwriting
  the old mid-execution. By salting the postscript filenames (with both an
  overarching session ID and a step counter).
- Docs: document websites, environment variables, and exit codes in
  'doom --help'
- Feat: add imenu support for def{cli,alias,obsolete}!

Ref: #4273
Fix: #3746
Fix: #3844
2022-06-18 23:53:12 +02:00

304 lines
12 KiB
Bash
Executable file

#!/usr/bin/env sh
:; set -e # -*- mode: emacs-lisp; lexical-binding: t -*-
:; case "$EMACS" in *term*) EMACS=emacs ;; *) EMACS="${EMACS:-emacs}" ;; esac
:; tmpdir=`$EMACS -Q --batch --eval '(princ (temporary-file-directory))' 2>/dev/null`
:; [ -z "$tmpdir" ] && { >&2 echo "Error: failed to run Emacs with command '$EMACS'"; >&2 echo; >&2 echo "Are you sure Emacs is installed and in your \$PATH?"; exit 1; }
:; export __DOOMPID="${__DOOMPID:-$$}"
:; export __DOOMSTEP="$((__DOOMSTEP+1))"
:; export __DOOMGEOM="${__DOOMGEOM:-`tput cols lines 2>/dev/null`}"
:; export __DOOMGPIPE=${__DOOMGPIPE:-$__DOOMPIPE}
:; export __DOOMPIPE=; [ -t 0 ] || __DOOMPIPE+=0; [ -t 1 ] || __DOOMPIPE+=1
:; $EMACS -Q --batch --load "$0" -- "$@" || exit=$?
:; [ "${exit:-0}" -eq 254 ] && { sh "${tmpdir}/doom.${__DOOMPID}.${__DOOMSTEP}.sh" "$0" "$@" && true; exit="$?"; }
:; exit $exit
;; This magical mess of a shebang is necessary for any script that relies on
;; Doom's CLI framework, because Emacs' tty libraries and capabilities are too
;; immature (borderline non-existent) at the time of writing (28.1). This
;; shebang sets out to accomplish these three goals:
;;
;; 1. To produce a more helpful error if Emacs isn't installed or broken. It
;; must do so without assuming whether $EMACS is a shell command (e.g. 'snap
;; run emacs') or an absolute path (to an emacs executable). I've avoided
;; 'command -v $EMACS' for this reason.
;;
;; 2. To allow this Emacs session to "exit into" a child process (since Elisp
;; lacks an analogue for exec system calls) by calling an auto-generated and
;; self-destructing "exit script" if the parent Emacs process exits with code
;; 254. It takes care to prevent nested child instances from clobbering the
;; exit script.
;;
;; 3. To expose some information about the terminal and session:
;; - $__DOOMGEOM holds the dimensions of the terminal (W . H).
;; - $__DOOMPIPE indicates whether the script has been piped (in and/or out).
;; - $__DOOMGPIPE indicates whether one of this process' parent has been
;; piped to/from.
;; - $__DOOMPID is a unique identifier for the parent script, so
;; child processes can identify which persistent data files (like logs) it
;; has access to.
;; - $__DOOMSTEP counts how many levels deep we are in the dream (appending
;; this to the exit script's filename avoids child processes clobbering the
;; same exit script and causing read errors).
;; - $TMPDIR (or $TEMP and $TMP on Windows) aren't guaranteed to have values,
;; and mktemp isn't available on all systems, but you know what is? Emacs!
;; So I use it to print `temporary-file-directory'. And it seconds as a
;; quick sanity check for Emacs' existence (for goal #1).
;;
;; Other weird facts about this shebang line:
;;
;; - The :; hack exploits properties of : and ; in shell scripting and elisp to
;; allow shell script and elisp to coexist in the same file without either's
;; interpreter throwing foreign syntax errors:
;;
;; - In elisp, ":" is a valid keyword symbol literal; it evaluates to itself
;; and has no side-effect.
;; - In the shell, ":" is a valid command that does nothing and ignores its
;; arguments.
;; - In elisp, ";" begins a comment. I.e. the interpreter ignores everything
;; after it.
;; - In the shell, ";" is a command separator.
;;
;; Put together, plus a strategically placed exit call, the shell will read
;; one part of this file and ignore the rest, while the elisp interpreter will
;; do the opposite.
;; - I intentionally avoid loading site files (using -Q), because
;; core/core-cli.el loads them by hand later. There, I can suppress and deal
;; with unhelpful warnings (e.g. "package cl is deprecated"), "Loading
;; X...DONE" spam, and any other disasterous side-effects.
;; - POSIX-compliancy is paramount: there's no guarantee what /bin/sh will be
;; symlinked to in the esoteric OSes/distros Emacs users use.
;; - The user may have a noexec flag set on /tmp, so pass the exit script to
;; /bin/sh rather than executing them directly.
;; Ensure Doom runs out of this file's parent directory (or $EMACSDIR), where
;; Doom is presumably installed.
(setq user-emacs-directory
(if (getenv-internal "EMACSDIR")
(file-name-as-directory
(file-truename (getenv-internal "EMACSDIR")))
(expand-file-name
"../" (file-name-directory (file-truename load-file-name)))))
;;
;;; Sanity checks
(when (equal (user-real-uid) 0)
;; If ~/.emacs.d is owned by root, assume the user genuinely wants root to be
;; their primary user, otherwise complain.
(unless (= 0 (file-attribute-user-id (file-attributes user-emacs-directory)))
(message
(concat
"Error: this script is being run as root, which is likely not what you want.\n"
"It will cause file permissions errors later, when you run Doom as another\n"
"user.\n\n"
"If this really *is* what you want, then change the owner of your Emacs\n"
"config to root:\n\n"
;; TODO Add cmd.exe/powershell commands
" chown root:root -R " (abbreviate-file-name user-emacs-directory) "\n\n"
"Aborting..."))
(kill-emacs 2)))
;;
;;; Load Doom's CLI framework
(require 'core-cli (expand-file-name "core/core-cli" user-emacs-directory))
;;
;;; Entry point
(defcli! doom (&args _command)
"A command line interface to Doom Emacs.
Includes package management, diagnostics, unit tests, and byte-compilation.
This tool also makes it trivial to launch Emacs out of a different folder or
with a different private module.
ENVIRONMENT VARIABLES:
`$EMACS'
The Emacs executable or command to use for any Emacs operations in this or
other Doom CLI shell scripts (default: first emacs found in `$PATH').
`$EMACSDIR'
The location of your Doom Emacs installation (defaults to ~/.config/emacs or
~/.emacs.d; whichever is found first). This is *not* your private Doom
configuration. The `--emacsdir' option also sets this variable.
`$DOOMDIR'
The location of your private configuration for Doom Emacs (defaults to
~/.config/doom or ~/.doom.d; whichever it finds first). This is *not* the
place you've cloned doomemacs/doomemacs to. The `--doomdir' option also sets
this variable.
`$DOOMPAGER'
The pager to invoke for large output (default: \"less +g\"). The `--pager'
option also sets this variable.
`$DOOMPROFILE'
(Not implemented yet) Which Doom profile to activate (default: \"current\").
`$DOOMPROFILESDIR'
(Not implemented yet) Where to find or write generated Doom profiles
(default: `$EMACSDIR'/profiles).
EXIT CODES:
0 Successful run
1 General internal error
2 Error with Emacs/Doom install or execution context
3 Unrecognized user input error
4 Command not found, or is incorrect/deprecated
5 Invalid, missing, or extra options/arguments
6-49 Reserved for Doom
50-200 Reserved for custom user codes
254 Successful run (but then execute `doom-cli-restart-script')
255 Uncaught internal errors
SEE ALSO:
https://doomemacs.org Homepage
https://docs.doomemacs.org Official documentation
https://discourse.doomemacs.org Discourse (discussion & support forum)
https://doomemacs.org/discord Discord chat server
https://doomemacs.org/roadmap Development roadmap
https://git.doomemacs.org Shortcut to Github org
https://git.doomemacs.org/issues Global issue tracker"
:partial t)
(defcli! (:before doom)
((force? ("-!" "--force") "Suppress prompts by auto-accepting their consequences")
(debug? ("-D" "--debug") "Enable verbose output")
(doomdir ("--doomdir" dir) "Use Doom config living in `DIR' (e.g. ~/.doom.d)")
(emacsdir ("--emacsdir" dir) "Use Doom install living in `DIR' (e.g. ~/.emacs.d)")
(pager ("--pager" bool) "Pager command to use for large output")
;; TODO Implement after v3.0
;; (profile ("--profile" name) "Use profile named NAME")
&flags
(color? ("--color") "Whether or not to show ANSI color codes")
&multiple
(loads ("-L" "--load" "--strict-load" file) "Load elisp `FILE' before executing `COMMAND'")
(evals ("-E" "--eval" form) "Evaluate `FORM' before executing commands")
&input input
&context context
&args _)
"OPTIONS:
-E, -eval
Can be used multiple times.
-L, --load, --strict-load
Can be used multiple times to load multiple files. Both -L and --load will
silently fail on missing files, but --strict-load won't.
Warning: files loaded this way load too late to define new commands. To
define commands, do so from `$DOOMDIR'/cli.el or `$DOOMDIR'/init.el
instead."
(when color?
(setq doom-print-backend (if (eq color? :yes) 'ansi)))
(when (and (equal (doom-cli-context-step context) 0)
(or ;; profile
debug?
force?
emacsdir
doomdir
pager))
;; TODO Implement after v3.0
;; (when profile
;; (setenv "DOOMPROFILE" profile))
(when debug?
(setenv "DEBUG" "1")
(print! (item "Debug mode enabled")))
(when force?
(setenv "__DOOMFORCE" (and force? "1"))
(print! (item "Suppressing all prompts")))
(when emacsdir
(setenv "EMACSDIR" emacsdir))
(when doomdir
(setenv "DOOMDIR" doomdir))
(when pager
(setenv "DOOMPAGER" pager))
(exit! :restart))
;; Load $DOOMDIR/init.el, so users can customize things, if they like.
(doom-log "Loading $DOOMDIR/init.el")
(load! doom-module-init-file doom-private-dir t)
;; Load extra files and forms, as per given options.
(dolist (file loads)
(load (doom-path (cdr file))
(not (equal (car file) "--strict-load"))
(not doom-debug-p) t))
(dolist (form evals)
(eval (read (cdr form)) t)))
;;
;;; Commands
(let ((dir (doom-path doom-core-dir "cli")))
;; It'd be simple to just load these files directly, but because there could
;; be a lot of them (and some of them have expensive dependencies), I use
;; `defautoload!' to load them lazily.
(add-to-list 'doom-cli-load-path dir)
;; Library for generating autoloads files for Doom modules & packages.
(load! "autoloads" dir)
(defgroup!
:prefix 'doom
;; Import this for implicit 'X help' commands for your script:
(defalias! ((help h)) (:root :help))
;; And suggest its use when errors occur.
(add-to-list 'doom-help-commands "%p h[elp] %c")
(defgroup! "Config Management"
:docs "Commands for maintaining your Doom Emacs configuration."
(defautoload! ((sync s)))
(defautoload! ((upgrade up)))
(defautoload! (env))
(defautoload! ((build b purge p rollback)) "packages")
(defautoload! ((install i)))
(defautoload! ((compile c)))
;; TODO Post-3.0 commands
;; (load! "gc" dir)
;; (load! "module" dir)
;; (load! "nuke" dir)
;; (load! "package" dir)
;; (load! "profile" dir)
;; (defobsolete! ((compile c)) "doom sync --compile" "v3.0.0")
;; (defobsolete! ((build b)) "doom sync --rebuild" "v3.0.0")
)
(defgroup! "Diagnostics"
:docs "Commands for troubleshooting and debugging Doom."
(defautoload! ((doctor doc)))
(defautoload! (info))
(defalias! ((version v)) (:root :version)))
(defgroup! "Development"
:docs "Commands for developing or launching Doom."
(defautoload! (ci))
;; TODO (defautoload! (make))
(defautoload! (run))
;; FIXME Test framework
;; (load! "test" dir)
)
(let ((cli-file "cli"))
(defgroup! "Module commands"
(dolist (key (hash-table-keys doom-modules))
(when-let* ((path (plist-get (gethash key doom-modules) path))
(path (car (doom-glob path cli-file))))
(defgroup! :prefix (format "+%s" (cdr key))
(defautoload! () path)))))
(doom-log "Loading $DOOMDIR/cli.el")
(load! cli-file doom-private-dir t))))
;;
;;; Let 'er rip
(run! "doom" (cdr (member "--" argv)))
;;; doom ends here, unless...