Adapted from: https://github.com/babashka/pod-babashka-parcera/blob/master/examples/sort_requires.clj
#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod "pod-babashka-parcera")
(require '[pod.babashka.parcera :as parcera])
(defn sort-ns [node]
(if (seq? node)
(if (and (= :list (first node))
(= '(:keyword ":require") (second node)))
(let [children (nnext node)
first-whitespace (some #(when (= :whitespace (first %)) %) children)
children (remove #(= :whitespace (first %)) children)
loc (meta (first children))
start (-> loc :parcera.core/start :column)
whitespace (str (apply str "\n" (repeat start " ")))
children (sort-by (comp str parcera/code) children)
children (butlast (interleave children (repeat (list :whitespace whitespace))))]
(cons :list (cons '(:keyword ":require") (cons first-whitespace children))))
(cons (first node) (map sort-ns (rest node))))
node))
(defn format-file
[filename]
(let [parsed (parcera/ast (slurp filename))
processed (parcera/code (sort-ns parsed))]
(spit filename processed)
))
(when-let [[& files] *command-line-args*]
(doseq [file files]
(prn "Processing: " file)
(format-file file)))
#!/bin/sh
# https://github.com/magit/magit/issues/3419#issuecomment-380255939
unset GIT_LITERAL_PATHSPECS
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.clj" "*.cljc" "*.cljs" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0
# Prettify all selected files
echo "$FILES" | xargs ./script/format.bb
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
exit 0
#!/bin/sh
# https://github.com/magit/magit/issues/3419#issuecomment-380255939
unset GIT_LITERAL_PATHSPECS
git update-index -g