Freeze Other Gems to Rails' ./lib Directory 12 comments

posted Thursday, December 22, 2005 by topfunky

UPDATE

This post is sorely out of date. Chris Wanstrath’s vendor everything technique is more useful and is what I do now.

In fact, I’m converting all my Rails plugins to RubyGems and am building all my development around gems instead of Subversion-based plugins.

If you still need this functionality as a rake task, see my topfunky_power_tools plugin for a current implementation of the idea.

Original Article

If you have any Rails apps on shared hosts, you know that you have to copy your own libraries into the ./lib folder of your app.

I thought that the new “rake freeze_gems” would do that, but it only freezes the Rails-relevant gems (ActiveRecord, ActionMailer, etc.). If you use any other libraries (like Gruff), you’re on your own.

I wanted to have an automated solution for doing this in a way that worked with Subversion and Switchtower. So I whipped up a little Christmas gift for myself (and I’m re-gifting it to you).

Download Freeze Other Gems Rake Task

Usage

Copy gems.rake into ./lib/tasks. This will make it available to rake.

Edit gems.rake to list the names of the other gems you want to synchronize. In the example, I’m using gruff, sparklines, and pdf-writer.

Then, do

rake freeze_other_gems

The files will be copied from your local installed gems and will keep any .svn files intact. You can just “svn update” and your updated gems will be ready to deploy with Switchtower!

And also…

New podcast with the brilliant Avi Bryant.

12 comments

Leave a response

  • This is great!

    One of the things on my TODO list for the weekend was to write this kind of Rakefile :-)

  • Gravatar icon Sean

    This is fantastic. Thank you!

    This works perfectly for me because I’ve had to hack ruby-activeldap to deal with a schema-related bug we haven’t otherwise been able to work around. Now, instead of hoping the edited gem is installed on deployed hosts or that a ‘gem update’ won’t overwrite the changes, I’m sure we have a local version.

    Thanks again!

  • Gravatar icon http://aslakhellesoy.com

    Nice! This will be very useful to me!

    Aslak

  • Gravatar icon Sean

    Ok, so, I’m hitting a small problem. I’ve added some gems to the list I want to freeze, and when I run ‘rake freeze_other_gems’, I get this:

    rake aborted! undefined method `version’ for nil:NilClass

    I removed all of the new gems from the list and left it at the one gem I had previously frozen. Same error.

    I’ve recently upgraded to Rails 1.0, and I’m using Locomotive. I was using Locomotive before, but it was version 0.4.1. It’s now 1.0.

    Any thoughts?

  • Gravatar icon topfunky

    I hope to test it with Locomotive later this weekend.

  • Gravatar icon Sean

    Thanks! I’ll keep an eye on the comments for this entry.

  • It does not work with windows, I get for each gem the error “the system cannot find the path specified”. I took a quick lok at the source, but didnt see what exactly was causing the error. The last command to delete the folders with “rm ..” obviously also does not work on windows.

  • I’m having the same problem – I’m not using Locomotive, but using rails 1.2.2 . I think it has to do with line 4 – you need to put in which gems you want in there. I changed that line and got rid of the error. Now, if I only knew an easy way of telling which gems I have install my app actually uses!

  • I just ran into the undefined version method problem.

    In my case this was because one of the other developers had already used this to freeze another gem, which I didn’t have on my development system. I fixed it by:

    1) checking lib for the gem source for the other gem to get the version. 2) installing the same version of the gem on my system (in this case there was only one version). 3) running rake freeze_other_gems again.

    Success!

  • Hola, I had the same problem too, and believed it was because the gem could not find the installation directory of the gem; make sure the gem is really installed locally too. You would have to check your gem environement to see wich is the path for the installation directory : gem environment.

    At the end I updated the gem to the latest version 0.9.4 and everything works fine again. sudo gem update—system

    Gracias for the rake it works excellent !

  • the classses that are we write in lib directory automatically load when the application start.and i also want to know all the features of lib directoy.

  • Gravatar icon topfunky

    The lib directory is just another place to keep Ruby code. Rails organizes things so you have app/controllers, app/models, etc. but they are really just ways to organize more Ruby code.

    The lib directory is in the load path, which means that it will be searched when you do require 'foo'.

    Techniques like “vendor everything” add more directories to the load path so they will be searched for Ruby files when you call require.

Your Comment

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Ads by The Lounge

Manufactured with

Subscribe

Subscribe (RSS)