Class: Pik::Add

Inherits:
Command show all
Includes:
ConfigFileEditor
Defined in:
lib/pik/commands/add_command.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#config, #debug, #options, #output, #version

Instance Method Summary collapse

Methods included from ConfigFileEditor

#initialize

Methods inherited from Command

#actual_gem_home, #add_sigint_handler, aka, choose_from, clean_gem_batch, #close, cmd_name, #cmd_name, #create, #current_gem_bin_path, #current_version?, #default_gem_home, #delete_old_pik_batches, description, #editors, #find_config_from_path, #get_gem_home, #get_version, hl, inherited, #initialize, it, names, #parse_options, #pik_version, #sh, summary

Instance Attribute Details

#interactiveObject (readonly)

Returns the value of attribute interactive.



8
9
10
# File 'lib/pik/commands/add_command.rb', line 8

def interactive
  @interactive
end

Instance Method Details

#add(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pik/commands/add_command.rb', line 16

def add(path)
  path = Pathname.new(path)
  path = path.dirname if path.file?
  if Which::Ruby.exist?(path) 
    if find_config_from_path(path)
      puts "This version has already been added."
    else
      version = get_version(path)
      path    = Pathname(path.expand_path.to_ruby)
      puts "** Adding:  #{version}\n Located at:  #{path}\n"
      @config[version] = {}
      @config[version][:path] = path
    end
  else
    puts "Couldn't find a Ruby version at #{path}"
  end
end

#add_interactiveObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pik/commands/add_command.rb', line 43

def add_interactive
  @hl.choose do |menu|  
    menu.prompt = ""
    menu.choice('e]nter a path') do
      dir = @hl.ask("Enter a path to a ruby/bin dir (enter to quit)")
      execute(dir) unless dir.empty? || !@hl.agree("Add '#{dir}'?"){|answer| answer.default = 'yes' }
      add_interactive
    end
    menu.choice('s]earch') do
      search_dir = @hl.ask("Enter a search path")
      files = Which::Ruby.glob(search_dir + '**')
      files.uniq.each do |file| 
        dir = File.dirname(file)
        add(dir) if @hl.agree("Add '#{dir}'?"){|answer| answer.default = 'yes' }
      end
      add_interactive
    end
    menu.choice('q]uit'){raise QuitError}
  end        

end

#command_optionsObject



34
35
36
37
38
39
40
41
# File 'lib/pik/commands/add_command.rb', line 34

def command_options
  super
  options.banner += "[path_to_ruby]"
  options.separator ""      
  options.on("--interactive", "-i", "Add interactively") do |value|
    @interactive = value
  end 
end

#execute(path = nil) ⇒ Object



10
11
12
13
14
# File 'lib/pik/commands/add_command.rb', line 10

def execute(path=nil)
  return add_interactive if interactive
  path = @args.first || Which::Ruby.find
  add(path)
end