Merge pull request #137 from zhaocai/feature/_gem

Use cache-policy for _gem
This commit is contained in:
Julien Nicoulaud 2013-05-26 02:28:04 -07:00
commit f038f70214
1 changed files with 48 additions and 10 deletions

View File

@ -19,6 +19,12 @@
_gem() { _gem() {
local -a gem_general_flags local -a gem_general_flags
local context curcontext="$curcontext" update_policy
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && \
zstyle ":completion:${curcontext}:" cache-policy _gem_extensions_caching_policy
gem_general_flags=("(-h --help)"{-h,--help}"[Get help on this command]" gem_general_flags=("(-h --help)"{-h,--help}"[Get help on this command]"
"(-V --verbose)"{-V,--verbose}"[Set the verbose level of output]" "(-V --verbose)"{-V,--verbose}"[Set the verbose level of output]"
"(-q --quiet)"{-q,--quiet}"[Silence commands]" "(-q --quiet)"{-q,--quiet}"[Silence commands]"
@ -31,22 +37,33 @@ _gem() {
extra_gems=$(_call_program commands ${words[1]} list --no-version gem-browse gemedit gem-edit) extra_gems=$(_call_program commands ${words[1]} list --no-version gem-browse gemedit gem-edit)
local gem_edit_command="edit[Edit installed gem in editor]" local gem_edit_command="edit[Edit installed gem in editor]"
local gem_open_command="open[open installed gem in editor]"
local gem_clone_command="clone[Clone a gem's source from GitHub]" local gem_clone_command="clone[Clone a gem's source from GitHub]"
local gem_browse_command="browse[browse the gem's homepage]" local gem_browse_command="browse[browse the gem's homepage]"
if [ ${#extra_gems} -eq 0 ] ; then unset _gem_extra_commands
extra_gem_commands_desc="" if ( [[ ${+_gem_extra_commands} -eq 0 ]] || \
extra_gem_commands="" _cache_invalid "gem_extensions" ) && ! _retrieve_cache "gem_extensions"; then
elif [[ "${extra_gems[(i)gem-browse]}" -le ${#extra_gems} ]] ; then
extra_gem_commands_desc=(${gem_edit_command} ${gem_browse_command} ${gem_clone_command})
extra_gem_commands=(clone browse edit)
elif [[ "${extra_gems[(r)gem-edit]}" -le ${#extra_gems} ]] \ _gem_extensions=$(_call_program commands ${words[1]} list --no-version gem-browse gemedit gem-edit)
|| [[ "${extra_gems[(r)gemedit]}" -le ${#extra_gems} ]] ; then
extra_gem_commands_desc=${gem_edit_command} _store_cache "gem_extensions" _gem_extensions
extra_gem_commands=edit
fi fi
if [ ${#_gem_extensions} -eq 0 ] ; then
extra_gem_commands_desc=""
extra_gem_commands=""
elif [[ "${extra_gems[(i)gem-browse]}" -le ${#extra_gems} ]] ; then
extra_gem_commands_desc=(${gem_edit_command} ${gem_open_command} ${gem_browse_command} ${gem_clone_command})
extra_gem_commands=(clone browse edit)
elif [[ "${_gem_extensions[(r)gem-edit]}" -le ${#_gem_extensions} ]] \
|| [[ "${_gem_extensions[(r)gemedit]}" -le ${#_gem_extensions} ]] ; then
extra_gem_commands_desc=${gem_edit_command}
extra_gem_commands=edit
fi
if (( CURRENT > 2 )); then if (( CURRENT > 2 )); then
(( CURRENT-- )) (( CURRENT-- ))
shift words shift words
@ -211,6 +228,10 @@ _gem_browse() {
_gem_edit _gem_edit
} }
_gem_open() {
_gem_edit
}
_gem_environment() { _gem_environment() {
typeset -g _environment_arg= typeset -g _environment_arg=
_environment_arg='::environment:((packageversion\:"display the package version" _environment_arg='::environment:((packageversion\:"display the package version"
@ -493,6 +514,22 @@ _gem_which() {
$gem_general_flags && ret=0 $gem_general_flags && ret=0
} }
_gem_extensions_caching_policy() {
if [[ -n $GEM_HOME ]]; then
gem_home=$GEM_HOME
else
gem_home=$(_call_program commands ${words[1]} env gemdir)
fi
# Rebuild if gems directory is more recent than cache.
[[ $gem_home/gems -nt "$1" ]] && return 0
# Rebuild if cache is older than one week.
local -a oldp
oldp=( "$1"(Nmw+1) )
(( $#oldp ))
}
# Local Variables: # Local Variables:
# mode: Shell-Script # mode: Shell-Script
# sh-indentation: 2 # sh-indentation: 2
@ -501,3 +538,4 @@ _gem_which() {
# End: # End:
# vim: ft=zsh sw=2 ts=2 et # vim: ft=zsh sw=2 ts=2 et