Isolate

Description

Isolate is a very simple RubyGems sandbox. It provides a way to express and install your code’s Gem dependencies.

When Isolate runs, it uses GEM_HOME, GEM_PATH, and a few other tricks to completely separate your code from the system’s RubyGems configuration, leaving it free to run in blissful solitude.

While Isolate doesn’t make any assumptions about what sort of code you’re writing, it was extracted from a few Rails apps, so it’s naturally going to be most useful with stuff like Rails, Merb, or Sinatra.

Isolate is very, very, very stupid simple. For a much more full-featured Gem bundler, check out Yehuda Katz and Carl Lerche’s Bundler: It does a lot of fancy AOT dependency resolution, supports non-gem resources, and is probably a better fit for you. For a widely used Gem manager and installer, check out Chad Woolley’s GemInstaller.

YMMV, but I haven’t tried Isolate with anything older than RubyGems 1.3.5.

Examples

Defining Your Isolated Environment

It’spretty easy: gem is similar to RubyGems’ method of the same name. Version specifiers are optional, and any hash args tacked on to the end are passed through to Gem::DependencyInstaller as flags.

require "rubygems"
require "isolate"

Isolate.gems "vendor/isolated" do
  gem "johnson", "~> 1.1" # or maybe...
  gem "jbarnette-johnson", :source => "http://gems.github.com"
end

At the end of the Isolate.gems block, you’re completely isolated. GEM_PATH and GEM_HOME are set, and all your specified gems have been activated.

Conditionals

Sometimes different sets of gems are appropriate at different times. Isolate allows you to restrict gems by ‘environment’ (which is really just a string passed in when things are activated).

Isolate.gems "vendor/isolated" do
  gem "intercession"

  environment :test, :cucumber do
    gem "mocha"
  end
end

Unsurprisingly, the mocha gem will only be activated in the test and cucumber environments. See the Rails example below for an example of how to use RAILS_ENV to set your environment.

Installing Isolated Gems

Isolate.gems "vendor/isolated", :install => true, :verbose => true do
  gem "intercession"
end

Isolate can install your gems automatically. Just pass an :install option to Isolate.gems. Pass :verbose if you want to see what’s going on.

A Rails Example

Here’s a quick example (extracted from a real project) of how to use Isolate with Rails. This project doesn’t use vendored Rails, and doesn’t want to depend on any system gems (except isolate, of course).

Gem dependencies are defined in config/preinitializer.rb. If you want, you could just as easily put them in config/{gems,deps,whatever}.rb, just make sure it’s loaded in the preinitializer:

require "rubygems"
require "isolate"

Isolate.gems "vendor/isolated", :install => true, :verbose => true do
  gem "rails", "= 2.2.2"

  # async emails!
  gem "ar_mailer", "~> 1.3", '>= 1.3.3'

  # Facebook integration
  gem "facebooker", ">= 1.0.31"

  # Google contacts integration
  gem "gmail_contacts", "~> 1.7"

  # View templates
  gem "haml", "~> 2.0"

  # Session as model
  gem "intercession", "~> 1.0"

  # XML/HTML parsing in Facebooker and tests
  gem "nokogiri", ">= 1.2.3"

  # Twitter authentication
  gem "oauth", "~> 0.3"

  environment :development, :test do
    gem "modelizer" # easy model factories
    gem "sqlite3-ruby" # database support
    gem "vlad"         # deployment
    gem "webrat"       # integration tests
  end

  environment :cucumber do
    gem "cucumber" # stories!
  end
end

Since this is in the preinitializer, Isolate will install and activate the fundamental gems before Rails loads.

This is early enough in Rails’ lifecycle that RAILS_ENV isn’t set, so we need a way to activate gems for the current environment. Let’s add one line to config/environment.rb, right below where boot.rb is required:

require File.join(File.dirname(__FILE__), 'boot')
Isolate.activate RAILS_ENV

Pow. Isolated!

Installation

$ gem install isolate

License

Copyright 2009 John Barnette ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.