- Similar utilities are grouped together by namespace.
- Only update-babashka is still a script. TODO maybe.
# 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.
--- /dev/null
+{: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))}}}
+++ /dev/null
-#! /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)))))
+++ /dev/null
-#! /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)
+++ /dev/null
-#! /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)
-(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...")
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))))
-#! /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)
--- /dev/null
+(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"]])))))
--- /dev/null
+(ns meta)
+
+; Operations on babashka itself or scripts in this repository.
--- /dev/null
+(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]))))