Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Gem command line tool

I am trying to build a command line ruby gem. I am using bundler to create the gem and install the gem locally. 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 rspec works:

rspec test/whatever.rb

Any help on how to be able to execute through the command line would be great.

like image 702
wallerjake Avatar asked Mar 20 '23 19:03

wallerjake


1 Answers

In order to declared the executables you have to just make a proper line in your 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. Just execute it, and then fix the yourgem.gemspec according your needs. Put executables into bin/ folder of your gem, and all libraries, including the version, into lib/ folder.

The next step is to use the binary. When you are installing the gem 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 bundler's exec as follows:

 bundle exec bin/your_exec

It picks the require librarires up from lib/ folder, and the executable will work properly.

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.

like image 93
Малъ Скрылевъ Avatar answered Mar 31 '23 15:03

Малъ Скрылевъ