0

I've written a command-line tool for manipulating with genome scaffolds called "Scaffolder". At the moment all the tools I want to use are hard-coded into the library. For instance these tools "validate" or "build" the scaffold. I'd like to split these tools out into their own gems, make it more modular, and to allow third parties to write their own commands.

The ideal case would be that I run "gem install scaffolder-validate" and this gem-bundled command would then be available as part of scaffolder. I know a couple of libraries make it easy to build a command-line interface: thor, commander, gli, .... However I don't think any of them cater for this type of functionality.

My question is how can I use a gem structure to create a module structure for installing these commands? Specifically how can the installed commands be auto-detected and loaded? With some prefix in the gem name scaffolder-* then searching rubygems? How could I test this with cucumber?

2
  • I'm not 100% clear on what you are asking. Do you want to create a command-line tool that then detects if certain "plugins" are installed and, for each one that's installed, surface that plugin as a feature/option of the command-line app? Sep 6, 2011 at 12:06
  • That's exactly what I'd like to do. Ideally I'd like to use ruby gems to manage the plugins rather than roll my own. Sep 8, 2011 at 0:45

2 Answers 2

1

So, one thing you can do is to decide on a canonical name for your plugins, and then use that convention to load things dynamically.

It looks like your code is all under a module Scaffolder, so you can create plugins following the following rules:

  • Scaffolder gems must be named scaffold-tools-plugin-pluginname
  • All plugins expose one class, named Scaffolder::Plugin::Pluginname
  • That class must conform to some interface you document, and possibly provide a base class for

Given that, you can then accept a command-line argument of the plugins to load (assuming OptionParser):

plugin_names = []
opts.on('--plugins PLUGINS','List of plugins') do |plug|
  plugin_names << plug
end

Then:

plugin_classes = []
plugin_names.each do |plugin_name|
  require "scaffold-tools-plugin-#{plugin_name}"
  plugin_classes << Kernel.const_get("Scaffold::Plugin::#{plugin_name}")
end

Now plugin_classes is an Array of the class objects for the plugins configured. Supposing they all conform to some common constructor and some common methods:

plugin_classes.each do |plugin_class|
  plugin = plugin_class.new(args)
  plugin.do_its_thing(other,args)
end

Obviously, when doing a lot of dynamic class loading like this, you need to be careful and trust the code that you are running. I'm assuming for such a small domain, it won't be a concern, but just be wary of requireing random code.

1
  • That should work, thank you. I think I'll try to specify the plugins in a scaffolder specific gemfile. That way the versions of plugins can be managed. Sep 13, 2011 at 1:34
1

Hm, tricky one. One simple idea I have is that the main gem just tries to require all the others and catches the load error when they are not there and disables the respective features. I do this in one of my gems. If HighLine is present, the user gets prompted for a password, if it isn't there has to be a config file.

begin
  require 'highline'
rescue LoadError
  highline = false
end

If you have a lot of gems this could become ugly though...

7
  • I think this would make it difficult for third-party developers to write plugins. I would have to update the code with a require statement for each library. Aug 31, 2011 at 13:31
  • Fair enough, I didn't know if only you would develop scafold-* commands. Aug 31, 2011 at 13:37
  • I was thinking there might be some way in rubygems to search for all gems beginning with the scaffold- prefix. This would allow other parties to develop gems as well. Aug 31, 2011 at 14:23
  • 1
    I think I prefer the ./scaffolder option, there's no guarantee someone else won't create a gem named scaffolder-something, even it's unlikely. Aug 31, 2011 at 14:46
  • 1
    I'd probably go with Thor. You know the good old YAGNI (you ain't gonna need it) rule? If you like Thor's syntax and it can handle what you need it for now, go with it. BTW: this discussion is getting long, that's not what SO comments are for. My Twitter is on my profile if you want to continue this discussion. :-) Aug 31, 2011 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.