|
1 | 1 | (ns com.github.clojure-lsp.intellij.server
|
| 2 | + (:require |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.string :as string] |
| 5 | + [com.github.clojure-lsp.intellij.config :as config] |
| 6 | + [com.github.clojure-lsp.intellij.db :as db] |
| 7 | + [com.github.clojure-lsp.intellij.notification :as notification] |
| 8 | + [com.github.clojure-lsp.intellij.server :as server] |
| 9 | + [com.github.ericdallo.clj4intellij.logger :as logger] |
| 10 | + [com.github.ericdallo.clj4intellij.tasks :as tasks]) |
2 | 11 | (:import
|
3 | 12 | [com.intellij.openapi.project Project]
|
4 |
| - [com.redhat.devtools.lsp4ij LanguageServerManager])) |
| 13 | + [com.intellij.openapi.project Project] |
| 14 | + [com.redhat.devtools.lsp4ij LanguageServerManager] |
| 15 | + [java.io File] |
| 16 | + [java.util.zip ZipInputStream])) |
5 | 17 |
|
6 | 18 | (set! *warn-on-reflection* true)
|
7 | 19 |
|
8 |
| -(defn start-server! [^Project project] |
| 20 | +(def ^:private latest-version-uri |
| 21 | + "https://raw.githubusercontent.com/clojure-lsp/clojure-lsp/master/lib/resources/CLOJURE_LSP_RELEASED_VERSION") |
| 22 | + |
| 23 | +(def ^:private download-artifact-uri |
| 24 | + "https://github.com/clojure-lsp/clojure-lsp/releases/download/%s/%s") |
| 25 | + |
| 26 | +(defn ^:private os-name [] |
| 27 | + (let [os-name (string/lower-case (System/getProperty "os.name" "generic"))] |
| 28 | + (cond |
| 29 | + (string/includes? os-name "win") :windows |
| 30 | + (string/includes? os-name "mac") :macos |
| 31 | + :else :linux))) |
| 32 | + |
| 33 | +(defn ^:private os-arch [] |
| 34 | + (if (= "aarch64" (System/getProperty "os.arch")) |
| 35 | + :aarch64 |
| 36 | + :amd64)) |
| 37 | + |
| 38 | +(def ^:private artifacts |
| 39 | + {:linux {:amd64 "clojure-lsp-native-static-linux-amd64.zip" |
| 40 | + :aarch64 "clojure-lsp-native-linux-aarch64.zip"} |
| 41 | + :macos {:amd64 "clojure-lsp-native-macos-amd64.zip" |
| 42 | + :aarch64 "clojure-lsp-native-macos-aarch64.zip"} |
| 43 | + :windows {:amd64 "clojure-lsp-native-windows-amd64.zip"}}) |
| 44 | + |
| 45 | +(defn ^:private unzip-file [input ^File dest-file] |
| 46 | + (with-open [stream (-> input io/input-stream ZipInputStream.)] |
| 47 | + (loop [entry (.getNextEntry stream)] |
| 48 | + (when entry |
| 49 | + (if (.isDirectory entry) |
| 50 | + (when-not (.exists dest-file) |
| 51 | + (.mkdirs dest-file)) |
| 52 | + (clojure.java.io/copy stream dest-file)) |
| 53 | + (recur (.getNextEntry stream)))))) |
| 54 | + |
| 55 | +(defn ^:private download-server! [project indicator ^File download-path ^File server-version-path latest-version] |
| 56 | + (tasks/set-progress indicator "LSP: Downloading clojure-lsp") |
| 57 | + (let [platform (os-name) |
| 58 | + arch (os-arch) |
| 59 | + artifact-name (get-in artifacts [platform arch]) |
| 60 | + uri (format download-artifact-uri latest-version artifact-name) |
| 61 | + dest-server-file download-path |
| 62 | + dest-path (.getCanonicalPath dest-server-file)] |
| 63 | + (logger/info "Downloading clojure-lsp from" uri) |
| 64 | + (unzip-file (io/input-stream uri) dest-server-file) |
| 65 | + (doto (io/file dest-server-file) |
| 66 | + (.setWritable true) |
| 67 | + (.setReadable true) |
| 68 | + (.setExecutable true)) |
| 69 | + (spit server-version-path latest-version) |
| 70 | + (db/assoc-in project [:downloaded-server-path] dest-path) |
| 71 | + (logger/info "Downloaded clojure-lsp to" dest-path))) |
| 72 | + |
| 73 | +(defn install-server [project installed-fn] |
| 74 | + (tasks/run-background-task! |
| 75 | + project |
| 76 | + "Clojure LSP download" |
| 77 | + (fn [indicator] |
| 78 | + (tasks/set-progress indicator "Clojure LSP: downloading server") |
| 79 | + (let [download-path (config/download-server-path) |
| 80 | + server-version-path (config/download-server-version-path) |
| 81 | + latest-version* (delay (try (string/trim (slurp latest-version-uri)) (catch Exception _ nil))) |
| 82 | + custom-server-path (db/get-in project [:settings :server-path])] |
| 83 | + (cond |
| 84 | + custom-server-path |
| 85 | + (installed-fn {:status :installed :path custom-server-path}) |
| 86 | + |
| 87 | + (and (.exists download-path) |
| 88 | + (or (not @latest-version*) ;; on network connection issues we use any downloaded server |
| 89 | + (= (try (slurp server-version-path) (catch Exception _ :error-checking-local-version)) |
| 90 | + @latest-version*))) |
| 91 | + (installed-fn {:status :installed :path download-path}) |
| 92 | + |
| 93 | + @latest-version* |
| 94 | + (do (download-server! project indicator download-path server-version-path @latest-version*) |
| 95 | + (installed-fn {:status :installed :path download-path})) |
| 96 | + |
| 97 | + :else |
| 98 | + (notification/show-notification! {:project project |
| 99 | + :type :error |
| 100 | + :title "Clojure LSP download error" |
| 101 | + :message "There is no server downloaded and there was a network issue to download the latest one"})))))) |
| 102 | + |
| 103 | +(defn start! [^Project project] |
9 | 104 | (.start (LanguageServerManager/getInstance project) "clojure-lsp"))
|
10 | 105 |
|
11 | 106 | (defn shutdown! [^Project project]
|
|
0 commit comments