-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrake_completion
executable file
·43 lines (35 loc) · 1.37 KB
/
rake_completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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
# Nicholas Seckar <[email protected]>
# Saimon Moore <[email protected]>
# http://www.webtypes.com/2006/03/31/rake-completion-script-that-handles-namespaces
# http://errtheblog.com/post/33
exit 0 unless File.file?(File.join(Dir.pwd, 'Rakefile'))
require 'rubygems'
require 'rake'
DOTCACHE = File.join(File.expand_path('~'), ".rake_task_cache" , Dir.pwd.hash.to_s)
RAKE_FILES = FileList[ __FILE__, 'Rakefile', 'lib/tasks/**/*.rake', 'vendor/plugins/*/tasks/**/*.rake']
file DOTCACHE => RAKE_FILES do
tasks = `rake --silent --tasks`.split("\n").map { |line| line.split[1] }
require 'fileutils'
dirname = File.dirname(DOTCACHE)
FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
File.open(DOTCACHE, 'w') { |f| f.puts tasks }
end
Rake::Task[DOTCACHE].invoke
tasks = File.read(DOTCACHE)
exit 0 unless /\brake\b/ =~ ENV["COMP_LINE"]
after_match = $'
task_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.split.last
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