Skip to content
Open
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
28 changes: 16 additions & 12 deletions src/main/clojure/hawk/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(ns hawk.core
(:require [clojure.java.io :as io]
[hawk.watcher :refer [new-watcher] :as watcher]))
[hawk.watcher :refer [new-watcher] :as watcher])
(:import [java.nio.file Path]
[java.io File]))

(set! *warn-on-reflection* true)

(defn catch-errors [f]
(fn [& args]
Expand All @@ -13,13 +17,13 @@
(merge spec
(->> paths
(map #(-> % io/file .getCanonicalFile))
(group-by #(if (.isFile %) :files :directories)))))
(group-by #(if (.isFile ^File %) :files :directories)))))

(defn process-paths [{:keys [files directories] :as spec}]
(assoc spec :paths
(concat
(map #(-> % .toPath) directories)
(map #(-> % .getParentFile .toPath) files))))
(map #(.toPath ^File %) directories)
(map #(-> ^File % .getParentFile .toPath) files))))

(defn process-context [spec]
(assoc spec :context
Expand All @@ -29,10 +33,10 @@
(defn process-handler [{:keys [files paths] :as spec}]
(assoc spec :handler
(catch-errors
(fn [ctx {:keys [file kind] :as e}]
(let [path (-> file .toPath)]
(fn [ctx {:keys [^File file kind] :as e}]
(let [path ^Path (.toPath file)]
(if (and
(some #(.startsWith path %) paths)
(some #(.startsWith path ^Path %) paths)
(or (empty? files)
(some (partial = file) files))
(or (not (:filter spec))
Expand All @@ -44,15 +48,15 @@
(defn remove-duplicate-paths [paths]
(seq
(apply
sorted-set-by #(.compareTo %1 %2) paths)))
sorted-set-by #(.compareTo ^Path %1 ^Path %2) paths)))

(defn remove-children-paths [paths]
(loop [a []
[p & ps] paths]
(cond
(seq ps) (recur
(conj a p)
(remove #(.startsWith % p) ps))
(remove #(.startsWith ^Path % ^Path p) ps))
p (conj a p)
:else a)))

Expand Down Expand Up @@ -93,14 +97,14 @@

(defn stop! [watch]
(doto
(:thread watch) .interrupt .join)
^Thread (:thread watch) .interrupt .join)
(-> watch :watcher watcher/stop!))

(defn file? [_ {:keys [file]}]
(.isFile file))
(.isFile ^File file))

(defn directory? [_ {:keys [file]}]
(.isDirectory file))
(.isDirectory ^File file))

(defn created? [_ {:keys [kind]}]
(= kind :create))
Expand Down
11 changes: 5 additions & 6 deletions src/main/clojure/hawk/watcher.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
(java.nio.file FileSystems Path Paths StandardWatchEventKinds WatchEvent WatchEvent$Modifier WatchKey WatchService)
(com.barbarysoftware.watchservice StandardWatchEventKind WatchableFile)))

(set! *warn-on-reflection* true)

(def barbary-watch-event-kinds
{:create StandardWatchEventKind/ENTRY_CREATE
:modify StandardWatchEventKind/ENTRY_MODIFY
Expand Down Expand Up @@ -48,10 +50,7 @@
:context (.context e)})

(defn- handle-watch-event! [this ^Path path {:keys [kind context]}]
(let [file (.. path
(resolve context)
toFile
getCanonicalFile)]
(let [file (.getCanonicalFile (.toFile ^Path (resolve path (cast Path context))))]
(if (and (= kind :create) (.isDirectory file))
(do
(register! this (.toPath file) [:create :modify :delete])
Expand All @@ -64,7 +63,7 @@
file-seq
(map (fn [f]
{:kind :create
:file (.getCanonicalFile f)}))))
:file (.getCanonicalFile ^File f)}))))
[{:kind kind
:file file}])))

Expand Down Expand Up @@ -110,7 +109,7 @@
context (.context event)]
{:file (-> context str io/file .getCanonicalFile)
:kind ((map-invert barbary-watch-event-kinds) kind)}))
#(.reset ^com.barbarysoftware.watchservice.WatchService %))
#(.reset ^com.barbarysoftware.watchservice.WatchKey %))
(catch com.barbarysoftware.watchservice.ClosedWatchServiceException _ _ nil)))
:stop! (fn [^com.barbarysoftware.watchservice.WatchService this]
(.close this))})
Expand Down