Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Thor for generators in a Ruby gem

How do I set up a gem to have a binary command, e.g. "project newProject" that uses Thor's set of generator commands to create files, etc.?

A good answer would describe how to lay out the skeleton of a gem that when run from the command line, i.e. "project newProject", creates one file named "newProject.txt" in the directory it's run from.

I've seen that Rails 3 is using Thor to power its generators; seems like a really good solution and I'd like to use a similar approach in a non-Rails ruby gem I'm working on. Tried looking at the Rails 3 source but it's a bit labyrinthine, hence the question.

like image 532
David Burrows Avatar asked Dec 29 '22 20:12

David Burrows


1 Answers

in your gem project, you need to include a "bin" folder. this folder needs to include the ruby script that is your generator, without a file extension. if you are using something like jeweler, it will automatically scan the bin folder during packaging. when the gem is installed, the gem system will put the files from the bin folder into your ruby installation so that you can run them like you are wanting.

the bin files are just plain old ruby scripts, nothing special.

like image 84
Derick Bailey Avatar answered Jan 15 '23 10:01

Derick Bailey