From 15b010e6950cf917f026833d89b123be11284582 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 19 Nov 2021 13:47:56 +0100 Subject: [PATCH] v0.1.0 --- README.md | 19 ++++++++++++++++++- deps.edn | 1 + resources/CARVE_VERSION | 2 +- src/carve/api.clj | 21 ++++++++++++++++++++- src/carve/main.clj | 24 ++++++------------------ 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f097b5c..b24798c 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,20 @@ where the latest SHA can be found with: $ git ls-remote https://github.com/borkdude/carve.git refs/heads/master ``` +#### Clojure tool + +To use as a [clojure tool](https://clojure.org/reference/deps_and_cli#tool_install): + +``` shell +$ clj -Ttools install io.github.borkdude/carve '{:git/tag "v0.1.0"}' :as carve +``` + ## How does it work? -Carve invokes [clj-kondo](https://github.com/borkdude/clj-kondo) and uses the [analysis](https://github.com/borkdude/clj-kondo/tree/master/analysis) information to check which vars are unused. To remove the relevant bits of code it uses [rewrite-cljc](https://github.com/lread/rewrite-cljc-playground). +Carve invokes [clj-kondo](https://github.com/borkdude/clj-kondo) and uses the +[analysis](https://github.com/borkdude/clj-kondo/tree/master/analysis) +information to check which vars are unused. To remove the relevant bits of code +it uses [rewrite-cljc](https://github.com/lread/rewrite-cljc-playground). ## Usage @@ -72,6 +83,12 @@ clojure -M:carve --opts '{:paths ["src" "test"]}' on the JVM. +As a [clojure tool](https://clojure.org/reference/deps_and_cli#_tool_usage): + +``` clojure +$ clj -Tcarve carve! '{:paths ["src"] :report {:format :text}}' +``` + You can also store the config for your project in `.carve/config.edn`. When invoking carve with no options, the options in `.carve/config.edn` will be used. When providing options, the CLI options will take precedence over the configuration diff --git a/deps.edn b/deps.edn index 557d007..77025d0 100644 --- a/deps.edn +++ b/deps.edn @@ -1,6 +1,7 @@ {:deps {clj-kondo/clj-kondo {:mvn/version "2021.09.26-20211013.125030-9"} rewrite-clj/rewrite-clj {:mvn/version "1.0.572-alpha"} expound/expound {:mvn/version "0.8.6"}} + :tools/usage {:ns-default carve.api} :aliases {:kaocha {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "0.10.0"} lambdaisland/kaocha {:mvn/version "0.0-590"} diff --git a/resources/CARVE_VERSION b/resources/CARVE_VERSION index 4e379d2..6e8bf73 100644 --- a/resources/CARVE_VERSION +++ b/resources/CARVE_VERSION @@ -1 +1 @@ -0.0.2 +0.1.0 diff --git a/src/carve/api.clj b/src/carve/api.clj index 8949683..779c26f 100644 --- a/src/carve/api.clj +++ b/src/carve/api.clj @@ -1,6 +1,8 @@ (ns carve.api (:refer-clojure :exclude [run!]) - (:require [carve.impl :as impl])) + (:require [carve.impl :as impl] + [clojure.edn :as edn] + [clojure.java.io :as io])) (defn print! [{:keys [:report :config]}] (let [format (-> config :report :format)] @@ -19,3 +21,20 @@ ([] (report {:merge-config true})) ([opts] (impl/run+ (assoc opts :report true)))) + +(defn carve! + "Similar as main function but with opts already parsed. Use nil opts for passing no opts. + Intended to be used with clojure -T or clojure -X." + [opts] + (let [config-file (io/file ".carve/config.edn") + config (when (.exists config-file) + (edn/read-string (slurp config-file)))] + (if (and (empty? opts) (not config)) + (binding [*err* *out*] + (println "No config found in .carve/config.edn.\nSee https://github.com/borkdude/carve#usage on how to use carve.") + 1) + (let [{:keys [:report :config]} (impl/run+ opts) + format (-> config :report :format)] + (when (:report config) + (impl/print-report report format)) + (if (empty? report) 0 1))))) diff --git a/src/carve/main.clj b/src/carve/main.clj index a68b49e..fc8b0c2 100644 --- a/src/carve/main.clj +++ b/src/carve/main.clj @@ -1,27 +1,15 @@ (ns carve.main (:require - [carve.impl :as impl] - [clojure.edn :as edn] - [clojure.java.io :as io]) + [carve.api :as api] + [clojure.edn :as edn]) (:gen-class)) (defn main [& [flag opts & _args]] - (let [config-file (io/file ".carve/config.edn") - config (when (.exists config-file) - (edn/read-string (slurp config-file)))] - (if (and (not flag) (not config)) - (binding [*err* *out*] - (println "No config found in .carve/config.edn.\nSee https://github.com/borkdude/carve#usage on how to use carve.") - 1) - (do (when (and (not (= "--opts" flag)) (not config)) - (throw (ex-info (str "Unrecognized option: " flag) {:flag flag}))) - (let [opts (edn/read-string opts) - {:keys [:report :config]} (impl/run+ opts) - format (-> config :report :format)] - (when (:report config) - (impl/print-report report format)) - (if (empty? report) 0 1)))))) + (when (not (= "--opts" flag)) + (throw (ex-info (str "Unrecognized option: " flag) {:flag flag}))) + (let [opts (if opts (edn/read-string opts) nil)] + (api/carve! opts))) (defn -main [& options]