|
3 | 3 | (:require [babashka.fs :as fs] |
4 | 4 | [babashka.http-client :as http] |
5 | 5 | [cheshire.core :as json] |
| 6 | + [clojure.edn :as edn] |
6 | 7 | [clojure.java.io :as io] |
7 | 8 | [noum.daemon :as daemon] |
8 | 9 | [noum.paths :as paths] |
|
63 | 64 | {:expected expected :actual actual}))))) |
64 | 65 | (tui/eprintln " Warning: no .sha256 sidecar in release, skipping checksum verification."))) |
65 | 66 |
|
66 | | -(defn- current-version |
67 | | - "Read the installed JAR's version from the running daemon, or nil." |
| 67 | +(defn- jar-version |
| 68 | + "Read version.edn from the installed JAR, or nil." |
68 | 69 | [] |
69 | | - (when-let [{:keys [port]} (daemon/connection)] |
| 70 | + (when (installed?) |
70 | 71 | (try |
71 | | - (let [resp (http/get (str "http://127.0.0.1:" port "/health") |
72 | | - {:timeout 2000 :throw false})] |
73 | | - (when (= 200 (:status resp)) |
74 | | - (-> (json/parse-string (:body resp) true) :data :version))) |
| 72 | + (let [jar-url (java.net.URL. (str "jar:file:" paths/jar-path "!/version.edn"))] |
| 73 | + (with-open [in (.openStream jar-url)] |
| 74 | + (:version (edn/read-string (slurp in))))) |
75 | 75 | (catch Exception _ nil)))) |
76 | 76 |
|
77 | 77 | (defn download! |
|
84 | 84 | ((:stop s) "No JAR found in release") |
85 | 85 | (throw (ex-info (str "No JAR asset found in release " (:tag release)) {}))) |
86 | 86 | (let [remote-ver (:tag release) |
87 | | - local-ver (current-version)] |
| 87 | + local-ver (jar-version)] |
88 | 88 | (if (and local-ver (= (str "v" local-ver) remote-ver)) |
89 | 89 | (do ((:stop s) (str "Already at latest version (" remote-ver ").")) nil) |
90 | 90 | (do ((:stop s) (str "Found " remote-ver (when local-ver (str " (current: v" local-ver ")")))) |
|
98 | 98 | (verify-checksum! paths/jar-path (:assets release) (:name asset)) |
99 | 99 | paths/jar-path))))) |
100 | 100 |
|
| 101 | +(defn- stale? |
| 102 | + "True when the installed JAR version doesn't match the launcher version." |
| 103 | + [launcher-version] |
| 104 | + (when launcher-version |
| 105 | + (let [jar-ver (jar-version)] |
| 106 | + (and jar-ver (not= jar-ver launcher-version))))) |
| 107 | + |
101 | 108 | (defn ensure! |
102 | | - "Ensure noumenon.jar is installed. Download if not. Returns jar path." |
103 | | - [] |
104 | | - (if (installed?) |
105 | | - paths/jar-path |
106 | | - (do (tui/eprintln "First run: downloading noumenon.jar (~50MB) to ~/.noumenon/") |
107 | | - (download!)))) |
| 109 | + "Ensure noumenon.jar is installed and up to date. Returns jar path. |
| 110 | + When launcher-version is supplied, triggers download if the running |
| 111 | + JAR version differs." |
| 112 | + ([] (ensure! nil)) |
| 113 | + ([launcher-version] |
| 114 | + (if (installed?) |
| 115 | + (if (stale? launcher-version) |
| 116 | + (do (daemon/stop!) |
| 117 | + (download!) |
| 118 | + paths/jar-path) |
| 119 | + paths/jar-path) |
| 120 | + (do (tui/eprintln "First run: downloading noumenon.jar (~50MB) to ~/.noumenon/") |
| 121 | + (download!))))) |
0 commit comments