简体   繁体   English

将Thor用于Ruby gem中的生成器

[英]Using Thor for generators in a Ruby gem

How do I set up a gem to have a binary command, eg "project newProject" that uses Thor's set of generator commands to create files, etc.? 如何设置gem以获得二进制命令,例如“project newProject”使用Thor的一组生成器命令来创建文件等?

A good answer would describe how to lay out the skeleton of a gem that when run from the command line, ie "project newProject", creates one file named "newProject.txt" in the directory it's run from. 一个好的答案将描述如何布置gem的骨架,当从命令行运行时,即“project newProject”,在它运行的目录中创建一个名为“newProject.txt”的文件。

I've seen that Rails 3 is using Thor to power its generators; 我已经看到Rails 3使用Thor为其发电机供电; 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. 似乎是一个非常好的解决方案,我想在我正在研究的非Rails ruby​​ gem中使用类似的方法。 Tried looking at the Rails 3 source but it's a bit labyrinthine, hence the question. 尝试看Rails 3来源,但它有点迷宫,因此问题。

in your gem project, you need to include a "bin" folder. 在您的gem项目中,您需要包含一个“bin”文件夹。 this folder needs to include the ruby script that is your generator, without a file extension. 此文件夹需要包含作为生成器的ruby脚本,而不包含文件扩展名。 if you are using something like jeweler, it will automatically scan the bin folder during packaging. 如果你使用像珠宝商这样的东西,它会在包装过程中自动扫描bin文件夹。 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. 安装gem时,gem系统会将bin文件夹中的文件放入ruby安装中,以便您可以像想要的那样运行它们。

the bin files are just plain old ruby scripts, nothing special. bin文件只是普通的旧ruby脚本,没什么特别的。

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. 不知道你是否找到了你的解决方案,但有一个关于如何使用Thor创建生成器的Railscast插曲 ,如在Rails 3中所做的那样。这应该有所帮助。 There's also this tutorial via Platformatec. 还有通过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. 我意识到你不想制作Rails生成器,但是它们工作方式背后的基本思想得到了很好的解释,我认为这将帮助你走上正确的轨道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM