Command line utilities in rubygems?

I have a small project that I’m working on and I would like the
executable to be put in the usr bin path and setup so that it can be
used as a command line utility automatically when installed using gem
install. How would I go about setting it up that way? I’ve been trying
to access the rubygems reference but it seems to be down.

On Dec 14, 12:57 am, Ch Ba [email protected] wrote:

I have a small project that I’m working on and I would like the
executable to be put in the usr bin path and setup so that it can be
used as a command line utility automatically when installed using gem
install. How would I go about setting it up that way? I’ve been trying
to access the rubygems reference but it seems to be down.

Posted viahttp://www.ruby-forum.com/.

You can simply add it to the Rakefile. Here is an example Rakefile
for a gem.

require ‘rubygems’
Gem::manage_gems
require ‘rake/gempackagetask’

spec = Gem::Specification.new do |s|
s.name = “some_gem”
s.version = “0.0.1”
s.author = “The Author”
s.email = “[email protected]
s.homepage = “http://www.author.com
s.platform = Gem::Platform::RUBY
s.summary = “Some Gem”
s.files = FileList[“{bin,tests,lib}/**/*”].to_a
s.require_path = “lib”
s.autorequire = “some_gem”
s.has_rdoc = true
s.extra_rdoc_files = [“README.txt”]
s.executables = [‘the_executable’]
s.add_dependency(‘dep1’)
s.add_dependency(‘dep2’)
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_tar = true
end

The part that says s.executables lists the files that will be
installed into the path. In this case, its calling the file named
the_executable, which would be in the bin subdirectory of the gem.

So, my directory structure would be

$ ls
README.txt bin/ pkg/
Rakefile lib/ test/

$ ls bin/
the_executable*

$ ls lib/
some_gem.rb

$ ls test/
test_some_gem.rb

Then to package it up, run rake gem. It will create a directory
called pkg, and place the gem in there.

Hope that is helpful.

Dusty Doris

One more quick question if you don’t mind, I had been executing the
executable from the base directory of the project, and using "require
‘lib/foo.rb’. Obviously this no longer works, how would I access
“foo.rb” in it’s new location after it is installed through a gem?
Sorry, I’m new to this whole gem thing =)

That is exactly what I needed! Thanks a million Dusty. I was using a
.gemspec file and gem build foo.gemspec in order to gem it up, and even
though I included the executable it wasn’t putting it in the bin. This
Rakefile is much more useful though, I need to learn more about them.

On Dec 14, 2:14 am, Ch Ba [email protected] wrote:

One more quick question if you don’t mind, I had been executing the
executable from the base directory of the project, and using "require
‘lib/foo.rb’. Obviously this no longer works, how would I access
“foo.rb” in it’s new location after it is installed through a gem?
Sorry, I’m new to this whole gem thing =)

Posted viahttp://www.ruby-forum.com/.

In the spec we have:

s.require_path = “lib”
s.autorequire = “some_gem”

So, when you require rubygems, it will know how to find your new gem.
Then, when you require ‘some_gem’, it will look in the require_path
based off the root of your gem and automatically require some_gem.rb.
In this case it would be lib/some_gem.rb.

So, in the executable, you can simply require your gem.

Here is an example of what the executable could look like, assuming
you have a class method called do_something that is doing what you
need.

#!/usr/bin/env ruby

require ‘rubygems’
require ‘some_gem’

SomeGem.do_something