a449a7c3a5db5bab9cafbaeae74e34b3d649aff0
[babashka-scripts.git] / update-babashka.clj
1 #! /usr/bin/env bb
2
3 (require '[clojure.java.shell :refer [sh]]
4 '[cheshire.core :as json]
5 '[babashka.curl :as curl]
6 '[babashka.fs :as fs])
7
8 (defn babashka-latest-version
9 []
10 (-> (curl/get "https://api.github.com/repos/babashka/babashka/tags")
11 :body
12 (json/parse-string true)
13 first
14 :name
15 (subs 1)))
16
17 (defn download-binary
18 [url destination]
19 (io/copy (:body (curl/get url {:as :stream})) (io/file destination)))
20
21 (defn get-download-url
22 [version architecture]
23 (str "https://github.com/babashka/babashka/releases/download/v"
24 version
25 "/babashka-"
26 version
27 "-"
28 architecture
29 ".zip"))
30
31 (defn check-latest
32 [version]
33 (when (= (System/getProperty "babashka.version") version)
34 (println "Already using the latest version.")
35 (System/exit 0)))
36
37 (defn download-latest
38 [version architecture output]
39 (let [url (get-download-url version architecture)]
40 (println (str "Latest version is " version))
41 (download-binary url output)))
42
43 (when (= *file* (System/getProperty "babashka.file"))
44 (let [architecture "linux-static-amd64" ;; TODO support multiple
45 ;; architectures
46 zip-file "babashka.zip"
47 destination (expand-home "~/bin/bb")
48 version (babashka-latest-version)]
49 (check-latest version)
50 (download-latest version architecture zip-file)
51 (extract-file-from-zip zip-file "bb" destination)
52 (fs/set-posix-file-permissions destination "rwxr-xr-x")
53 (io/delete-file zip-file)))