Commit graph

147 commits

Author SHA1 Message Date
Henrik Lissner
a4aab45656
fix(emacs-lisp): flycheck false positives in Doom configs 2022-06-22 23:04:00 +02:00
Henrik Lissner
9d1df5f298
nit: minor refactors & comment/docstring revisions 2022-06-21 14:40:15 +02:00
Henrik Lissner
0511445339
feat(cli): add :dump pseudo command
And fix a void-variable doom-cli--dump error accidentally introduced in
d231755.

Ref: d231755bdf
2022-06-21 00:32:15 +02:00
Henrik Lissner
d231755bdf
refactor(cli): rename struct constructors/copiers
To maintain our namespaces.
2022-06-20 23:43:23 +02:00
Henrik Lissner
d1472c191e
tweak(cli): be verbose about site files in debug mode
More information for hapless debuggers like me!

Ref: #6473
2022-06-20 13:37:54 +02:00
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
Henrik Lissner
797866b596
fix(cli): doom run not launching from parent dir
'doom run' was launching out of Emacs' default
directories (~/.config/emacs and ~/.emacs.d) instead of the parent
directory of bin/doom. This commit corrects that, but is a temporary
measure until the CLI rewrite. I'm also not totally sure this will work
on Windows...

Fix: #6389
2022-06-04 15:01:49 +02:00
Henrik Lissner
f5c1332a31 refactor: minor refactors & nit picks across core 2021-10-10 18:36:46 +02:00
Henrik Lissner
5541ee1c68 feat(cli): add -l/--load switch
Post-rewrite bin/doom will accept multiple -l's, but for now will only
accept one. This change was made to prepare for the documentation
generator, which will live outside the repo.
2021-10-05 02:01:15 +02:00
Henrik Lissner
abc16ef68c refactor(cli): make all searches case-sensitive
This is more predictable, and is safe as a global default in CLI
sessions (but not in interactive ones). This indirectly fixes case
insensitivity in our commit linter rules.
2021-08-05 12:53:20 -04:00
Henrik Lissner
0b4e1c30b7 dev: fix commit linter
Forgot to add the new -C/--nocolor option for 'doom sync', and to load
the 'doom ci *' family.
2021-08-01 01:02:51 -04:00
Henrik Lissner
d79cea2e4c Minor refactors, reformatting, & comment revision 2021-07-11 17:52:08 -04:00
Henrik Lissner
cb574f5d42 cli: show Emacs version at the start of output 2021-06-07 21:04:46 -04:00
Henrik Lissner
df3c221c73 cli: don't repeat "Executing..." line on restart 2021-05-24 17:32:46 -04:00
Henrik Lissner
4b5cf7d46f bin/doom: polish output
Reduces the amount of "noise" included in bin/doom's output.

Also fixes an issue where warnings during autoloads generation would
sneak into Doom's autoloads file, producing weird void-variable errors,
like

  (void-variable . rainbow-delimiters:)
  (void-variable . diredfl:)
  (void-variable . company:)
2021-05-23 21:49:02 -04:00
Henrik Lissner
1e9870e13a Refactor bin/doom
So innocuous CLI command return values don't break the post-script
written to /tmp/doom.sh.
2021-05-17 22:29:50 -04:00
Henrik Lissner
6d2c6b44fa Refactor comp-effective-async-max-jobs advice
We only need this magic in CLI sessions. It's better to only use half
the CPUs in interactive sessions (if the user has enabled
comp-deferred-compilation for some reason).

