Module: Travis::Tools::Completion

Includes:
FileUtils
Defined in:
lib/travis/tools/completion.rb

Constant Summary collapse

RCS =
['.zshrc', '.bashrc'].map { |f| File.expand_path(f, ENV['HOME']) }

Class Method Summary collapse

Class Method Details

.cmp_fileObject



20
21
22
# File 'lib/travis/tools/completion.rb', line 20

def cmp_file
  File.expand_path('travis.sh', config_path)
end

.compileObject



50
51
52
53
54
55
# File 'lib/travis/tools/completion.rb', line 50

def compile
  commands = Travis::CLI.commands.sort_by { |c| c.command_name }
  template = Assets.read('travis.sh.erb')
  source   = ERB.new(template).result(binding).gsub(/^ +\n/, '')
  File.write(Assets['travis.sh'], source)
end

.completion_installed?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'lib/travis/tools/completion.rb', line 41

def completion_installed?
  source = 'source ' << config_path
  RCS.each do |file|
    next unless File.exist?(file) && File.writable?(file)
    return false unless File.read(file).include? source
  end
  true
end

.config_pathObject



16
17
18
# File 'lib/travis/tools/completion.rb', line 16

def config_path
  ENV.fetch('TRAVIS_CONFIG_PATH') { File.expand_path('.travis', ENV['HOME']) }
end

.install_completionObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/travis/tools/completion.rb', line 24

def install_completion
  update_completion
  source = 'source ' << cmp_file

  RCS.each do |file|
    next unless File.exist?(file) && File.writable?(file)
    next if File.read(file).include? source

    File.open(file, 'a') { |f| f.puts('', '# added by travis gem', "[ ! -s #{cmp_file} ] || #{source}") }
  end
end

.update_completionObject



36
37
38
39
# File 'lib/travis/tools/completion.rb', line 36

def update_completion
  FileUtils.mkdir_p(config_path)
  FileUtils.cp(Assets['travis.sh'], cmp_file)
end