Class: Isolate
- Inherits:
-
Object
- Object
- Isolate
- 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.10.2"
- @@instance =
nil
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
:nodoc:.
-
#path ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.disable(&block) ⇒ Object
Disable Isolate.
-
.env ⇒ Object
:nodoc:.
-
.gems(path, options = {}, &block) ⇒ Object
Declare an isolated RubyGems environment, installed in
path
. -
.instance ⇒ Object
:nodoc:.
-
.now! ⇒ Object
:nodoc:.
-
.refresh ⇒ Object
Poke RubyGems, we’ve probably monkeyed with a bunch of paths and suchlike.
Instance Method Summary collapse
-
#activate(environment = nil) ⇒ Object
Activate this set of isolated entries, respecting an optional
environment
. -
#cleanup ⇒ Object
:nodoc:.
-
#cleanup? ⇒ Boolean
:nodoc:.
-
#disable(&block) ⇒ Object
:nodoc:.
-
#enable ⇒ Object
:nodoc:.
-
#enabled? ⇒ Boolean
:nodoc:.
-
#environment(*environments, &block) ⇒ Object
Restricts
gem
calls insideblock
to a set ofenvironments
. -
#gem(name, *requirements) ⇒ Object
Express a gem dependency.
-
#initialize(path, options = {}, &block) ⇒ Isolate
constructor
Create a new Isolate instance.
-
#install(environment) ⇒ Object
:nodoc:.
-
#install? ⇒ Boolean
:nodoc:.
-
#log(s) ⇒ Object
:nodoc:.
-
#system? ⇒ Boolean
:nodoc:.
-
#verbose? ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(path, options = {}, &block) ⇒ Isolate
Create a new Isolate instance. See Isolate.gems for the public API. You probably don’t want to use this constructor directly.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/isolate.rb', line 83 def initialize path, = {}, &block @enabled = false @entries = [] @environments = [] @path = File. path @install = .fetch :install, true @system = .fetch :system, false @verbose = .fetch :verbose, true @cleanup = @install && .fetch(:cleanup, true) file = [:file] file = Dir["{Isolate,config/isolate.rb}"].first if TrueClass === file instance_eval IO.read(file), file if file instance_eval(&block) if block_given? end |
Instance Attribute Details
#entries ⇒ Object (readonly)
:nodoc:
26 27 28 |
# File 'lib/isolate.rb', line 26 def entries @entries end |
#path ⇒ Object (readonly)
:nodoc:
27 28 29 |
# File 'lib/isolate.rb', line 27 def path @path end |
Class Method Details
.disable(&block) ⇒ Object
Disable Isolate. If a block is provided, isolation will be disabled for the scope of the block.
32 33 34 |
# File 'lib/isolate.rb', line 32 def self.disable &block instance.disable(&block) end |
.env ⇒ Object
:nodoc:
36 37 38 |
# File 'lib/isolate.rb', line 36 def self.env # :nodoc: ENV["ISOLATE_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" end |
.gems(path, options = {}, &block) ⇒ Object
Declare an isolated RubyGems environment, installed in path
. Any block given will be instance_eval
ed, see Isolate#gem and Isolate#environment for the sort of stuff you can do.
If you’d like to specify gems and environments in a separate file, you can pass an optional :file
option.
Option defaults:
{
:cleanup => true,
:install => true,
:system => false,
:verbose => true
}
56 57 58 59 |
# File 'lib/isolate.rb', line 56 def self.gems path, = {}, &block @@instance = new path, , &block @@instance.activate end |
.instance ⇒ Object
:nodoc:
63 64 65 |
# File 'lib/isolate.rb', line 63 def self.instance # :nodoc: @@instance end |
.now! ⇒ Object
:nodoc:
67 68 69 |
# File 'lib/isolate.rb', line 67 def self.now! #:nodoc: gems "tmp/gems", :file => true, :system => true end |
.refresh ⇒ Object
Poke RubyGems, we’ve probably monkeyed with a bunch of paths and suchlike. Clears paths, loaded specs, and source indexes.
74 75 76 77 78 |
# File 'lib/isolate.rb', line 74 def self.refresh # :nodoc: Gem.loaded_specs.clear Gem.clear_paths Gem.source_index.refresh! end |
Instance Method Details
#activate(environment = nil) ⇒ Object
Activate this set of isolated entries, respecting an optional environment
. Points RubyGems to a separate repository, messes with paths, auto-installs gems (if necessary), activates everything, and removes any superfluous gem (again, if necessary). If environment
isn’t specified, ISOLATE_ENV
, RAILS_ENV
, and RACK_ENV
are checked before falling back to "development"
.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/isolate.rb', line 109 def activate environment = nil enable unless enabled? env = (environment || self.class.env).to_s install env if install? entries.each do |e| Gem.activate e.name, *e.requirement.as_list if e.matches? env end cleanup if cleanup? self end |
#cleanup ⇒ Object
:nodoc:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/isolate.rb', line 125 def cleanup # :nodoc: activated = Gem.loaded_specs.values.map { |s| s.full_name } extra = Gem.source_index.gems.values.sort.reject { |spec| !spec.loaded_from.include?(path) or activated.include? spec.full_name or entries.any? { |e| e.matches_spec? spec } } return if extra.empty? padding = Math.log10(extra.size).to_i + 1 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 => path).uninstall end end end |
#cleanup? ⇒ Boolean
:nodoc:
152 153 154 |
# File 'lib/isolate.rb', line 152 def cleanup? # :nodoc: @cleanup end |
#disable(&block) ⇒ Object
:nodoc:
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/isolate.rb', line 156 def disable &block # :nodoc: return self if not 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 begin; return yield ensure enable end if block_given? self end |
#enable ⇒ Object
:nodoc:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/isolate.rb', line 174 def enable # :nodoc: return self if 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 ENV["GEM_HOME"] = path unless system? $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"] = path end bin = File.join path, "bin" ENV["PATH"] = [bin, ENV["PATH"]].join File::PATH_SEPARATOR self.class.refresh Gem.path.unshift path if system? @enabled = true self end |
#enabled? ⇒ Boolean
:nodoc:
210 211 212 |
# File 'lib/isolate.rb', line 210 def enabled? # :nodoc: @enabled end |
#environment(*environments, &block) ⇒ Object
Restricts gem
calls inside block
to a set of environments
.
216 217 218 219 220 221 222 223 |
# File 'lib/isolate.rb', line 216 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.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/isolate.rb', line 229 def gem name, *requirements = 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, entries << entry entry end |
#install(environment) ⇒ Object
:nodoc:
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/isolate.rb', line 245 def install environment # :nodoc: installable = entries.select do |e| !Gem.available?(e.name, *e.requirement.as_list) && e.matches?(environment) end return self if installable.empty? padding = Math.log10(installable.size).to_i + 1 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 = e..merge(:development => false, :generate_rdoc => false, :generate_ri => false, :install_dir => path) source = .delete :source args = .delete :args Gem.sources += Array(source) if source installer = Gem::DependencyInstaller.new Gem::Command.build_args = Array(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:
280 281 282 |
# File 'lib/isolate.rb', line 280 def install? # :nodoc: @install end |
#log(s) ⇒ Object
:nodoc:
284 285 286 |
# File 'lib/isolate.rb', line 284 def log s # :nodoc: $stderr.puts s if verbose? end |
#system? ⇒ Boolean
:nodoc:
288 289 290 |
# File 'lib/isolate.rb', line 288 def system? # :nodoc: @system end |
#verbose? ⇒ Boolean
:nodoc:
292 293 294 |
# File 'lib/isolate.rb', line 292 def verbose? # :nodoc: @verbose end |