From ec37ad05d576d79b3dc2d05b2bcae632a93e8c35 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 5 Jan 2022 22:03:02 +0530 Subject: [PATCH] Shift from standalone scripts to babashka tasks - Similar utilities are grouped together by namespace. - Only update-babashka is still a script. TODO maybe. --- README.md | 28 ++---------- bb.edn | 21 +++++++++ git-pull-all | 39 ---------------- install-deb | 8 ---- install-deb-gh | 8 ---- utils.clj => scripts/debian.clj | 30 ++---------- ebook-to-audio-book => scripts/ebooks.clj | 33 +++++++------ scripts/git.clj | 56 +++++++++++++++++++++++ lib.clj => scripts/lib.clj | 0 scripts/meta.clj | 3 ++ scripts/utils.clj | 15 ++++++ 11 files changed, 119 insertions(+), 122 deletions(-) create mode 100644 bb.edn delete mode 100755 git-pull-all delete mode 100755 install-deb delete mode 100755 install-deb-gh rename utils.clj => scripts/debian.clj (66%) rename ebook-to-audio-book => scripts/ebooks.clj (52%) mode change 100755 => 100644 create mode 100755 scripts/git.clj rename lib.clj => scripts/lib.clj (100%) create mode 100644 scripts/meta.clj create mode 100644 scripts/utils.clj diff --git a/README.md b/README.md index 9986907..6d27301 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,23 @@ # Babashka Scripts -Miscellaneous [babashka](https://babashka.org "babashka website") scripts I wrote for my own personal use. +Miscellaneous [babashka](https://babashka.org "babashka website") scripts written for my own personal use. ## Requirements -babashka (>= 0.2.9) +babashka (>= 0.4.0) ## 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. +The list of available commands can be found by running `bb tasks` in the directory where the file `bb.edn` is present. -``` sh -export BABASHKA_PRELOADS='(run! load-file (map #(str (System/getProperty "user.home") "/dev/babashka-scripts/" %) ["lib.clj" "utils.clj"]))' -export BABASHKA_PRELOADS=$BABASHKA_PRELOADS" (require '[utils :refer :all])" -export PATH=$PATH:$HOME/dev/babashka-scripts -``` - -Some useful aliases and functions. +Some tasks can also take arguments. ``` sh -alias gup="bb -e '(git-pull-rebase-branch)'" -alias bbr="rlwrap bb --repl" +bb ebook-to-audiobook the-great-gatsby.epub ``` - -## Organization - -`lib.clj` contains functions that the other scripts might use. It must be loaded -first. - -`utils.clj` contains standalone utilities that can be executed directly. These are -convenient to use through shell aliases. - -The remaining Clojure files are executable scripts. diff --git a/bb.edn b/bb.edn new file mode 100644 index 0000000..350ba7e --- /dev/null +++ b/bb.edn @@ -0,0 +1,21 @@ +{:paths ["scripts"] + :tasks + {gup {:requires ([git :refer [git-pull-rebase-branch]]) + :doc "Do a git pull and rebase branch with master in a given directory." + :task (git-pull-rebase-branch (get (into [] *command-line-args*) 0))} + + gpa {:requires ([git :refer [git-pull-all]]) + :doc "Runs `git pull` on all the git repositories in a given directory." + :task (git-pull-all (get (into [] *command-line-args*) 0))} + + ebook-to-audiobook {:requires ([ebooks :refer [convert-ebook-to-audiobook]]) + :doc "A utility to listen to your ebooks using TTS programs. Only tested on macOS with epub files." + :task (convert-ebook-to-audiobook (get (into [] *command-line-args*) 0))} + + install-deb {:requires ([debian :refer [install-deb-from-url]]) + :doc "Install a Debian package given a direct URL to the .deb file." + :task (install-deb-from-url (get (into [] *command-line-args*) 0))} + + install-deb-gh {:requires ([debian :refer [install-deb-from-GitHub]]) + :doc "Install a Debian package given a GitHub URL." + :task (install-deb-from-GitHub (get (into [] *command-line-args*) 0))}}} diff --git a/git-pull-all b/git-pull-all deleted file mode 100755 index 78c4000..0000000 --- a/git-pull-all +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env bb -; -*- mode: clojure -*- - -; Runs `git pull` on all the git repositories in a directory - -(require '[babashka.process :as p] '[clojure.java.io :as io]) - -(def default-root ".") - -(defn has-git-repo - [dir] - (first (filter #(= ".git" %) - (map #(.getName %) - (filter #(.isDirectory %) (.listFiles (io/file dir))))))) - -(defn list-dirs - [root] - (filter #(has-git-repo %) - (filter #(.isDirectory %) (.listFiles (io/file root))))) - -(defn git-pull [dir] (p/process ["git" "-C" dir "pull" "--rebase"])) - -(when (= *file* (System/getProperty "babashka.file")) - (let [root (get (into [] *command-line-args*) 0 default-root) - dirs (list-dirs root) - pulls (->> root - list-dirs - (map git-pull) - doall) - outputs (map #(->> % - p/check - :out - slurp) - pulls)] - ;; Print corresponding directory name when pulling - ;; Skip printing already up to date repos - (doseq [[dir out] (filter #(not (.contains (second %) "is up to date.")) - (map vector dirs outputs))] - (println (str dir "\n" out))))) diff --git a/install-deb b/install-deb deleted file mode 100755 index 2aebfc8..0000000 --- a/install-deb +++ /dev/null @@ -1,8 +0,0 @@ -#! /usr/bin/env bb -; -*- mode: clojure -*- - -; A utility to install a deb package from a URL - -(require '[lib :refer [unixify]] '[utils :refer [install-deb]]) - -(unixify install-deb) diff --git a/install-deb-gh b/install-deb-gh deleted file mode 100755 index 3aa8ef1..0000000 --- a/install-deb-gh +++ /dev/null @@ -1,8 +0,0 @@ -#! /usr/bin/env bb -; -*- mode: clojure -*- - -; A utility to install a deb package from a GitHub URL - -(require '[lib :refer [unixify]] '[utils :refer [install-deb-from-GitHub]]) - -(unixify install-deb-from-GitHub) diff --git a/utils.clj b/scripts/debian.clj similarity index 66% rename from utils.clj rename to scripts/debian.clj index a19a9aa..6c07149 100644 --- a/utils.clj +++ b/scripts/debian.clj @@ -1,35 +1,11 @@ -(ns utils +(ns debian (:require [clojure.string :as str] [clojure.java.io :as io] [cheshire.core :as json] [org.httpkit.client :as http] [lib :refer [download-binary run-cmd]])) -(defn git-pull-rebase-branch - "Do git pull and rebase branch with master" - [] - (let [current-branch (str/trim (run-cmd ["git" "branch" "--show-current"]))] - (print (run-cmd ["git" "pull" "--rebase"])) - (when (not (contains? #{"master" "main"} current-branch)) - (run! print - (map run-cmd - '[["git" "checkout" "master"] ["git" "pull" "--rebase"] - ["git" "checkout" "-"] ["git" "rebase" "master"]]))))) - -(defn run-seq - "Run a sequence of shell commands on a sequence of arguments. - - Examples: - - 1. Play and delete each video from a folder - ls | bb -i '(run-seq [\"mpv\" \"rm\"] *input*)' - " - [commands arguments] - (doseq [argument arguments - command commands] - (println (run-cmd [command argument])))) - -(defn install-deb +(defn install-deb-from-url "Install a Debian package given a direct URL to the .deb file." [url] (println "Downloading deb package...") @@ -69,4 +45,4 @@ assets (get-assets api-url) deb-url (find-url-to-deb assets)] ;; Download and install Debian package - (install-deb deb-url)))) + (install-deb-from-url deb-url)))) diff --git a/ebook-to-audio-book b/scripts/ebooks.clj old mode 100755 new mode 100644 similarity index 52% rename from ebook-to-audio-book rename to scripts/ebooks.clj index ce2fe4b..45944ce --- a/ebook-to-audio-book +++ b/scripts/ebooks.clj @@ -1,24 +1,24 @@ -#! /usr/bin/env bb -; -*- mode: clojure -*- +(ns ebooks + (:require [clojure.java.io :as io] + [clojure.string :refer [split]] + [lib :refer [run-cmd extract-file-from-zip]])) -; A utility to listen to your ebooks using TTS programs +; Utilities for dealing with ebooks ; -; This utility only works on macOS for now. - -(require '[clojure.java.io :as io] - '[clojure.string :refer [split]] - '[lib :refer [run-cmd extract-file-from-zip unixify]]) +; Dependencies: +; 1. calibre +; 2. ffmpeg +; 3. OS-dependent TTS software ;; TODO Check if all required utilities are installed -;; TODO Allow voice selection - -(let [default-voice (run-cmd ["defaults" "read" "com.apple.speech.voice.prefs" "SelectedVoiceName"])] - (println (str "Will read using the default voice " default-voice))) -;; TODO Use festival-tts or say depending on OS - -(defn- convert +;; TODO Use tools.cli to provide more options +(defn convert-ebook-to-audiobook + "A utility to listen to your ebooks using TTS programs. This utility only works on macOS for now." [book-file] + ;; TODO Allow voice selection + (let [default-voice (run-cmd ["defaults" "read" "com.apple.speech.voice.prefs" "SelectedVoiceName"])] + (println (str "Will read using the default voice " default-voice))) (let [title (first (split book-file #"\.")) txtz-file (str title ".txtz") txt-file (str title ".txt") @@ -28,11 +28,10 @@ (run-cmd ["ebook-convert" book-file txtz-file]) (extract-file-from-zip txtz-file "index.txt" txt-file) (println "Converting text to audio. Don't hold your breath!") + ;; TODO Use festival-tts or say depending on OS (run-cmd ["say" "-f" txt-file "-o" aiff-file]) (println "Creating mp3 file...") (run-cmd ["ffmpeg" "-i" aiff-file mp3-file]) (println "Cleaning up...") (run! io/delete-file [txtz-file txt-file aiff-file]) (println (str "Done! Check out " mp3-file)))) - -(unixify convert) diff --git a/scripts/git.clj b/scripts/git.clj new file mode 100755 index 0000000..b84cffb --- /dev/null +++ b/scripts/git.clj @@ -0,0 +1,56 @@ +(ns git + (:require [babashka.process :as p] + [clojure.java.io :as io] + [clojure.string :as str] + [lib :refer [run-cmd]])) + +; Utilities for git operations + +(def default-root ".") + +(defn- has-git-repo + [dir] + (first (filter #(= ".git" %) + (map #(.getName %) + (filter #(.isDirectory %) (.listFiles (io/file dir))))))) + +(defn- list-dirs + [root] + (filter #(has-git-repo %) + (filter #(.isDirectory %) (.listFiles (io/file root))))) + +(defn- git-pull [dir] + (p/process ["git" "-C" dir "pull" "--rebase"])) + +(defn git-pull-all + "Runs `git-pull` on all the git repositories in a directory" + [root] + (let [dirs (list-dirs root) + pulls (->> root + list-dirs + (map git-pull) + doall) + outputs (map #(->> % + p/check + :out + slurp) + pulls)] + ;; Print corresponding directory name when pulling + ;; Skip printing already up to date repos + (doseq [[dir out] (filter #(not (.contains (second %) "is up to date.")) + (map vector dirs outputs))] + (println (str dir "\n" out))))) + + +(defn git-pull-rebase-branch + "Do git pull and rebase branch with master" + [dir] + (let [current-branch (str/trim (run-cmd ["git" "branch" "--show-current"]))] + (git-pull dir) + (when (not (contains? #{"master" "main"} current-branch)) + (run! print + (map run-cmd + '[["git" "checkout" "master"] + ["git" "pull" "--rebase"] + ["git" "checkout" "-"] + ["git" "rebase" "master"]]))))) diff --git a/lib.clj b/scripts/lib.clj similarity index 100% rename from lib.clj rename to scripts/lib.clj diff --git a/scripts/meta.clj b/scripts/meta.clj new file mode 100644 index 0000000..57b7886 --- /dev/null +++ b/scripts/meta.clj @@ -0,0 +1,3 @@ +(ns meta) + +; Operations on babashka itself or scripts in this repository. diff --git a/scripts/utils.clj b/scripts/utils.clj new file mode 100644 index 0000000..8ea7664 --- /dev/null +++ b/scripts/utils.clj @@ -0,0 +1,15 @@ +(ns utils + (:require [lib :refer [run-cmd]])) + +(defn run-seq + "Run a sequence of shell commands on a sequence of arguments. + + Examples: + + 1. Play and delete each video from a folder + ls | bb -i '(run-seq [\"mpv\" \"rm\"] *input*)' + " + [commands arguments] + (doseq [argument arguments + command commands] + (println (run-cmd [command argument])))) -- 2.43.0