]> njoseph.me Git - babashka-scripts.git/commitdiff
Shift from standalone scripts to babashka tasks
authorJoseph Nuthalapati <njoseph@riseup.net>
Wed, 5 Jan 2022 16:33:02 +0000 (22:03 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Wed, 5 Jan 2022 16:34:22 +0000 (22:04 +0530)
- Similar utilities are grouped together by namespace.
- Only update-babashka is still a script. TODO maybe.

README.md
bb.edn [new file with mode: 0644]
git-pull-all [deleted file]
install-deb [deleted file]
install-deb-gh [deleted file]
scripts/debian.clj [moved from utils.clj with 66% similarity]
scripts/ebooks.clj [moved from ebook-to-audio-book with 52% similarity, mode: 0644]
scripts/git.clj [new file with mode: 0755]
scripts/lib.clj [moved from lib.clj with 100% similarity]
scripts/meta.clj [new file with mode: 0644]
scripts/utils.clj [new file with mode: 0644]

index 9986907c7521e7861d9ce5d546e4c96d77c38763..6d2730165bc099b94c8de16c1b58d0c4d1625f9f 100644 (file)
--- 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 (file)
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 (executable)
index 78c4000..0000000
+++ /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 (executable)
index 2aebfc8..0000000
+++ /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 (executable)
index 3aa8ef1..0000000
+++ /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)
similarity index 66%
rename from utils.clj
rename to scripts/debian.clj
index a19a9aa63e63ac795cc22b5458e7f53eb5739ef8..6c07149cf15a83c06fc6d9f51812b6b7f33ea6dd 100644 (file)
--- a/utils.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))))
old mode 100755 (executable)
new mode 100644 (file)
similarity index 52%
rename from ebook-to-audio-book
rename to scripts/ebooks.clj
index ce2fe4b..45944ce
@@ -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")
     (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 (executable)
index 0000000..b84cffb
--- /dev/null
@@ -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"]])))))
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 (file)
index 0000000..57b7886
--- /dev/null
@@ -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 (file)
index 0000000..8ea7664
--- /dev/null
@@ -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]))))