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

Raises:

  • (Gem::FilePermissionError)


22
23
24
25
26
# 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
  raise Gem::FilePermissionError.new(wrappers_path) unless File.writable?(wrappers_path)
end

#executable_expandedObject



58
59
60
61
62
63
# File 'lib/gem-wrappers/installer.rb', line 58

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

#install(executable) ⇒ Object



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

def install(executable)
  raise "Missing environment file for initialize!" unless @environment_file
  @executable = executable
  content = ERB.new(template).result(binding)
  install_file(executable, content)
  install_ty_formatted(executable, content)
end

#install_file(executable, content) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/gem-wrappers/installer.rb', line 50

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_ty_formatted(executable, content) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/gem-wrappers/installer.rb', line 41

def install_ty_formatted(executable, content)
  formated_executble = @executable_format % executable
  if
    formated_executble != executable
  then
    install_file(formated_executble, content)
  end
end

#templateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gem-wrappers/installer.rb', line 65

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



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

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