Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Created March 22, 2019 13:20
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 geraldodev/d4e329534b7cd68c679b0ae29ab78efb to your computer and use it in GitHub Desktop.
Save geraldodev/d4e329534b7cd68c679b0ae29ab78efb to your computer and use it in GitHub Desktop.
(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