refactor(cli): use new API to write temp files

Ref: 8d4b6b3028
This commit is contained in:
Henrik Lissner 2022-09-06 21:47:05 +02:00
parent c370cb1784
commit 8971ee36e5
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -1128,49 +1128,48 @@ Emacs' batch library lacks an implementation of the exec system call."
(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.sh") pid step)) (script-file (format (doom-path temporary-file-directory "doom.%s.%s.sh") pid step))
(command (if (listp args) (combine-and-quote-strings (remq nil args)) args)) (command (if (listp args) (combine-and-quote-strings (remq nil args)) args))
(coding-system-for-write 'utf-8-auto) (persistent-files
(coding-system-for-read 'utf-8-auto)) (combine-and-quote-strings (delq nil (list script-file context-file))))
(persisted-env
(save-match-data
(cl-loop with initial-env = (get 'process-environment 'initial-value)
for env in (seq-difference process-environment initial-env)
if (string-match "^\\([a-zA-Z0-9_][^=]+\\)=\\(.+\\)$" env)
collect (format "%s=%s"
(match-string 1 env)
(shell-quote-argument (match-string 2 env)))))))
(cl-incf (doom-cli-context-step context)) (cl-incf (doom-cli-context-step context))
(make-directory (file-name-directory context-file) t) (with-file-modes #o600
(with-temp-file context-file (doom-log "Writing context file: %s" context-file)
;; DEPRECATED Use `print-unreadable-function' when 28 support is dropped (doom-file-write
(let ((newcontext (doom-cli-context-copy context)) context-file (let ((newcontext (copy-doom-cli-context context))
(print-level nil) (print-level nil)
(print-length nil) (print-length nil)
(print-circle nil) (print-circle nil)
(print-escape-newlines t)) (print-escape-newlines t))
(letf! (defmacro convert-buffer (fn) ;; REVIEW: Use `print-unreadable-function' when 28 support
`(setf (,fn newcontext) (with-current-buffer (,fn context) ;; is dropped.
(buffer-string)))) (letf! (defmacro convert-buffer (fn)
(convert-buffer doom-cli-context-stdin) `(setf (,fn newcontext) (with-current-buffer (,fn context)
(convert-buffer doom-cli-context-stdout) (buffer-string))))
(convert-buffer doom-cli-context-stderr)) (convert-buffer doom-cli-context-stdin)
(prin1 newcontext (current-buffer)))) (convert-buffer doom-cli-context-stdout)
(set-file-modes context-file #o400) (convert-buffer doom-cli-context-stderr))
(make-directory (file-name-directory script-file) t) newcontext))
(let ((coding-system-for-write 'utf-8-auto) (doom-log "Writing post-script file: %s" script-file)
(persistent-files (combine-and-quote-strings (delq nil (list script-file context-file)))) (doom-file-write
(env (save-match-data script-file `("#!/usr/bin/env sh\n"
(cl-loop with initial-env = (get 'process-environment 'initial-value) "trap _doomcleanup EXIT\n"
for env in (seq-difference process-environment initial-env) "_doomcleanup() {\n rm -f " ,persistent-files "\n}\n"
if (string-match "^\\([a-zA-Z0-9_][^=]+\\)=\\(.+\\)$" env) "_doomrun() {\n " ,command "\n}\n"
collect (format "%s=%s" ,(string-join persisted-env " \\\n")
(match-string 1 env) "__DOOMCONTEXT=" ,(shell-quote-argument context-file) " \\\n"
(shell-quote-argument (match-string 2 env))))))) ,(format "PATH=\"%s%s$PATH\" \\\n"
(with-temp-file script-file (doom-path doom-emacs-dir "bin")
(doom-log "_doomrun: %s %s" (string-join env " ") command) path-separator)
(doom-log "_doomcleanup: %s" persistent-files) "_doomrun \"$@\"\n")))
(insert "#!/usr/bin/env sh\n" (doom-log "_doomrun: %s %s" (string-join persisted-env " ") command)
"trap _doomcleanup EXIT\n" (doom-log "_doomcleanup: %s" persistent-files)
"_doomcleanup() {\n rm -f " persistent-files "\n}\n"
"_doomrun() {\n " command "\n}\n"
(string-join env " \\\n")
"__DOOMCONTEXT=" (shell-quote-argument context-file) " \\\n"
(format "PATH=\"%s%s$PATH\" \\\n"
(doom-path doom-emacs-dir "bin")
path-separator)
"_doomrun \"$@\"\n")))
(set-file-modes script-file #o600)
;; Error code 254 is special: it indicates to the caller that the ;; Error code 254 is special: it indicates to the caller that the
;; post-script should be executed after this session ends. It's up to ;; post-script should be executed after this session ends. It's up to
;; `doom-cli-run's caller to enforce this (see bin/doom's shebang for a ;; `doom-cli-run's caller to enforce this (see bin/doom's shebang for a