Skip to content

Commit 0f55c1e

Browse files
committed
Updates Registry and Raw Command loader to allow loading plugins from command files
1 parent 4819cac commit 0f55c1e

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

lib/git_commander/command/loaders/raw.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "../../loader"
4+
require_relative "../../plugin/loader"
45

56
module GitCommander
67
class Command
@@ -35,6 +36,11 @@ def command(name, &block)
3536
configuration_error.set_backtrace e.backtrace
3637
result.errors << configuration_error
3738
end
39+
40+
def plugin(name, **options)
41+
plugin_result = GitCommander::Plugin::Loader.new(registry).load(name, **options)
42+
result.plugins |= plugin_result.plugins
43+
end
3844
end
3945
end
4046
end

lib/git_commander/registry.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "loader"
4+
require_relative "plugin/loader"
45
require_relative "command/loaders/file_loader"
56
require_relative "command/loaders/raw"
67

@@ -10,10 +11,11 @@ class Registry
1011
class CommandNotFound < StandardError; end
1112
class LoadError < StandardError; end
1213

13-
attr_accessor :commands, :name
14+
attr_accessor :commands, :name, :plugins
1415

1516
def initialize
1617
@commands = {}
18+
@plugins = {}
1719
end
1820

1921
# Adds a command to the registry
@@ -33,12 +35,24 @@ def register_command(command)
3335
commands[command.name] = command
3436
end
3537

38+
# Adds a pre-built Plugin to the registry
39+
# @param [Plugin] plugin the Plugin instance to add to the registry
40+
def register_plugin(plugin)
41+
GitCommander.logger.debug "[#{logger_tag}] Registering plugin `#{plugin.name}`..."
42+
43+
plugins[plugin.name] = plugin
44+
end
45+
3646
# Adds command(s) to the registry using the given loader
3747
#
3848
# @param [CommandLoader] loader the class to use to load with
3949
def load(loader, *args)
4050
result = loader.new(self).load(*args)
41-
result.commands.each { |cmd| register_command(cmd) } if result.success?
51+
52+
if result.success?
53+
result.plugins.each { |plugin| register_plugin(plugin) }
54+
result.commands.each { |cmd| register_command(cmd) }
55+
end
4256

4357
result
4458
end

spec/lib/git_commander/command/loaders/raw_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,42 @@
100100
expect(resulting_error.message).to include "undefined method \`danger!"
101101
expect(resulting_error.backtrace).to_not be_empty
102102
end
103+
104+
context "with plugins" do
105+
it "registers comamnds with native plugins" do
106+
raw_command_string = <<~COMMANDS
107+
plugin :git
108+
109+
command :local_branches do |cmd = nil|
110+
cmd.on_run do |options|
111+
say git.branches.local.map(&:name)
112+
end
113+
end
114+
115+
command :love do
116+
end
117+
COMMANDS
118+
119+
native_plugin_loader_spy = spy("Native Plugin Loader")
120+
native_plugin_loader_result = GitCommander::LoaderResult.new
121+
native_plugin_loader_result.plugins << GitCommander::Plugin.new(:git, registry: registry)
122+
expect(loader.result.commands).to be_empty
123+
expect(GitCommander::Plugin::Loader).to receive(:new).with(registry).and_return(native_plugin_loader_spy)
124+
expect(native_plugin_loader_spy).to receive(:load).with(:git, {}).and_return(native_plugin_loader_result)
125+
126+
loader.load(raw_command_string)
127+
128+
expect(loader.result.plugins.size).to eq 1
129+
expect(loader.result.commands.size).to eq 2
130+
131+
(loader.result.commands + loader.result.plugins).flatten.each do |command_or_plugin|
132+
expect(command_or_plugin.registry).to eq registry
133+
end
134+
135+
registered_command = loader.result.commands.first
136+
output = spy("output")
137+
registered_command.output = output
138+
end
139+
end
103140
end
104141
end

spec/lib/git_commander/registry_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
expect { registry.find(:wtf) }.to raise_error GitCommander::Registry::CommandNotFound
9898
end
9999

100+
it "allows registering pre-built plugins" do
101+
plugin = GitCommander::Plugin.new(:wtf)
102+
registry.register_plugin plugin
103+
expect(registry.plugins[:wtf]).to eq(plugin)
104+
end
105+
100106
describe "#load(loader, options = {})" do
101107
it "uses the provided loader to load the given options" do
102108
options = { content: "command :hello do |cmd|; cmd.on_run { say 'hello' }; end" }
@@ -108,5 +114,37 @@
108114

109115
registry.load(loader_class_spy, options)
110116
end
117+
118+
it "registers commands that are successfully loaded" do
119+
options = { content: "command :hello do |cmd|; cmd.on_run { say 'hello' }; end" }
120+
loader_class_spy = spy("loader class")
121+
loader_instance_spy = spy("loader instance")
122+
results = GitCommander::LoaderResult.new
123+
results.commands = [GitCommander::Command.new(:hello)]
124+
125+
expect(loader_class_spy).to receive(:new).with(registry).and_return(loader_instance_spy)
126+
expect(loader_instance_spy).to receive(:load).with(options).and_return(results)
127+
results.commands.each do |cmd|
128+
expect(registry).to receive(:register_command).with(cmd)
129+
end
130+
131+
registry.load(loader_class_spy, options)
132+
end
133+
134+
it "registers plugins that are successfully loaded" do
135+
options = { content: "plugin :git;" }
136+
loader_class_spy = spy("loader class")
137+
loader_instance_spy = spy("loader instance")
138+
results = GitCommander::LoaderResult.new
139+
results.plugins = [GitCommander::Plugin.new(:git)]
140+
141+
expect(loader_class_spy).to receive(:new).with(registry).and_return(loader_instance_spy)
142+
expect(loader_instance_spy).to receive(:load).with(options).and_return(results)
143+
results.plugins.each do |plugin|
144+
expect(registry).to receive(:register_plugin).with(plugin)
145+
end
146+
147+
registry.load(loader_class_spy, options)
148+
end
111149
end
112150
end

0 commit comments

Comments
 (0)