简体   繁体   English

Ruby Gem命令行工具

[英]Ruby Gem command line tool

I am trying to build a command line . 我正在尝试构建一个命令行 I am using bundler to create the gem and install the gem locally. 我使用bundler来创建gem并在本地安装gem。 It generated the needed directories. 它生成了所需的目录。 I also was able to test that if I require my Gem I can use methods inside of it. 我也能够测试,如果我需要我的宝石,我可以使用它内部的方法。 I am trying to get the command line piece working now and can't seem to get it working. 我正在尝试让命令行工作现在正常工作,似乎无法使其正常工作。 I want to be able to do something like 我希望能够做类似的事情

gemname command

Similar to how works: 工作方式类似:

rspec test/whatever.rb

Any help on how to be able to execute through the command line would be great. 有关如何通过命令行执行的任何帮助都会很棒。

In order to declared the executables you have to just make a proper line in your yourgem.gemspec : 为了声明可执行文件,您必须在yourgem.gemspec中创建一个正确的行:

`git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

This line, along with an other useful line is generated by bundle gem yourgem command. 此行以及另一个有用的行由bundle gem yourgem命令生成。 Just execute it, and then fix the yourgem.gemspec according your needs. 只需执行它,然后根据您的需要修复yourgem.gemspec Put executables into bin/ folder of your gem, and all libraries, including the version, into lib/ folder. 将可执行文件放入gem的bin /文件夹,将所有库(包括版本)放入lib /文件夹。

The next step is to use the binary. 下一步是使用二进制文件。 When you are installing the into a system, the binary folder is automatically included into binary search path. 安装到系统中时,二进制文件夹会自动包含在二进制搜索路径中。 So your gem is avaiable to execute from anywere. 所以你的宝石可以从任何地方执行。 But when your gem isn't installed you are still able to simelate the case with a 's exec as follows: 但是,当没有安装你的宝石,你仍然能够simelate用的情况下如下的EXEC:

 bundle exec bin/your_exec

It picks the require librarires up from lib/ folder, and the executable will work properly. 它从lib /文件夹中选择了需要的librarires,并且可执行文件将正常工作。

To make sure that the executable will work, build the gem with gem build yourgem.gemspec , then install it with gem install yourgem.gem , and try. 要确保可执行文件能够正常工作,请使用gem build yourgem.gemspec ,然后使用gem install yourgem.gem安装它,并尝试。

I think the problem is that you've not commited your code yet. 我认为问题是你还没有提交你的代码。 Check if git ls-files -- bin/* shows you the script you want to execute. 检查git ls-files -- bin/*显示了您要执行的脚本。

When you commit your file, then git ls-files will return it and the spec will be able to load the script. 当您提交文件时, git ls-files将返回它,并且规范将能够加载脚本。

Checkout these blog posts: http://robdodson.me/blog/2012/06/14/how-to-write-a-command-line-ruby-gem/ http://rubylearning.com/blog/2011/01/03/how-do-i-make-a-command-line-tool-in-ruby/ 查看这些博客文章: http//robdodson.me/blog/2012/06/14/how-to-write-a-command-line-ruby-gem/ http://rubylearning.com/blog/2011/01 / 03 /怎么办-我-让-一个命令行工具功能于红宝石/

I'm not really sure what you've built so far but here are a few points of interest: 我不确定你到目前为止所建的是什么,但这里有一些兴趣点:

  1. #!/usr/bin/env ruby for first line in the executable file (in the /bin folder). #!/usr/bin/env ruby用于可执行文件中的第一行(在/bin文件夹中)。
  2. chmod +x filname to make it executable chmod +x filname使其可执行
  3. ARGV[0] Variables passed in are retrieved from ARGV[0] 传递的ARGV[0]变量从ARGV[0]中检索
  4. s.executables << 'your_file' Add executable to gemspec s.executables << 'your_file'将可执行文件添加到gemspec

I figured this out at least for my specific issue. 我至少在我的具体问题上想到了这一点。 I just need to call out the bin command directly instead of that git ls loop. 我只需要直接调用bin命令而不是git ls循环。 The top one works the bottom one doesn't for some reason. 最上面的一个是底部的,不是出于某种原因。

spec.executables = ["toolshed"]

vs VS

spec.executables = `git ls-files -- bin/*`.split("\\n").map{ |f| File.basename(f) }

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

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