Gemsconfig

Description

A gem to manage gem configuration for a project, similar to Rails’ config.gem (but in a very simple, dumb way)

Installation

sudo gem install kelredd-gemsconfig --source http://gems.github.com

Usage

(in any main project ruby file that is loaded on project start)
require 'rubygems'
require 'config/gems.rb'      # custom Gemsconfig configuration for your project
Gemsconfig::load

The file ‘config/gems.rb’ should require in ‘gemsconfig’, call Gemsconfig::init, and specify the gem configurations (see example in ‘Configuration’ below). Gemsconfig::load will attempt to load each gem configured. Any load errors will be thrown and alerted.

Rake Usage

Gemsconfig comes with a rake task that will attempt to install any gems not yet installed. To use it, be sure to install rake and have a Rakefile that contains something like this:

# Rakefile example
require 'rubygems'
require 'config/gems.rb'      # custom Gemsconfig configuration for your project
require 'gemsconfig/tasks/rake'

Now to use the install task:

(in terminal)
rake -T                   # make sure gemsconfig:install task is loaded and available
rake gemsconfig:install   # attemp to install any gems not yet installed

Capistrano Usage

So it would be really awesome to have a cap task like ‘cap deploy:gems’ that would do a capistrano deploy and run ‘rake gemsconfig:install’ as part of said deploy. I’ll add this someday…

Configuration

Gemsconfig expects to be initialized with configuration entries for each gem that needs to be configured for your project. This initialization is typically done in a configuration file that is required in to the main app file. Here’s an example:

# gems.rb example
require 'gemsconfig'

Gemsconfig::init do |config|
  config.gem 'sinatra'

  config.gem 'kelredd-useful',
    :lib => 'useful/ruby_extensions',
    :source => 'http://gems.github.com'

  config.gem 'kelredd-useful',
    :lib => 'useful/sinatra_helpers',
    :source => 'http://gems.github.com'

  config.gem 'kelredd-activerecord-sinatra',
    :lib => 'activerecord_sinatra',
    :source => 'http://gems.github.com'

  config.gem 'kelredd-sprockets-sinatra',
    :lib => 'sprockets_sinatra',
    :version => '0.2.1',
    :source => 'http://gems.github.com'

  config.gem 'kelredd-repository',
    :lib => 'repository/filesystem',
    :source => 'http://gems.github.com'
end

Gotchas

  • Sinatra: I would be careful configuring the Sinatra gem using Gemsconfig. Sinatra expects to be required in the main app.rb

file. If you only configure the sinatra gem in your Gemsconfig, it gets required from one of Gemsconfig’s lib files and this messes up the Sinatra::Application.root variable (among other things I’m sure). A best practice would be to go ahead and configure sinatra in Gemsconfig (as in the example above), AND require it in your main app.rb file BEFORE requiring your Gemsconfig. This way sinatra gets required like it expects, and when Gemsconfig attempts to require it, nothing will happen, and you still get the rake task benefit from having Sinatra in the Gemsconfig.

License

Copyright © 2009 Kelly Redding

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.