|
1 | 1 | (ns refactor-nrepl.core |
2 | | - (:require [clojure.java.io :as io] |
3 | | - [clojure.string :as str] |
4 | | - [clojure.tools.namespace.parse :as parse] |
5 | | - [clojure.tools.reader.reader-types :as readers] |
6 | | - [orchard.java.classpath :as cp] |
7 | | - [orchard.misc :as misc] |
8 | | - [me.raynes.fs :as fs] |
9 | | - [refactor-nrepl.util :as util :refer [normalize-to-unix-path]] |
10 | | - [refactor-nrepl.s-expressions :as sexp] |
11 | | - [refactor-nrepl.config :as config]) |
12 | | - (:import [java.io File FileReader PushbackReader StringReader])) |
| 2 | + (:require |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.string :as str] |
| 5 | + [clojure.tools.namespace.parse :as parse] |
| 6 | + [clojure.tools.reader.reader-types :as readers] |
| 7 | + [me.raynes.fs :as fs] |
| 8 | + [orchard.java.classpath :as cp] |
| 9 | + [orchard.misc :as misc] |
| 10 | + [refactor-nrepl.config :as config] |
| 11 | + [refactor-nrepl.s-expressions :as sexp] |
| 12 | + [refactor-nrepl.util :as util :refer [normalize-to-unix-path]]) |
| 13 | + (:import |
| 14 | + (java.io File FileReader PushbackReader StringReader))) |
13 | 15 |
|
14 | 16 | (defn version [] |
15 | 17 | (let [v (-> (or (io/resource "refactor-nrepl/refactor-nrepl/project.clj") |
|
57 | 59 | (when (.isDirectory ^File f) f))) |
58 | 60 | (remove (comp ignore-dir-on-classpath? str)))) |
59 | 61 |
|
| 62 | +(defn source-dirs-on-classpath |
| 63 | + "Like `#'dirs-on-classpath`, but restricted to dirs that look like |
| 64 | + (interesting) source/test dirs." |
| 65 | + [] |
| 66 | + (->> (dirs-on-classpath) |
| 67 | + (remove (fn [^File f] |
| 68 | + (let [s (-> f .toString)] |
| 69 | + (or (-> s (.contains "resources")) |
| 70 | + (-> s (.contains "target")) |
| 71 | + (-> s (.contains ".gitlibs")))))) |
| 72 | + (remove util/dir-outside-root-dir?))) |
| 73 | + |
60 | 74 | (defn project-root |
61 | 75 | "Return the project root directory. |
62 | 76 |
|
|
141 | 155 | {:read-cond :allow :features #{dialect}}) |
142 | 156 | (catch Exception _ nil)))))) |
143 | 157 |
|
144 | | -(defn- data-file? |
145 | | - "True of f is named like a clj file but represents data. |
146 | | -
|
147 | | - E.g. true for data_readers.clj" |
148 | | - [path-or-file] |
149 | | - (let [path (.getPath (io/file path-or-file)) |
150 | | - data-files #{"data_readers.clj" "project.clj" "boot.clj"}] |
151 | | - (reduce (fn [acc data-file] (or acc (.endsWith path data-file))) |
152 | | - false |
153 | | - data-files))) |
| 158 | +(defn cljc-extension? [^String path] |
| 159 | + (.endsWith path ".cljc")) |
154 | 160 |
|
155 | 161 | (defn cljc-file? |
156 | 162 | [path-or-file] |
157 | 163 | (let [path (.getPath (io/file path-or-file))] |
158 | | - (and (.endsWith path ".cljc") |
| 164 | + (and (cljc-extension? path) |
159 | 165 | (read-ns-form path)))) |
160 | 166 |
|
| 167 | +(defn cljs-extension? [^String path] |
| 168 | + (.endsWith path ".cljs")) |
| 169 | + |
161 | 170 | (defn cljs-file? |
162 | 171 | [path-or-file] |
163 | 172 | (let [path (.getPath (io/file path-or-file))] |
164 | | - (and (.endsWith path ".cljs") |
| 173 | + (and (cljs-extension? path) |
165 | 174 | (read-ns-form path)))) |
166 | 175 |
|
| 176 | +(defn clj-extension? [^String path] |
| 177 | + (.endsWith path ".clj")) |
| 178 | + |
167 | 179 | (defn clj-file? |
168 | 180 | [path-or-file] |
169 | 181 | (let [path (.getPath (io/file path-or-file))] |
170 | | - (and (not (data-file? path-or-file)) |
171 | | - (.endsWith path ".clj") |
| 182 | + (and (not (util/data-file? path-or-file)) |
| 183 | + (clj-extension? path) |
172 | 184 | (read-ns-form path)))) |
173 | 185 |
|
174 | 186 | (defn source-file? |
|
194 | 206 |
|
195 | 207 | (defn find-in-project |
196 | 208 | "Return the files in the project satisfying (pred ^File file)." |
197 | | - [pred] |
198 | | - (->> (dirs-on-classpath) |
199 | | - (pmap (partial find-in-dir pred)) |
200 | | - (apply concat) |
201 | | - distinct)) |
| 209 | + ([pred] |
| 210 | + (find-in-project pred (dirs-on-classpath))) |
| 211 | + ([pred dirs] |
| 212 | + (->> dirs |
| 213 | + (pmap (partial find-in-dir pred)) |
| 214 | + (apply concat) |
| 215 | + distinct))) |
| 216 | + |
| 217 | +(defn source-files-with-clj-like-extension |
| 218 | + "Finds files with .clj* extension in the project, without inspecting them. |
| 219 | +
|
| 220 | + Meant as a particularly fast operation (as it doesn't slurp files)." |
| 221 | + ([ignore-errors?] |
| 222 | + (source-files-with-clj-like-extension ignore-errors? (source-dirs-on-classpath))) |
| 223 | + ([ignore-errors? dirs] |
| 224 | + (find-in-project (util/with-suppressed-errors |
| 225 | + (comp (some-fn clj-extension? |
| 226 | + cljc-extension? |
| 227 | + cljs-extension?) |
| 228 | + (fn [^File f] |
| 229 | + (.getPath f))) |
| 230 | + ignore-errors?) |
| 231 | + dirs))) |
202 | 232 |
|
203 | 233 | (defn throw-unless-clj-file [file-path] |
204 | 234 | (when-not (re-matches #".+\.clj$" file-path) |
|
0 commit comments