Class: Isolate::Sandbox
- Inherits:
-
Object
- Object
- Isolate::Sandbox
- Includes:
- Events
- Defined in:
- lib/isolate/sandbox.rb
Overview
An isolated environment. This class exposes lifecycle events for extension, see Isolate::Events for more information.
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
:nodoc:.
-
#environments ⇒ Object
readonly
:nodoc:.
-
#files ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#activate(environment = nil) ⇒ Object
Activate this set of isolated entries, respecting an optional
environment
. -
#cleanup ⇒ Object
:nodoc:.
- #cleanup? ⇒ Boolean
- #disable(&block) ⇒ Object
-
#enable ⇒ Object
:nodoc:.
- #enabled? ⇒ Boolean
-
#environment(*environments, &block) ⇒ Object
(also: #env)
Restricts
gem
calls insideblock
to a set ofenvironments
. -
#gem(name, *requirements) ⇒ Object
Express a gem dependency.
-
#index ⇒ Object
A source index representing only isolated gems.
-
#initialize(options = {}, &block) ⇒ Sandbox
constructor
Create a new Isolate::Sandbox instance.
-
#install(environment) ⇒ Object
:nodoc:.
-
#install? ⇒ Boolean
:nodoc:.
-
#load(file) ⇒ Object
:nodoc:.
-
#log(s) ⇒ Object
:nodoc:.
- #multiruby? ⇒ Boolean
- #options(options = nil) ⇒ Object
- #path ⇒ Object
- #system? ⇒ Boolean
- #verbose? ⇒ Boolean
Methods included from Events
Constructor Details
#initialize(options = {}, &block) ⇒ Sandbox
Create a new Isolate::Sandbox instance. See Isolate.now! for the most common use of the API. You probably don’t want to use this constructor directly. Fires :initializing
and :initialized
.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/isolate/sandbox.rb', line 25 def initialize = {}, &block @enabled = false @entries = [] @environments = [] @files = [] @options = fire :initializing user = File. "~/.isolate/user.rb" load user if File.exist? user file, local = nil unless FalseClass === [:file] file = [:file] || Dir["{Isolate,config/isolate.rb}"].first local = "#{file}.local" if file end load file if file if block_given? block.to_s =~ /\@([^:]+):/ files << ($1 || "inline block") instance_eval(&block) end load local if local && File.exist?(local) fire :initialized end |
Instance Attribute Details
#entries ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/isolate/sandbox.rb', line 16 def entries @entries end |
#environments ⇒ Object (readonly)
:nodoc:
17 18 19 |
# File 'lib/isolate/sandbox.rb', line 17 def environments @environments end |
#files ⇒ Object (readonly)
:nodoc:
18 19 20 |
# File 'lib/isolate/sandbox.rb', line 18 def files @files 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"
. Fires :activating
and :activated
.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/isolate/sandbox.rb', line 65 def activate environment = nil enable unless enabled? fire :activating env = (environment || Isolate.env).to_s install env if install? entries.each do |e| e.activate if e.matches? env end cleanup if cleanup? fire :activated self end |
#cleanup ⇒ Object
:nodoc:
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/isolate/sandbox.rb', line 83 def cleanup # :nodoc: fire :cleaning installed = index.gems.values.sort legit = legitimize! extra = installed - legit unless 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 fire :cleaned end |
#cleanup? ⇒ Boolean
110 111 112 |
# File 'lib/isolate/sandbox.rb', line 110 def cleanup? install? and @options.fetch(:cleanup, true) end |
#disable(&block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/isolate/sandbox.rb', line 114 def disable &block return self if not enabled? fire :disabling ENV.replace @old_env $LOAD_PATH.replace @old_load_path @enabled = false Isolate.refresh fire :disabled begin; return yield ensure enable end if block_given? self end |
#enable ⇒ Object
:nodoc:
131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 174 175 176 |
# File 'lib/isolate/sandbox.rb', line 131 def enable # :nodoc: return self if enabled? fire :enabling @old_env = ENV.to_hash @old_load_path = $LOAD_PATH.dup FileUtils.mkdir_p path ENV["GEM_HOME"] = path lib = File. "../..", __FILE__ unless system? $LOAD_PATH.reject! do |p| p != lib && 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. dirname = Regexp.escape lib unless ENV["RUBYOPT"] =~ /\s+-I\s*#{lib}\b/ ENV["RUBYOPT"] = "#{ENV['RUBYOPT']} -I#{lib}" end ENV["GEM_PATH"] = path end bin = File.join path, "bin" unless ENV["PATH"].split(File::PATH_SEPARATOR).include? bin ENV["PATH"] = [bin, ENV["PATH"]].join File::PATH_SEPARATOR end ENV["ISOLATED"] = path Isolate.refresh Gem.path.unshift path if system? @enabled = true fire :enabled self end |
#enabled? ⇒ Boolean
178 179 180 |
# File 'lib/isolate/sandbox.rb', line 178 def enabled? @enabled end |
#environment(*environments, &block) ⇒ Object Also known as: env
Restricts gem
calls inside block
to a set of environments
.
184 185 186 187 188 189 190 191 |
# File 'lib/isolate/sandbox.rb', line 184 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.
199 200 201 202 203 204 205 |
# File 'lib/isolate/sandbox.rb', line 199 def gem name, *requirements entry = entries.find { |e| e.name == name } return entry.update(*requirements) if entry entries << entry = Entry.new(self, name, *requirements) entry end |
#index ⇒ Object
A source index representing only isolated gems.
209 210 211 |
# File 'lib/isolate/sandbox.rb', line 209 def index @index ||= Gem::SourceIndex.from_gems_in File.join(path, "specifications") end |
#install(environment) ⇒ 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 |
# File 'lib/isolate/sandbox.rb', line 213 def install environment # :nodoc: fire :installing installable = entries.select do |e| !Gem.available?(e.name, *e.requirement.as_list) && e.matches?(environment) end unless installable.empty? padding = Math.log10(installable.size).to_i + 1 format = "[%0#{padding}d/%s] Isolating %s (%s)." installable.each_with_index do |entry, i| log format % [i + 1, installable.size, entry.name, entry.requirement] entry.install end index.refresh! Gem.source_index.refresh! end fire :installed self end |
#install? ⇒ Boolean
:nodoc:
239 240 241 |
# File 'lib/isolate/sandbox.rb', line 239 def install? # :nodoc: @options.fetch :install, true end |
#load(file) ⇒ Object
:nodoc:
243 244 245 246 |
# File 'lib/isolate/sandbox.rb', line 243 def load file # :nodoc: files << file instance_eval IO.read(file), file, 1 end |
#log(s) ⇒ Object
:nodoc:
248 249 250 |
# File 'lib/isolate/sandbox.rb', line 248 def log s # :nodoc: $stderr.puts s if verbose? end |
#multiruby? ⇒ Boolean
253 254 255 |
# File 'lib/isolate/sandbox.rb', line 253 def multiruby? @options.fetch :multiruby, true end |
#options(options = nil) ⇒ Object
257 258 259 260 |
# File 'lib/isolate/sandbox.rb', line 257 def = nil @options.merge! if @options end |
#path ⇒ Object
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/isolate/sandbox.rb', line 262 def path base = @options.fetch :path, "tmp/isolate" unless @options.key?(:multiruby) && @options[:multiruby] == false suffix = "#{Gem.ruby_engine}-#{RbConfig::CONFIG['ruby_version']}" base = File.join(base, suffix) unless base =~ /#{suffix}/ end File. base end |
#system? ⇒ Boolean
273 274 275 |
# File 'lib/isolate/sandbox.rb', line 273 def system? @options.fetch :system, true end |
#verbose? ⇒ Boolean
277 278 279 |
# File 'lib/isolate/sandbox.rb', line 277 def verbose? @options.fetch :verbose, true end |