Create and use namespace for lib.clj
[babashka-scripts.git] / update-babashka.clj
1 #! /usr/bin/env bb
2
3 (require '[cheshire.core :as json]
4 '[org.httpkit.client :as http]
5 '[babashka.fs :as fs]
6 '[clojure.java.io :as io]
7 '[lib :refer [download-binary expand-home extract-file-from-zip]])
8
9 (defn babashka-latest-version
10 []
11 (-> @(http/get "https://api.github.com/repos/babashka/babashka/tags")
12 :body
13 (json/parse-string true)
14 first
15 :name
16 (subs 1)))
17
18 (defn get-download-url
19 [version architecture]
20 (str "https://github.com/babashka/babashka/releases/download/v"
21 version
22 "/babashka-"
23 version
24 "-"
25 architecture
26 ".zip"))
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)]
37 (download-binary url output)))
38
39 (defn update-babashka
40 []
41 (let [architecture "linux-amd64" ;; TODO support other architectures
42 zip-file "babashka.zip"
43 destination (expand-home "~/bin/bb")
44 version (babashka-latest-version)]
45 (println (str "Latest version is " version))
46 (check-latest version)
47 (println "Updating...")
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")
51 (io/delete-file zip-file)
52 (println "Done!")))
53
54 (when (= *file* (System/getProperty "babashka.file")) (update-babashka))