]> njoseph.me Git - babashka-scripts.git/commitdiff
Add babashka self-updater script
authorJoseph Nuthalapati <njoseph@riseup.net>
Fri, 12 Feb 2021 08:15:30 +0000 (13:45 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Fri, 12 Feb 2021 08:15:30 +0000 (13:45 +0530)
Also refactored common functions into lib.clj

Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
README.md [new file with mode: 0644]
ebook-to-audio-book.clj
lib.clj [new file with mode: 0644]
update-babashka.clj [new file with mode: 0755]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..f02c678
--- /dev/null
+++ b/README.md
@@ -0,0 +1,27 @@
+# Babashka Scripts
+
+Miscellaneous [babashka](https://babashka.org "babashka website") scripts I wrote for my own personal use.
+
+## Requirements
+
+babashka (>= 0.2.9)
+
+## Usage
+
+Clone this repository.
+
+``` sh
+cd ~/dev
+git clone https://njoseph.me/gitweb/babashka-scripts.git
+```
+
+Add the following lines to your shell configuration file.
+
+``` sh
+export BABASHKA_PRELOADS='(load-file (str (System/getProperty "user.home") "/dev/babashka-scripts/lib.clj"))'
+export PATH=$PATH:$HOME/dev/babashka-scripts
+```
+
+## Organization
+
+`lib.clj` contains functions that the other scripts might use. It must be loaded first.
index 8305c2e92b45429be875c6f40df61ba7d228a17c..c09b8d00aaf3bad531407d4e25d5d41649a9b4ae 100755 (executable)
@@ -2,8 +2,6 @@
 
 ; A utility to listen to your ebooks using TTS programs
 
-(import '[java.nio.file Files FileSystems CopyOption])
-
 (require '[babashka.process :as p]
          '[clojure.java.io :as io]
          '[clojure.string :refer [split]])
@@ -15,7 +13,6 @@
 
 ;; TODO Use festival-tts or say depending on OS
 
-
 (defn run-cmd
   [command]
   (->> command
        :out
        slurp))
 
-(defn extract-file-from-zip
-  [zip-file-name source destination]
-  (let [zip-file (io/file zip-file-name)
-        src (io/file source)
-        dest (io/file destination)
-        fs (FileSystems/newFileSystem (.toPath zip-file) nil)
-        file-in-zip (.getPath fs source (into-array String []))]
-    (Files/copy file-in-zip (.toPath dest)
-                (into-array CopyOption []))))
-
 (let [book-file (first *command-line-args*)
       title (first (split book-file #"\."))
       txtz-file (str title ".txtz")
diff --git a/lib.clj b/lib.clj
new file mode 100644 (file)
index 0000000..86dc702
--- /dev/null
+++ b/lib.clj
@@ -0,0 +1,18 @@
+;
+
+(import '[java.nio.file Files FileSystems CopyOption StandardCopyOption])
+
+(defn extract-file-from-zip
+  [zip-file-name source destination]
+  (let [zip-file (io/file zip-file-name)
+        src (io/file source)
+        dest (io/file destination)
+        fs (FileSystems/newFileSystem (.toPath zip-file) nil)
+        file-in-zip (.getPath fs source (into-array String []))]
+    (Files/copy file-in-zip (.toPath dest)
+                (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING]))))
+
+(defn expand-home [s]
+  (if (.startsWith s "~")
+    (clojure.string/replace-first s "~" (System/getProperty "user.home"))
+    s))
diff --git a/update-babashka.clj b/update-babashka.clj
new file mode 100755 (executable)
index 0000000..56ece2d
--- /dev/null
@@ -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)))