X-Git-Url: https://njoseph.me/gitweb/babashka-scripts.git/blobdiff_plain/ead73d37568631a52a3759179a8fde48939d4afa..57e618f2b43f69a280d1e73cb06cb470bec24e56:/update-babashka diff --git a/update-babashka b/update-babashka new file mode 100755 index 0000000..bf4bb8e --- /dev/null +++ b/update-babashka @@ -0,0 +1,55 @@ +#! /usr/bin/env bb +; -*- mode: clojure -*- + +(require '[cheshire.core :as json] + '[org.httpkit.client :as http] + '[babashka.fs :as fs] + '[clojure.java.io :as io] + '[lib :refer [download-binary expand-home run-cmd]]) + +(defn babashka-latest-version + [] + (-> @(http/get "https://api.github.com/repos/babashka/babashka/tags") + :body + (json/parse-string true) + first + :name + (subs 1))) + +(defn get-download-url + [version architecture] + (str "https://github.com/babashka/babashka/releases/download/v" + version + "/babashka-" + version + "-" + architecture + ".tar.gz")) + +(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)] + (download-binary url output))) + +(defn update-babashka + [] + (let [architecture "linux-amd64" ;; TODO support other architectures + tarball "babashka.tar.gz" + destination (expand-home "~/bin") + version (babashka-latest-version)] + (println (str "Latest version is " version)) + (check-latest version) + (println "Updating...") + (download-latest version architecture tarball) + (run-cmd ["tar" "-xzf" tarball "--directory" destination]) + (fs/set-posix-file-permissions (str destination "/bb") "rwxr-xr-x") + (io/delete-file tarball) + (println "Done!"))) + +(when (= *file* (System/getProperty "babashka.file")) (update-babashka))