Skip to content

Instantly share code, notes, and snippets.

@sogaiu
Last active October 27, 2024 18:21
Show Gist options
  • Save sogaiu/0c542ee238858adfe33f1200e3a86634 to your computer and use it in GitHub Desktop.
Save sogaiu/0c542ee238858adfe33f1200e3a86634 to your computer and use it in GitHub Desktop.
which - find binary on PATH accounting for PATHEXT on Windows - (JVM Clojure and babashka)
(require '[clojure.java.io :as cji])
(require '[clojure.string :as cs])
(defn which
[bin-name]
(let [paths (clojure.string/split (or (System/getenv "PATH") "")
(re-pattern (System/getProperty "path.separator")))
;; for windows
pathexts (clojure.string/split (or (System/getenv "PATHEXT") "")
(re-pattern (System/getProperty "path.separator")))]
;; adapted work by taylorwood
(first
(for [path (distinct paths)
pathext pathexts
:let [exe-file (clojure.java.io/file path (str bin-name pathext))]
:when (.exists exe-file)]
(.getAbsolutePath exe-file)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment