Skip to content

Commit

Permalink
Merge pull request #71 from RyanMcG/master
Browse files Browse the repository at this point in the history
Add GFM reporter, Allow user to select reporter with CL option
  • Loading branch information
jonase committed Feb 7, 2013
2 parents 65b9fda + 09ada8b commit ce7d9b6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -7,6 +7,7 @@
:comments "Contact if any questions"}
:dependencies [[org.clojure/clojure "1.5.0-beta1"]
[org.clojure/core.logic "0.8.0-beta2"]
[org.clojure/tools.namespace "0.2.1"]]
[org.clojure/tools.namespace "0.2.1"]
[org.clojure/tools.cli "0.2.2"]]
:dev-dependencies [[lein-marginalia "0.7.0"]]
:warn-on-reflection false)
19 changes: 12 additions & 7 deletions src/kibit/driver.clj
Expand Up @@ -2,17 +2,22 @@
(:require [clojure.tools.namespace :refer [find-clojure-sources-in-dir]]
[clojure.java.io :as io]
[kibit.check :refer [check-file]]
[kibit.reporters :refer [cli-reporter]]))
[kibit.reporters :refer :all]
[clojure.tools.cli :refer [cli]]))

(def cli-specs [["-r" "--reporter"
"The reporter used when rendering suggestions"
:default "text"]])

(defn run [project & args]
(let [source-files (if (empty? args)
(let [[options file-args usage-text] (apply (partial cli args) cli-specs)
source-files (if (empty? file-args)
(mapcat #(-> % io/file find-clojure-sources-in-dir)
(or (:source-paths project) [(:source-path project)]))
args)]
file-args)]
(doseq [file source-files]
(try (->> (check-file file)
(map cli-reporter)
doall)
(try (check-file file :reporter (name-to-reporter (:reporter options)
cli-reporter))
(catch Exception e
(println "Check failed -- skipping rest of file")
(println (.getMessage e)))))))
(println (.getMessage e)))))))
18 changes: 17 additions & 1 deletion src/kibit/reporters.clj
Expand Up @@ -26,7 +26,7 @@
println)))

(defn cli-reporter
"Print a check-map to `*out*`"
"Print a check-map to `*out*` in plain text."
[check-map]
(let [{:keys [file line expr alt]} check-map]
(do
Expand All @@ -36,3 +36,19 @@
(pprint-code expr)
(newline))))

(defn gfm-reporter
"Print a check-map to `*out*` in github flavored markdown."
[check-map]
(let [{:keys [file line expr alt]} check-map]
(printf "----\n##### `%s:%s`\nConsider using:\n" file line)
(println "```clojure")
(pprint-code alt)
(println "```")
(println "instead of:")
(println "```clojure")
(pprint-code expr)
(println "```")
(newline)))

(def name-to-reporter {"markdown" gfm-reporter
"text" cli-reporter})
36 changes: 36 additions & 0 deletions test/kibit/test/reporters.clj
@@ -0,0 +1,36 @@
(ns kibit.test.reporters
(:require [kibit.reporters :as reporters]
[clojure.string :as string]
[clojure.test :refer :all]))

(deftest plain
(are [check-map result]
(= (with-out-str (reporters/cli-reporter check-map))
(string/join "\n" result))
{:file "some/file.clj"
:line 30
:expr '(+ x 1)
:alt '(inc x)} ["At some/file.clj:30:"
"Consider using:"
" (inc x)"
"instead of:"
" (+ x 1)"
"" ""]))
(deftest gfm
(are [check-map result]
(= (with-out-str (reporters/gfm-reporter check-map))
(string/join "\n" result))
{:file "some/file.clj"
:line 30
:expr '(+ x 1)
:alt '(inc x)} ["----"
"##### `some/file.clj:30`"
"Consider using:"
"```clojure"
" (inc x)"
"```"
"instead of:"
"```clojure"
" (+ x 1)"
"```"
"" ""]))

0 comments on commit ce7d9b6

Please sign in to comment.