Skip to content

Instantly share code, notes, and snippets.

@mnewt
Forked from bartojs/build.boot
Last active April 17, 2016 20:56
Show Gist options
  • Save mnewt/eada2e8f3cb9523a6e3f682db7fa2851 to your computer and use it in GitHub Desktop.
Save mnewt/eada2e8f3cb9523a6e3f682db7fa2851 to your computer and use it in GitHub Desktop.
command line formatter for clojure using cljfmt
#!/usr/bin/env boot
;; Format files using cljfmt (https://github.com/weavejester/cljfmt)
(set-env! :dependencies '[[cljfmt "0.5.2"]])
(require '[cljfmt.core :as fmt]
'[clojure.java.io :as io])
(def help-text
"cljfmt: Format files using cljfmt (https://github.com/weavejester/cljfmt)
Usage: cljfmt [FILE | DIRECTORY]
It will change files in place")
(defn fmt-file [f]
(println "formatting" (.getName f))
(spit f (fmt/reformat-string (slurp f))))
(defn clj-file? [f]
(and (.exists f) (.isFile f) (not (.isHidden f))
(contains? #{"clj" "cljs" "cljc" "cljx" "boot"}
(last (.split (.toLowerCase (.getName f)) "\\.")))))
(defn fmt [files]
(doseq [file files]
(let [f (io/file file)]
(when (.exists f)
(doall (map fmt-file (filter clj-file? (if (.isDirectory f) (file-seq f) [f]))))))))
(defn -main [& args]
(if (= (first args) "-h")
(println help-text)
(fmt args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment