]> njoseph.me Git - babashka-scripts.git/commitdiff
Create and use namespace for lib.clj
authorJoseph Nuthalapati <njoseph@riseup.net>
Sat, 13 Feb 2021 04:43:40 +0000 (10:13 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Sat, 13 Feb 2021 04:43:40 +0000 (10:13 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
ebook-to-audio-book.clj
install-deb.clj
lib.clj
update-babashka.clj

index 0d178554e600bd2af2949d036641937f31a782b5..39da869ed28aff7630f3ebac1b1d483e218ddbc2 100755 (executable)
@@ -2,9 +2,9 @@
 
 ; A utility to listen to your ebooks using TTS programs
 
-(require '[babashka.process :as p]
-         '[clojure.java.io :as io]
-         '[clojure.string :refer [split]])
+(require '[clojure.java.io :as io]
+         '[clojure.string :refer [split]]
+         '[lib :refer [run-cmd extract-file-from-zip]])
 
 ;; TODO Check if all required utilities are installed
 
index 63b91df066ffb439d63f53d0460353c207238629..c8990a84785333cc242cd5cbe898b1f46bc9e5fb 100755 (executable)
@@ -2,8 +2,7 @@
 
 ; Install a deb package from URL
 
-(require '[babashka.process :as p]
-         '[clojure.java.io :as io])
+(require '[clojure.java.io :as io] '[lib :refer [download-binary run-cmd]])
 
 (when (= *file* (System/getProperty "babashka.file"))
   (let [url (first *command-line-args*)]
diff --git a/lib.clj b/lib.clj
index 70fef53590648bb52015649d4ad872c4bc326737..1f88070ba456a7b9e516567a17180a9c3a8cf26c 100644 (file)
--- a/lib.clj
+++ b/lib.clj
@@ -1,8 +1,11 @@
 ; Common utility functions used by scripts
 
-(import '[java.nio.file Files FileSystems CopyOption StandardCopyOption])
-
-(require '[babashka.process :as p] '[org.httpkit.client :as http])
+(ns lib
+  (:require [babashka.process :as p]
+            [org.httpkit.client :as http]
+            [clojure.java.io :as io]
+            [clojure.string :refer [replace-first]])
+  (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
 
 (defn run-cmd
   [command]
@@ -14,7 +17,6 @@
 (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 []))]
@@ -25,7 +27,7 @@
 (defn expand-home
   [s]
   (if (.startsWith s "~")
-    (clojure.string/replace-first s "~" (System/getProperty "user.home"))
+    (replace-first s "~" (System/getProperty "user.home"))
     s))
 
 (defn download-binary
index b14d0b9b69ab281b49d33d2b1d115267f16a93c7..afcaff1e819d49dad5e199c21dfc32a5ab1ab799 100755 (executable)
@@ -1,9 +1,10 @@
 #! /usr/bin/env bb
 
-(require '[clojure.java.shell :refer [sh]]
-         '[cheshire.core :as json]
+(require '[cheshire.core :as json]
          '[org.httpkit.client :as http]
-         '[babashka.fs :as fs])
+         '[babashka.fs :as fs]
+         '[clojure.java.io :as io]
+         '[lib :refer [download-binary expand-home extract-file-from-zip]])
 
 (defn babashka-latest-version
   []
@@ -35,9 +36,9 @@
   (let [url (get-download-url version architecture)]
     (download-binary url output)))
 
-(when (= *file* (System/getProperty "babashka.file"))
-  (let [architecture "linux-amd64"  ;; TODO support multiple
-                                           ;; architectures
+(defn update-babashka
+  []
+  (let [architecture "linux-amd64"  ;; TODO support other architectures
         zip-file "babashka.zip"
         destination (expand-home "~/bin/bb")
         version (babashka-latest-version)]
@@ -49,3 +50,5 @@
     (fs/set-posix-file-permissions destination "rwxr-xr-x")
     (io/delete-file zip-file)
     (println "Done!")))
+
+(when (= *file* (System/getProperty "babashka.file")) (update-babashka))