Command line utilities in rubygems?

C

Ch Ba

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.
 
D

dusty

 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.

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 = "(e-mail address removed)"
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
 
C

Ch Ba

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.
 
C

Ch Ba

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 =)
 
D

dusty

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 =)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top