Skip to content

Commit

Permalink
Add kibit.driver/exec to support call via clojure -X:
Browse files Browse the repository at this point in the history
  • Loading branch information
carrete committed Apr 28, 2023
1 parent f7f7fbe commit a34beb6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,16 @@ instead of:
Add the following to your aliases

```clojure
:kibit {:extra-deps {jonase/kibit {:mvn/version "0.1.8"}}
:main-opts ["-e" "(require,'[kibit.driver,:as,k])(k/external-run,[\"src\"],nil)"]}
:kibit {:extra-deps {jonase/kibit {:mvn/version "0.2.0"}}
:exec-fn kibit.driver/exec
:exec-args {:paths ["."]}
```

Then run `clojure -X:kibit`. For more options, please see the
docstring on `kibit.driver/exec`.

**NOTE:** At least Clojure v1.9 is required to use this exec-fn method.

For alternative options to use kibit from inside deps.edn, check out [this issue](https://github.com/clj-commons/kibit/issues/221)

## Usage from inside Emacs
Expand Down
9 changes: 8 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
org.clojure/core.logic {:mvn/version "1.0.1"}
org.clojure/tools.cli {:mvn/version "1.0.214"}
org.clojure/tools.reader {:mvn/version "1.3.6"}
rewrite-clj/rewrite-clj {:mvn/version "1.1.47"}}}
rewrite-clj/rewrite-clj {:mvn/version "1.1.47"}}
;; At least Clojure v1.9 is required to run `clojure -X:`. Run this as
;; `clojure -X:exec` or `clojure -X:exec -i -r markdown`, for example.
:aliases {:exec {:extra-deps {org.clojure/clojure {:mvn/version "1.9.0"}
org.babashka/cli {:mvn/version "0.7.51"}}
:exec-fn kibit.driver/exec
:exec-args {:paths ["."]}
:main-opts ["-m" "babashka.cli.exec"]}}}
59 changes: 59 additions & 0 deletions src/kibit/driver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,62 @@
(if (zero? (count (apply run source-paths rules args)))
(System/exit 0)
(System/exit 1)))

(defn exec
"Given a [Clojure CLI-style map][Execute a function] `options`, turn this map
into an equivalent set of options as expected by run and external-run above.
Please note that rules is not supported. nil is passed to external-run below
which enables all rules (see kibit.rules/all-rules). I don't know what rules
should be. A list of rule names as strings doesn't seem to be it.
**DO NOT** escape the dobule-quotes in deps.edn or on the command-line.
To make use of this, add an alias, e.g. kibit, to deps.edn:
:kibit {:extra-deps {jonase/kibit {:mvn/version \"0.2.0\"}}
:exec-fn kibit.driver/exec
:exec-args {:paths [\"src\" \"test\"]}}
Then run:
clojure -X:kibit
Additional command-line options can be added in deps.edn or on the
command-line. For example, in deps.edn:
:kibit {:extra-deps {jonase/kibit {:mvn/version \"0.2.0\"}}
:exec-fn kibit.driver/exec
:exec-args {:paths [\"src\" \"test\"]
:interactive true}}
Or on the command-line:
clojure -X:kibit :interactive true
To use [babashka.cli][babashka.cli], update the kibit aliasn in deps.edn:
:kibit {:extra-deps {jonase/kibit {:mvn/version \"0.2.0\"}}
org.babashka/cli {:mvn/version \"0.7.51\"}}
:exec-fn kibit.driver/exec
:exec-args {:paths [\"src\" \"test\"]}
:main-opts [\"-m\" \"babashka.cli.exec\"]}
Then run:
clojure -X:kibit -i -r markdown
[Execute a function]: https://clojure.org/reference/deps_and_cli#_execute_a_function
[babashka.cli]: https://github.com/babashka/cli
"
{:org.babashka/cli {:alias {:r :reporter
:e :replace
:i :interactive}
:coerce {:paths [:string]
:reporter :string
:replace :boolean
:interactive :boolean}}}
[{:keys [paths reporter replace interactive] :as _options}]
(apply (partial external-run paths nil) ["-r" (or reporter "text")
(when replace "-e")
(when interactive "-i")]))

0 comments on commit a34beb6

Please sign in to comment.