Class: InstallTabTab::CLI

Inherits:
Object
  • Object
show all
Includes:
TabTab::LocalConfig
Defined in:
lib/install_tabtab/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TabTab::LocalConfig

#config, #home

Instance Attribute Details

#development_cliObject (readonly)

Returns the value of attribute development_cli.



12
13
14
# File 'lib/install_tabtab/cli.rb', line 12

def development_cli
  @development_cli
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/install_tabtab/cli.rb', line 11

def options
  @options
end

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



14
15
16
# File 'lib/install_tabtab/cli.rb', line 14

def self.execute(stdout, arguments=[])
  self.new.execute(stdout, arguments)
end

Instance Method Details

#aliasesObject



134
135
136
# File 'lib/install_tabtab/cli.rb', line 134

def aliases
  config["alias"] || config["aliases"]
end

#execute(stdout, arguments = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/install_tabtab/cli.rb', line 18

def execute(stdout, arguments=[])
  $stdout = stdout
  parse_options(arguments)
  @to_file = []
  install_externals
  install_for_files
  install_from_gems
  @file = File.open(File.join(home, ".tabtab.bash"), "w")
  @to_file.each { |line| @file << "#{line}\n" }
  @file.close
end

#find_gems_with_definition_filesObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/install_tabtab/cli.rb', line 81

def find_gems_with_definition_files
  Gem.latest_load_paths.inject([]) do |mem, path|
    root = path.gsub(/(lib|bin)$/,'')
    mem << root unless mem.include?(root)
    mem
  end.inject([]) do |mem, path|
    common_file = Dir[File.join(path, "**", "tabtab_definitions.rb")].reject { |tabtab_path| tabtab_path =~ /(spec|test)/ }.first
    gem_name = nil
    unless common_file.nil? || common_file.empty?
      gem_name = common_file.match(/\/([^\/]*)-\d+(\.\d+)+\/lib\//)[1]
      TabTab::Definition.clear
      load common_file
      mem << { :gem_name => gem_name, :app_names => TabTab::Definition.app_names }
    end
    files = Dir[File.join(path, "**", "tabtab_definitions", "**", "*.rb")].reject { |tabtab_path| tabtab_path =~ /(spec|test)/ }
    if files && files.size > 0
      gem_path, gem_name = files.first.match(/^(.*\/([^\/]*)-\d+(\.\d+)+)/)[1..2]
      files.each do |file|
        TabTab::Definition.clear
        load file
        tabtab_path = file.gsub(gem_path, "")
        mem << { :gem_name => "#{gem_name}#{tabtab_path}", :app_names => TabTab::Definition.app_names }
      end
    end
    mem
  end
end

#install_cmd_and_aliases(app_name, arg_str) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/install_tabtab/cli.rb', line 109

def install_cmd_and_aliases(app_name, arg_str)
  tabtab = tabtab_cmd(arg_str)
  @to_file << "complete -o default -C '#{tabtab}' #{app_name}"
  aliases.each do |alias_cmd, cmd|
    if cmd == app_name
      tabtab = tabtab_cmd(arg_str, cmd)
      @to_file << "complete -o default -C '#{tabtab}' #{alias_cmd}"
    end
  end if aliases
end

#install_externalsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/install_tabtab/cli.rb', line 41

def install_externals
  return unless externals = config['external'] || config['externals']
  for app_name_or_hash in externals
    if app_name_or_hash.is_a?(String) || app_name_or_hash.is_a?(Symbol)
      install_cmd_and_aliases(app_name_or_hash.to_s, "--external")
    elsif app_name_or_hash.is_a?(Hash)
      app_name_or_hash.each do |flag, app_list|
        app_list.each do |app_name|
          install_cmd_and_aliases(app_name, "--external")
        end
      end
    end
  end
end

#install_file(file, app_name) ⇒ Object



68
69
70
# File 'lib/install_tabtab/cli.rb', line 68

def install_file(file, app_name)
  install_cmd_and_aliases(app_name, "--file #{File.expand_path(file)}")
end

#install_for_filesObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/install_tabtab/cli.rb', line 56

def install_for_files
  return unless files_and_names = config['file'] || config['files']
  for file in files_and_names.keys
    case app_names = files_and_names[file]
    when String
      install_file file, app_names
    when Array
      app_names.each { |app_name| install_file(file, app_name) }
    end
  end
end

#install_from_gemsObject



72
73
74
75
76
77
78
79
# File 'lib/install_tabtab/cli.rb', line 72

def install_from_gems
  find_gems_with_definition_files.each do |gem|
    gem[:app_names].each do |app_name|
      $stdout.puts "RubyGem #{gem[:gem_name]} contains TabTab definition for #{app_name}"
      install_cmd_and_aliases(app_name, "--gem #{gem[:gem_name]}")
    end
  end
end

#parse_options(arguments) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/install_tabtab/cli.rb', line 30

def parse_options(arguments)
  @options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options]"

    opts.on("-d", "--development", "Generate .tabtab.bash to use local bin/tabtab instead of RubyGems tabtab") do |v|
      options[:development_cli] = v
    end
  end.parse!(arguments)
end

#tabtab_cmd(flags, aliased_to = nil) ⇒ Object



127
128
129
130
131
132
# File 'lib/install_tabtab/cli.rb', line 127

def tabtab_cmd(flags, aliased_to=nil)
  tabtab = options[:development_cli] ? File.expand_path(File.dirname(__FILE__) + "/../../bin/tabtab") : "tabtab"
  aliased_to ? 
  "#{tabtab} #{flags} --alias #{aliased_to}" :
  "#{tabtab} #{flags}"
end

#usageObject



120
121
122
123
124
125
# File 'lib/install_tabtab/cli.rb', line 120

def usage
  puts <<-EOS.gsub(/^      /, '')
  USAGE: create a file ~/.tabtab.yml
  EOS
  exit 1
end