Fork me on GitHub

Custom Gems in Gemfile

This post has been updated at 16/05/2013.

Switching between your custom libraries from your local directory and from RubyGems.org can be a real pain. Fortunately with Bundler it's getting much easier.

# encoding: utf-8

source :rubygems

# Use local clones if possible.
# If you want to use your local copy, just symlink it to vendor.
# See http://blog.101ideas.cz/posts/custom-gems-in-gemfile.html
extend Module.new {
  def gem(name, *args)
    options = args.last.is_a?(Hash) ? args.last : Hash.new

    local_path = File.expand_path("../vendor/#{name}", __FILE__)
    if File.exist?(local_path)
      super name, options.merge(:path => local_path).
        delete_if { |key, _| [:git, :branch].include?(key) }
    else
      super name, *args
    end
  end
}

# Padrino
gem "padrino-core", git: "git://github.com/botanicus/padrino-framework.git"
gem "padrino-gen", git: "git://github.com/botanicus/padrino-framework.git", group: "bundle"

# Component requirements
gem "template-inheritance"
gem "formidable"
gem "pupu"
gem "helpers"

When I run bundle install, it downloads all the gems from RubyGems.org as usual. The fun starts when I need to debug some of my gems: I simply symlink them from my local projects directory to vendor/ and re-run bundle install.

From the very moment the symlinked libraries will be loaded from the local directory, so I can put debug statements there, polish features and generally play with them till I'm done. When I'm done, I simply remove the symlink, release a new version of such gem and re-run bundle install. Simple as that!

Just in case you don't understand the code: we create a new anonymous module where we define gem method. Then we extend current object (which is an instance of some DSL class of Bundler) with the module, therefore super call invokes the original gem method. You could alias the method, but this feels much cleaner to me.

blog comments powered by Disqus
About

RSS

All Posts IT Ruby RubyGems bundler Gemfile

Tags

GitHub projects

Twitter @botanicus

Recent Comments