Class: Sandbox::Installer
- Inherits:
-
Object
- Object
- Sandbox::Installer
show all
- Extended by:
- Output
- Includes:
- Output
- Defined in:
- lib/sandbox/installer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Output
tell, tell_unless_quiet, tell_unless_really_quiet, tell_when_really_verbose, tell_when_verbose
Constructor Details
#initialize(options = {}) ⇒ Installer
Returns a new instance of Installer.
14
15
16
17
|
# File 'lib/sandbox/installer.rb', line 14
def initialize(options={})
@options = options.dup
@target = nil
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
12
13
14
|
# File 'lib/sandbox/installer.rb', line 12
def options
@options
end
|
Instance Method Details
#check_path!(path) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/sandbox/installer.rb', line 127
def check_path!(path)
if File.directory?(path)
if File.writable?(path)
return true
else
raise Sandbox::Error, "path '#{path}' has a permission problem"
end
elsif File.exists?(path)
raise Sandbox::Error, "path '#{path}' is not a directory"
end
false
end
|
#create_directories ⇒ Object
Create folders:
mkdir -p /path/to/sandbox/rubygems/bin
Symlink the bin directory, because when gems are installed, binaries are installed in GEM_HOME/bin:
$ ln -s /path/to/sandbox/rubygems/bin /path/to/sandbox/bin
45
46
47
48
49
50
51
|
# File 'lib/sandbox/installer.rb', line 45
def create_directories
gembin = File.join(target, 'rubygems', 'bin')
FileUtils.mkdir_p(gembin)
bin = File.join(target, 'bin')
FileUtils.ln_s(gembin, bin)
end
|
#fix_path(path) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/sandbox/installer.rb', line 140
def fix_path(path)
unless path.index('/') == 0
path = File.join(FileUtils.pwd, path)
end
path
end
|
#install_gemrc ⇒ Object
53
54
55
56
57
58
|
# File 'lib/sandbox/installer.rb', line 53
def install_gemrc
filename = File.join(target, '.gemrc')
template = File.read(File.dirname(__FILE__) + '/templates/gemrc.erb')
output = ERB.new(template).result(binding)
File.open(filename, 'w') { |f| f.write(output) }
end
|
#install_gems ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/sandbox/installer.rb', line 67
def install_gems
gems = options[:gems] || []
if gems.size == 0
tell(" nothing to install")
return
end
begin
setup_sandbox_env
gems.each do |gem|
tell_unless_really_quiet(" gem: #{gem}")
cmd = "gem install #{gem}"
status, output = shell_out(cmd)
unless status
tell_unless_really_quiet(" failed to install gem: #{gem}")
end
end
ensure
restore_sandbox_env
end
end
|
#install_scripts ⇒ Object
60
61
62
63
64
65
|
# File 'lib/sandbox/installer.rb', line 60
def install_scripts
filename = File.join(target, 'bin', 'activate')
template = File.read(File.dirname(__FILE__) + '/templates/activate.erb')
output = ERB.new(template).result(binding)
File.open(filename, 'w') { |f| f.write(output) }
end
|
#populate ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/sandbox/installer.rb', line 24
def populate
tell("creating sandbox at: #{target}")
create_directories
tell("installing activation script")
install_scripts
tell("installing .gemrc")
install_gemrc
tell("installing gems")
install_gems
end
|
#resolve_target(path) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/sandbox/installer.rb', line 109
def resolve_target(path)
path = fix_path(path)
if File.exists?(path)
raise Sandbox::Error, "target '#{path}' exists"
end
base = path
while base = File.dirname(base)
if check_path!(base)
break
elsif base == '/'
raise "something is seriously wrong; we should never get here"
end
end
path
end
|
#restore_sandbox_env ⇒ Object
103
104
105
106
107
|
# File 'lib/sandbox/installer.rb', line 103
def restore_sandbox_env
ENV['HOME'] = @old_env['HOME']
ENV['GEM_HOME'] = @old_env['GEM_HOME']
ENV['GEM_PATH'] = @old_env['GEM_PATH']
end
|
#setup_sandbox_env ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/sandbox/installer.rb', line 95
def setup_sandbox_env
@old_env = Hash[ *ENV.select { |k,v| ['HOME','GEM_HOME','GEM_PATH'].include?(k) }.flatten ]
ENV['HOME'] = target
ENV['GEM_HOME'] = "#{target}/rubygems"
ENV['GEM_PATH'] = "#{target}/rubygems"
end
|
#shell_out(cmd) ⇒ Object
89
90
91
92
93
|
# File 'lib/sandbox/installer.rb', line 89
def shell_out(cmd)
out = `#{cmd} 2>/dev/null`
result = $?.exitstatus == 0
[result, out]
end
|
#target ⇒ Object
19
20
21
22
|
# File 'lib/sandbox/installer.rb', line 19
def target
return @target unless @target.nil?
@target = resolve_target(options[:target])
end
|