|
| 1 | +(ns leiningen.clojure-lsp.binary |
| 2 | + (:refer-clojure :exclude [run!]) |
| 3 | + (:require |
| 4 | + [babashka.process :as process] |
| 5 | + [clojure.java.io :as io] |
| 6 | + [clojure.string :as string]) |
| 7 | + (:import |
| 8 | + [java.io BufferedReader File] |
| 9 | + [java.util.zip ZipInputStream])) |
| 10 | + |
| 11 | +(set! *warn-on-reflection* true) |
| 12 | + |
| 13 | +(defn ^:private global-cache-dir [] |
| 14 | + (let [cache-home (or (System/getenv "XDG_CACHE_HOME") |
| 15 | + (io/file (System/getProperty "user.home") ".cache"))] |
| 16 | + (io/file cache-home "lein-clojure-lsp"))) |
| 17 | + |
| 18 | +(def ^:private download-artifact-uri |
| 19 | + "https://github.com/clojure-lsp/clojure-lsp/releases/download/%s/%s") |
| 20 | + |
| 21 | +(defn ^:private os-name [] |
| 22 | + (let [os-name (string/lower-case (System/getProperty "os.name" "generic"))] |
| 23 | + (cond |
| 24 | + (string/includes? os-name "win") :windows |
| 25 | + (string/includes? os-name "mac") :macos |
| 26 | + :else :linux))) |
| 27 | + |
| 28 | +(defn ^:private os-arch [] |
| 29 | + (if (= "aarch64" (System/getProperty "os.arch")) |
| 30 | + :aarch64 |
| 31 | + :amd64)) |
| 32 | + |
| 33 | +(def ^:private artifacts |
| 34 | + {:linux {:amd64 "clojure-lsp-native-static-linux-amd64.zip" |
| 35 | + :aarch64 "clojure-lsp-native-linux-aarch64.zip"} |
| 36 | + :macos {:amd64 "clojure-lsp-native-macos-amd64.zip" |
| 37 | + :aarch64 "clojure-lsp-native-macos-aarch64.zip"} |
| 38 | + :windows {:amd64 "clojure-lsp-native-windows-amd64.zip"}}) |
| 39 | + |
| 40 | +(defn ^:private unzip-file [input ^File dest-file] |
| 41 | + (with-open [stream (-> input io/input-stream ZipInputStream.)] |
| 42 | + (loop [entry (.getNextEntry stream)] |
| 43 | + (when entry |
| 44 | + (if (.isDirectory entry) |
| 45 | + (when-not (.exists dest-file) |
| 46 | + (.mkdirs dest-file)) |
| 47 | + (clojure.java.io/copy stream dest-file)) |
| 48 | + (recur (.getNextEntry stream)))))) |
| 49 | + |
| 50 | +(defn ^:private download! [^File download-path version] |
| 51 | + (let [platform (os-name) |
| 52 | + arch (os-arch) |
| 53 | + artifact-name (get-in artifacts [platform arch]) |
| 54 | + uri (format download-artifact-uri version artifact-name)] |
| 55 | + (io/make-parents download-path) |
| 56 | + (unzip-file (io/input-stream uri) download-path) |
| 57 | + (doto download-path |
| 58 | + (.setWritable true) |
| 59 | + (.setReadable true) |
| 60 | + (.setExecutable true)))) |
| 61 | + |
| 62 | +(defn ^:private server-version [] |
| 63 | + (string/trim (slurp (io/resource "CLOJURE_LSP_VERSION")))) |
| 64 | + |
| 65 | +(defn ^:private server-path ^File [] |
| 66 | + (io/file (global-cache-dir) "clojure-lsp")) |
| 67 | + |
| 68 | +(defn ^:private run-lsp! [^File path args] |
| 69 | + (let [p (process/process {:cmd (concat [(.getAbsolutePath path)] args)})] |
| 70 | + (future |
| 71 | + (with-open [out-rdr ^BufferedReader (io/reader (:out p))] |
| 72 | + (loop [] |
| 73 | + (when-let [line (.readLine out-rdr)] |
| 74 | + (println line) |
| 75 | + (recur))))) |
| 76 | + (future |
| 77 | + (with-open [out-rdr ^BufferedReader (io/reader (:err p))] |
| 78 | + (binding [*out* *err*] |
| 79 | + (loop [] |
| 80 | + (when-let [line (.readLine out-rdr)] |
| 81 | + (println line) |
| 82 | + (recur)))))) |
| 83 | + @p)) |
| 84 | + |
| 85 | +(defn run! [args] |
| 86 | + (let [server-path (server-path) |
| 87 | + server-version (server-version)] |
| 88 | + (when-not (.exists server-path) |
| 89 | + (binding [*out* *err*] |
| 90 | + (println "Downloading and caching clojure-lsp to" (str server-path))) |
| 91 | + (let [t (System/currentTimeMillis)] |
| 92 | + (download! server-path server-version) |
| 93 | + (binding [*out* *err*] |
| 94 | + (println (format "Downloaded clojure-lsp took %sms" (- (System/currentTimeMillis) t)))))) |
| 95 | + (run-lsp! server-path args))) |
0 commit comments