Skip to content

Commit

Permalink
Parse shebangs on Windows (#293)
Browse files Browse the repository at this point in the history
Parses the shebang from formatter scripts on Windows, and inserts the
binary in to the command. This fixes
#285.
Please let me know if there is still something that need more work,
thanks.

One minor issue I noticed is that errors will now get logged to
`*aphleia-bash-log*` instead of `*apheleia-npx-log*`.
  • Loading branch information
ItsHoff authored Mar 23, 2024
1 parent dd24c54 commit 26ef9ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ The format is based on [Keep a Changelog].
### Bugs fixed
* `apheleia-indent-lisp-buffer` updated to apply local variables after
calling major-mode. Also includes setting for `indent-tabs-mode` ([#286]).
* [Formatter scripts](scripts/formatters) will now work on Windows if Emacs
can find the executable defined in the shebang.

[#286]: https://github.com/radian-software/apheleia/pull/286
[#285]: https://github.com/radian-software/apheleia/issues/285

## 4.1 (released 2024-02-25)
### Enhancements
Expand Down
28 changes: 24 additions & 4 deletions apheleia-formatters.el
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,26 @@ machine from the machine file is available on"))
collect val
else do (error "Result of command evaluation must be a string \
or list of strings: %S" arg)))

;; Windows fails to run formatter scripts. Check whether the
;; command executable is a script that contains a shebang. Parse
;; the shebang and insert the binary into the command.
(when (member system-type '(ms-dos windows-nt))
(when-let ((arg1-file (locate-file (car command) exec-path)))
(with-temp-buffer
(insert-file-contents arg1-file nil 0 2)
(when (string= (buffer-string) "#!")
;; Assumes that the full shebang is max 200 characters
(insert-file-contents arg1-file nil 2 200 t)
(let* ((shebang-components (split-string (thing-at-point 'line)))
(shebang-binary
(if (string= (car shebang-components) "/usr/bin/env")
(cdr shebang-components)
(last (split-string (car shebang-components) "/")))))
(setq command (append shebang-binary
(list arg1-file)
(cdr command))))))))

(setf (apheleia-formatter--arg1 context) (car command)
(apheleia-formatter--argv context) (cdr command))
context)))
Expand All @@ -1009,17 +1029,17 @@ purposes."
;; resolve for the whole formatting process (for example
;; `apheleia--current-process').
(with-current-buffer buffer
(when-let ((ctx
(apheleia--formatter-context formatter command remote stdin))
(exec-path
(when-let ((exec-path
(append `(,(expand-file-name
"scripts/formatters"
(file-name-directory
(file-truename
;; Borrowed with love from Magit
(let ((load-suffixes '(".el")))
(locate-library "apheleia"))))))
exec-path)))
exec-path))
(ctx
(apheleia--formatter-context formatter command remote stdin)))
(if (executable-find (apheleia-formatter--arg1 ctx)
(eq apheleia-remote-algorithm 'remote))
(apheleia--execute-formatter-process
Expand Down

0 comments on commit 26ef9ac

Please sign in to comment.