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) ⇒ Installer

Returns a new instance of Installer.



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

def initialize(environment_file)
  @environment_file = environment_file
end

Instance Attribute Details

#environment_fileObject (readonly)

Returns the value of attribute environment_file.



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

def environment_file
  @environment_file
end

Class Method Details

.wrappers_pathObject



11
12
13
14
# File 'lib/gem-wrappers/installer.rb', line 11

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

Instance Method Details

#ensureObject

Raises:

  • (Gem::FilePermissionError)


20
21
22
23
24
# File 'lib/gem-wrappers/installer.rb', line 20

def ensure
  FileUtils.mkdir_p(wrappers_path) unless File.exist?(wrappers_path)
  # exception based on Gem::Installer.generate_bin
  raise Gem::FilePermissionError.new(wrappers_path) unless File.writable?(wrappers_path)
end

#install(executable) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/gem-wrappers/installer.rb', line 26

def install(executable)
  content = ERB.new(template).result(binding)
  file = File.join(wrappers_path, executable)
  File.open(file, 'w') do |file|
    file.write(content)
  end
  File.chmod(0755, file)
end

#templateObject



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

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

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

#wrappers_pathObject



16
17
18
# File 'lib/gem-wrappers/installer.rb', line 16

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