|
69 | 69 | (when (.exists sources-file) |
70 | 70 | sources-path)))) |
71 | 71 |
|
72 | | -(defn needs-java-sources? |
73 | | - "Check if the search options indicate we're looking for Java files." |
74 | | - [{:keys [type glob]}] |
75 | | - (or (= "java" type) |
76 | | - (and glob (re-find #"\.java" glob)))) |
| 72 | +(def ^:private binary-extensions |
| 73 | + "File extensions that are binary and should never be searched." |
| 74 | + #{".class" ".jar" ".war" ".ear" |
| 75 | + ".png" ".jpg" ".jpeg" ".gif" ".ico" ".bmp" ".svg" ".webp" |
| 76 | + ".so" ".dll" ".dylib" ".o" ".a" |
| 77 | + ".zip" ".gz" ".tar" ".bz2" ".xz" |
| 78 | + ".ttf" ".otf" ".woff" ".woff2" |
| 79 | + ".pdf" ".doc" ".docx"}) |
| 80 | + |
| 81 | +(defn binary-entry? |
| 82 | + "Returns true if a jar entry path has a known binary extension." |
| 83 | + [entry-path] |
| 84 | + (some #(str/ends-with? (str/lower-case entry-path) %) binary-extensions)) |
77 | 85 |
|
78 | 86 | (defn get-jars-with-sources |
79 | 87 | "Given a list of jars and search opts, return jars plus any available sources jars. |
80 | | - When searching for Java files, downloads missing sources from Maven Central." |
| 88 | + When type is \"java\", downloads missing sources from Maven Central." |
81 | 89 | [jars opts] |
82 | 90 | (let [;; First find sources jars already in Maven cache |
83 | 91 | existing-sources (->> jars |
84 | 92 | (keep find-sources-jar) |
85 | 93 | (remove (set jars)))] |
86 | | - (if (needs-java-sources? opts) |
87 | | - ;; For Java searches, also download missing sources |
| 94 | + (if (= "java" (:type opts)) |
| 95 | + ;; For Java ecosystem jars, download source jars to get searchable text |
88 | 96 | (let [jars-with-sources (set (map #(str/replace % #"-sources\.jar$" ".jar") |
89 | 97 | existing-sources)) |
90 | 98 | jars-missing-sources (remove jars-with-sources jars) |
91 | 99 | _ (log/debug "Checking for Java sources for" (count jars-missing-sources) "jars") |
92 | 100 | downloaded-sources (deps-sources/ensure-sources-jars! jars-missing-sources)] |
93 | 101 | (log/debug "Downloaded" (count downloaded-sources) "sources jars") |
94 | 102 | (into (vec jars) (concat existing-sources downloaded-sources))) |
95 | | - ;; For non-Java searches, just use existing sources |
| 103 | + ;; For Clojure ecosystem jars, source is in the jar already |
96 | 104 | (into (vec jars) existing-sources)))) |
97 | 105 |
|
98 | 106 | (defn parse-library-filter |
|
153 | 161 | (str "(" (str/replace alts "," "|") ")"))))] |
154 | 162 | (boolean (re-find (re-pattern (str pattern-regex "$")) path))))) |
155 | 163 |
|
156 | | -(defn type-to-glob |
157 | | - "Convert a file type (like 'clj') to a glob pattern." |
158 | | - [type-str] |
159 | | - (when type-str |
160 | | - (str "*." type-str))) |
161 | | - |
162 | 164 | (defn filter-entries |
163 | | - "Filter jar entries by glob and/or type patterns." |
164 | | - [entries {:keys [glob type]}] |
165 | | - (let [effective-glob (or glob (type-to-glob type))] |
166 | | - (if effective-glob |
167 | | - (filter #(glob-matches? effective-glob %) entries) |
168 | | - entries))) |
| 165 | + "Filter jar entries, removing binary files and applying optional glob pattern. |
| 166 | + Binary extensions (.class, images, native libs, etc.) are always excluded." |
| 167 | + [entries {:keys [glob]}] |
| 168 | + (let [text-entries (remove binary-entry? entries)] |
| 169 | + (if glob |
| 170 | + (filter #(glob-matches? glob %) text-entries) |
| 171 | + text-entries))) |
169 | 172 |
|
170 | 173 | (defn search-jar-entry-rg |
171 | 174 | "Search using ripgrep. Reads jar entry via Java, pipes content to rg via stdin. |
|
242 | 245 | - pattern: Regex pattern to search for |
243 | 246 | - opts: Map of options |
244 | 247 | :library - Required. Maven group or group/artifact to search |
245 | | - :glob - Filter files by glob pattern (e.g., \"*.clj\") |
246 | | - :type - Filter files by type (e.g., \"clj\", \"java\") |
| 248 | + :type - Required. Jar ecosystem type: \"clj\" or \"java\" |
| 249 | + \"clj\" searches the jar directly (source is in the jar) |
| 250 | + \"java\" downloads source jars from Maven Central |
| 251 | + :glob - Optional. Filter files by glob pattern (e.g., \"*.java\") |
247 | 252 | :output-mode - :content, :files-with-matches, or :count |
248 | 253 | :case-insensitive - Case insensitive search |
249 | 254 | :line-numbers - Include line numbers (default true for content mode) |
|
263 | 268 | (if-not base-jars |
264 | 269 | {:error "Failed to resolve classpath. Is this a deps.edn project?"} |
265 | 270 | (let [library (:library opts) |
266 | | - cache-key [project-dir library (needs-java-sources? opts)] |
| 271 | + cache-key [project-dir library (:type opts)] |
267 | 272 | filtered-jars (filter-jars-by-library base-jars library)] |
268 | 273 | (if (empty? filtered-jars) |
269 | 274 | {:error (str "No libraries found matching: " (:library opts) |
|
0 commit comments