Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created February 5, 2010 03:58
Show Gist options
  • Select an option

  • Save mikehale/295486 to your computer and use it in GitHub Desktop.

Select an option

Save mikehale/295486 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Save this somewhere, chmod 755 it, then add
# complete -C path/to/this/script -o default rake
# to your ~/.bashrc
#
# If you update your tasks, just $ rm ~/.raketabs*
#
# Adapted from
# http://onrails.org/articles/2006/08/30/namespaces-and-rake-command-completion
exit 0 unless File.file?(File.join(Dir.pwd, 'Rakefile'))
exit 0 unless /^rake\b/ =~ ENV["COMP_LINE"]
require 'fileutils'
def rake_silent_tasks
FileUtils.mkdir_p(cache_dir = File.join(File.expand_path('~'), ".raketabs"))
if File.exists?(dotcache = File.join(cache_dir, Dir.pwd.hash.to_s))
File.read(dotcache)
else
tasks = `rake --silent --tasks`
File.open(dotcache, 'w') { |f| f.puts tasks }
tasks
end
end
after_match = $'
task_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.split.last
tasks = rake_silent_tasks.split("\n")[1..-1].map { |line| line.split[1] }
tasks = tasks.select { |t| /^#{Regexp.escape task_match}/ =~ t } if task_match
# handle namespaces
if task_match =~ /^([-\w:]+:)/
upto_last_colon = $1
after_match = $'
tasks = tasks.map { |t| (t =~ /^#{Regexp.escape upto_last_colon}([-\w:]+)$/) ? "#{$1}" : t }
end
puts tasks
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment