cli.zsh 9.41 KB
Newer Older
1
2
3
#!/usr/bin/env zsh

function omz {
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  [[ $# -gt 0 ]] || {
    _omz::help
    return 1
  }

  local command="$1"
  shift

  # Subcommand functions start with _ so that they don't
  # appear as completion entries when looking for `omz`
  (( $+functions[_omz::$command] )) || {
    _omz::help
    return 1
  }

  _omz::$command "$@"
20
21
22
23
24
25
}

function _omz {
    local -a cmds subcmds
    cmds=(
        'help:Usage information'
26
27
28
        'plugin:Commands for Oh My Zsh plugins management'
        'pr:Commands for Oh My Zsh Pull Requests management'
        'theme:Commands for Oh My Zsh themes management'
29
        'update:Update Oh My Zsh'
30
31
32
33
34
35
    )

    if (( CURRENT == 2 )); then
        _describe 'command' cmds
    elif (( CURRENT == 3 )); then
        case "$words[2]" in
36
37
38
39
40
            plugin) subcmds=('list:List plugins')
                _describe 'command' subcmds ;;
            pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
                _describe 'command' subcmds ;;
            theme) subcmds=('use:Load a theme' 'list:List themes')
41
42
                _describe 'command' subcmds ;;
        esac
43
44
45
46
47
    elif (( CURRENT == 4 )); then
        case "$words[2]::$words[3]" in
            theme::use) compadd "$ZSH"/themes/*.zsh-theme(.N:t:r) \
                "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::) ;;
        esac
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    fi

    return 0
}

compdef _omz omz


function _omz::help {
    cat <<EOF
Usage: omz <command> [options]

Available commands:

    help                Print this help message
63
64
65
    plugin <command>    Manage plugins
    pr <command>        Manage Oh My Zsh Pull Requests
    theme <command>     Manage themes
66
    update              Update Oh My Zsh
67
68
69
70

EOF
}

71
function _omz::confirm {
72
73
74
75
76
77
78
79
80
81
82
83
84
  # If question supplied, ask it before reading the answer
  # NOTE: uses the logname of the caller function
  if [[ -n "$1" ]]; then
    _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  fi

  # Read one character
  read -r -k 1

  # If no newline entered, add a newline
  if [[ "$REPLY" != $'\n' ]]; then
    echo
  fi
85
86
}

87
function _omz::log {
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  # if promptsubst is set, a message with `` or $()
  # will be run even if quoted due to `print -P`
  setopt localoptions nopromptsubst

  # $1 = info|warn|error|debug
  # $2 = text
  # $3 = (optional) name of the logger

  local logtype=$1
  local logname=${3:-${${functrace[1]#_}%:*}}

  # Don't print anything if debug is not active
  if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
    return
  fi

  # Choose coloring based on log type
  case "$logtype" in
    prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
    debug) print -P "%F{white}$logname%f: $2" ;;
    info) print -P "%F{green}$logname%f: $2" ;;
    warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
    error) print -P "%S%F{red}$logname%f%s: $2" ;;
  esac >&2
112
113
}

114
function _omz::plugin {
115
116
  (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
    cat <<EOF
117
118
119
120
Usage: omz plugin <command> [options]

Available commands:

121
  list            List all available Oh My Zsh plugins
122
123

EOF
124
125
    return 1
  }
126

127
128
  local command="$1"
  shift
129

130
  _omz::plugin::$command "$@"
131
132
133
}

function _omz::plugin::list {
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  local -a custom_plugins builtin_plugins
  custom_plugins=("$ZSH_CUSTOM"/plugins/*(-/N:t))
  builtin_plugins=("$ZSH"/plugins/*(-/N:t))

  # If the command is being piped, print all found line by line
  if [[ ! -t 1 ]]; then
    print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
    return
  fi

  if (( ${#custom_plugins} )); then
    print -P "%U%BCustom plugins%b%u:"
    print -l ${(q-)custom_plugins} | column
  fi

  if (( ${#builtin_plugins} )); then
    (( ${#custom_plugins} )) && echo # add a line of separation

    print -P "%U%BBuilt-in plugins%b%u:"
    print -l ${(q-)builtin_plugins} | column
  fi
155
156
}

157
function _omz::pr {
158
159
  (( $# > 0 && $+functions[_omz::pr::$1] )) || {
    cat <<EOF
160
161
162
163
Usage: omz pr <command> [options]

Available commands:

164
165
  clean                       Delete all PR branches (ohmyzsh/pull-*)
  test <PR_number_or_URL>     Fetch PR #NUMBER and rebase against master
166
167

EOF
168
169
    return 1
  }
170

171
172
  local command="$1"
  shift
173

174
  _omz::pr::$command "$@"
175
176
177
}

function _omz::pr::clean {
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  (
    set -e
    builtin cd -q "$ZSH"

    # Check if there are PR branches
    local fmt branches
    fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
    branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"

    # Exit if there are no PR branches
    if [[ -z "$branches" ]]; then
      _omz::log info "there are no Pull Request branches to remove."
      return
    fi

    # Print found PR branches
    echo "$branches\n"
    # Confirm before removing the branches
    _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
    # Only proceed if the answer is a valid yes option
    [[ "$REPLY" != [yY$'\n'] ]] && return

    _omz::log info "removing all Oh My Zsh Pull Request branches..."
    command git branch --list 'ohmyzsh/pull-*' | while read branch; do
      command git branch -D "$branch"
    done
  )
205
206
207
}

function _omz::pr::test {
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  # Allow $1 to be a URL to the pull request
  if [[ "$1" = https://* ]]; then
    1="${1:t}"
  fi

  # Check the input
  if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
    echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
    return 1
  fi

  # Save current git HEAD
  local branch
  branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
    _omz::log error "error when getting the current git branch. Aborting..."
    return 1
  }


  # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  # If any of these operations fail, undo the changes made
  (
    set -e
    builtin cd -q "$ZSH"

    # Get the ohmyzsh git remote
    command git remote -v | while read remote url _; do
      case "$url" in
      https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
      git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
      esac
    done

    (( $found )) || {
      _omz::log error "could not found the ohmyzsh git remote. Aborting..."
      return 1
    }
245

246
247
248
249
250
    # Fetch pull request head
    _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
    command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
      _omz::log error "error when trying to fetch PR #$1."
      return 1
251
252
    }

253
254
255
256
257
258
259
260
261
    # Rebase pull request branch against the current master
    _omz::log info "rebasing PR #$1..."
    command git rebase master ohmyzsh/pull-$1 || {
      command git rebase --abort &>/dev/null
      _omz::log warn "could not rebase PR #$1 on top of master."
      _omz::log warn "you might not see the latest stable changes."
      _omz::log info "run \`zsh\` to test the changes."
      return 1
    }
262

263
264
    _omz::log info "fetch of PR #${1} successful."
  )
265

266
267
  # If there was an error, abort running zsh to test the PR
  [[ $? -eq 0 ]] || return 1
268

269
270
271
  # Run zsh to test the changes
  _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  command zsh -l
272

273
274
275
276
  # After testing, go back to the previous HEAD if the user wants
  _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  # Only proceed if the answer is a valid yes option
  [[ "$REPLY" != [yY$'\n'] ]] && return
277

278
279
280
  (
    set -e
    builtin cd -q "$ZSH"
281

282
283
284
285
286
    command git checkout "$branch" -- || {
      _omz::log error "could not go back to the previous branch ('$branch')."
      return 1
    }
  )
287
}
288

289
function _omz::theme {
290
291
  (( $# > 0 && $+functions[_omz::theme::$1] )) || {
    cat <<EOF
292
293
294
295
Usage: omz theme <command> [options]

Available commands:

296
297
  list            List all available Oh My Zsh themes
  use <theme>     Load an Oh My Zsh theme
298
299

EOF
300
301
    return 1
  }
302

303
304
  local command="$1"
  shift
305

306
  _omz::theme::$command "$@"
307
308
309
}

function _omz::theme::list {
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  local -a custom_themes builtin_themes
  custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r))

  # If the command is being piped, print all found line by line
  if [[ ! -t 1 ]]; then
    print -l ${(q-)custom_themes} ${(q-)builtin_themes}
    return
  fi

  if (( ${#custom_themes} )); then
    print -P "%U%BCustom themes%b%u:"
    print -l ${(q-)custom_themes} | column
  fi

  if (( ${#builtin_themes} )); then
    (( ${#custom_themes} )) && echo # add a line of separation

    print -P "%U%BBuilt-in themes%b%u:"
    print -l ${(q-)builtin_themes} | column
  fi
331
332
333
}

function _omz::theme::use {
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  if [[ -z "$1" ]]; then
    echo >&2 "Usage: omz theme use <theme>"
    return 1
  fi

  # Respect compatibility with old lookup order
  if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
    source "$ZSH_CUSTOM/$1.zsh-theme"
  elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
    source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
    source "$ZSH/themes/$1.zsh-theme"
  else
    _omz::log error "theme '$1' not found"
    return 1
  fi
350
351
}

352
function _omz::update {
353
354
355
356
357
358
359
360
361
362
363
364
365
  # Run update script
  env ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  local ret=$?
  # Update last updated file
  zmodload zsh/datetime
  echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  # Remove update lock if it exists
  command rm -rf "$ZSH/log/update.lock"
  # Restart the zsh session
  if [[ $ret -eq 0 ]]; then
    # Check whether to run a login shell
    [[ "$ZSH_ARGZERO" = -* ]] && exec -l "${ZSH_ARGZERO#-}" || exec "$ZSH_ARGZERO"
  fi
366
}