Skip to content

Instantly share code, notes, and snippets.

@sveri
Created July 2, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sveri/701aa9f1cd1af12a37d7 to your computer and use it in GitHub Desktop.
Save sveri/701aa9f1cd1af12a37d7 to your computer and use it in GitHub Desktop.
Find if any of the given strings exists in any of .java source files in a folder
(ns playground
(:require [clojure.java.io :as io]
[clojure.string :as s])
(:import java.io.File))
(defn walk [dirpath pattern]
(doall (filter #(re-matches pattern (.getName %))
(file-seq (io/file dirpath)))))
(defn get-lib [lib-s]
(second (s/split (first (s/split lib-s #",")) #" ")))
(defn search-libs [fp]
(with-open [rdr (clojure.java.io/reader fp)]
(reduce (fn [a b] (conj a (get-lib b))) [] (line-seq rdr))))
(defn get-all-files [path]
(reduce (fn [a b] (conj a b)) [] (walk path #".*\.java")))
(defn search-string-in-file [needle fp]
(try
(let [haystack (slurp fp)]
(if (and needle haystack (.contains (.toLowerCase haystack) (.toLowerCase needle))) fp nil))
(catch Exception e nil)))
(defn search-libs-in-file [file libs]
(remove #(= nil %) (map #(search-string-in-file % file)
libs)))
(defn search-package-in-source [package-txt-file source-path]
(let [files (get-all-files source-path)]
(reduce (fn [a b]
(let [found-files (search-libs-in-file b (search-libs package-txt-file))]
(if-not (empty? found-files)
(conj a found-files)
a)))
[] files)))
(search-package-in-source "e:/bundles.txt" "e:/svn/trunk")
; bundles.txt
; - foo.bar, sometext
; - foo2.bar, sometext
; ....
; e:/svn/trunk
; is a folder containing a lot of .java files
; The goal is to find any file that contains at least one of the namespaces in bundles.txt.
; So xyz.java: import foo.bar will hit, whereas
; abc.java: import somewhat.bar will not hit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment