From 17e292f8d7065ddfab59dd1c4e67fad66de06445 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Wed, 25 Sep 2024 19:22:51 -0700 Subject: [PATCH] Use --stdin-filename conditionally for mix-format --- CHANGELOG.md | 4 ++++ apheleia-formatters.el | 29 ++++++++++++---------- scripts/formatters/apheleia-mix-format | 33 ++++++++++++++++++++++++++ test/formatters/apheleia-ft.el | 14 ++++++++--- 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100755 scripts/formatters/apheleia-mix-format diff --git a/CHANGELOG.md b/CHANGELOG.md index bce8fbd..fbbc873 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/apheleia-formatters.el b/apheleia-formatters.el index 2446d12..ec3d83b 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -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")) @@ -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 diff --git a/scripts/formatters/apheleia-mix-format b/scripts/formatters/apheleia-mix-format new file mode 100755 index 0000000..79900c2 --- /dev/null +++ b/scripts/formatters/apheleia-mix-format @@ -0,0 +1,33 @@ +#!/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 "${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[@]}" diff --git a/test/formatters/apheleia-ft.el b/test/formatters/apheleia-ft.el index 0409606..b10fe87 100755 --- a/test/formatters/apheleia-ft.el +++ b/test/formatters/apheleia-ft.el @@ -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)) @@ -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