Class: Isolate

Inherits:
Object
  • Object
show all
Defined in:
lib/isolate.rb

Overview

Restricts GEM_PATH and GEM_HOME and provides a DSL for expressing your code’s runtime Gem dependencies. See README.rdoc for rationale, limitations, and examples.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =

:nodoc:

"1.6.1"
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}, &block) ⇒ Isolate

Create a new Isolate instance. See Isolate.gems for the public API. Don’t use this constructor directly.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/isolate.rb', line 70

def initialize path, options = {}, &block
  @enabled      = false
  @entries      = []
  @environments = []
  @passthrough  = false
  @path         = File.expand_path path

  @install      = options.fetch :install, true
  @verbose      = options.fetch :verbose, true
  @cleanup      = @install && options.fetch(:cleanup, true)

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#entriesObject (readonly)

:nodoc:



26
27
28
# File 'lib/isolate.rb', line 26

def entries
  @entries
end

#pathObject (readonly)

:nodoc:



28
29
30
# File 'lib/isolate.rb', line 28

def path
  @path
end

Class Method Details

.activate(environment) ⇒ Object

Activate (and possibly install) gems for a specific environment. This allows two-stage isolation, which is necessary for stuff like Rails. See README.rdoc for a detailed example.



34
35
36
37
# File 'lib/isolate.rb', line 34

def self.activate environment
  instance.activate environment
  instance.cleanup if instance.cleanup?
end

.gems(path, options = {}, &block) ⇒ Object

Declare an isolated RubyGems environment, installed in path. The block given will be instance_evaled, see Isolate#gem and Isolate#environment for the sort of stuff you can do.

Option defaults:

{ :cleanup => true, :install => true, :verbose => true }


47
48
49
50
# File 'lib/isolate.rb', line 47

def self.gems path, options = {}, &block
  @@instance = new path, options, &block
  @@instance.activate
end

.instanceObject

:nodoc:



54
55
56
# File 'lib/isolate.rb', line 54

def self.instance # :nodoc:
  @@instance
end

.refreshObject

Poke RubyGems, we’ve probably monkeyed with a bunch of paths and suchlike. Clears paths, loaded specs, and source indexes.



61
62
63
64
65
# File 'lib/isolate.rb', line 61

def self.refresh # :nodoc:
  Gem.loaded_specs.clear
  Gem.clear_paths
  Gem.source_index.refresh!
end

Instance Method Details

#activate(environment = nil) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/isolate.rb', line 84

def activate environment = nil # :nodoc:
  return self if passthrough?
  enable unless enabled?

  env = environment.to_s if environment
  install environment if install?

  entries.each do |e|
    Gem.activate e.name, *e.requirement.as_list if e.matches? env
  end

  self
end

#cleanupObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/isolate.rb', line 98

def cleanup
  return self if passthrough?

  activated = Gem.loaded_specs.values.map { |s| s.full_name }
  extra     = Gem.source_index.gems.values.sort.reject { |spec|
    activated.include? spec.full_name or
      entries.any? { |e| e.matches_spec? spec }
  }

  log "Cleaning..." unless extra.empty?

  padding = extra.size.to_s.size # omg... heaven forbid you use math
  format  = "[%0#{padding}d/%s] Nuking %s."
  extra.each_with_index do |e, i|
    log format % [i + 1, extra.size, e.full_name]

    Gem::DefaultUserInteraction.use_ui Gem::SilentUI.new do
      Gem::Uninstaller.new(e.name,
                           :version     => e.version,
                           :ignore      => true,
                           :executables => true,
                           :install_dir => self.path).uninstall
    end
  end
end

#cleanup?Boolean

:nodoc:

Returns:

  • (Boolean)


124
125
126
# File 'lib/isolate.rb', line 124

def cleanup? # :nodoc:
  @cleanup
end

#disableObject

:nodoc:



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

def disable # :nodoc:
  return self if passthrough?
  return self unless enabled?

  ENV["GEM_PATH"] = @old_gem_path
  ENV["GEM_HOME"] = @old_gem_home
  ENV["PATH"]     = @old_path
  ENV["RUBYOPT"]  = @old_ruby_opt

  $LOAD_PATH.replace @old_load_path

  @enabled = false

  self.class.refresh
  self
