56ece2d17f3f567c8f317982910f1016cedd318b
[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 (-> (curl/get "https://api.github.com/repos/babashka/babashka/tags")
10 :body
11 (json/parse-string true)
12 first
13 :name
14 (subs 1)))
15
16 (defn download-binary
17 [url destination]
18 (io/copy
19 (:body (curl/get url {:as :stream}))
20 (io/file destination)))
21
22 (defn get-download-url
23 [version architecture]
24 (str "https://github.com/babashka/babashka/releases/download/v" version "/babashka-"
25 version "-" architecture ".zip"))
26
27 (defn check-latest
28 [version]
29 (when (= (System/getProperty "babashka.version") version)
30 (println "Already using the latest version.")
31 (System/exit 0)))
32
33 (defn download-latest
34 [version architecture output]
35 (let [url (get-download-url version architecture)]
36 (println (str "Latest version is " version))
37 (download-binary url output)))
38
39 (= *file* (System/getProperty "babashka.file")
40 (let [architecture "linux-static-amd64" ;; TODO support multiple architectures
41 zip-file "babashka.zip"
42 destination (expand-home "~/bin/bb")
43 version (babashka-latest-version)]
44 (check-latest version)
45 (download-latest version architecture zip-file)
46 (extract-file-from-zip zip-file "bb" destination)
47 (fs/set-posix-file-permissions "/home/joseph/bin/bb" "rwxr-xr-x")
48 (io/delete-file zip-file)))