Class: Pik::Run

Inherits:
Command show all
Defined in:
lib/pik/commands/run_command.rb

Direct Known Subclasses

Gem, Ruby

Instance Attribute Summary

Attributes inherited from Command

#config, #options, #output, #version

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

This class inherits a constructor from Pik::Command

Instance Method Details

#command(cmd = 'CALL') ⇒ Object



17
18
19
20
# File 'lib/pik/commands/run_command.rb', line 17

def command(cmd='CALL')
  args = @args.map{|a| a.sub(/.*\s.*/m, '"\&"')}.join(' ')
  "#{cmd} #{args}"
end

#command_optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pik/commands/run_command.rb', line 22

def command_options
  super
  sep ="  Examples:\n\nC:\\\\>pik run \"ruby -v\"\n\nC:\\\\>pik run \"rake spec\"\n\n"
  options.separator sep  
end

#echo_ruby_version(path) ⇒ Object



68
69
70
71
# File 'lib/pik/commands/run_command.rb', line 68

def echo_ruby_version(path)
  rb = Which::Ruby.exe(path)
  puts `#{rb} -v `
end

#executeObject



7
8
9
10
11
12
13
14
15
# File 'lib/pik/commands/run_command.rb', line 7

def execute
  @config.sort.each do |version,hash|
    switch_path_to(hash)
    switch_gem_home_to(hash[:gem_home])
    echo_ruby_version(hash[:path])
    puts `#{command}`
    puts
  end
end

#parse_optionsObject



35
36
# File 'lib/pik/commands/run_command.rb', line 35

def parse_options
end

#switch_gem_home_to(gem_home) ⇒ Object



62
63
64
65
66
# File 'lib/pik/commands/run_command.rb', line 62

def switch_gem_home_to(gem_home)
  gem_home = Pathname(gem_home).to_windows rescue nil
  ENV['GEM_PATH'] = gem_home
  ENV['GEM_HOME'] = gem_home
end

#switch_path_to(new_ver) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pik/commands/run_command.rb', line 38

def switch_path_to(new_ver)
  dir = Which::Ruby.find
  current_config = config[ find_config_from_path(dir) ]
  
  new_path = SearchPath.new(ENV['PATH']).replace_or_add(dir, new_ver[:path])
  if new_gem_home = new_ver[:gem_home]
    
    new_gem_bin = Pathname.new(new_gem_home) + 'bin'
    
    if current_config && (current_gem_home = current_config[:gem_home])
      current_gem_bin = Pathname.new(current_gem_home) + 'bin'
      new_path.replace(current_gem_bin, new_gem_bin)
    else
      new_path.add(new_gem_bin)
    end
  else
    if current_config && (current_gem_home = current_config[:gem_home])
      current_gem_bin = Pathname.new(current_gem_home) + 'bin'
      new_path.remove(current_gem_bin)
    end
  end
  ENV['PATH'] = new_path.join
end