Fixes #5042
2021-05-12 14:52:00 -04:00
Henrik Lissner
5b3f52f5fb Fix missing doom.error.log and silent straight errors 2021-03-22 21:11:22 -04:00
Henrik Lissner
e9c4c7471c Reorganize CLI libraries 2021-03-12 17:55:41 -05:00
Henrik Lissner
fc75573962 Include straight error in doom.error.log 2021-03-12 17:06:10 -05:00
Henrik Lissner
26319322b2 Remove redundant straight--build-compile advice
May address #4778 and #4783
2021-03-11 11:44:18 -05:00
Benjamin Tan
f151b3de45
Load autoload/system in core-cli
Fixes #4715.
2021-03-02 15:57:45 +08:00
Henrik Lissner
987da645c2 core/cli: show run duration in human-readable format
e.g. "Finished in 2h27m11s"

I pray for your soul if you ever see "h".
2021-02-13 09:47:13 -05:00
Henrik Lissner
2c4b5b2f93 Fix type error when printing straight errors 2021-01-26 22:38:53 -05:00
Henrik Lissner
fd7073240c Complete docstring for doom--straight-inject-load-path-a
Whoops. I pushed before finishing the
2021-01-23 22:37:28 -05:00
Henrik Lissner
eacbc5e36f Inject load-path when compiling straight packages 2021-01-23 18:50:21 -05:00
Henrik Lissner
9490d42cd3
Comment revision 2020-12-14 15:48:29 -05:00
Henrik Lissner
9edcdd0633
Mention supported envvars in 'doom help' 2020-10-08 17:07:36 -04:00
Henrik Lissner
2337f14563
bin/doom: enable-dir-local-variables = nil
So user .dir-locals.el doesn't interfere with our package manager.
2020-08-28 23:34:12 -04:00
Henrik Lissner
5de08af8da
bin/doom: improve error output
Show different message for straight errors (but log backtrace to
doom.error.log either way).
2020-08-28 04:34:56 -04:00
Henrik Lissner
098f10306d
Fix CLI error not including straight output 2020-08-27 03:11:46 -04:00
Henrik Lissner
a6dc9bf7e5
core-cli: minor refactors 2020-08-27 01:10:08 -04:00
Henrik Lissner
e459842a9b
Escape newlines in backtrace frames
And quote strings
2020-08-26 16:02:55 -04:00
Henrik Lissner
63a03848a3
Fix literate tangling on 'doom sync'
Relevant to #3746
2020-08-25 06:01:35 -04:00
Henrik Lissner
c0500df5fb
bin/doom: move debugger config to core-cli
And remove unnecessary path expansion.
2020-08-25 05:08:22 -04:00
Henrik Lissner
6e22a15e43
Fix 'doom help'
doom-cli-internal-p was accidentally removed in e632871a1, and the
psuedo CLI command :main was renamed to :doom.
2020-08-24 23:36:11 -04:00
Henrik Lissner
0dfee56e35
Fix string type error on 'doom upgrade' 2020-08-24 23:22:57 -04:00
Henrik Lissner
e632871a11
core-cli: backport more refactors from rewrite
Still a long way to go, but this introduces a few niceties for
debugging CLI failures:

+ The (extended) output of the last bin/doom command is now logged to
  ~/.emacs.d/.local/doom.log
+ If an error occurs, short backtraces are displayed whether or not you
  have debug mode on. The full backtrace is written to
  ~/.emacs.d/.local/doom.error.log.
+ bin/doom now aborts with a warning if:
  - The script itself or its parent directory is a symlink. It's fine if
    ~/.emacs.d is symlinked though.
  - Running bin/doom as root when your DOOMDIR isn't in /root/.
  - If you're sporting Emacs 26.1 (now handled in the elisp side rather
    than the /bin/sh shebang preamble).
+ If a 'doom sync' was aborted prematurely, you'll be warned that Doom
  was left in an inconsistent state and that you must run `doom sync`
  again.

