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

Adds support to change the top level directory for plugins #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*.gem
.bundle
/Gemfile.lock
/coverage
/doc
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ plugins.types => Array of types
- `type: "<type>"` - Load plugins only of given type, optional, makes `type` method accessible.
- `prefix: "/prefix"` - Load plugins only from this paths, optional, default `/lib`.
- `extends: %i[<extensions>]` - Extend pluginator with given extensions.
- `base_dir: 'plugins'` - the top level directory name to use when looking for plugins, defaults to "plugins"

## Extensions

Expand Down
17 changes: 9 additions & 8 deletions lib/pluginator/autodetect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ class Autodetect

# Automatically load plugins for given group (and type)
#
# @param group [String] name of the plugins group
# @param options [Hash] options to pass to creating Pluginator instance
# @option type [String] name of the plugin type
# @option prefix [String] a prefix for finding plugins if forcing,
# by default only `/lib` is checked,
# regexp notation is allowed, for example `/(lib|local_lib)`

# @param group [String] name of the plugins group
# @param options [Hash] options to pass to creating Pluginator instance
# @option type [String] name of the plugin type
# @option prefix [String] a prefix for finding plugins if forcing,
# by default only `/lib` is checked,
# regexp notation is allowed, for example `/(lib|local_lib)`
# @option base_dir [String] the top level directory name to use when looking for plugins
def initialize(group, options={})
super(group)
@force_prefix = options[:prefix]
@force_type = options[:type]
@base_dir = options[:base_dir] || "plugins"
refresh
end

Expand All @@ -55,7 +56,7 @@ def initialize(group, options={})
#
# Use it after gem list change, for example after `Gem.install("new_gem")`
def refresh
plugin_lists = FormattedFinder.new(@force_prefix, @group, @force_type)
plugin_lists = FormattedFinder.new(@force_prefix, @base_dir, @group, @force_type)
register_plugins(plugin_lists.loaded_plugins_path)
load_plugins(plugin_lists.load_path_plugins_paths)
activate_plugins(plugin_lists.gem_plugins_paths)
Expand Down
8 changes: 5 additions & 3 deletions lib/pluginator/autodetect/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class Finder
# @param force_prefix [String] a prefix for finding plugins if forcing,
# by default only `/lib` is checked,
# regexp notation is allowed, for example `/[lib|]`
# @param base_dir [String] the top level directory name to use when looking for plugins
# @param group [String] name of the plugins group
# @param force_type [String] name of the plugin type if forcing
def initialize(force_prefix, group, force_type)
def initialize(force_prefix, base_dir, group, force_type)
@force_prefix = force_prefix
@base_dir = base_dir || "plugins"
@group = group
@force_type = force_type
@pattern = file_name_pattern
Expand All @@ -44,7 +46,7 @@ def initialize(force_prefix, group, force_type)

# group => pattern
def file_name_pattern
"plugins/#{@group}/#{@force_type || "**"}/*.rb"
File.join(@base_dir, @group, (@force_type || "**"), "*.rb" )
end

def find_paths
Expand Down Expand Up @@ -81,7 +83,7 @@ def split_file_names(file_names)
def split_file_name(file_name)
prefix = @force_prefix || "/lib"
type = @force_type || ".*"
match = file_name.match(%r{.*#{prefix}/(plugins/(#{@group}/(#{type})/[^/]*)\.rb)$})
match = file_name.match(%r{.*#{prefix}/(#{@base_dir}/(#{@group}/(#{type})/[^/]*)\.rb)$})
match[-3..-1] if match
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pluginator/autodetect/formatted_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Autodetect
class FormattedFinder < Finder

# Reformat plugin lists
def initialize(force_prefix, group, force_type)
def initialize(force_prefix, base_dir, group, force_type)
super
map_loaded_plugins
map_gem_plugins
Expand Down
11 changes: 6 additions & 5 deletions lib/pluginator/extendable_autodetect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ class ExtendableAutodetect < Autodetect
# Automatically load plugins for given group (and type)
# Extend instance with extensions if given.
#
# @param group [String] name of the plugins group
# @param options [Hash] options to pass to creating Pluginator instance
# @option type [String] name of type to load
# @option extends [Array<Symbol>|Symbol] list of extension to extend into pluginator instance
# @param group [String] name of the plugins group
# @param options [Hash] options to pass to creating Pluginator instance
# @option type [String] name of type to load
# @option extends [Array<Symbol>|Symbol] list of extension to extend into pluginator instance
# @option base_dir [String] the top level directory name to use when looking for plugins
def initialize(group, options={})
super(group, options)
super
extend_plugins(options[:extends] || [])
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=begin
Copyright 2017 Michal Papis <[email protected]>

This file is part of pluginator.

pluginator is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

pluginator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with pluginator. If not, see <http://www.gnu.org/licenses/>.
=end

module Test
module Kitchen
class Sink
def self.type
"sink"
end
end
end
end
23 changes: 19 additions & 4 deletions test/pluginator/autodetect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ def gem_files(*paths)
end

describe Pluginator::Autodetect do
before do
@math_parsed = [
let(:math_files) do
gem_files(math_parsed.map(&:first))
end

let(:math_parsed) do
[
["plugins/something/math/increase.rb", "something/math/increase", "math"],
["plugins/something/math/decrease.rb", "something/math/decrease", "math"]
]
@math_files = gem_files(@math_parsed.map(&:first))
@all_files = gem_files(
end

let(:all_files) do
gem_files(
"plugins/something/math/increase.rb", "plugins/something/math/decrease.rb",
"plugins/something/nested/structure/test.rb", "plugins/something/stats/max.rb"
)
Expand All @@ -64,6 +70,15 @@ def gem_files(*paths)
plugins.wont_include("Something::Math::Add")
end

it :loads_plugins_automatically_from_another_base do
pluginator = Pluginator::Autodetect.new("test", :base_dir => 'kitchen')
pluginator.types.must_include("kitchen")
pluginator.types.size.must_equal(1)
plugins = pluginator["kitchen"].map(&:to_s)
plugins.size.must_equal(1)
plugins.must_include("Test::Kitchen::Sink")
end

it :loads_plugins_automatically_for_group_type do
pluginator = Pluginator::Autodetect.new("something", :type => "stats")
pluginator.types.sort.must_equal(["stats"])
Expand Down