Skip to content

Commit

Permalink
Ignore (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-bruemmer authored May 29, 2022
1 parent 9d4e81e commit a179eca
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 86 deletions.
2 changes: 1 addition & 1 deletion resources/CLERK_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.4.1
v0.5.0
26 changes: 16 additions & 10 deletions src/clerk/checks.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns clerk.checks
(:gen-class)
(:require [clerk
[error :as error]
[system :as sys]]
[error :as error]]
[clojure
[edn :as edn]
[walk :as walk]]
Expand All @@ -23,10 +22,8 @@

(defn path
"Builds full path for `filename`."
[filename]
(->> filename
(sys/filepath ".clerk")
(#(str % ".edn"))))
[check-dir filename]
(str check-dir filename ".edn"))

(defn valid-config?
"Does the `filepath` exist?"
Expand All @@ -40,18 +37,27 @@
Clerk will exit if it cannot load a check."
[filename]
(->> filename
(path)
(valid-config?)
(slurp)
(edn/read-string)
(walk/keywordize-keys)
(make)))

(defn load-ignore-set!
"Takes a checks directory and a file name for an edn file that
lists specimens to ignore."
[check-dir filename]
(if (nil? filename) #{}
(let [f (path check-dir filename)]
(->> f
slurp
edn/read-string))))

(defn create
"Takes a config, and loads all the specified checks."
[{:keys [checks]}]
[check-dir config]
(let [all-checks (mapcat (fn
[{:keys [directory files]}]
(map #(str directory java.io.File/separator %) files)) checks)]
(pmap load-edn! all-checks)))
(map #(str check-dir directory (java.io.File/separator) % ".edn") files)) (:checks config))]
(map load-edn! all-checks)))

29 changes: 21 additions & 8 deletions src/clerk/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@

(def remote-address "https://github.com/jeff-bruemmer/clerk-default-checks/archive/main.zip")

(defrecord Config [checks])
(defrecord Config [checks ignore])

(defn make-config
(defn load-config
[file]
(map->Config (edn/read-string file)))

(defn default
"If current config isn't valid, use the default."
[options]
(let [cur-config (:config options)
new-config (sys/filepath ".clerk" "config.edn")]
(if (or (nil? cur-config)
(not (.exists (io/file cur-config))))
(assoc options :config new-config)
options)))

(def invalid-msg "config must be an edn file.")

(defn valid?
Expand Down Expand Up @@ -76,14 +86,17 @@
(defn fetch-or-create!
"Fetches or creates config file. Will exit on failure."
[config-filepath]
(if (.exists (io/file config-filepath))
(make-config (fetch! config-filepath))
(do
(let [default-config (sys/filepath ".clerk" "config.edn")]
(cond
(and (not (nil? config-filepath))
(.exists (io/file config-filepath))) (load-config (fetch! config-filepath))
(.exists (io/file default-config)) (load-config (fetch! default-config))
:else (do
(println "Initializing Clerk...")
(println "Downloading default checks from: " remote-address ".")
(try
(unzip-file! (get-remote-zip! remote-address) (sys/home-dir) "clerk-default-checks-main" ".clerk")
(catch Exception e (error/exit (str "Couldn't unzip default checks\n" (.getMessage e)))))
(println "Created Clerk directory: " (sys/filepath ".clerk"))
(println "You can store custom checks in: " (sys/filepath ".clerk" "custom"))
(make-config (fetch! config-filepath)))))
(println "Created Clerk directory: " (sys/filepath ".clerk/"))
(println "You can store custom checks in: " (sys/filepath ".clerk" "custom/"))
(load-config (fetch! default-config))))))
42 changes: 20 additions & 22 deletions src/clerk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[error :as error]
[shipping :as ship]
[text :as text]
[vet :as vet]
[system :as sys]]
[vet :as vet]]
[clojure.tools.cli :as cli]))

(set! *warn-on-reflection* true)
Expand All @@ -32,8 +31,10 @@
["-C" "--checks" "List enabled checks."]
["-c" "--config CONFIG" "Set temporary configuration file." :default nil]
["-h" "--help" "Prints this help message."]
["-i" "--ignore IGNORE" "EDN file listing specimens to ignore." :default "ignore"]
["-b" "--code-blocks" "Include code blocks." :default false]
["-n" "--no-cache" "Don't use cached results." :default false]
["-t" "--time" "Print time elapsed." :default false]
["-v" "--version" "Prints version number."]])

(defn clerk
Expand All @@ -43,40 +44,37 @@
(vet/compute-or-cached)
(ship/out)))

(defn generate-config
[options]
(if (nil? (:config options))
(assoc options :config (sys/filepath ".clerk" "config.edn"))
options))

(defn reception
"Parses command line `args` and applies the relevant function."
[args]
(let [opts (cli/parse-opts args options :summary-fn format-summary)
{:keys [options errors]} opts
options (generate-config options)
{:keys [file config help checks version]} options]
expanded-options (conf/default (merge opts options))
{:keys [file config help checks version]} expanded-options]
(if (seq errors)
(do (error/message errors)
(error/exit))
(cond
file (clerk options)
checks (ship/print-checks config)
help (ship/print-usage opts)
version (ship/print-version)
:else (ship/print-usage opts "You must supply an option.")))))
(do (cond
file (clerk expanded-options)
checks (ship/print-checks config)
help (ship/print-usage expanded-options)
version (ship/print-version)
:else (ship/print-usage expanded-options "You must supply an option."))
expanded-options))))

