Skip to content

Commit

Permalink
add update_plugin updating a plugin afeter loading (#154, rggen/rgg…
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani authored Jan 17, 2025
1 parent acb3f2e commit 60250b9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/rggen/core/builder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def register_input_components
def_delegator :plugin_manager, :load_plugin
def_delegator :plugin_manager, :load_plugins
def_delegator :plugin_manager, :setup_plugin
def_delegator :plugin_manager, :update_plugin

private

Expand Down
13 changes: 12 additions & 1 deletion lib/rggen/core/builder/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@ def setup_plugin(plugin_name, &)
@plugins << PluginSpec.new(plugin_name, &)
end

def update_plugin(plugin_name)
plugin =
find_plugin(plugin_name) ||
(raise PluginError.new("unknown plugin: #{plugin_name}"))
block_given? && yield(plugin)
end

def activate_plugins
do_normal_activation
do_addtional_activation
end

def activate_plugin_by_name(plugin_name)
@plugins.find { |plugin| plugin.name == plugin_name }
find_plugin(plugin_name)
&.then do |plugin|
plugin.activate(@builder)
plugin.activate_additionally(@builder)
Expand All @@ -103,6 +110,10 @@ def read_plugin_file(info)
raise Core::PluginError.new("cannot load such plugin: #{info}")
end

def find_plugin(plugin_name)
@plugins.find { |plugin| plugin.name == plugin_name }
end

def activate_plugin_gem(info)
if (gemspec = find_gemspec(info))
gem gemspec.name, gemspec.version
Expand Down
3 changes: 2 additions & 1 deletion lib/rggen/core/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module DSL
:enable,
:enable_all,
:delete,
:setup_plugin
:setup_plugin,
:update_plugin,
].each do |method_name|
def_delegator :'RgGen.builder', method_name
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rggen/core/builder/plugin_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,22 @@ def setup_plugin_expectation(**args)
end
end
end

describe '#update_plugin' do
it '指定されたプラグインを更新する' do
plugin = nil
plugin_manager.setup_plugin(:foo) { plugin = _1 }

expect { |b| plugin_manager.update_plugin(:foo, &b) }
.to yield_with_args(equal(plugin))
end

context '指定されたプラグインが未定義の場合' do
it 'PluginErrorを起こす' do
plugin_manager.setup_plugin(:foo) {}
expect { plugin_manager.update_plugin(:bar) }
.to raise_rggen_error RgGen::Core::PluginError, 'unknown plugin: bar'
end
end
end
end
4 changes: 0 additions & 4 deletions spec/rggen/core/builder/plugin_spec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# frozen_string_literal: true

RSpec.describe RgGen::Core::Builder::PluginSpec do
let(:plugin_spec) do
described_class.new(plugin_name, plugin_module)
end

let(:plugin_name) do
:foo
end
Expand Down

0 comments on commit 60250b9

Please sign in to comment.