Skip to content

Commit

Permalink
Use --stdin-filename conditionally for mix-format (#322)
Browse files Browse the repository at this point in the history
Closes #319 because this does the same thing but it transparently works
for all versions of mix-format.
  • Loading branch information
raxod502 authored Sep 26, 2024
1 parent 9343b86 commit 302a41c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The format is based on [Keep a Changelog].
the project ([#301]).
* Ormolu is now passed the `--stdin-input-file` argument, which has
become required ([#312]).
* `mix format` is now passed the `--stdin-filename` argument which is
required in some cases. The version of Mix is autodetected and this
option is only passed when it is supported ([#319]).

## Internal
* Improvements to formatter test framework, it is now possible to
Expand All @@ -38,6 +41,7 @@ The format is based on [Keep a Changelog].
[#313]: https://github.com/radian-software/apheleia/pull/313
[#316]: https://github.com/radian-software/apheleia/pull/316
[#317]: https://github.com/radian-software/apheleia/issues/317
[#319]: https://github.com/radian-software/apheleia/pull/319

## 4.2 (released 2024-08-03)
### Changes
Expand Down
29 changes: 17 additions & 12 deletions apheleia-formatters.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
(ktlint . ("ktlint" "--log-level=none" "--stdin" "-F" "-"))
(latexindent . ("latexindent" "--logfile=/dev/null"))
(mix-format . ("apheleia-from-project-root"
".formatter.exs" "mix" "format" "-"))
".formatter.exs" "apheleia-mix-format" filepath))
(nixfmt . ("nixfmt"))
(ocamlformat . ("ocamlformat" "-" "--name" filepath
"--enable-outside-detected-project"))
Expand Down Expand Up @@ -1063,17 +1063,22 @@ purposes."
;; resolve for the whole formatting process (for example
;; `apheleia--current-process').
(with-current-buffer buffer
(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))
(ctx
(apheleia--formatter-context formatter command remote stdin)))
(when-let* ((script-dir (expand-file-name
"scripts/formatters"
(file-name-directory
(file-truename
;; Borrowed with love from Magit
(let ((load-suffixes '(".el")))
(locate-library "apheleia"))))))
;; Gotta set both `exec-path' and the PATH env-var,
;; the former is for Emacs itself while the latter is
;; for subprocesses of the proc we start.
(exec-path (cons script-dir exec-path))
(process-environment
(cons (concat "PATH=" script-dir ":" (getenv "PATH"))
process-environment))
(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
35 changes: 35 additions & 0 deletions scripts/formatters/apheleia-mix-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -euo pipefail

if (( "$#" != 1 )); then
echo >&2 "usage: apheleia-mix-format PATH"
exit 1
fi

path="$1"

mix_binary="$(which mix)"
checksum="$(md5sum "${mix_binary}" | awk '{print $1}')"

cache_dir="${XDG_CACHE_DIR:-${HOME}/.cache}/apheleia"
ver_file="${cache_dir}/mix-format-version-${checksum}"

if [[ ! -f "${ver_file}" ]]; then
mkdir -p "$(dirname "${ver_file}")"
mix --version </dev/null | \
grep -Eo 'Mix [^ ]+' | \
awk '{print $2}' > "${ver_file}"
fi

ver="$(< "${ver_file}")"
needed_ver="1.14.0"
higher_ver="$( (echo "${ver}"; echo "${needed_ver}") | sort -V | tail -n1)"

args=(mix format)
if [[ "${ver}" == "${higher_ver}" ]]; then
args+=(--stdin-filename "${path}")
fi
args+=(-)

exec "${args[@]}"
14 changes: 11 additions & 3 deletions test/formatters/apheleia-ft.el
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ returned context."
(interactive
(unless (or current-prefix-arg noninteractive)
(list (completing-read "Formatter: " (apheleia-ft--get-formatters)))))
;; Yeah this code is super duplicative and really we should just
;; refactor the original `apheleia--run-formatter-process' to be
;; testable and run that instead. It would better ensure that
;; behavior is the same between the test suite and the actual
;; runtime. That is a future project. Or it could be a now project
;; if you, dear reader, are feeling up to it.
(dolist (formatter (or formatters (apheleia-ft--get-formatters)))
(dolist (in-file (apheleia-ft--input-files formatter))
(let* ((extension (file-name-extension in-file))
Expand All @@ -337,15 +343,17 @@ returned context."
(exit-status nil)
(out-file (replace-regexp-in-string
"/in\\([^/]+\\)" "/out\\1" in-file 'fixedcase))
(exec-path
(append `(,(expand-file-name
(script-dir (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 (cons script-dir exec-path))
(process-environment
(cons (concat "PATH=" script-dir ":" (getenv "PATH"))
process-environment))
(display-fname
(replace-regexp-in-string
(concat "^" (regexp-quote
Expand Down

0 comments on commit 302a41c

Please sign in to comment.