Class: Sandbox::Installer

Inherits:
Object
  • Object
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

#optionsObject

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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/sandbox/installer.rb', line 145

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_directoriesObject

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



158
159
160
161
162
163
# File 'lib/sandbox/installer.rb', line 158

def fix_path( path )
  unless path.index( '/' ) == 0
    path = File.join( FileUtils.pwd, path )
  end
  path
end

#install_gemrcObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/sandbox/installer.rb', line 53

def install_gemrc
  filename = File.join(target, '.gemrc')
  template = File.read(File.dirname( __FILE__ ) + '/templates/gemrc.erb')
  script = ERB.new(template)
  output = script.result(binding)

  File.open(filename, 'w') do |f|
    f.write output
  end
end

#install_gemsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sandbox/installer.rb', line 75

def install_gems
  # gem = `which gem`.chomp
  # return if gem.empty?
  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}"
      # cmd = cmd + ' -V' if Sandbox.really_verbose?
      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_scriptsObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/sandbox/installer.rb', line 64

def install_scripts
  filename = File.join(target, 'bin', 'activate')
  template = File.read(File.dirname( __FILE__ ) + '/templates/activate.erb')
  script = ERB.new(template)
  output = script.result(binding)

  File.open(filename, 'w') do |f|
    f.write output
  end
end

#populateObject



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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sandbox/installer.rb', line 127

def resolve_target( path )
  # should consider replacing with 'pathname' => Pathname.new( 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
  return path
end

#restore_sandbox_envObject



120
121
122
123
124
125
# File 'lib/sandbox/installer.rb', line 120

def restore_sandbox_env
  # ENV.update( @old_env )
  ENV[ 'HOME' ]     = @old_env[ 'HOME' ]
  ENV[ 'GEM_HOME' ] = @old_env[ 'GEM_HOME' ]
  ENV[ 'GEM_PATH' ] = @old_env[ 'GEM_PATH' ]
end

#setup_sandbox_envObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sandbox/installer.rb', line 108

def setup_sandbox_env
  @old_env = Hash[ *ENV.select { |k,v| ['HOME','GEM_HOME','GEM_PATH'].include?( k ) }.flatten ]
  # @old_env = {}
  # @old_env[ 'HOME' ]      = ENV[ 'HOME' ]
  # @old_env[ 'GEM_HOME' ]  = ENV[ 'GEM_HOME' ]
  # @old_env[ 'GEM_PATH' ]  = ENV[ 'GEM_PATH' ]
  
  ENV[ 'HOME' ]     = target
  ENV[ 'GEM_HOME' ] = "#{target}/rubygems"
  ENV[ 'GEM_PATH' ] = "#{target}/rubygems"
end

#shell_out(cmd) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/sandbox/installer.rb', line 100

def shell_out( cmd )
  # err_capture = Sandbox.really_verbose? '2>&1' : '2>/dev/null'
  # out = `#{cmd} #{err_capture}`
  out = `#{cmd} 2>/dev/null`
  result = $?.exitstatus == 0
  [ result, out ]
end

#targetObject



19
20
21
22
# File 'lib/sandbox/installer.rb', line 19

def target
  return @target unless @target.nil?
  @target = resolve_target(options[:target])
end