Created
March 22, 2019 13:20
-
-
Save geraldodev/d4e329534b7cd68c679b0ae29ab78efb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns launch-reply.core | |
(:require | |
[clojure.java.io :as io] | |
) | |
(:import | |
[java.nio.file WatchService StandardWatchEventKinds FileSystems Path Paths] | |
[java.io File] | |
) | |
) | |
(def port-file-name ".nrepl-port") | |
(defn launch-reply | |
[port] | |
(require '[reply.main]) | |
(let [cmd `(reply.main/-main "--attach" ~(str "localhost:" port))] | |
(println cmd) | |
(eval cmd)) | |
) | |
(defn watch-and-launch | |
[] | |
(let [watchService (.newWatchService (FileSystems/getDefault)) | |
cwd (File. ".") | |
directory (Paths/get (.toURI cwd)) | |
watchKey (.register directory watchService (into-array [StandardWatchEventKinds/ENTRY_MODIFY])) | |
content (atom nil) | |
] | |
(if (.exists (io/file port-file-name)) | |
(launch-reply (slurp port-file-name)) | |
(do | |
(println "Watching" (.getCanonicalPath cwd) "for" port-file-name) | |
;; from https://gist.github.com/yogthos/911e6aba9802ceacd83c#file-file_watcher-clj-L30 | |
(while (not @content) | |
(let [k (.take watchService)] | |
(doseq [event (.pollEvents k)] | |
(when (= port-file-name (.toString (.getFileName (.context event)))) | |
(reset! content (slurp (.toFile (.context event)))) | |
(println port-file-name "written with" @content) | |
) | |
))) | |
(launch-reply @content) | |
)) | |
)) | |
(defn -main [& args] | |
(watch-and-launch)) | |
(comment | |
(Paths/get ".") | |
(.get Paths ".") | |
(println StandardWatchEventKinds/ENTRY_CREATE) | |
(into-array [StandardWatchEventKinds/ENTRY_CREATE]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment