Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Don't know if you found your solution, but there's a Railscast episode on how to make generators using Thor as was done in Rails 3. This should help. There's also this tutorial via Platformatec.

I realize you don't want to make Rails generators, but the basic idea behind how they work is pretty well explained, and I think this will help you get on the right track.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top