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?
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:Scaffoldergems must be namedscaffold-tools-plugin-pluginnameScaffolder::Plugin::PluginnameGiven that, you can then accept a command-line argument of the plugins to load (assuming
OptionParser):Then:
Now
plugin_classesis anArrayof the class objects for the plugins configured. Supposing they all conform to some common constructor and some common methods: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.