]> njoseph.me Git - babashka-scripts.git/blame - update-babashka
git: minor changes
[babashka-scripts.git] / update-babashka
CommitLineData
f1d839f5 1#! /usr/bin/env bb
57e618f2 2; -*- mode: clojure -*-
f1d839f5 3
67d5a15a 4(require '[cheshire.core :as json]
9da8be80 5 '[org.httpkit.client :as http]
67d5a15a
JN
6 '[babashka.fs :as fs]
7 '[clojure.java.io :as io]
10c4979d 8 '[lib :refer [download-binary expand-home run-cmd]])
f1d839f5 9
0b0f2aa3
JN
10(defn babashka-latest-version
11 []
9da8be80 12 (-> @(http/get "https://api.github.com/repos/babashka/babashka/tags")
f1d839f5
JN
13 :body
14 (json/parse-string true)
15 first
16 :name
17 (subs 1)))
18
f1d839f5
JN
19(defn get-download-url
20 [version architecture]
0b0f2aa3
JN
21 (str "https://github.com/babashka/babashka/releases/download/v"
22 version
23 "/babashka-"
24 version
25 "-"
26 architecture
10c4979d 27 ".tar.gz"))
f1d839f5
JN
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)]
f1d839f5
JN
38 (download-binary url output)))
39
67d5a15a
JN
40(defn update-babashka
41 []
42 (let [architecture "linux-amd64" ;; TODO support other architectures
10c4979d
JN
43 tarball "babashka.tar.gz"
44 destination (expand-home "~/bin")
f448b4b2 45 version (babashka-latest-version)]
9da8be80 46 (println (str "Latest version is " version))
f448b4b2 47 (check-latest version)
9da8be80 48 (println "Updating...")
10c4979d
JN
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)
9da8be80 53 (println "Done!")))
67d5a15a
JN
54
55(when (= *file* (System/getProperty "babashka.file")) (update-babashka))