cli.zsh 11 KB

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