Skip to content

Commit

Permalink
Avoid question marks in options
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 3, 2020
1 parent 542078b commit c95d2de
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Carve invokes [clj-kondo](https://github.com/borkdude/clj-kondo) and uses the [a
The usage for a typical Clojure app looks like:

```
clojure -A:carve --opts '{:paths ["src" "test"]}'
clojure -M:carve --opts '{:paths ["src" "test"]}'
```

Currently `carve` only has one command line option, `--opts`, which
Expand All @@ -46,18 +46,18 @@ expects an EDN map of the following options of which only `:paths` is required:
be reported.
- `:carve-ignore-file`: a file where ignored vars can be stored, `.carve_ignore`
by default.
- `:interactive?`: ask what to do with an unused var: remove from the file, add
- `:interactive`: ask what to do with an unused var: remove from the file, add
to `.carve_ignore` or continue. Set to `true` by default.
- `:out-dir`: instead of writing back to the original file, write to this dir.
- `:dry-run?`: just print the unused var expression.
- `:aggressive?`: runs multiple times until no unused vars are left. Defaults to `false`.
- `:report`: when truthy, prints unused vars to stdout. Implies `:dry-run?
- `:dry-run`: just print the unused var expression.
- `:aggressive`: runs multiple times until no unused vars are left. Defaults to `false`.
- `:report`: when truthy, prints unused vars to stdout. Implies `:dry-run
true`. The output format may be set using `:report {:format ...}` where format
can be `:edn` or `:text`. The text output can be interpreted by editors like
Emacs. This option can be combined with `:aggressive?`.
Emacs. This option can be combined with `:aggressive`.

``` shell
$ clojure -A:carve --opts '{:paths ["test-resources"] :dry-run? true}'
$ clojure -A:carve --opts '{:paths ["test-resources"] :dry-run true}'
Carving test-resources/app.clj

Found unused var:
Expand Down Expand Up @@ -154,6 +154,6 @@ or alter the command used by `cider-jack-in` by prefixing the invocation with

## License

Copyright © 2019 Michiel Borkent
Copyright © 2019-2020 Michiel Borkent

Distributed under the EPL License. See LICENSE.
10 changes: 5 additions & 5 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{:deps {clj-kondo {:git/url "https://github.com/borkdude/clj-kondo"
:sha "f3fb45ab2a734a5862db5d20b83234f440f3e755"}
rewrite-cljc {:git/url "https://github.com/borkdude/rewrite-cljc-playground"
:sha "a60f2d3b172c6a8f1c25875f2af6c964b3f5fcc0"}
expound {:mvn/version "0.8.4"}}
{:deps {clj-kondo/clj-kondo {:git/url "https://github.com/borkdude/clj-kondo"
:sha "f4941cb3f2a1e643413dde11a7b09f24087b7afc"}
borkdude/rewrite-cljc {:git/url "https://github.com/borkdude/rewrite-cljc-playground"
:sha "a60f2d3b172c6a8f1c25875f2af6c964b3f5fcc0"}
expound/expound {:mvn/version "0.8.6"}}
:aliases {:kaocha {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "0.10.0"}
lambdaisland/kaocha {:mvn/version "0.0-590"}
Expand Down
36 changes: 21 additions & 15 deletions src/carve/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@

(defn sanitize-opts [opts]
(when (empty? (:paths opts)) (throw (ex-info ":paths must not be empty" opts)))
(let [opts (update opts :api-namespaces set)
(let [{:keys [:dry-run? :dry-run :aggressive? :aggressive :interactive? :interactive]} opts
;;_ (prn opts)
opts (assoc opts
:dry-run (or dry-run dry-run?)
:aggressive (or aggressive aggressive?)
:interactive (or interactive interactive?))
opts (update opts :api-namespaces set)
opts (update opts :carve-ignore-file
(fn [ci]
(if (nil? ci) ".carve_ignore"
ci)))
opts (if (:report opts)
;; report implies dry-run
(assoc opts :dry-run? true)
(assoc opts :dry-run true)
opts)
opts (if (:dry-run? opts)
(assoc opts :interactive? false)
opts (if (:dry-run opts)
(assoc opts :interactive false)
opts)
opts (if (:out-dir opts)
opts
Expand All @@ -42,16 +48,16 @@
(when-not (.exists ignore-file) (.createNewFile ignore-file))
(spit carve-ignore-file s :append true)))

(defn interactive [{:keys [:carve-ignore-file]} sym]
(defn interact [{:keys [:carve-ignore-file]} sym]
(println (format "Type Y to remove or i to add %s to %s" sym carve-ignore-file))
(let [input (read-line)]
(when (= "i" input)
(add-to-carve-ignore-file carve-ignore-file (str sym "\n")))
input))

(defn remove-locs [zloc locs locs->syms {:keys [:interactive?
:dry-run?]
:or {interactive? true}
(defn remove-locs [zloc locs locs->syms {:keys [:interactive
:dry-run]
:or {interactive true}
:as opts}]
(loop [zloc zloc
locs (seq locs)
Expand All @@ -68,9 +74,9 @@
(println "------------------")
(println (node/string node))
(println "------------------")
(let [remove? (cond dry-run? false
interactive?
(= "Y" (interactive opts sym))
(let [remove? (cond dry-run false
interactive
(= "Y" (interact opts sym))
:else true)
zloc (if remove? (z/remove zloc) (z/next zloc))]
(recur zloc (next locs) (or remove? made-changes?))))
Expand Down Expand Up @@ -145,13 +151,13 @@
:ignore-vars
:paths
:api-namespaces
:aggressive?
:dry-run?
:aggressive
:dry-run
:out-dir] :as opts} (sanitize-opts opts)
ignore (map (fn [ep]
[(symbol (namespace ep)) (symbol (name ep))])
ignore-vars)
re-analyze? (not dry-run?)]
re-analyze? (not dry-run)]
(loop [removed #{}
results []
analysis (analyze paths)]
Expand Down Expand Up @@ -187,7 +193,7 @@
(let [data-by-file (group-by :filename unused-vars-data)]
(doseq [[file vs] data-by-file]
(carve! file vs opts))))
(if aggressive?
(if aggressive
(recur (into removed unused-vars)
results
(if re-analyze?
Expand Down
16 changes: 11 additions & 5 deletions src/carve/main.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns carve.main
(:require
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[carve.impl :as impl]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[expound.alpha :as expound]))

(set! *warn-on-reflection* true)
Expand All @@ -14,10 +14,13 @@
(s/def ::ignore-vars (s/coll-of symbol?))
(s/def ::api-namespaces (s/coll-of symbol?))
(s/def ::carve-ignore-file string?)
(s/def ::interactive? boolean?)
(s/def ::dry-run? boolean?)
(s/def ::interactive boolean?)
(s/def ::interactive? boolean?) ;; deprecated
(s/def ::dry-run boolean?)
(s/def ::dry-run? boolean?) ;; deprecated
(s/def ::format #{:edn :text})
(s/def ::aggressive? boolean?)
(s/def ::aggressive boolean?)
(s/def ::aggressive? boolean?) ;; deprecated
(s/def ::out-dir string?)
(s/def ::report-format (s/keys :req-un [::format]))
(s/def ::report (s/or :bool boolean? :map ::report-format))
Expand All @@ -26,9 +29,12 @@
:opt-un [::ignore-vars
::api-namespaces
::carve-ignore-file
::interactive
::interactive?
::out-dir
::dry-run
::dry-run?
::aggressive
::aggressive?
::report]))

Expand Down
6 changes: 3 additions & 3 deletions test/carve/impl_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns carve.impl-test
(:require [carve.impl :as impl]
[clojure.test :as t :refer [deftest is testing]]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[clojure.test :as t :refer [deftest is testing]]))

(deftest impl-test []
(testing "without aggressive"
Expand Down Expand Up @@ -51,4 +51,4 @@
:name only-used-by-unused-function}}

(set (impl/run! {:paths [(.getPath (io/file "test-resources" "app"))]
:ignore-vars ['app/-main] :api-namespaces ['api] :report true :aggressive? true}))))))
:ignore-vars ['app/-main] :api-namespaces ['api] :report true :aggressive true}))))))
8 changes: 6 additions & 2 deletions test/carve/main_test.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
(ns carve.main-test
(:require
[carve.main :as main]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as g]
[clojure.test :as t :refer [deftest is testing]]
[clojure.string :as str]
[clojure.java.io :as io]))
[clojure.test :as t :refer [deftest is testing]]))

(defmethod clojure.test/report :begin-test-var [m]
(println "===" (-> m :var meta :name))
(println))

(defn- run-main [opts]
(with-out-str
Expand Down

0 comments on commit c95d2de

Please sign in to comment.