Last active
October 27, 2024 18:21
-
-
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)
This file contains 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
(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