Module: Pik::BatchFileEditor

Included in:
Config, Default, GemSync, Switch
Defined in:
lib/pik/commands/batch_file_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch.



5
6
7
# File 'lib/pik/commands/batch_file_editor.rb', line 5

def batch
  @batch
end

Instance Method Details

#closeObject



14
15
16
17
# File 'lib/pik/commands/batch_file_editor.rb', line 14

def close
  update_gem_batch
  super
end

#echo_ruby_version(path, verb = '') ⇒ Object



57
58
59
60
# File 'lib/pik/commands/batch_file_editor.rb', line 57

def echo_ruby_version(path, verb='')
  rb = Which::Ruby.exe(path).basename
  @batch.file_data << "for /f \"delims=\" %%a in ('#{rb} -v') do @echo #{verb} %%a "
end

#echo_running_with_ruby_version(path) ⇒ Object



62
63
64
# File 'lib/pik/commands/batch_file_editor.rb', line 62

def echo_running_with_ruby_version(path)
  echo_ruby_version('Running with')
end

#initialize(args = ARGV, config = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/pik/commands/batch_file_editor.rb', line 7

def initialize(args=ARGV,config=nil)
  super
  batch_file = File.join(PIK_HOME, "#{File.basename($0)}_#{$$}.bat")
  @batch = BatchFile.new( batch_file )
  editors << @batch
end

#set(items) ⇒ Object



19
20
21
22
23
24
# File 'lib/pik/commands/batch_file_editor.rb', line 19

def set(items)
  items.each do |k, v|
    @batch.set(k => v)
    WindowsEnv.user.set(k => v) if global
  end
end

#switch_gem_home_to(gem_home_dir) ⇒ Object



50
51
52
53
54
55
# File 'lib/pik/commands/batch_file_editor.rb', line 50

def switch_gem_home_to(gem_home_dir)
  gem_home_dir ||= ''
  gem_path = Pathname.new(gem_home_dir).to_windows 
  @batch.set('GEM_PATH' => gem_path )
  @batch.set('GEM_HOME' => gem_path )
end

#switch_path_to(new_ver) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pik/commands/batch_file_editor.rb', line 26

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_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_gem_home = current_config[:gem_home]
      current_gem_bin = Pathname.new(current_gem_home) + 'bin'
      new_path.remove(current_gem_bin)
    end
  end
  @batch.set('PATH' => new_path.join )
end

#update_gem_batchObject



66
67
68
69
70
71
72
# File 'lib/pik/commands/batch_file_editor.rb', line 66

def update_gem_batch
  BatchFile.open(PIK_BATCH) do |gem_bat|
    # call new .pik/pik batch
    gem_bat.call(%Q("#{@batch.path.to_windows}")) if @batch
    gem_bat.write        
  end
end