aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRadon Rosborough <radon@intuitiveexplanations.com>2023-11-11 16:32:40 -0800
committerGitHub <noreply@github.com>2023-11-11 16:32:40 -0800
commitddea9bea708ce029cea6126b5be8e9f8979c58b3 (patch)
treebaa50a227b944f0d20b4bff8ccf881d470524cf2 /scripts
parent598ef3c7cbe718925cece64ad28b2061969fe4a8 (diff)
[#232] Format from elixir project dir (#239)
Make it so that if there is a `.formatter.exs` file somewhere in the parent directories, then Apheleia will run `mix format` from there instead of the current directory. Close #232
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/formatters/apheleia-mix22
1 files changed, 22 insertions, 0 deletions
diff --git a/scripts/formatters/apheleia-mix b/scripts/formatters/apheleia-mix
new file mode 100755
index 0000000..d2038e1
--- /dev/null
+++ b/scripts/formatters/apheleia-mix
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+
+# This function prints the name of the current directory if it
+# contains a file or directory named after the first argument, or the
+# parent directory if it contains such a file, or the parent's parent,
+# and so on. If no such file is found it returns nonzero.
+# https://unix.stackexchange.com/a/22215
+find_upwards() {
+ fname="$1"
+
+ path="${PWD}"
+ while [[ -n "${path}" && ! -e "${path}/${fname}" ]]; do
+ path="${path%/*}"
+ done
+ [[ -n "${path}" ]] && echo "${path}"
+}
+
+if dir="$(find_upwards .formatter.exs)"; then
+ cd -- "${dir}" || exit
+fi
+
+exec mix "$@"