-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrake_completion
executable file
·70 lines (54 loc) · 1.49 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
# to install, add the following line to your .bash_profile or .bashrc
# complete -C path/to/rake_completion -o default rake
# Rake completion will return matching rake tasks given typed text. This way
# you can auto-complete tasks as you are typing them by hitting [tab] or [tab][tab]
# This also caches the rake tasks for optimium speed
class RakeCompletion
CACHE_FILE_NAME = '.rake_tasks~'
def initialize(command)
@command = command
end
def matches
exit 0 if rakefile.nil?
matching_tasks.map do |task|
task.sub(typed_before_colon, '')
end
end
private
def typed
@command[/\s(.+?)$/, 1] || ''
end
def typed_before_colon
typed[/.+\:/] || ''
end
def matching_tasks
all_tasks.select do |task|
task[0, typed.length] == typed
end
end
def all_tasks
cache_current? ? tasks_from_cache : generate_tasks
end
def cache_current?
File.exist?(cache_file) && File.mtime(cache_file) >= File.mtime(rakefile)
end
def rakefile
['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].detect do |file|
File.file? File.join(Dir.pwd, file)
end
end
def cache_file
File.join(Dir.pwd, CACHE_FILE_NAME)
end
def tasks_from_cache
IO.read(cache_file).split
end
def generate_tasks
tasks = `rake --tasks`.split("\n")[1..-1].collect {|line| line.split[1]}
File.open(cache_file, 'w') { |f| f.write tasks.join("\n") }
tasks
end
end
puts RakeCompletion.new(ENV["COMP_LINE"]).matches
exit 0