(defn -main
[& args]
(let [start-time (System/currentTimeMillis)]
(reception args)
(let [start-time (System/currentTimeMillis)
options (reception args)]
(shutdown-agents)
(println "Completed in" (- (System/currentTimeMillis) start-time) "ms.")))
(when (:time options) (println "Completed in" (- (System/currentTimeMillis) start-time) "ms."))))

;;;; For development; prevents Cider REPL from closing.

;; (defn -main
;; [& args]
;; (let [start-time (System/currentTimeMillis)]
;; (reception args)
;; (println "Completed in" (- (System/currentTimeMillis) start-time) "ms.")))
;; (let [start-time (System/currentTimeMillis)
;; options (reception args)]
;; (when (:time options) (println "Completed in" (- (System/currentTimeMillis) start-time) "ms."))))


65 changes: 33 additions & 32 deletions src/clerk/shipping.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[version :as ver]
[fmt :as fmt]
[checks :as checks]
[system :as sys]
[config :as conf]]
[clojure
[pprint :as pp]
Expand Down Expand Up @@ -110,21 +111,22 @@

(defn print-opts
"Utiltiy for printing usage."
[summary title]
[summary title config]
(println title)
(print-options [:option :required :desc] summary)
(println "\nConfig file: " (:default (first (filter #(= "CONFIG" (:required %)) summary))) "\n"))
(println "\nConfig file: " config)
(print-version))

(defn print-usage
"Prints usage, optionally with a message."
([{:keys [summary]}]
([{:keys [summary config]}]
(println "\nClerk vets a text with the supplied checks.\n")
(print-opts summary "USAGE:"))
(print-opts summary "USAGE:" config))

([opts message]
(let [{:keys [summary]} opts]
(let [{:keys [summary config]} opts]
(println (str "\n" message "\n"))
(print-opts summary "USAGE:"))))
(print-opts summary "USAGE:" config))))

(defn results-table
"Takes results and prints them as a table."
Expand All @@ -142,11 +144,11 @@

(defn print-checks
"Prints a table of the enabled checks: names, kind, and description."
[c]
[config]
(println "Enabled checks:")
(->> c
(->> config
(conf/fetch-or-create!)
(checks/create)
((partial checks/create (sys/check-dir config)))
(map (fn [{:keys [name kind explanation]}]
{:name (string/capitalize name)
:kind (string/capitalize kind)
Expand All @@ -163,30 +165,29 @@
(let [heading (string/capitalize name)]
(str "| **" heading "** | " (fmt/sentence-dress explanation) " |")))

(defn generate-checks-readme
"Creates markdown table with checks and their descriptions."
[config]
(->> config
(conf/fetch!)
(conf/make-config)
(checks/create)
(sort-by :name)
(map print-explanation)
(string/join \newline)
(str "| **Check** | **Description** |" \newline "|-|-|" \newline)))

;;;; Main egress

(defn ignore?
"Is the specimen in the ignore file?"
[ignore-set issue]
(contains? ignore-set (:specimen issue)))

(defn out
"Takes results, preps them, and prints them in the supplied output format."
[{:keys [results output]}]
(cond
(empty? results) nil
(some? results) (let [r (sort-by (juxt :file :line-num :col-num) (mapcat prep results))]
(case (string/lower-case output)
"edn" (pp/pprint r)
"json" (json/write-value *out* r)
"group" (group-results r)
(results-table r)))
:else nil))
"Takes results, preps them, removes specimens to ignore, and
prints them in the supplied output format."
[payload]
(let [{:keys [results output check-dir config]} payload]
(cond
(empty? results) nil
(some? results) (let [ignore-set (set (checks/load-ignore-set! check-dir (:ignore config)))
prepped-results (mapcat prep (:results results))
results-minus-ignored (if (empty? ignore-set) prepped-results
(remove (partial ignore? ignore-set) prepped-results))
final-results (sort-by (juxt :file :line-num :col-num) results-minus-ignored)]
(case (string/lower-case output)
"edn" (pp/pprint final-results)
"json" (json/write-value *out* final-results)
"group" (group-results final-results)
(results-table final-results)))
:else nil)))

3 changes: 3 additions & 0 deletions src/clerk/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(:require [clerk
[checks :as checks]
[error :as error]
[config :as conf]
[text :as text]]
[clojure
[edn :as edn]
Expand All @@ -15,6 +16,7 @@
(defrecord Result [lines
lines-hash
file-hash
config
config-hash
check-hash
output
Expand Down Expand Up @@ -55,6 +57,7 @@
'clerk.text.Line text/map->Line
'clerk.text.Issue text/map->Issue
'clerk.checks.Check checks/map->Check
'clerk.config.Config conf/map->Config
'clerk.checks.Recommendation checks/map->Recommendation
'clerk.checks.Expression checks/map->Expression})})

Expand Down
15 changes: 15 additions & 0 deletions src/clerk/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@
"Builds filepath using the home directory."
([& args]
(str (string/join (java.io.File/separator) (concat [(home-dir)] args)))))

(defn check-dir
"Infer the directory when supplied a config filepath.
The config file must be in the same directory as the check directories."
[config]
(let [dd (filepath ".clerk/")]
(if (nil? config)
(do (println "Using default directory: " dd)
dd)
(-> config
(string/split (re-pattern (java.io.File/separator)))
drop-last
(#(string/join "/" %))
(str (java.io.File/separator))))))

Loading

0 comments on commit a179eca

Please sign in to comment.