Skip to content

Commit

Permalink
add shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Aug 5, 2013
1 parent a74a8fc commit 1c8d984
Show file tree
Hide file tree
Showing 6 changed files with 1,418 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ If you have the old `travis-cli` gem installed, you should `gem uninstall travis
* Add streaming body API.
* Add event listener API.
* Add simple plugin system (will load any ~/.travis/*/init.rb when running cli).
* Implement shell completion for bash and zsh.
* Be smarter about warnings when running `travis encrypt`.
* Improve documentation.

Expand Down
76 changes: 75 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ desc "run specs"
task(:spec) { ruby "-S rspec spec#{" -c" unless windows}" }

desc "generate gemspec, update readme"
task 'update' do
task :update => :completion do
require 'travis/version'
content = File.read('travis.gemspec')

Expand Down Expand Up @@ -43,6 +43,80 @@ task 'update' do
File.write('README.md', readme)
end

task :completion do
require 'travis/cli'
commands = Travis::CLI.commands.sort_by { |c| c.command_name }
names = commands.map(&:command_name).join("\n")
options = commands.map do |command|
flags = command.new.parser.candidate("-").flat_map do |option|
next option unless option.start_with?('--[no-]')
[option.sub('[no-]', ''), option.sub('[no-]', 'no-')]
end.join("\n")

case command.command_name
when "setup"
completions = command.public_instance_methods.
select { |m| m.to_s.start_with? "setup_" }.
map { |m| m.to_s.sub('setup_', '') + "\n" }.join << flags
when "help"
completions = "#{names}\n#{flags}"
else
completions = flags
end

<<-SHELL
#{command.command_name})
completions="#{completions}"
;;
SHELL
end.join("\n")

zsh = <<-SHELL
_travis_complete() {
local words completions
read -cA words
if [ "${#words}" -eq 2 ]; then
completions="#{names}"
else
case "${words[2]}" in
#{options}
esac
fi
reply=("${(ps:\n:)completions}")
}
compctl -K _travis_complete travis
SHELL

bash = <<-SHELL
_travis_complete() {
COMPREPLY=()
local completions
if [ "$COMP_CWORD" -eq 1 ]; then
completions="#{names}"
else
case "${COMP_WORDS[1]}" in
#{options}
esac
fi
COMPREPLY=( $(compgen -W "$completions" -- "${COMP_WORDS[COMP_CWORD]}") )
}
complete -F _travis_complete travis
SHELL

File.write('completion/travis.sh', <<-SHELL.gsub(/^\s+/, ''))
# This file is generated by `rake completion`
if [ -n "$BASH_VERSION" ]; then
#{bash}
fi
if [ -n "$ZSH_VERSION" ]; then
#{zsh}
fi
SHELL
end

task 'travis.gemspec' => :update
task 'README.md' => :update

Expand Down
17 changes: 17 additions & 0 deletions completion/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
config_path = ENV.fetch('TRAVIS_CONFIG_PATH') { File.expand_path('.travis', ENV['HOME']) }

require 'fileutils'
FileUtils.mkdir_p(config_path)
FileUtils.cp(File.expand_path('../travis.sh', __FILE__), config_path)

rcs = ['.zshrc', '.bashrc'].map { |f| File.expand_path(f, ENV['HOME']) }
source = "source " << File.expand_path('travis.sh', config_path)

rcs.each do |file|
next unless File.exist? file and File.writable? file
next if File.read(file).include? source
File.open(file, "a") { |f| f.puts("", "# added by travis gem", source) }
end

# fake Makefile
File.open('Makefile', 'w') { |f| f.puts 'all:', 'install:' }
Loading

0 comments on commit 1c8d984

Please sign in to comment.