Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

update mage alias + fix it for fish (#66526)

bryan2aec7ccbe1a3
diff --git a/bb.edn b/bb.edn
index 047a1bc522e..ecc59f1a60e 100644
--- a/bb.edn
+++ b/bb.edn
@@ -21,11 +21,13 @@
alias
{:doc "Installs 'mage' alias and autocomplete"
:examples [["./bin/mage alias" "Prints instructions to install the 'mage' alias and autocomplete"]
- ["./bin/mage alias zsh" "Prints shell script to put into .zshrc for zsh users"]]
+ ["./bin/mage alias install fish" "Installs mage alias for fish shell"]
+ ["./bin/mage alias print zsh" "Prints shell script to put into .zshrc for zsh users"]]
:requires [[mage.alias :as alias]]
:arg-schema [:or
[:tuple]
- [:tuple [:enum "bash" "zsh" "fish"]]]
+ [:tuple [:enum "bash" "zsh" "fish"]]
+ [:tuple [:enum "install" "print"] [:enum "bash" "zsh" "fish"]]]
:task (task! (alias/instructions parsed))}
cljfmt-staged
diff --git a/mage/resources/bash_alias.sh b/mage/resources/bash_alias.sh
index 7b5edd0119e..b1cacffe228 100644
--- a/mage/resources/bash_alias.sh
+++ b/mage/resources/bash_alias.sh
@@ -1,5 +1,3 @@
-
-## MAGE (Metabase Automation Genius Engine) alias + autocomplete. [auto-installed]
export MB_DIR="{{mb-dir}}"
# alias:
mage() {
@@ -10,4 +8,3 @@ _bb_tasks() {
COMPREPLY=( $(compgen -W "$(bb tasks |tail -n +3 |cut -f1 -d ' ')" -- ${COMP_WORDS[COMP_CWORD]}) );
}
complete -f -F _bb_tasks mage
-## END MAGE [auto-installed]
diff --git a/mage/resources/fish_alias.sh b/mage/resources/fish_alias.sh
deleted file mode 100644
index 73a9edc1262..00000000000
--- a/mage/resources/fish_alias.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-
-## MAGE (Metabase Automation Genius Engine) alias + autocomplete. [auto-installed]
-set -x MB_DIR {{mb-dir}}
-function mage
- cd $MB_DIR && exec ./bin/mage $argv
-end
-# autocomplete:
-function __bb_complete_tasks
- if not test "$__bb_tasks"
- set -g __bb_tasks (bb tasks |tail -n +3 |cut -f1 -d ' ')
- end
-
- printf "%s
-" $__bb_tasks
-end
-
-complete -c mage -a "(__bb_complete_tasks)" -d 'tasks'
-## END MAGE [auto-installed]
diff --git a/mage/resources/fish_completions.fish b/mage/resources/fish_completions.fish
new file mode 100644
index 00000000000..6e34d9551f2
--- /dev/null
+++ b/mage/resources/fish_completions.fish
@@ -0,0 +1,11 @@
+function __mage_complete_tasks
+ if not test "$__mage_tasks"
+ if test -n "$MB_DIR"
+ set -g __mage_tasks (cd $MB_DIR && bb tasks 2>/dev/null |tail -n +3 |cut -f1 -d ' ')
+ end
+ end
+
+ printf "%s\n" $__mage_tasks
+end
+
+complete -c mage -a "(__mage_complete_tasks)" -d 'tasks'
diff --git a/mage/resources/fish_function.fish b/mage/resources/fish_function.fish
new file mode 100644
index 00000000000..b79c45f8275
--- /dev/null
+++ b/mage/resources/fish_function.fish
@@ -0,0 +1,4 @@
+set -x MB_DIR {{mb-dir}}
+function mage --description 'Metabase Automation Genius Engine'
+ cd $MB_DIR && ./bin/mage $argv
+end
diff --git a/mage/resources/zsh_alias.sh b/mage/resources/zsh_alias.sh
index e070eb15c4f..84a3c1181bb 100644
--- a/mage/resources/zsh_alias.sh
+++ b/mage/resources/zsh_alias.sh
@@ -1,5 +1,3 @@
-
-## MAGE (Metabase Automation Genius Engine) [auto-installed]
export MB_DIR="{{mb-dir}}"
# alias:
mage() {
@@ -13,4 +11,3 @@ _bb_tasks() {
_files # autocomplete filenames as well
}
compdef _bb_tasks mage
-## END MAGE [auto-installed]
diff --git a/mage/src/mage/alias.clj b/mage/src/mage/alias.clj
index e888bb7559a..e754f07282a 100644
--- a/mage/src/mage/alias.clj
+++ b/mage/src/mage/alias.clj
@@ -1,23 +1,92 @@
(ns mage.alias
- (:require [clojure.string :as str]
- [mage.color :as c]
- [mage.util :as u]
- [selmer.parser :as selmer]))
-
-(defn zsh-instructions []
- (selmer/render
- (slurp "mage/resources/zsh_alias.sh")
- {:mb-dir u/project-root-directory}))
-
-(defn bash-instructions []
- (selmer/render
- (slurp "mage/resources/bash_alias.sh")
- {:mb-dir u/project-root-directory}))
-
-(defn fish-instructions []
- (selmer/render
- (slurp "mage/resources/fish_alias.sh")
- {:mb-dir u/project-root-directory}))
+ (:require
+ [babashka.fs :as fs]
+ [clojure.string :as str]
+ [mage.color :as c]
+ [mage.util :as u]
+ [selmer.parser :as selmer]))
+
+;; DO NOT change these, it will break idempotent alias installation
+(def ^:private marker-start "## MAGE (Metabase Automation Genius Engine)")
+(def ^:private marker-end "## END MAGE [auto-installed]")
+
+(defn- render-template [template-file]
+ (selmer/render (slurp template-file) {:mb-dir u/project-root-directory}))
+
+(defn- read-file-safe [path] (when (fs/exists? path) (slurp path)))
+
+(defn- find-marker-positions [content]
+ (let [lines (str/split-lines content)
+ start-idx (first (keep-indexed #(when (str/includes? %2 marker-start) %1) lines))
+ end-idx (when start-idx
+ (first (keep-indexed #(when (and (> %1 start-idx)
+ (str/includes? %2 marker-end)) %1) lines)))]
+ (when (and start-idx end-idx)
+ {:start start-idx :end end-idx})))
+
+(defn- replace-between-markers [content new-content]
+ (if-let [{:keys [start end]} (find-marker-positions content)]
+ (let [lines (str/split-lines content)
+ before (take start lines)
+ after (drop (inc end) lines)]
+ (str/join "\n" (concat before [new-content] after)))
+ ;; No markers found, append to end
+ (str content "\n" new-content)))
+
+(defn- install-to-file [file-path new-content]
+ (let [existing-content (or (read-file-safe file-path) "")
+ new-content-with-markers (str marker-start "\n" (str/replace new-content #"\n$" "") "\n" marker-end)
+ updated-content (replace-between-markers existing-content new-content-with-markers)]
+ (fs/create-dirs (fs/parent file-path))
+ (spit file-path updated-content)
+ file-path))
+
+(defn- expand-home [path]
+ (str/replace path #"^~" (System/getProperty "user.home")))
+
+(defn- zsh-instructions []
+ (render-template "mage/resources/zsh_alias.sh"))
+
+(defn- bash-instructions []
+ (render-template "mage/resources/bash_alias.sh"))
+
+(defn- fish-instructions []
+ (render-template "mage/resources/fish_alias.sh"))
+
+(defn- fish-function []
+ (render-template "mage/resources/fish_function.fish"))
+
+(defn- fish-completions []
+ (render-template "mage/resources/fish_completions.fish"))
+
+(defn- install-zsh []
+ (let [zshrc (expand-home "~/.zshrc")
+ content (zsh-instructions)]
+ (install-to-file zshrc content)
+ (println (c/green "✓") "Installed mage alias to" zshrc)
+ (println "Run:" (c/cyan "source ~/.zshrc") "to activate")))
+
+(defn- install-bash []
+ (let [bashrc (expand-home "~/.bashrc")
+ content (bash-instructions)]
+ (install-to-file bashrc content)
+ (println (c/green "✓") "Installed mage alias to" bashrc)
+ (println "Run:" (c/cyan "source ~/.bashrc") "to activate")))
+
+(defn- install-fish
+ "Fish needs 2 files to get updated."
+ []
+ (let [conf-dir (expand-home "~/.config/fish/conf.d")
+ completions-dir (expand-home "~/.config/fish/completions")
+ function-file (str conf-dir "/mage.fish")
+ completions-file (str completions-dir "/mage.fish")
+ function-content (fish-function)
+ completions-content (fish-completions)]
+ (install-to-file function-file function-content)
+ (install-to-file completions-file completions-content)
+ (println (c/green "✓") "Installed mage function to" function-file)
+ (println (c/green "✓") "Installed mage completions to" completions-file)
+ (println "Fish will automatically load these on next shell start, or run:" (c/cyan "exec fish"))))
(defn print-instructions
"prints instructions to setup tab-completion for mage."
@@ -25,21 +94,43 @@
(println
(str/join "\n"
["This will install the 'mage' alias as well as tab-completion for your shell."
- "Quick Setup:"
+ ""
+ "Installation (automatically manages existing installations):"
""
(str "For " (c/magenta "ZSH") ":")
- " ./bin/mage alias zsh >> ~/.zshrc && source ~/.zshrc"
+ " ./bin/mage alias install zsh"
""
(str "For " (c/red "Bash") ":")
- " ./bin/mage alias bash >> ~/.bashrc && source ~/.bashrc"
+ " ./bin/mage alias install bash"
""
(str "For " (c/cyan "Fish") ":")
- " ./bin/mage alias fish >> ~/.config/fish/completions/bb.fish && source ~/.config/fish/completions/bb.fish"
+ " ./bin/mage alias install fish"
+ ""
+ "Or to just print the alias (for manual installation):"
+ " ./bin/mage alias print <shell-type>"
""])))
-(defn instructions [{[shell-type] :arguments}]
- (case shell-type
- "zsh" (println (zsh-instructions))
- "bash" (println (bash-instructions))
- "fish" (println (fish-instructions))
- (print-instructions)))
+(defn instructions [{[action shell-type] :arguments}]
+ (case action
+ "install" (case shell-type
+ "zsh" (install-zsh)
+ "bash" (install-bash)
+ "fish" (install-fish)
+ (print-instructions))
+ "print" (case shell-type
+ "zsh" (do (println (c/cyan "# Add to ~/.zshrc:"))
+ (println (zsh-instructions)))
+ "bash" (do (println (c/cyan "# Add to ~/.bashrc:"))
+ (println (bash-instructions)))
+ "fish" (do (println (c/cyan "# Add to ~/.config/fish/conf.d/mage.fish:"))
+ (println (fish-function))
+ (println)
+ (println (c/cyan "# Add to ~/.config/fish/completions/mage.fish:"))
+ (println (fish-completions)))
+ (print-instructions))
+ ;; Backward compatibility: no action means print
+ (case action
+ "zsh" (println (zsh-instructions))
+ "bash" (println (bash-instructions))
+ "fish" (println (fish-instructions))
+ (print-instructions))))
diff --git a/mage/test/mage/alias_test.clj b/mage/test/mage/alias_test.clj
new file mode 100644
index 00000000000..de7b4db1b8b
--- /dev/null
+++ b/mage/test/mage/alias_test.clj
@@ -0,0 +1,8 @@
+(ns mage.alias-test
+ (:require
+ [clojure.test :refer [deftest is]]
+ [mage.alias]))
+
+(deftest noone-changed-the-markers
+ (is (= "## MAGE (Metabase Automation Genius Engine)" mage.alias/marker-start))
+ (is (= "## END MAGE [auto-installed]" mage.alias/marker-end)))
diff --git a/mage/test/mage/core_test.clj b/mage/test/mage/core_test.clj
index 8c058c447b9..c5cd923bed5 100644
--- a/mage/test/mage/core_test.clj
+++ b/mage/test/mage/core_test.clj
@@ -2,17 +2,14 @@
(:require
[babashka.tasks :as bt]
[clojure.string :as str]
- [clojure.test :as t :refer [deftest is testing]]
+ [clojure.test :refer [deftest is testing]]
+ [mage.alias-test]
[mage.token-scan-test]
[mage.util :as u]
[mage.util-test]))
(set! *warn-on-reflection* true)
-(comment
- mage.token-scan-test/keep-me
- mage.util-test/keep-me)
-
(deftest bin-mage-has-help-test
(doseq [help-cmds [[] [" "] [" "] ["-h"] ["--help"] [" -h"] [" --help"] [" -h"] [" --help"]]
:let [cmd (str "./bin/mage " (str/join " " help-cmds))