Class: GemWrappers::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/gem-wrappers/installer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment_file = nil, executable_format = Gem.default_exec_format) ⇒ Installer

Returns a new instance of Installer.



8
9
10
11
# File 'lib/gem-wrappers/installer.rb', line 8

def initialize(environment_file = nil, executable_format = Gem.default_exec_format)
  @environment_file = environment_file
  @executable_format = executable_format
end

Instance Attribute Details

#environment_fileObject (readonly)

Returns the value of attribute environment_file.



6
7
8
# File 'lib/gem-wrappers/installer.rb', line 6

def environment_file
  @environment_file
end

Class Method Details

.wrappers_pathObject



13
14
15
16
# File 'lib/gem-wrappers/installer.rb', line 13

def self.wrappers_path
  Gem.configuration && Gem.configuration[:wrappers_path] ||
  File.join(Gem.dir, 'wrappers')
end

Instance Method Details

#ensureObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/gem-wrappers/installer.rb', line 22

def ensure
  FileUtils.mkdir_p(wrappers_path) unless File.exist?(wrappers_path)
  # exception based on Gem::Installer.generate_bin
  unless File.writable?(wrappers_path)
    raise Gem::FilePermissionError.new(wrappers_path)
  end
  unless @environment_file
    raise "Missing environment file for initialize!"
  end
end

#executable_expandedObject



80
81
82
83
84
85
# File 'lib/gem-wrappers/installer.rb', line 80

def executable_expanded
  if File.extname(@executable) == ".rb"
  then "ruby #{@executable}"
  else @executable
  end
end

#find_executable_path(executable) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/gem-wrappers/installer.rb', line 54

def find_executable_path(executable)
  return executable if File.exist?(executable)
  ENV['PATH'].split(File::PATH_SEPARATOR).each { |dir|
    full_path = File.join(dir, executable)
    return full_path if File.exist?(full_path)
  }
  nil
end

#install(executable) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gem-wrappers/installer.rb', line 38

def install(executable)
  target_path = find_executable_path(executable)
  unless target_path
    warn "GemWrappers: Can not wrap missing file: #{executable}"
    return
  end
  unless File.executable?(target_path)
    warn "GemWrappers: Can not wrap not executable file: #{target_path}"
    return
  end
  @executable = executable
  content = ERB.new(template).result(binding)
  install_file(executable, content)
  install_to_formatted(executable, content)
end

#install_file(executable, content) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/gem-wrappers/installer.rb', line 72

def install_file(executable, content)
  file_name = File.join(wrappers_path, File.basename(executable))
  File.open(file_name, 'w') do |file|
    file.write(content)
  end
  file_set_executable(file_name)
end

#install_to_formatted(executable, content) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/gem-wrappers/installer.rb', line 63

def install_to_formatted(executable, content)
  formatted_executable = @executable_format % executable
  if
    formatted_executable != executable
  then
    install_file(formatted_executable, content)
  end
end

#templateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gem-wrappers/installer.rb', line 87

def template
  @template ||= <<-TEMPLATE
#!/usr/bin/env bash

if
  [[ -s "<%= environment_file %>" ]]
then
  source "<%= environment_file %>"
  exec <%= executable_expanded %> "$@"
else
  echo "ERROR: Missing RVM environment file: '<%= environment_file %>'" >&2
  exit 1
fi
TEMPLATE
end

#uninstall(executable) ⇒ Object



33
34
35
36
# File 'lib/gem-wrappers/installer.rb', line 33

def uninstall(executable)
  file_name = File.join(wrappers_path, executable)
  File.delete(file_name) if File.exist?(file_name)
end

#wrappers_pathObject



18
19
20
# File 'lib/gem-wrappers/installer.rb', line 18

def wrappers_path
  @wrappers_path ||= self.class.wrappers_path
end