Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I structure my ruby gem command line service?

I'm writing a ruby gem that users can install and use the command line ruby tool to interact with the service. You can start and stop the service (it will spawn off a child process).

I've done a lot of research into the best things to use to write a network service, such as ØMQ/EventMachine and I get how to create a Ruby gem that will install a binary you can use in the command line, but I'm struggling to set out a good code structure.

My command line utility will take various arguments (I'll use Trollop) and the it will use various classes to do things, and use various other ruby gems.

I'm not sure where I should put my class files, and how to require them in my binary so the paths are correct.

like image 255
tarnfeld Avatar asked Dec 12 '22 04:12

tarnfeld


1 Answers

Largely, RubyGems will take care of this for you. You'll need to include your executable in the files list, and put it in the executables in your gemspec. It's common to put your executable in bin in your directory, e.g.:

$ ls
bin/   myapp.gemspec  lib/    Rakefile
$ ls bin
bin/myapp

Your gemspec would then look like:

Gem::Specification.new do |s|
  s.name = 'myapp'

  # whatever else is in your gemspec

  s.files = ["bin/myapp","lib/myapp.rb"]  # or whatever other files you want
  s.executables = ["bin/todo"] 
end

At this point, when users install your app via RubyGems, myapp will be in their path, and lib will be in your app's loadpath, so your executable can simply start off with:

#!/usr/bin/env ruby

require 'myapp'
# whatever other requires

The only issue with this is that, during development, you cannot just do bin/myapp and have your app run. Some devs manipulate the load path via $: or $LOAD_PATH, but this is considered bad form.

If you are using bundler, it's easiest to just run your app locally with bundle exec, e.g. bundle exec bin/myapp. You can alternately use the RUBYLIB environment variable, e.g. RUBYLIB=lib bin/myapp, which will put lib in the load path.

like image 110
davetron5000 Avatar answered Dec 24 '22 17:12

davetron5000