Class: BundlerBashCompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler_bash_completion.rb

Constant Summary collapse

TASKS =
{
  'check' => {
    '--gemfile' => :block,
    '--no-color' => :continue,
    '--path' => :block,
  },
  'clean' => {
    '--force' => :continue,
    '--no-color' => :continue,
    '--verbose' => :continue,
  },
  'config' => {},
  'console' => {},
  'exec' => {
    :bin => :continue,
  },
  'gem' => {
    '--bin' => :block,
  },
  'help' => {
    :task => :continue,
  },
  'init' => {
    '--no-color' => :continue,
    '--verbose' => :continue,
  },
  'install' => {
    '--binstubs' => :continue,
    '--deployment' => :continue,
    '--gemfile' => :block,
    '--local' => :continue,
    '--no-color' => :continue,
    '--path' => :block,
    '--shebang' => :block,
    '--standalone' => :block,
    '--system' => :continue,
    '--verbose' => :continue,
    '--without' => :block,
  },
  'list' => {
    '--no-color' => :continue,
    '--paths' => :continue,
    '--verbose' => :continue,
    :gem => :continue,
  },
  'open' => {
    :gem => :continue,
  },
  'outdated' => {
    '--local' => :continue,
    '--no-color' => :continue,
    '--pre' => :continue,
    '--source' => :block,
    '--verbose' => :continue,
    :gem => :continue,
  },
  'package' => {},
  'platform' => {},
  'show' => {
    '--no-color' => :continue,
    '--paths' => :continue,
    '--verbose' => :continue,
    :gem => :continue,
  },
  'update' => {
    '--no-color' => :continue,
    '--source' => :block,
    '--verbose' => :continue,
    :gems => :continue,
  },
  'viz' => {
    '--file' => :block,
    '--format' => :block,
    '--no-color' => :continue,
    '--requirements' => :block,
    '--verbose' => :continue,
    '--version' => :block,
  },
}
CONFIG_PATH =
'.bundle/config'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ BundlerBashCompletion

Returns a new instance of BundlerBashCompletion.



89
90
91
# File 'lib/bundler_bash_completion.rb', line 89

def initialize(line)
  @line = line.to_s.gsub(/^\s+/, '').freeze
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



87
88
89
# File 'lib/bundler_bash_completion.rb', line 87

def line
  @line
end

Instance Method Details

#argumentsObject



93
94
95
# File 'lib/bundler_bash_completion.rb', line 93

def arguments
  @arguments ||= line.split(/\s+/)
end

#binsObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bundler_bash_completion.rb', line 97

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

#commandObject



110
111
112
# File 'lib/bundler_bash_completion.rb', line 110

def command
  arguments.first.to_s
end

#completeObject



118
119
120
121
122
# File 'lib/bundler_bash_completion.rb', line 118

def complete
  return task_options_completion if task_options_completion?
  return tasks_completion if tasks_completion?
  []
end

#completion_wordObject



114
115
116
# File 'lib/bundler_bash_completion.rb', line 114

def completion_word
  @completion_word ||= (line =~ /\s+$/) ? '' : arguments.last
end

#gemsObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bundler_bash_completion.rb', line 124

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

#taskObject



142
143
144
# File 'lib/bundler_bash_completion.rb', line 142

def task
  @task ||= (completion_step > 1) ? arguments[1].to_s : ''
end

#task_optionsObject



146
147
148
# File 'lib/bundler_bash_completion.rb', line 146

def task_options
  @task_options ||= (completion_step > 2) ? arguments[2..(completion_step - 1)] : []
end