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