Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore this - Support wildcards in middle #113

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions src/me/raynes/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,24 @@
([prefix suffix] (temp-dir prefix suffix 10))
([prefix suffix tries] (temp-create prefix suffix tries mkdirs)))

(defn ephemeral-file
"Create an ephemeral file (will be deleted on JVM exit).
Returns nil if file could not be created even after n tries
(default 10)."
([prefix] (ephemeral-file prefix "" 10))
([prefix suffix] (ephemeral-file prefix suffix 10))
([prefix suffix tries] (when-let [created (temp-create prefix suffix tries create)]
(doto created .deleteOnExit))))

(defn ephemeral-dir
"Create an ephemeral directory (will be deleted on JVM exit).
Returns nil if dir could not be created even after n tries
(default 10)."
([prefix] (ephemeral-dir prefix "" 10))
([prefix suffix] (ephemeral-dir prefix suffix 10))
([prefix suffix tries] (when-let [created (temp-create prefix suffix tries mkdirs)]
(doto created .deleteOnExit))))

; Taken from https://github.com/jkk/clj-glob. (thanks Justin!)
(defn- glob->regex
"Takes a glob-format string and returns a regex."
Expand All @@ -369,12 +387,12 @@
curly-depth)
:else (recur (next stream) (str re c) curly-depth)))))

(defn glob
(defn- glob-1
"Returns files matching glob pattern."
([pattern]
(let [parts (split pattern)
root (apply file (if (= (count parts) 1) ["."] (butlast parts)))]
(glob root (last parts))))
(glob-1 root (last parts))))
([^File root pattern]
(let [regex (glob->regex pattern)]
(seq (.listFiles
Expand All @@ -383,6 +401,58 @@
(accept [_ _ filename]
(boolean (re-find regex filename)))))))))

(defn- has-wildcard?
"Checks if a path part has a wildcard in it."
[s]
(boolean (or (re-find #"(?<!\\)[\*\?]" s)
(re-find #"(?<!\\)\[[^\]]+(?<!\\)\]" s))))

(defn- fix-empty-prefix
"If the prefix list is empty, replace it with [\".\"]"
[prefix]
(if (empty? prefix)
["."]
prefix))

(defn- find-first-pattern
"Finds the first part (from a list of strings) that contains a wildcard, then returns a 3-tuple of
[prefix, part-with-wildcard, suffix], where both prefix and suffix are lists."
[parts]
(loop [prefix []
part (first parts)
suffix (rest parts)]
(cond
(and part (has-wildcard? part))
[(fix-empty-prefix prefix) part suffix]

(not-empty suffix)
(recur (conj prefix part)
(first suffix)
(rest suffix))

:else
[(fix-empty-prefix prefix) part nil])))

(defn glob
"Returns files matching glob pattern."
[path]
(loop [to-process [(find-first-pattern (split path))]
found []]
(if (empty? to-process)
found
(let [[prefix pattern suffix] (first to-process)
files (glob-1 (apply file prefix) pattern)]
(if (empty? suffix)
(recur (rest to-process)
(concat found files))
(recur (concat
(reduce (fn [acc f]
(conj acc (find-first-pattern (concat (split (.getPath f)) suffix))))
[]
files)
(rest to-process))
found))))))

(defn- iterzip
"Iterate over a zip, returns a sequence of the nodes with a nil suffix"
[z]
Expand Down
10 changes: 10 additions & 0 deletions test/me/raynes/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@
(directory? tmp) => true
(delete tmp)))

(fact
(let [tmp (ephemeral-file "fs-")]
(exists? tmp) => true
(file? tmp) => true)) ;; is deleted on JVM exit

(fact
(let [tmp (ephemeral-dir "fs-")]
(exists? tmp) => true
(directory? tmp) => true)) ;; is deleted on JVM exit

(fact
(absolute "foo") => (io/file *cwd* "foo"))

Expand Down