在 Ruby gem 中使用 Thor 作为生成器
如何设置 gem 具有二进制命令,例如“project newProject”,它使用 Thor 的生成器命令集来创建文件等?
一个好的答案将描述如何布置 gem 的骨架,当从命令行(即“project newProject”)运行时,会在运行它的目录中创建一个名为“newProject.txt”的文件。
我已经看到 Rails 3 使用 Thor 为其发电机提供动力;似乎是一个非常好的解决方案,我想在我正在开发的非 Rails ruby gem 中使用类似的方法。尝试查看 Rails 3 源代码,但它有点迷宫,因此提出了这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 gem 项目中,您需要包含一个“bin”文件夹。该文件夹需要包含作为生成器的 ruby 脚本,不带文件扩展名。如果您使用的是 Jeweler 之类的工具,它会在打包过程中自动扫描 bin 文件夹。安装 gem 后,gem 系统会将 bin 文件夹中的文件放入您的 ruby 安装中,以便您可以像您想要的那样运行它们。
bin 文件只是普通的旧 ruby 脚本,没什么特别的。
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.
不知道您是否找到了解决方案,但是有Railscast 剧集 如何使用 Thor 制作生成器,就像 Rails 3 中所做的那样。这应该会有所帮助。还有来自 Platformatec 的本教程 。
我知道你不想制作 Rails 生成器,但它们工作原理背后的基本思想已经得到很好的解释,我认为这将帮助你走上正确的轨道。
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.