Sha256: b49fe803019fce62072fb8fd421e963a8363b9b241d9afe11b4cd58f3df6a062

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'travis/tools/assets'
require 'travis/cli'
require 'fileutils'
require 'erb'

module Travis
  module Tools
    module Completion
      RCS = ['.zshrc', '.bashrc'].map { |f| File.expand_path(f, ENV['HOME']) }
      include FileUtils

      module_function

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

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

      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

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

      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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
travis-1.13.3 lib/travis/tools/completion.rb
travis-1.13.2 lib/travis/tools/completion.rb
travis-1.13.1 lib/travis/tools/completion.rb