thinking about things from eight sides

Things I Learned: Git Add Your Gem's Generator Files


I’ve been spending some of my free time creating a Ruby Gem for Rails apps that makes it easy to set up tables with fixed data that you want to always be present in your database: static-data. This is handy for lookup tables, predefined category lists, and other such things.

The Problem

I created some new files to add a new rails generator to static-data, I built and installed my gem, and… nothing. In researching other people’s challenges in getting their generators to work, I tried moving my generator file all over the place. I even tried adding extra code to my railtie to tell Rails where the generator file was, but that just caused rails generate to blow up entirely when it couldn’t find my generator file.

After scratching my head for more than a few minutes, I hacked up an existing gem that was showing up in rails generate, tracked down the code that attempts to require generator files (which contains a rescue clause that swallows all errors in a generator being required—bad code! no biscuit!), and eventually figured out that my generator file wasn’t even included in the installed gem files!

The Solution!

Then it dawned on me: the gemspec for my static-data gem is set up to populate the gem’s file list from git ls-files, but since I’d set that up a few weeks ago (I don’t have much free time, and it tends to come in small, infrequent chunks), I had totally forgotten that little—but very important—detail.

Once I did a git add lib/generators in my gem repository, rebuilt and reinstalled my gem, the generator was finally listed in rails generate and with a few tweaks to my template, my generator was in business.

Apparently this particular gotcha isn’t biting too many other people, since no one ever mentioned this in my search for clues on the interwebs. Nevertheless, I am writing this detail down so hopefully the next person with this issue won’t have to scratch their head for quite as long as I did.