end

#enableObject

:nodoc:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/isolate.rb', line 145

def enable # :nodoc:
  return self if passthrough? || enabled?

  @old_gem_path  = ENV["GEM_PATH"]
  @old_gem_home  = ENV["GEM_HOME"]
  @old_path      = ENV["PATH"]
  @old_ruby_opt  = ENV["RUBYOPT"]
  @old_load_path = $LOAD_PATH.dup

  $LOAD_PATH.reject! do |p|
    p != File.dirname(__FILE__) &&
      Gem.path.any? { |gp| p.include?(gp) }
  end

  # HACK: Gotta keep isolate explicitly in the LOAD_PATH in
  # subshells, and the only way I can think of to do that is by
  # abusing RUBYOPT.

  ENV["RUBYOPT"]  = "#{ENV['RUBYOPT']} -I#{File.dirname(__FILE__)}"
  ENV["GEM_PATH"] = ENV["GEM_HOME"] = path

  bin = File.join path, "bin"
  ENV["PATH"] = [bin, ENV["PATH"]].join File::PATH_SEPARATOR

  self.class.refresh

  @enabled = true
  self
end

#enabled?Boolean

:nodoc:

Returns:

  • (Boolean)


175
176
177
# File 'lib/isolate.rb', line 175

def enabled? # :nodoc:
  @enabled
end

#environment(*environments, &block) ⇒ Object

Restricts gem calls inside block to a set of environments.



181
182
183
184
185
186
187
188
# File 'lib/isolate.rb', line 181

def environment *environments, &block
  old = @environments
  @environments = @environments.dup.concat environments.map { |e| e.to_s }

  instance_eval(&block)
ensure
  @environments = old
end

#gem(name, *requirements) ⇒ Object

Express a gem dependency. Works pretty much like RubyGems’ gem method, but respects environment and doesn’t activate ‘til later.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/isolate.rb', line 194

def gem name, *requirements
  options = Hash === requirements.last ? requirements.pop : {}

  requirement = if requirements.empty? then
                  Gem::Requirement.default
                else
                  Gem::Requirement.new(requirements)
                end

  entry = Entry.new name, requirement, @environments,  options

  entries << entry
  entry
end

#install(environment = nil) ⇒ Object

:nodoc:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/isolate.rb', line 213

def install environment = nil # :nodoc:
  return self if passthrough?

  env = environment.to_s if environment

  installable = entries.select do |e|
    !Gem.available?(e.name, *e.requirement.as_list) && e.matches?(env)
  end

  log "Isolating #{environment}..." unless installable.empty?

  padding = installable.size.to_s.size # omg... heaven forbid you use math
  format  = "[%0#{padding}d/%s] Isolating %s (%s)."
  installable.each_with_index do |e, i|
    log format % [i + 1, installable.size, e.name, e.requirement]

    old         = Gem.sources.dup
    options     = e.options.merge(:install_dir   => path,
                                  :generate_rdoc => false,
                                  :generate_ri   => false)
    source      = options.delete :source
    args        = options.delete :args
    Gem.sources = Array(source) if source
    installer   = Gem::DependencyInstaller.new options

    Gem::Command.build_args = args if args
    installer.install e.name, e.requirement

    Gem.sources = old
    Gem::Command.build_args = nil if args
  end

  Gem.source_index.refresh!
  self
end

#install?Boolean

:nodoc:

Returns:

  • (Boolean)


249
250
251
# File 'lib/isolate.rb', line 249

def install? # :nodoc:
  @install
end

#log(s) ⇒ Object



209
210
211
# File 'lib/isolate.rb', line 209

def log s
  $stderr.puts s if verbose?
end

#passthrough(&block) ⇒ Object

:nodoc:



253
254
255
# File 'lib/isolate.rb', line 253

def passthrough &block # :nodoc:
  @passthrough = yield
end

#passthrough?Boolean

:nodoc:

Returns:

  • (Boolean)


257
258
259
# File 'lib/isolate.rb', line 257

def passthrough? # :nodoc:
  @passthrough
end

#verbose?Boolean

:nodoc:

Returns:

  • (Boolean)


261
262
263
# File 'lib/isolate.rb', line 261

def verbose? # :nodoc:
  @verbose
end