From 57e618f2b43f69a280d1e73cb06cb470bec24e56 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Fri, 30 Apr 2021 14:16:12 +0530 Subject: [PATCH 01/13] Remove .clj suffix for scripts Signed-off-by: Joseph Nuthalapati --- README.md | 8 -------- ebook-to-audio-book.clj => ebook-to-audio-book | 1 + git-pull-all.clj => git-pull-all | 1 + install-deb | 8 ++++++++ install-deb-gh | 8 ++++++++ update-babashka.clj => update-babashka | 1 + 6 files changed, 19 insertions(+), 8 deletions(-) rename ebook-to-audio-book.clj => ebook-to-audio-book (97%) rename git-pull-all.clj => git-pull-all (97%) create mode 100755 install-deb create mode 100755 install-deb-gh rename update-babashka.clj => update-babashka (98%) diff --git a/README.md b/README.md index a5c24b4..9986907 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,6 @@ Some useful aliases and functions. ``` sh alias gup="bb -e '(git-pull-rebase-branch)'" alias bbr="rlwrap bb --repl" - -function bb-wrap { - expr="($1 \"$2\")" - bb -e $expr -} - -alias install-deb="bb-wrap install-deb" -alias install-deb-gh="bb-wrap install-deb-from-GitHub" ``` ## Organization diff --git a/ebook-to-audio-book.clj b/ebook-to-audio-book similarity index 97% rename from ebook-to-audio-book.clj rename to ebook-to-audio-book index 48eb115..ec12e96 100755 --- a/ebook-to-audio-book.clj +++ b/ebook-to-audio-book @@ -1,4 +1,5 @@ #! /usr/bin/env bb +; -*- mode: clojure -*- ; A utility to listen to your ebooks using TTS programs diff --git a/git-pull-all.clj b/git-pull-all similarity index 97% rename from git-pull-all.clj rename to git-pull-all index 647db21..6b0de16 100755 --- a/git-pull-all.clj +++ b/git-pull-all @@ -1,4 +1,5 @@ #! /usr/bin/env bb +; -*- mode: clojure -*- ; Runs `git pull` on all the git repositories in a directory diff --git a/install-deb b/install-deb new file mode 100755 index 0000000..2aebfc8 --- /dev/null +++ b/install-deb @@ -0,0 +1,8 @@ +#! /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 new file mode 100755 index 0000000..3aa8ef1 --- /dev/null +++ b/install-deb-gh @@ -0,0 +1,8 @@ +#! /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/update-babashka.clj b/update-babashka similarity index 98% rename from update-babashka.clj rename to update-babashka index c122fb4..bf4bb8e 100755 --- a/update-babashka.clj +++ b/update-babashka @@ -1,4 +1,5 @@ #! /usr/bin/env bb +; -*- mode: clojure -*- (require '[cheshire.core :as json] '[org.httpkit.client :as http] -- 2.43.0 From 269ff235d32dde012c671f258fcbdeba6504144f Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Mon, 29 Nov 2021 11:02:56 +0530 Subject: [PATCH 02/13] git-pull-all: Fix failure on non-git directories Signed-off-by: Joseph Nuthalapati --- git-pull-all | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/git-pull-all b/git-pull-all index 6b0de16..78c4000 100755 --- a/git-pull-all +++ b/git-pull-all @@ -7,7 +7,16 @@ (def default-root ".") -(defn list-dirs [root] (filter #(.isDirectory %) (.listFiles (io/file 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"])) -- 2.43.0 From 746d64040956017457d38f0bf59649e96d0ab449 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 5 Jan 2022 18:52:39 +0530 Subject: [PATCH 03/13] ebook-to-audio-book: Get the correct default voice --- ebook-to-audio-book | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ebook-to-audio-book b/ebook-to-audio-book index ec12e96..ce2fe4b 100755 --- a/ebook-to-audio-book +++ b/ebook-to-audio-book @@ -2,15 +2,18 @@ ; -*- mode: clojure -*- ; A utility to listen to your ebooks using TTS programs +; +; 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]]) ;; TODO Check if all required utilities are installed - ;; TODO Allow voice selection -(println "Selected voice is Samantha") + +(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 -- 2.43.0 From ec37ad05d576d79b3dc2d05b2bcae632a93e8c35 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 5 Jan 2022 22:03:02 +0530 Subject: [PATCH 04/13] 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 From 300f9af5a37252590c2be331c86f7932059f1d7e Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Thu, 6 Jan 2022 08:08:20 +0530 Subject: [PATCH 05/13] List tasks in the README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 6d27301..8cbda08 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,16 @@ git clone https://njoseph.me/gitweb/babashka-scripts.git The list of available commands can be found by running `bb tasks` in the directory where the file `bb.edn` is present. +``` sh +The following tasks are available: + +gup Do a git pull and rebase branch with master in a given directory. +gpa Runs `git pull` on all the git repositories in a given directory. +ebook-to-audiobook A utility to listen to your ebooks using TTS programs. Only tested on macOS with epub files. +install-deb Install a Debian package given a direct URL to the .deb file. +install-deb-gh Install a Debian package given a GitHub URL. +``` + Some tasks can also take arguments. ``` sh -- 2.43.0 From 0b0a16bdc1598bc64f3cc4ac8195865525b2e9f2 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sun, 9 Jan 2022 14:34:34 +0530 Subject: [PATCH 06/13] Add .lsp/ folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3c11bf6..6faa94b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .clj-kondo/ +.lsp/ -- 2.43.0 From 66364757cbb0f829838573b642ccdacce9a1a726 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sun, 9 Jan 2022 18:08:58 +0530 Subject: [PATCH 07/13] Add utility to run tasks in the current directory --- README.md | 13 +++++++++++++ bb-aliases.sh | 8 ++++++++ scripts/utils.clj | 12 +++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 bb-aliases.sh diff --git a/README.md b/README.md index 8cbda08..6abd22a 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,16 @@ Some tasks can also take arguments. ``` sh bb ebook-to-audiobook the-great-gatsby.epub ``` + +There are a utility file that makes running the tasks easier. Load it up first. +You can also add the following line with the absolute path to the file in your "~/.profile" file. + +``` sh +source bb-aliases.sh +``` + +*Example:* To pull all the repositories in a directory, run + +``` sh +bbe gpa +``` diff --git a/bb-aliases.sh b/bb-aliases.sh new file mode 100644 index 0000000..5e82ad8 --- /dev/null +++ b/bb-aliases.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +export BABASHKA_PRELOADS='(doseq [fil ["lib.clj" "utils.clj"]] (load-file (str (System/getProperty "user.home") "/dev/babashka-scripts/scripts/" fil)))' +export BABASHKA_PRELOADS=$BABASHKA_PRELOADS" (require '[utils :refer :all])" + +bbe() { + bb -e "(run-task-in-cwd \"$1\")" +} diff --git a/scripts/utils.clj b/scripts/utils.clj index 8ea7664..1de2099 100644 --- a/scripts/utils.clj +++ b/scripts/utils.clj @@ -1,5 +1,6 @@ (ns utils - (:require [lib :refer [run-cmd]])) + (:require [lib :refer [expand-home run-cmd]] + [babashka.process :refer [process]])) (defn run-seq "Run a sequence of shell commands on a sequence of arguments. @@ -13,3 +14,12 @@ (doseq [argument arguments command commands] (println (run-cmd [command argument])))) + +(defn run-task-in-cwd + "Run a bb task from this repository in the current working directory." + [task] + (let [current-dir (System/getenv "PWD") + babashka-scripts-dir (expand-home "~/dev/babashka-scripts/")] + (print (-> (process ["bb" task current-dir] {:dir babashka-scripts-dir}) + :out + slurp)))) -- 2.43.0 From acafde64a0503bc04bcdeff3bc8cd05e414eae93 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Mon, 11 Jul 2022 13:29:54 +0530 Subject: [PATCH 08/13] Add aliases for nushell --- bb-aliases.nu | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 bb-aliases.nu diff --git a/bb-aliases.nu b/bb-aliases.nu new file mode 100644 index 0000000..7519e2d --- /dev/null +++ b/bb-aliases.nu @@ -0,0 +1,8 @@ +#!/usr/bin/env nu + +let-env BABASHKA_PRELOADS = '(doseq [fil ["lib.clj" "utils.clj"]] (load-file (str (System/getProperty "user.home") "/dev/babashka-scripts/scripts/" fil)))' +let-env BABASHKA_PRELOADS = $env.BABASHKA_PRELOADS + " (require '[utils :refer :all])" + +def bbe [task] { + bb -e $"\(run-task-in-cwd \'($task)\)" +} -- 2.43.0 From 30f43df559bd79e663919dd8d957b36303c7a8d8 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Mon, 11 Jul 2022 14:29:09 +0530 Subject: [PATCH 09/13] Remove babashka preloads --- README.md | 20 +++++++++++++++++--- bb-aliases.nu | 3 --- bb-aliases.sh | 3 --- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6abd22a..fb44973 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,27 @@ Some tasks can also take arguments. bb ebook-to-audiobook the-great-gatsby.epub ``` -There are a utility file that makes running the tasks easier. Load it up first. -You can also add the following line with the absolute path to the file in your "~/.profile" file. +## Shell aliases ``` sh -source bb-aliases.sh +# bash, zsh +source /path/to/babashka-scripts/bb-aliases.sh +alias task = bb --config ~/dev/babashka-scripts/bb.edn + +# nu +source /path/to/babashka-scripts/bb-aliases.nu +alias task = bb --config ~/dev/babashka-scripts/bb.edn +``` + +*Example:* To pull all the repositories in a directory, run + +``` sh +task gpa . ``` +There is a utility `bbe` to run tasks in the current working directory in `bb-aliases.sh`. + + *Example:* To pull all the repositories in a directory, run ``` sh diff --git a/bb-aliases.nu b/bb-aliases.nu index 7519e2d..ed6c67a 100644 --- a/bb-aliases.nu +++ b/bb-aliases.nu @@ -1,8 +1,5 @@ #!/usr/bin/env nu -let-env BABASHKA_PRELOADS = '(doseq [fil ["lib.clj" "utils.clj"]] (load-file (str (System/getProperty "user.home") "/dev/babashka-scripts/scripts/" fil)))' -let-env BABASHKA_PRELOADS = $env.BABASHKA_PRELOADS + " (require '[utils :refer :all])" - def bbe [task] { bb -e $"\(run-task-in-cwd \'($task)\)" } diff --git a/bb-aliases.sh b/bb-aliases.sh index 5e82ad8..70349bb 100644 --- a/bb-aliases.sh +++ b/bb-aliases.sh @@ -1,8 +1,5 @@ #!/usr/bin/env bash -export BABASHKA_PRELOADS='(doseq [fil ["lib.clj" "utils.clj"]] (load-file (str (System/getProperty "user.home") "/dev/babashka-scripts/scripts/" fil)))' -export BABASHKA_PRELOADS=$BABASHKA_PRELOADS" (require '[utils :refer :all])" - bbe() { bb -e "(run-task-in-cwd \"$1\")" } -- 2.43.0 From 0df5525be671d19747f8cccb8291f84d8b2ee932 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 17 Aug 2022 17:26:51 +0530 Subject: [PATCH 10/13] git: Fix switching back to current branch in `gup` --- scripts/git.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/git.clj b/scripts/git.clj index b84cffb..aeacbbe 100755 --- a/scripts/git.clj +++ b/scripts/git.clj @@ -52,5 +52,5 @@ (map run-cmd '[["git" "checkout" "master"] ["git" "pull" "--rebase"] - ["git" "checkout" "-"] + ["git" "checkout" current-branch] ["git" "rebase" "master"]]))))) -- 2.43.0 From 181e14e7660c23ce20208b18dab7e021e7251e54 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 19 Oct 2022 10:40:06 +0530 Subject: [PATCH 11/13] Add a diagnostic to check if `task` alias works --- bb.edn | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bb.edn b/bb.edn index 350ba7e..f285ffa 100644 --- a/bb.edn +++ b/bb.edn @@ -1,6 +1,9 @@ {:paths ["scripts"] :tasks - {gup {:requires ([git :refer [git-pull-rebase-branch]]) + {test {:doc "Test whether the 'task' alias is working." + :task (println "Yes, it's working!")} + + 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))} -- 2.43.0 From e1035b9f7c0fbf7819409ca96eed64d92d3ef2d1 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sat, 26 Nov 2022 05:34:37 +0530 Subject: [PATCH 12/13] git: Fix function git-pull-rebase --- scripts/git.clj | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/scripts/git.clj b/scripts/git.clj index aeacbbe..370b315 100755 --- a/scripts/git.clj +++ b/scripts/git.clj @@ -6,8 +6,6 @@ ; Utilities for git operations -(def default-root ".") - (defn- has-git-repo [dir] (first (filter #(= ".git" %) @@ -19,8 +17,7 @@ (filter #(has-git-repo %) (filter #(.isDirectory %) (.listFiles (io/file root))))) -(defn- git-pull [dir] - (p/process ["git" "-C" dir "pull" "--rebase"])) +(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" @@ -34,23 +31,21 @@ p/check :out slurp) - pulls)] + 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))] + (map vector dirs outputs))] (println (str dir "\n" out))))) (defn git-pull-rebase-branch "Do git pull and rebase branch with master" [dir] + (run-cmd ["git" "pull" "--rebase"]) (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" current-branch] - ["git" "rebase" "master"]]))))) + [["git" "checkout" "master"] ["git" "pull" "--rebase"] + ["git" "checkout" current-branch] ["git" "rebase" "master"]]))))) -- 2.43.0 From 4dcdd4a9d879b2787550e849673e7e967878fd92 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sat, 26 Nov 2022 05:44:13 +0530 Subject: [PATCH 13/13] git: minor changes --- scripts/git.clj | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/git.clj b/scripts/git.clj index 370b315..e722c08 100755 --- a/scripts/git.clj +++ b/scripts/git.clj @@ -17,10 +17,13 @@ (filter #(has-git-repo %) (filter #(.isDirectory %) (.listFiles (io/file root))))) -(defn- git-pull [dir] (p/process ["git" "-C" dir "pull" "--rebase"])) +(defn- git-pull + "Do a git pull with rebase." + [dir] + (p/process ["git" "-C" dir "pull" "--rebase"])) (defn git-pull-all - "Runs `git-pull` on all the git repositories in a directory" + "Runs `git-pull` on all the git repositories in a directory." [root] (let [dirs (list-dirs root) pulls (->> root @@ -40,9 +43,9 @@ (defn git-pull-rebase-branch - "Do git pull and rebase branch with master" + "Do git pull and rebase branch with master." [dir] - (run-cmd ["git" "pull" "--rebase"]) + (git-pull dir) (let [current-branch (str/trim (run-cmd ["git" "branch" "--show-current"]))] (when (not (contains? #{"master" "main"} current-branch)) (run! print -- 2.43.0