Skip to content

Commit 067054d

Browse files
committed
Add config for ignoring specific paths
This change will allow users to configure refactor-nrepl to ignore certain paths when finding the dirs on classpath by specifying a seq of regexes which the paths will be matched against. Currently refactor-nrepl considers all the .clj files that are reachable from project root. This is a problem if the project repository includes example .clj files, if a user has symlinked other clj projects under 'checkouts' or if there are any other files that need to be ignored. This change is backward compatible and wouldn't affect the current behaviour in existing installations as the default value is an empty seq. Also added tests and updated README and Changelog.
1 parent 992e9a2 commit 067054d

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### New features
1616

1717
* New config setting `:libspec-whitelist` which makes it possible to create a seq of namespaces `clean-ns` shouldn't prune. This is useful for libspecs which aren't used except through side-effecting loads.
18+
* New config setting `:ignore-paths` for ignoring certain paths when finding dirs on classpath.
1819

1920
## 2.2.0
2021

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Configuration settings are passed along with each msg, currently the recognized
8585
;; This seq of strings will be used as regexp patterns to match
8686
;; against the libspec name.
8787
:libspec-whitelist ["^cljsjs"]
88+
89+
;; Regexes matching paths that are to be ignored
90+
:ignore-paths []
8891
}
8992
```
9093

src/refactor_nrepl/config.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
;; This seq of strings will be used as regexp patterns to match
2222
;; against the libspec name.
2323
:libspec-whitelist ["^cljsjs"]
24+
25+
;; Regexes matching paths that are to be ignored
26+
:ignore-paths []
2427
})
2528

2629
(defn opts-from-msg [msg]

src/refactor_nrepl/core.clj

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[clojure.tools.reader.reader-types :as readers]
77
[me.raynes.fs :as fs]
88
[refactor-nrepl.util :refer [normalize-to-unix-path]]
9-
[refactor-nrepl.s-expressions :as sexp])
9+
[refactor-nrepl.s-expressions :as sexp]
10+
[refactor-nrepl.config :as config])
1011
(:import [java.io File FileReader PushbackReader StringReader]))
1112

1213
(defn version []
@@ -30,13 +31,23 @@
3031
[readable]
3132
(-> readable slurp ns-from-string))
3233

34+
(defn ignore-dir-on-classpath?
35+
[^String path]
36+
(or (.endsWith path "target/srcdeps")
37+
(reduce (fn [acc x]
38+
(if (re-find x path)
39+
(reduced true)
40+
acc))
41+
false
42+
(:ignore-paths config/*config*))))
43+
3344
(defn dirs-on-classpath
3445
"Return all dirs on classpath, filtering out our inlined deps
35-
directory."
46+
directory and paths matching :ignore-paths specified in config."
3647
[]
3748
(->> (cp/classpath)
3849
(filter fs/directory?)
39-
(remove #(-> % str normalize-to-unix-path (.endsWith "target/srcdeps")))))
50+
(remove #(-> % str normalize-to-unix-path ignore-dir-on-classpath?))))
4051

4152
(defn project-root
4253
"Return the project root directory.

test/refactor_nrepl/core_test.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(ns refactor-nrepl.core-test
2+
(:require [clojure.test :refer :all]
3+
[refactor-nrepl.config :as config]
4+
[refactor-nrepl.core :refer [ignore-dir-on-classpath?]]))
5+
6+
7+
(defmacro assert-ignored-paths
8+
[paths pred]
9+
`(doseq [p# ~paths]
10+
(is (~pred (ignore-dir-on-classpath? p#)))))
11+
12+
13+
(deftest test-ignore-dir-on-classpath?
14+
(let [not-ignored ["/home/user/project/test"
15+
"/home/user/project/src"
16+
"/home/user/project/target/classes"]
17+
sometimes-ignored ["/home/user/project/checkouts/subproject"
18+
"/home/user/project/resources"]
19+
always-ignored ["/home/user/project/target/srcdeps"]]
20+
(testing "predicate to ignore dirs on classpath with default config"
21+
(assert-ignored-paths (concat not-ignored sometimes-ignored) false?)
22+
(assert-ignored-paths always-ignored true?))
23+
(testing "predicate to ignore dirs on classpath with custom config"
24+
(binding [config/*config* (assoc config/*config*
25+
:ignore-paths
26+
[#".+checkouts/.+" #"resources"])]
27+
(assert-ignored-paths not-ignored false?)
28+
(assert-ignored-paths (concat always-ignored sometimes-ignored) true?)))))

0 commit comments

Comments
 (0)