From: Joseph Nuthalapati Date: Sat, 13 Feb 2021 04:43:40 +0000 (+0530) Subject: Create and use namespace for lib.clj X-Git-Url: https://njoseph.me/gitweb/babashka-scripts.git/commitdiff_plain/67d5a15a20df70f416cf78efb2137344f239da2f Create and use namespace for lib.clj Signed-off-by: Joseph Nuthalapati --- diff --git a/ebook-to-audio-book.clj b/ebook-to-audio-book.clj index 0d17855..39da869 100755 --- a/ebook-to-audio-book.clj +++ b/ebook-to-audio-book.clj @@ -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 diff --git a/install-deb.clj b/install-deb.clj index 63b91df..c8990a8 100755 --- a/install-deb.clj +++ b/install-deb.clj @@ -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 70fef53..1f88070 100644 --- 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 diff --git a/update-babashka.clj b/update-babashka.clj index b14d0b9..afcaff1 100755 --- a/update-babashka.clj +++ b/update-babashka.clj @@ -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))