Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load subcommand plug-ins at runtime #1

Merged
merged 1 commit into from
Nov 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/claide/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def parse(argv)
# @return [void]
#
def run(argv)
load_plugins
command = parse(argv)
command.validate!
command.run
Expand Down Expand Up @@ -261,6 +262,39 @@ def banner(colorize = false)
Banner.new(self, colorize).formatted_banner
end

# Load additional plugins via rubygems looking for:
#
# <command-path>/plugin.rb
#
# where <command-path> is the namespace of the Command converted to a
# path, for example:
#
# Pod::Command
#
# maps to
#
# pod/command
#
def load_plugins
if Gem.respond_to? :find_latest_files
Gem.find_latest_files("#{underscore(name)}/plugin").each {|path| require path }
else
Gem.find_files("#{underscore(name)}/plugin").each {|path| require path }
end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make underscore private and add a link to ActiveSupport’s LICENSE file?

private

# Cribbed from ActiveSupport
# https://github.com/rails/rails/blob/master/activesupport/MIT-LICENSE
def underscore(str)
underscored = str.to_s.dup
underscored.gsub!(/::/, '/')
underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
underscored.tr!("-", "_")
underscored.downcase!
end
end

#-------------------------------------------------------------------------#
Expand Down
33 changes: 33 additions & 0 deletions spec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ module CLAide
Fixture::Command.parse(%w{ spec-file --verbose lint }).should.be.instance_of Fixture::Command::SpecFile::Lint
#Fixture::Command.parse(%w{ spec-file lint --help repo }).should.be.instance_of Fixture::Command::SpecFile::Lint::Repo
end

describe "plugins" do
describe "when the plugin is at <command-path>/plugin.rb" do
PLUGIN_FIXTURE = Pathname.new(ROOT) + 'spec/fixture/command/plugin_fixture.rb'
PLUGIN = Pathname.new(ROOT) + 'spec/fixture/command/plugin.rb'

before do
FileUtils.copy PLUGIN_FIXTURE, PLUGIN
end

after do
FileUtils.remove_file PLUGIN
end

it "loads the plugin" do
Fixture::Command.subcommands.find {|subcmd| subcmd.command == 'demo-plugin'}.should.be.nil
Fixture::Command.load_plugins
plugin_class = Fixture::Command.subcommands.find {|subcmd| subcmd.command == 'demo-plugin'}
plugin_class.ancestors.should.include Fixture::Command
plugin_class.description.should =~ /plugins/
end

it "is available for help" do
Fixture::Command.load_plugins
CLAide::Command::Banner.new(Fixture::Command, false).formatted_banner.should =~ /demo-plugin/
end
end

it "fails normally if there is no plugin" do
Fixture::Command.load_plugins
Fixture::Command.subcommands.find {|subcmd| subcmd.name == 'demo-plugin' }.should.be.nil
end
end
end

#-------------------------------------------------------------------------#
Expand Down
18 changes: 18 additions & 0 deletions spec/fixture/command/demo_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: utf-8
module Fixture
class Command
class DemoPlugin < Command
self.summary = 'Plugins!'
self.description = <<-DESC
Let’s add plugins to CLAide and CocoaPods.
DESC
self.arguments = '[NAME]'

attr_reader :name
def initialize(argv)
@name = argv.shift_argument
super
end
end
end
end
3 changes: 3 additions & 0 deletions spec/fixture/command/plugin_fixture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Load a CLAide plugin by requiring it in your `plugin.rb` or by
# defining it directly in the file.
require 'fixture/command/demo_plugin'