Skip to content

Commit

Permalink
Add new sample file
Browse files Browse the repository at this point in the history
  • Loading branch information
not-in-stock committed Jun 8, 2022
1 parent e4ff62e commit 7068f47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
21 changes: 0 additions & 21 deletions samples/Clojure/tell-number.bb

This file was deleted.

45 changes: 45 additions & 0 deletions samples/Clojure/validate-and-format.bb
@@ -0,0 +1,45 @@
#!/usr/bin/env bb

(def filter-regex "\\.(clj\\|cljs\\|cljc)$")

(defn clojure-source? [path]
(re-find #"\.(clj|cljs|cljc)$" path))

(defn modified-files []
(-> (shell/sh "git" "diff" "--cached" "--name-only" "--diff-filter=ACMR")
:out
(str/split #"\n")))

(defn lint-valid? [paths]
(apply shell/sh "clj-kondo" "--lint" paths))

(defn native-cljstyle? []
(-> (shell/sh "which" "cljstyle") :exit zero?))

(defn format-files [paths]
(if (native-cljstyle?)
(do
(println "Using native cljstyle...")
(apply shell/sh "cljstyle" "fix" paths))
(do
(println "Using cljstyle from clojure deps...")
(apply shell/sh "clojure" "-A:format" "fix" paths))))

(defn update-file-index
"Add unstaged modifications to git, so they get to be part of the current commit."
[path]
(let [hash (:out (shell/sh "git" "hash-object" "-w" path))]
(shell/sh "git" "update-index" "--add" "--cacheinfo" "100644" hash path)))

(let [paths (->> (modified-files)
(filter clojure-source?))]
(when (seq paths)
(format-files paths)

(doseq [path paths]
(update-file-index path))

(let [{:keys [exit out]} (lint-valid? paths)]
(when-not (= 0 exit)
(println "Lint failed.\n" out)
(System/exit 1)))))

0 comments on commit 7068f47

Please sign in to comment.