6e4c9df4a433849a2c636980dc317db0fba3a0f5

Author
James Eapen <jamespeapen@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(cli): add `plugin load` subcommand (#9872)

Fixes #9672

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/cli.zsh b/lib/cli.zsh
  2index 38e2f72f816be47abc47a659023c93ff21374db8..7e3e37be8ba39030583db468c1d0f82f7ce32963 100644
  3--- a/lib/cli.zsh
  4+++ b/lib/cli.zsh
  5@@ -37,7 +37,7 @@ function _omz {
  6       changelog) local -a refs
  7         refs=("${(@f)$(command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
  8         _describe 'command' refs ;;
  9-      plugin) subcmds=('info:Get plugin information' 'list:List plugins')
 10+      plugin) subcmds=('info:Get plugin information' 'list:List plugins' 'load:Load plugin(s)')
 11         _describe 'command' subcmds ;;
 12       pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
 13         _describe 'command' subcmds ;;
 14@@ -46,10 +46,26 @@ function _omz {
 15     esac
 16   elif (( CURRENT == 4 )); then
 17     case "$words[2]::$words[3]" in
 18-      plugin::info) compadd "$ZSH"/plugins/*/README.md(.N:h:t) \
 19-        "$ZSH_CUSTOM"/plugins/*/README.md(.N:h:t) ;;
 20-      theme::use) compadd "$ZSH"/themes/*.zsh-theme(.N:t:r) \
 21-        "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::) ;;
 22+      plugin::(info|load))
 23+        local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
 24+        _describe 'plugin' plugins ;;
 25+      theme::use)
 26+        local -aU themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
 27+        _describe 'theme' themes ;;
 28+    esac
 29+  elif (( CURRENT > 4 )); then
 30+    case "$words[2]::$words[3]" in
 31+      plugin::load)
 32+        local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
 33+
 34+        # Remove plugins already passed as arguments
 35+        # NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which
 36+        # has a space after them. This is to avoid removing plugins partially passed, which makes
 37+        # the completion not add a space after the completed plugin.
 38+        local -a args=(${words[4,$(( CURRENT - 1))]})
 39+        plugins=(${plugins:|args})
 40+
 41+        _describe 'plugin' plugins ;;
 42     esac
 43   fi
 44 
 45@@ -147,6 +163,7 @@ Available commands:
 46 
 47   info <plugin>   Get information of a plugin
 48   list            List all available Oh My Zsh plugins
 49+  load <plugin>   Load plugin(s)
 50 
 51 EOF
 52     return 1
 53@@ -205,6 +222,56 @@ function _omz::plugin::list {
 54   fi
 55 }
 56 
 57+function _omz::plugin::load {
 58+  if [[ -z "$1" ]]; then
 59+    echo >&2 "Usage: omz plugin load <plugin> [...]"
 60+    return 1
 61+  fi
 62+
 63+  local plugins=("$@")
 64+  local plugin base has_completion=0
 65+
 66+  for plugin in $plugins; do
 67+    if [[ -d "$ZSH_CUSTOM/plugins/$plugin" ]]; then
 68+      base="$ZSH_CUSTOM/plugins/$plugin"
 69+    elif [[ -d "$ZSH/plugins/$plugin" ]]; then
 70+      base="$ZSH/plugins/$plugin"
 71+    else
 72+      _omz::log warn "plugin '$plugin' not found"
 73+      continue
 74+    fi
 75+
 76+    # Check if its a valid plugin
 77+    if [[ ! -f "$base/_$plugin" && ! -f "$base/$plugin.plugin.zsh" ]]; then
 78+      _omz::log warn "'$plugin' is not a valid plugin"
 79+      continue
 80+    # It it is a valid plugin, add its directory to $fpath unless it is already there
 81+    elif (( ! ${fpath[(Ie)$base]} )); then
 82+      fpath=("$base" $fpath)
 83+    fi
 84+
 85+    # Check if it has completion to reload compinit
 86+    if [[ -f "$base/_$plugin" ]]; then
 87+      has_completion=1
 88+    fi
 89+
 90+    # Load the plugin
 91+    if [[ -f "$base/$plugin.plugin.zsh" ]]; then
 92+      source "$base/$plugin.plugin.zsh"
 93+    fi
 94+  done
 95+
 96+  # If we have completion, we need to reload the completion
 97+  # We pass -D to avoid generating a new dump file, which would overwrite our
 98+  # current one for the next session (and we don't want that because we're not
 99+  # actually enabling the plugins for the next session).
100+  # Note that we still have to pass -d "$_comp_dumpfile", so that compinit
101+  # doesn't use the default zcompdump location (${ZDOTDIR:-$HOME}/.zcompdump).
102+  if (( has_completion )); then
103+    compinit -D -d "$_comp_dumpfile"
104+  fi