Last active
December 24, 2015 20:59
-
-
Save chrishenry/6861535 to your computer and use it in GitHub Desktop.
A slightly improved version of a rake task autocomplete function. - Cache less data (we don't need the task descriptions) - Tasks with arguments would break the autocomplete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Complete rake tasks script for bash | |
# Save it somewhere and then add | |
# complete -C path/to/script -o default rake | |
# to your ~/.bashrc | |
# Chris Henry, combining work from | |
# Xavier Shay (http://rhnh.net) - http://rhnh.net/2008/06/08/rake-tab-completion-with-caching-and-namespace-support, | |
# Francis Hwang ( http://fhwang.net/ ) - http://fhwang.net/rb/rake-complete.rb | |
# Nicholas Seckar <nseckar@gmail.com> - http://www.webtypes.com/2006/03/31/rake-completion-script-that-handles-namespaces | |
# Saimon Moore <saimon@webtypes.com> | |
require 'fileutils' | |
RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'] | |
exit 0 unless RAKEFILES.any? { |rf| File.file?(File.join(Dir.pwd, rf)) } | |
exit 0 unless /^rake\b/ =~ ENV["COMP_LINE"] | |
after_match = $' | |
task_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.split.last | |
cache_dir = File.join( ENV['HOME'], '.rake', 'tc_cache' ) | |
FileUtils.mkdir_p cache_dir | |
rakefile = RAKEFILES.detect { |rf| File.file?(File.join(Dir.pwd, rf)) } | |
rakefile_path = File.join( Dir.pwd, rakefile ) | |
cache_file = File.join( cache_dir, rakefile_path.gsub( %r{/}, '_' ) ) | |
if File.exist?( cache_file ) && | |
File.mtime( cache_file ) >= (Dir['lib/tasks/*.rake'] << rakefile).collect {|x| File.mtime(x) }.max | |
task_lines = File.read( cache_file ) | |
else | |
task_lines = `rake --silent --tasks`.split("\n") | |
task_lines = task_lines.collect { |l| l.split("#")[0].split("[")[0] } | |
File.open( cache_file, 'w' ) do |f| f << task_lines.join("\n"); end | |
task_lines = task_lines.join("\n") | |
end | |
tasks = task_lines.split("\n")[1..-1].collect {|line| line.split[1].split("#")[0].split("[")[0] } | |
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.collect { |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