Using Thor for Ruby Stone Generators

How to set up a gem to have a binary command, for example. "project newProject" which uses Thor generator commands to create files, etc.

A good answer will describe how to lay out the gem skeleton, which when launched from the command line, that is, "project newProject", creates a single file called "newProject.txt" in the directory in which it was launched.

I saw Rails 3 use Thor to power its generators; seems like a really good solution, and I would like to use a similar approach in a ruby ​​stone other than the Rails I'm working on. I tried to look at the source of Rails 3, but it is a little labyrinth, hence the question.

+4
source share
2 answers

in your gem project, you need to include the bin folder. this folder should include a ruby ​​script, which is your generator, without a file extension. if you use something like a jeweler, it will automatically scan the bin folder during packaging. when the gem is installed, the gem system will output the files from the bin folder to your ruby ​​installation so that you can run them as you want.

bin files are just old ruby ​​scripts, nothing special.

+5
source

I don’t know if you found your solution, but there’s an Railscast episode about how to create generators using Thor, as it was done in Rails 3. This should help. There is also this tutorial through Platformatec.

I understand that you do not want to create Rails generators, but the basic idea of ​​how they work is pretty well explained, and I think this will help you get on the right track.

+5
source

Source: https://habr.com/ru/post/1307023/


All Articles