X-Git-Url: https://njoseph.me/gitweb/babashka-scripts.git/blobdiff_plain/f7a7c8852fc7f3a47f858ad4fbf6b177735f52c0..f1d839f5e616cf6f3b121c3b34a4fd16b2200a8c:/update-babashka.clj diff --git a/update-babashka.clj b/update-babashka.clj new file mode 100755 index 0000000..56ece2d --- /dev/null +++ b/update-babashka.clj @@ -0,0 +1,48 @@ +#! /usr/bin/env bb + +(require '[clojure.java.shell :refer [sh]] + '[cheshire.core :as json] + '[babashka.curl :as curl] + '[babashka.fs :as fs]) + +(defn babashka-latest-version [] + (-> (curl/get "https://api.github.com/repos/babashka/babashka/tags") + :body + (json/parse-string true) + first + :name + (subs 1))) + +(defn download-binary + [url destination] + (io/copy + (:body (curl/get url {:as :stream})) + (io/file destination))) + +(defn get-download-url + [version architecture] + (str "https://github.com/babashka/babashka/releases/download/v" version "/babashka-" + version "-" architecture ".zip")) + +(defn check-latest + [version] + (when (= (System/getProperty "babashka.version") version) + (println "Already using the latest version.") + (System/exit 0))) + +(defn download-latest + [version architecture output] + (let [url (get-download-url version architecture)] + (println (str "Latest version is " version)) + (download-binary url output))) + +(= *file* (System/getProperty "babashka.file") + (let [architecture "linux-static-amd64" ;; TODO support multiple architectures + zip-file "babashka.zip" + destination (expand-home "~/bin/bb") + version (babashka-latest-version)] + (check-latest version) + (download-latest version architecture zip-file) + (extract-file-from-zip zip-file "bb" destination) + (fs/set-posix-file-permissions "/home/joseph/bin/bb" "rwxr-xr-x") + (io/delete-file zip-file)))