LoginSignup
7
7

More than 5 years have passed since last update.

Rakeの補完をキャッシュする in zsh

Last updated at Posted at 2013-02-18

rakeのサブコマンドの補完がとにかく遅いと思う。

http://weblog.rubyonrails.org/2006/3/8/fast-rake-task-completion-for-zsh/ が動かなかったので作ってみた。GemfileかRakefileが更新されたらキャッシュ更新するようになってる。

_cachefile_updated_at() {
  echo $(stat .rake_tasks -c%Y)
}

_rakefile_updated_at() {
  echo $(stat Rakefile -c%Y)
}

_gemfile_updated_at() {
  echo $(stat Gemfile -c%Y)
}

_generate_cachefile() {
  rake --silent --tasks 2> /dev/null | cut  -f 2 -d " " > .rake_tasks
}

_rake() {
  if [ -f Rakefile ]; then
    if [ ! -f .rake_tasks ] || \
       [ "`cat .rake_tasks | wc -l`" = "0" ] || \
       [ `_cachefile_updated_at` -lt `_rakefile_updated_at` ] || \
       [ -f Gemfile -a `_cachefile_updated_at` -lt `_gemfile_updated_at` ]; then
      _generate_cachefile
    fi
    compadd `cat .rake_tasks`
  fi
}

compdef _rake rake

追記

BSDのstat使ってる場合は stat -f%m Gemfile で更新時刻取れるようです。coreutilsで入れてるの使ってることに気づきませんでした。

7
7
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7