May address #3746
2020-08-24 23:00:32 -04:00
Henrik Lissner
b4431dcd49
Fix #3746 (part 3): use correct PATH separator on Windows
An IS-WINDOWS check would be too naive, because cygwin and git-bash use
: as the path separator, while powershell and cmd.exe use ;
2020-08-19 15:00:25 -04:00
Henrik Lissner
8402f8aecc
Fix #3781 (part 2): error reading from stdin
Possibly addresses #3746
2020-08-19 13:57:24 -04:00
Henrik Lissner
9b991fc29f
Fix #3781: revert 55b87b3a9 2020-08-19 12:10:14 -04:00
Henrik Lissner
b2120e21eb
bin/doom: fix heredoc delimiter in postscript 2020-08-18 19:02:17 -04:00
Henrik Lissner
9f45561825
bin/doom: inhibit POSIX errors during postscript
Some doom commands will generate a temporary script at
~/.emacs.d/.local/.doom.sh so that it can run an arbitrary shell command
after the current invocation of bin/doom ends. Very useful for, say,
restarting the currently running doom command after a destructive
operation, like updating Doom's source code, tangling your literate
config, or for launching arbitrary programs, like a new instance of
Emacs. This is necessary because elisp lacks an execv implementation.

However, for some folks, .doom.sh wasn't executing at all. This meant:

1. Some `doom upgrade`s would upgrade Doom itself but never move on to
   the second step of the process: updating its packages.
2. Literate config users could tangle their configs on `doom sync`, but
   the actual syncing process would never happen (#3746).
3. `doom run` would do nothing.

I hadn't realized /bin/sh runs bash in POSIX mode (at least, on systems
where /bin/sh = bash, like nixOS or macOS). In POSIX mode the script
will abort the if a builtin command (like export) returns a non-zero
exit code. Since .doom.sh is basically a bunch of exports followed by an
arbitrary command, and there are some environment variables
that can trigger validation errors (like UID triggering a "read-only
variable" error), we have a problem.

Hopefully addresses #3746
2020-08-18 18:59:50 -04:00
Henrik Lissner
9a8fc46c74
bin/doom: escape envvars in post-script
Might address #3746
2020-08-15 15:13:50 -04:00
Henrik Lissner
d9739a2d10
Fix #3727: 'doom: command not found' error on 'doom upgrade' 2020-08-11 14:26:49 -04:00
Henrik Lissner
050ac73789
Prevent illegal envvars causing bash syntax errors 2020-08-10 23:07:21 -04:00
Henrik Lissner
eb9cb0c6e9
Fix org version conflicts due to literate config #3649
Tangling would load org libraries. If org hasn't been installed yet,
this means the older version is loaded, later interfering with the
installation and byte-compilation of the new package, causing down the
road.
2020-08-09 01:50:42 -04:00
Henrik Lissner
d2bc2ff44b
config/literate: improve tangling algorithm
- Tangling no longer adds temp files to recentf (#3685)
- If :tangle yes is used, the result is no longer tangled to
  /tmp/config.org.*.el
- In interactive sessions the org buffer is no longer interfered with
  when tangling (by scrolling up to the top of the page, or undoing
  overlays/markers).
- Tangling no longer triggers formatters (or any save/write hooks).
- Appease byte-compiler sama, complaining about free variables.
2020-08-08 02:57:04 -04:00
Henrik Lissner
f4e6d36574
Move 'restart Emacs to see changes' message to 'doom sync'
It isn't the autoloads generator's responsibility to do this. The
"changes" referred to consist of more than just the regenerated
autoloads file.
2020-05-26 04:09:11 -04:00
Henrik Lissner
a814239ec7
Implement daisy-chaining for CLI sessions
elisp lacks an execv implementation (or mature subprocess library), so
we exploit some splenderiffic hackery to get Emacs to execute arbitrary
shell commands after a 'doom ...' command completes. This allows us to
daisy chain doom commands in distinct sessions (wonderful for reloading
doom after a 'doom upgrade', which we do). This minimizes errors when a
'doom upgrade' pulls in breaking changes to Doom's CLI.

We also bring 'doom run' into elisp, since this new functionality
enables us to.
2020-05-26 02:30:54 -04:00