class BundlerBashCompletion

Constants

CONFIG_PATH
TASKS

Attributes

line[R]

Public Class Methods

new(line) click to toggle source
# File lib/bundler_bash_completion.rb, line 148
def initialize(line)
  @line = line.to_s.gsub(/^\s+/, '').freeze
end

Public Instance Methods

arguments() click to toggle source
# File lib/bundler_bash_completion.rb, line 152
def arguments
  @arguments ||= line.split(/\s+/)
end
bins() click to toggle source
# File lib/bundler_bash_completion.rb, line 156
def bins
  @bins ||= begin
    gem_paths.map { |path| Dir.glob("#{path}/{bin,exe}/*") }.tap do |paths|
      paths.flatten!
      paths.reject! { |path| !File.executable?(path) }
      paths.map! { |path| File.basename(path) }
      paths.push('gem', 'ruby')
      paths.sort!
      paths.uniq!
    end
  end
end
command() click to toggle source
# File lib/bundler_bash_completion.rb, line 169
def command
  arguments.first.to_s
end
complete() click to toggle source
# File lib/bundler_bash_completion.rb, line 177
def complete
  return task_options_completion if task_options_completion?
  return tasks_completion if tasks_completion?
  []
end
completion_word() click to toggle source
# File lib/bundler_bash_completion.rb, line 173
def completion_word
  @completion_word ||= (line =~ /\s+$/) ? '' : arguments.last
end
gems() click to toggle source
# File lib/bundler_bash_completion.rb, line 183
def gems
  @gems ||= begin
    gems = File.readlines("#{Dir.pwd}/Gemfile.lock").grep(/\(.+\)/).tap do |lines|
      lines.each do |line|
        line.gsub!(/\(.+/, '')
        line.gsub!(/\s+/, '')
        line.strip!
      end
    end.tap do |gems|
      gems.push('bundler')
      gems.sort!
      gems.uniq!
    end
  rescue Exception
    []
  end
end
task() click to toggle source
# File lib/bundler_bash_completion.rb, line 201
def task
  @task ||= (completion_step > 1) ? arguments[1].to_s : ''
end
task_options() click to toggle source
# File lib/bundler_bash_completion.rb, line 205
def task_options
  @task_options ||= (completion_step > 2) ? arguments[2..(completion_step - 1)] : []
end

Private Instance Methods

bundle_command?() click to toggle source
# File lib/bundler_bash_completion.rb, line 211
def bundle_command?
  command == 'bundle'
end
bundle_path() click to toggle source
# File lib/bundler_bash_completion.rb, line 215
def bundle_path
  @bundle_path ||= begin
    if File.exists?(CONFIG_PATH)
      require 'yaml'
      path = YAML.load_file(CONFIG_PATH)['BUNDLE_PATH']
      path && File.expand_path(path)
    end
  rescue
    nil
  end
end
completion_step() click to toggle source
# File lib/bundler_bash_completion.rb, line 227
def completion_step
  @completion_step ||= arguments.size - (completion_word.empty? ? 0 : 1)
end
gem_paths() click to toggle source
# File lib/bundler_bash_completion.rb, line 231
def gem_paths
  @gem_paths ||= begin
    paths = Gem.path.map do |path|
      Dir.glob("#{path}/gems/*")
    end
    paths << Dir.glob("#{bundle_path}/ruby/*/gems/*")
    paths.flatten!
    paths.uniq!
    paths
  end
end
task_options_completion() click to toggle source
# File lib/bundler_bash_completion.rb, line 251
def task_options_completion
  options = TASKS[task] || {}
  return [] if options[task_options.last] == :block
  completion = options.keys.map do |key|
    if key == :task
      (task_options & TASKS.keys).empty? ? TASKS.keys : nil
    elsif key == :gem
      task_options.empty? ? gems : nil
    elsif key == :gems
      gems - task_options
    elsif key == :bin
      task_options.empty? ? bins : nil
    else
      key
    end
  end
  completion.flatten!
  completion.compact!
  completion.select { |c| c.start_with?(completion_word) }
end
task_options_completion?() click to toggle source
# File lib/bundler_bash_completion.rb, line 272
def task_options_completion?
  bundle_command? && completion_step > 1 && TASKS.key?(task)
end
tasks_completion() click to toggle source
# File lib/bundler_bash_completion.rb, line 243
def tasks_completion
  TASKS.keys.select { |t| t.start_with?(completion_word) }
end
tasks_completion?() click to toggle source
# File lib/bundler_bash_completion.rb, line 247
def tasks_completion?
  bundle_command? && completion_step == 1
end