cli.zsh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/usr/bin/env zsh
  2. function omz {
  3. [[ $# -gt 0 ]] || {
  4. _omz::help
  5. return 1
  6. }
  7. local command="$1"
  8. shift
  9. # Subcommand functions start with _ so that they don't
  10. # appear as completion entries when looking for `omz`
  11. (( $+functions[_omz::$command] )) || {
  12. _omz::help
  13. return 1
  14. }
  15. _omz::$command "$@"
  16. }
  17. function _omz {
  18. local -a cmds subcmds
  19. cmds=(
  20. 'changelog:Print the changelog'
  21. 'help:Usage information'
  22. 'plugin:Manage plugins'
  23. 'pr:Manage Oh My Zsh Pull Requests'
  24. 'theme:Manage themes'
  25. 'update:Update Oh My Zsh'
  26. )
  27. if (( CURRENT == 2 )); then
  28. _describe 'command' cmds
  29. elif (( CURRENT == 3 )); then
  30. case "$words[2]" in
  31. changelog) local -a refs
  32. refs=("${(@f)$(command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
  33. _describe 'command' refs ;;
  34. plugin) subcmds=('list:List plugins')
  35. _describe 'command' subcmds ;;
  36. pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
  37. _describe 'command' subcmds ;;
  38. theme) subcmds=('use:Load a theme' 'list:List themes')
  39. _describe 'command' subcmds ;;
  40. esac
  41. elif (( CURRENT == 4 )); then
  42. case "$words[2]::$words[3]" in
  43. theme::use) compadd "$ZSH"/themes/*.zsh-theme(.N:t:r) \
  44. "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::) ;;
  45. esac
  46. fi
  47. return 0
  48. }
  49. compdef _omz omz
  50. ## Utility functions
  51. function _omz::confirm {
  52. # If question supplied, ask it before reading the answer
  53. # NOTE: uses the logname of the caller function
  54. if [[ -n "$1" ]]; then
  55. _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  56. fi
  57. # Read one character
  58. read -r -k 1
  59. # If no newline entered, add a newline
  60. if [[ "$REPLY" != $'\n' ]]; then
  61. echo
  62. fi
  63. }
  64. function _omz::log {
  65. # if promptsubst is set, a message with `` or $()
  66. # will be run even if quoted due to `print -P`
  67. setopt localoptions nopromptsubst
  68. # $1 = info|warn|error|debug
  69. # $2 = text
  70. # $3 = (optional) name of the logger
  71. local logtype=$1
  72. local logname=${3:-${${functrace[1]#_}%:*}}
  73. # Don't print anything if debug is not active
  74. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  75. return
  76. fi
  77. # Choose coloring based on log type
  78. case "$logtype" in
  79. prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
  80. debug) print -P "%F{white}$logname%f: $2" ;;
  81. info) print -P "%F{green}$logname%f: $2" ;;
  82. warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
  83. error) print -P "%S%F{red}$logname%f%s: $2" ;;
  84. esac >&2
  85. }
  86. ## User-facing commands
  87. function _omz::help {
  88. cat <<EOF
  89. Usage: omz <command> [options]
  90. Available commands:
  91. help Print this help message
  92. changelog Print the changelog
  93. plugin <command> Manage plugins
  94. pr <command> Manage Oh My Zsh Pull Requests
  95. theme <command> Manage themes
  96. update Update Oh My Zsh
  97. EOF
  98. }
  99. function _omz::changelog {
  100. local version=${1:-HEAD} format=${3:-"--text"}
  101. if ! command git -C "$ZSH" show-ref --verify refs/heads/$version &>/dev/null && \
  102. ! command git -C "$ZSH" show-ref --verify refs/tags/$version &>/dev/null && \
  103. ! command git -C "$ZSH" rev-parse --verify "${version}^{commit}" &>/dev/null; then
  104. cat <<EOF
  105. Usage: omz changelog [version]
  106. NOTE: <version> must be a valid branch, tag or commit.
  107. EOF
  108. return 1
  109. fi
  110. "$ZSH/tools/changelog.sh" "$version" "${2:-}" "$format"
  111. }
  112. function _omz::plugin {
  113. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  114. cat <<EOF
  115. Usage: omz plugin <command> [options]
  116. Available commands:
  117. list List all available Oh My Zsh plugins
  118. EOF
  119. return 1
  120. }
  121. local command="$1"
  122. shift
  123. _omz::plugin::$command "$@"
  124. }
  125. function _omz::plugin::list {
  126. local -a custom_plugins builtin_plugins
  127. custom_plugins=("$ZSH_CUSTOM"/plugins/*(-/N:t))
  128. builtin_plugins=("$ZSH"/plugins/*(-/N:t))
  129. # If the command is being piped, print all found line by line
  130. if [[ ! -t 1 ]]; then
  131. print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
  132. return
  133. fi
  134. if (( ${#custom_plugins} )); then
  135. print -P "%U%BCustom plugins%b%u:"
  136. print -l ${(q-)custom_plugins} | column
  137. fi
  138. if (( ${#builtin_plugins} )); then
  139. (( ${#custom_plugins} )) && echo # add a line of separation
  140. print -P "%U%BBuilt-in plugins%b%u:"
  141. print -l ${(q-)builtin_plugins} | column
  142. fi
  143. }
  144. function _omz::pr {
  145. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  146. cat <<EOF
  147. Usage: omz pr <command> [options]
  148. Available commands:
  149. clean Delete all PR branches (ohmyzsh/pull-*)
  150. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  151. EOF
  152. return 1
  153. }
  154. local command="$1"
  155. shift
  156. _omz::pr::$command "$@"
  157. }
  158. function _omz::pr::clean {
  159. (
  160. set -e
  161. builtin cd -q "$ZSH"
  162. # Check if there are PR branches
  163. local fmt branches
  164. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  165. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  166. # Exit if there are no PR branches
  167. if [[ -z "$branches" ]]; then
  168. _omz::log info "there are no Pull Request branches to remove."
  169. return
  170. fi
  171. # Print found PR branches
  172. echo "$branches\n"
  173. # Confirm before removing the branches
  174. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  175. # Only proceed if the answer is a valid yes option
  176. [[ "$REPLY" != [yY$'\n'] ]] && return
  177. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  178. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  179. command git branch -D "$branch"
  180. done
  181. )
  182. }
  183. function _omz::pr::test {
  184. # Allow $1 to be a URL to the pull request
  185. if [[ "$1" = https://* ]]; then
  186. 1="${1:t}"
  187. fi
  188. # Check the input
  189. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  190. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  191. return 1
  192. fi
  193. # Save current git HEAD
  194. local branch
  195. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  196. _omz::log error "error when getting the current git branch. Aborting..."
  197. return 1
  198. }
  199. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  200. # If any of these operations fail, undo the changes made
  201. (
  202. set -e
  203. builtin cd -q "$ZSH"
  204. # Get the ohmyzsh git remote
  205. command git remote -v | while read remote url _; do
  206. case "$url" in
  207. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  208. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  209. esac
  210. done
  211. (( $found )) || {
  212. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  213. return 1
  214. }
  215. # Fetch pull request head
  216. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  217. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  218. _omz::log error "error when trying to fetch PR #$1."
  219. return 1
  220. }
  221. # Rebase pull request branch against the current master
  222. _omz::log info "rebasing PR #$1..."
  223. command git rebase master ohmyzsh/pull-$1 || {
  224. command git rebase --abort &>/dev/null
  225. _omz::log warn "could not rebase PR #$1 on top of master."
  226. _omz::log warn "you might not see the latest stable changes."
  227. _omz::log info "run \`zsh\` to test the changes."
  228. return 1
  229. }
  230. _omz::log info "fetch of PR #${1} successful."
  231. )
  232. # If there was an error, abort running zsh to test the PR
  233. [[ $? -eq 0 ]] || return 1
  234. # Run zsh to test the changes
  235. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  236. command zsh -l
  237. # After testing, go back to the previous HEAD if the user wants
  238. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  239. # Only proceed if the answer is a valid yes option
  240. [[ "$REPLY" != [yY$'\n'] ]] && return
  241. (
  242. set -e
  243. builtin cd -q "$ZSH"
  244. command git checkout "$branch" -- || {
  245. _omz::log error "could not go back to the previous branch ('$branch')."
  246. return 1
  247. }
  248. )
  249. }
  250. function _omz::theme {
  251. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  252. cat <<EOF
  253. Usage: omz theme <command> [options]
  254. Available commands:
  255. list List all available Oh My Zsh themes
  256. use <theme> Load an Oh My Zsh theme
  257. EOF
  258. return 1
  259. }
  260. local command="$1"
  261. shift
  262. _omz::theme::$command "$@"
  263. }
  264. function _omz::theme::list {
  265. local -a custom_themes builtin_themes
  266. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  267. builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r))
  268. # If the command is being piped, print all found line by line
  269. if [[ ! -t 1 ]]; then
  270. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  271. return
  272. fi
  273. if (( ${#custom_themes} )); then
  274. print -P "%U%BCustom themes%b%u:"
  275. print -l ${(q-)custom_themes} | column
  276. fi
  277. if (( ${#builtin_themes} )); then
  278. (( ${#custom_themes} )) && echo # add a line of separation
  279. print -P "%U%BBuilt-in themes%b%u:"
  280. print -l ${(q-)builtin_themes} | column
  281. fi
  282. }
  283. function _omz::theme::use {
  284. if [[ -z "$1" ]]; then
  285. echo >&2 "Usage: omz theme use <theme>"
  286. return 1
  287. fi
  288. # Respect compatibility with old lookup order
  289. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  290. source "$ZSH_CUSTOM/$1.zsh-theme"
  291. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  292. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  293. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  294. source "$ZSH/themes/$1.zsh-theme"
  295. else
  296. _omz::log error "theme '$1' not found"
  297. return 1
  298. fi
  299. }
  300. function _omz::update {
  301. # Run update script
  302. if [[ "$1" != --unattended ]]; then
  303. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
  304. else
  305. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  306. fi
  307. local ret=$?
  308. # Update last updated file
  309. zmodload zsh/datetime
  310. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  311. # Remove update lock if it exists
  312. command rm -rf "$ZSH/log/update.lock"
  313. # Restart the zsh session
  314. if [[ $ret -eq 0 && "$1" != --unattended ]]; then
  315. # Check whether to run a login shell
  316. [[ "$ZSH_ARGZERO" = -* ]] && exec -l "${ZSH_ARGZERO#-}" || exec "$ZSH_ARGZERO"
  317. fi
  318. }