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