refactor(cli): introduce doom-cli-shell

Instead of constantly checking $__DOOMSH, since I may eventually add
additional values for it (like in the cases where the doom script is
invoked by bin/doomscript, from a terminal emulator in Emacs, or future
wrappers).
This commit is contained in:
Henrik Lissner 2024-09-07 13:52:25 -04:00
parent 2981aec4dd
commit 2d3821741a
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -180,6 +180,14 @@ If set to nil, only display benchmark if a CLI explicitly requested with a
non-nil :benchmark property. non-nil :benchmark property.
If set to `always', show the benchmark no matter what.") If set to `always', show the benchmark no matter what.")
(defvar doom-cli-shell
(pcase (getenv "__DOOMSH")
("ps1" 'pwsh)
(_ 'sh))
"What shell environment Doom has been started with.
Can be `pwsh' if invoked via bin/doom.ps1, or `sh' in unix environments.")
;;; Internal variables ;;; Internal variables
(defvar doom-cli--context nil) (defvar doom-cli--context nil)
(defvar doom-cli--exit-code 255) (defvar doom-cli--exit-code 255)
@ -1121,9 +1129,9 @@ Emacs' batch library lacks an implementation of the exec system call."
(error "__DOOMSTEP envvar missing; extended `exit!' functionality will not work")) (error "__DOOMSTEP envvar missing; extended `exit!' functionality will not work"))
(let* ((pid (doom-cli-context-pid context)) (let* ((pid (doom-cli-context-pid context))
(step (doom-cli-context-step context)) (step (doom-cli-context-step context))
(shtype (or (getenv "__DOOMSH") "sh")) (shext (if (eq doom-cli-shell 'pwsh) "ps1" "sh"))
(context-file (format (doom-path temporary-file-directory "doom.%s.%s.context") pid step)) (context-file (format (doom-path temporary-file-directory "doom.%s.%s.context") pid step))
(script-file (format (doom-path temporary-file-directory "doom.%s.%s.%s") pid step shtype)) (script-file (format (doom-path temporary-file-directory "doom.%s.%s.%s") pid step shext))
(command (if (listp args) (combine-and-quote-strings (remq nil args)) args)) (command (if (listp args) (combine-and-quote-strings (remq nil args)) args))
(persistent-files (persistent-files
(combine-and-quote-strings (delq nil (list script-file context-file)))) (combine-and-quote-strings (delq nil (list script-file context-file))))
@ -1163,20 +1171,20 @@ Emacs' batch library lacks an implementation of the exec system call."
(doom-log "restart: writing post-script to %s" script-file) (doom-log "restart: writing post-script to %s" script-file)
(doom-file-write (doom-file-write
script-file script-file
(pcase-exhaustive shtype (pcase-exhaustive doom-cli-shell
("sh" `(,(if (featurep :system 'android) (`sh `(,(if (featurep :system 'android)
"#!/bin/sh\n" "#!/bin/sh\n"
"#!/usr/bin/env sh\n") "#!/usr/bin/env sh\n")
"trap _doomcleanup EXIT\n" "trap _doomcleanup EXIT\n"
"_doomcleanup() {\n rm -f " ,persistent-files "\n}\n" "_doomcleanup() {\n rm -f " ,persistent-files "\n}\n"
"_doomrun() {\n " ,command "\n}\n" "_doomrun() {\n " ,command "\n}\n"
,(cl-loop for (var . val) in persisted-env ,(cl-loop for (var . val) in persisted-env
concat (format "%s=%s \\\n" var (shell-quote-argument val))) concat (format "%s=%s \\\n" var (shell-quote-argument val)))
,(format "PATH=\"%s%s$PATH\" \\\n" ,(format "PATH=\"%s%s$PATH\" \\\n"
(doom-path doom-emacs-dir "bin") (doom-path doom-emacs-dir "bin")
path-separator) path-separator)
"_doomrun \"$@\"\n")) "_doomrun \"$@\"\n"))
("ps1" `("try {\n" (`pwsh `("try {\n"
,(cl-loop for (var . val) in persisted-env ,(cl-loop for (var . val) in persisted-env
concat (format " $__%s = $env:%s; $env:%s = %S\n " concat (format " $__%s = $env:%s; $env:%s = %S\n "
var var var val)) var var var val))
@ -1286,7 +1294,7 @@ Arguments don't have to be switches either."
ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored." ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored."
(let ((pager (or doom-cli-pager (getenv "DOOMPAGER")))) (let ((pager (or doom-cli-pager (getenv "DOOMPAGER"))))
(cond ((equal (getenv "__DOOMSH") "ps1") (cond ((eq doom-cli-shell 'pwsh)
;; Pager isn't supported in powershell ;; Pager isn't supported in powershell
(doom-cli--exit 0 context)) (doom-cli--exit 0 context))
@ -1322,7 +1330,7 @@ ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored."
ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored." ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored."
(doom-cli--exit (doom-cli--exit
(if (equal (getenv "__DOOMSH") "ps1") (if (eq doom-cli-shell 'pwsh)
0 0
(let ((threshold (ceiling (* (doom-cli-context-height context) (let ((threshold (ceiling (* (doom-cli-context-height context)
doom-cli-pager-ratio)))) doom-cli-pager-ratio))))