Class: Pik::Run

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

Direct Known Subclasses

Gem, Rake, Ruby

Instance Attribute Summary

Attributes inherited from Command

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

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

This class inherits a constructor from Pik::Command

Instance Method Details

#args_required?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/pik/commands/run_command.rb', line 83

def args_required?
  true
end

#check_argsObject



77
78
79
80
81
# File 'lib/pik/commands/run_command.rb', line 77

def check_args
  if args_required? && @args.empty?
    raise "The #{cmd_name} command must be called with an argument"
  end
end

#command(cmd = 'CALL') ⇒ Object



24
25
26
27
# File 'lib/pik/commands/run_command.rb', line 24

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

#command_optionsObject



30
31
32
33
# File 'lib/pik/commands/run_command.rb', line 30

def command_options
  super
  options.separator help_message  
end

#echo_ruby_version(path) ⇒ Object



70
71
72
73
74
75
# File 'lib/pik/commands/run_command.rb', line 70

def echo_ruby_version(path)
  rb = Which::Ruby.exe(path)
  raise "Unable to find a Ruby executable at #{path}" unless rb
  puts `"#{rb}" -v `
  puts
end

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pik/commands/run_command.rb', line 7

def execute
  check_args
  @config.sort.each do |version,hash|
    begin
      switch_path_to(hash)
      switch_gem_home_to(hash[:gem_home])
      echo_ruby_version(hash[:path])
      system command
      puts
    rescue => e
      version = version.split(': ')[1..-1].join(': ')
      puts version
      Pik.print_error(e)
    end
  end
end

#help_messageObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/pik/commands/run_command.rb', line 35

def help_message
  sep =<<SEP
  Examples:

C:\\>pik run PATH

C:\\>pik run rake spec

SEP
end

#parse_optionsObject



46
47
# File 'lib/pik/commands/run_command.rb', line 46

def parse_options
end

#switch_gem_home_to(gem_home) ⇒ Object



64
65
66
67
68
# File 'lib/pik/commands/run_command.rb', line 64

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(other) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pik/commands/run_command.rb', line 49

def switch_path_to(other)
  current = Which::Ruby.find
  
  new_path = SearchPath.new(ENV['PATH'])
  new_path.replace_or_add(current, other[:path])
  
  # if there is currently a GEM_HOME, remove it's bin dir from the path
  new_path.remove(Pathname.new(ENV['GEM_HOME']) + 'bin') if ENV['GEM_HOME']
  
  # if the new version has a GEM_HOME, add it's bin dir to the path
  new_path.add(Pathname.new(other[:gem_home]) + 'bin') if other[:gem_home]
  
  ENV['PATH'] = new_path.join
end