pluginator 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +5 -0
- data/README.md +86 -0
- data/lib/pluginator.rb +84 -0
- data/pluginator.gemspec +17 -0
- metadata +51 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Pluginator
|
2
|
+
|
3
|
+
Gem plugin system management, detects plugins using `Gem.find_file`.
|
4
|
+
Is only supposed with ruby 1.9.2+
|
5
|
+
|
6
|
+
Pluginator tries to stay out of your way, you do not have to include or inherit anything.
|
7
|
+
Pluginator only finds and groups plugins, rest is up to you,
|
8
|
+
you decide what methods to define and how to find them.
|
9
|
+
|
10
|
+
## Defining plugins
|
11
|
+
|
12
|
+
crate a gem with a path:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
plugins/<prefix>/<type>/<name>.rb
|
16
|
+
```
|
17
|
+
|
18
|
+
with a class inside:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
<prefix>::<type>::<name>
|
22
|
+
```
|
23
|
+
|
24
|
+
where `<type>` can be nested
|
25
|
+
|
26
|
+
## Loading plugins
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
rvm2plugins = Pluginator.new("<prefix>")
|
30
|
+
type_plugins = rvm2plugins["<type>"]
|
31
|
+
types = rvm2plugins.types
|
32
|
+
```
|
33
|
+
|
34
|
+
## Examples
|
35
|
+
|
36
|
+
### Example 1 - task plugins
|
37
|
+
|
38
|
+
`plugins/rvm2/cli/echo.rb`:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class Rvm2::Cli::Echo
|
42
|
+
def self.question? command
|
43
|
+
command == "echo"
|
44
|
+
end
|
45
|
+
def answer param
|
46
|
+
puts param
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
where `question?` and `answer` are user defined methods
|
52
|
+
|
53
|
+
Now the plugin can be used:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require 'pluginator'
|
57
|
+
|
58
|
+
rvm2plugins = Pluginator.new("rvm2")
|
59
|
+
plugin = rvm2plugins["cli"].first{ |plugin|
|
60
|
+
plugin.question?('echo')
|
61
|
+
}
|
62
|
+
plugin.new.answer("Hello world")
|
63
|
+
```
|
64
|
+
|
65
|
+
### Example 2 - hook plugins
|
66
|
+
|
67
|
+
`plugins/rvm2/hooks/after_install/show.rb`:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class Rvm2::Hooks::AfterInstall::Show
|
71
|
+
def self.execute name, path
|
72
|
+
puts "Ruby '#{name}' was installed in '#{path}'."
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
and using hooks:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
require 'pluginator'
|
81
|
+
|
82
|
+
rvm2plugins = Pluginator.new("rvm2")
|
83
|
+
plugin = rvm2plugins["hooks/after_install"].each{ |plugin|
|
84
|
+
plugin.execute(name, path)
|
85
|
+
}
|
86
|
+
```
|
data/lib/pluginator.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
class Pluginator
|
2
|
+
attr_reader :plugins_name
|
3
|
+
|
4
|
+
def initialize(plugins_name)
|
5
|
+
@plugins = {}
|
6
|
+
@plugins_name = plugins_name
|
7
|
+
detect
|
8
|
+
end
|
9
|
+
|
10
|
+
def first_plugin type, method, *args
|
11
|
+
result = nil
|
12
|
+
name, plugin = all_by_type(type).detect do |name, plugin|
|
13
|
+
result = plugin.send(method.to_sym, *args)
|
14
|
+
end
|
15
|
+
[ name, plugin, result ]
|
16
|
+
end
|
17
|
+
|
18
|
+
def all_by_type type
|
19
|
+
@plugins[type.to_s]
|
20
|
+
end
|
21
|
+
alias :[] :all_by_type
|
22
|
+
|
23
|
+
def types
|
24
|
+
@plugins.keys
|
25
|
+
end
|
26
|
+
|
27
|
+
# TODO do we need those???
|
28
|
+
def all_plugins type, method, *args
|
29
|
+
@plugins[type.to_s].select do |name,plugin|
|
30
|
+
plugin.send(method.to_sym, *args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
def first_plugin_by_parent parent_class
|
34
|
+
@plugins.detect do |type, plugins|
|
35
|
+
plugins.detect do |name, plugin|
|
36
|
+
plugin < parent_class
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
def find_class_by_parent parent_class
|
41
|
+
classes = []
|
42
|
+
ObjectSpace.each_object(Class) do |klass|
|
43
|
+
classes << klass if klass < parent_class
|
44
|
+
end
|
45
|
+
classes
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def detect
|
51
|
+
Gem.find_files("plugins/#{@plugins_name}/*/*.rb").each do |file_name|
|
52
|
+
# $stderr.puts "parsing #{file_name}"
|
53
|
+
path, name, type = file_name.match(/.*\/(plugins\/(#{@plugins_name}\/(.*)\/[^\/]*)\.rb)$/)[1..3]
|
54
|
+
load_plugin path, file_name
|
55
|
+
register_plugin type, name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def load_plugin path, file_name
|
60
|
+
gemspec = Gem::Specification.find_by_path(path)
|
61
|
+
gemspec.activate if gemspec && !gemspec.activated?
|
62
|
+
require path
|
63
|
+
end
|
64
|
+
|
65
|
+
def register_plugin type, name
|
66
|
+
type = type.to_s
|
67
|
+
@plugins[type] ||= {}
|
68
|
+
if @plugins[type][name].nil?
|
69
|
+
# $stderr.puts "loading #{name}"
|
70
|
+
@plugins[type][name] = name2class(name)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def name2class name
|
75
|
+
klass = Kernel
|
76
|
+
name.split(/\//).map{ |part|
|
77
|
+
part.capitalize.gsub(/_(.)/){ $1.upcase }
|
78
|
+
}.each{|part|
|
79
|
+
klass = klass.const_get( part )
|
80
|
+
}
|
81
|
+
klass
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
data/pluginator.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- encoding: utf-8 -*-
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "pluginator"
|
6
|
+
s.version = "0.9.0"
|
7
|
+
s.authors = ["Michal Papis"]
|
8
|
+
s.email = ["mpapis@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/mpapis/pluginator"
|
10
|
+
s.summary = %q{Rubygesm plugin system using Gem.find_files.}
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.required_ruby_version = '>= 1.9.2'
|
15
|
+
|
16
|
+
#s.add_development_dependency "smf-gem"
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pluginator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michal Papis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
- mpapis@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- lib/pluginator.rb
|
25
|
+
- pluginator.gemspec
|
26
|
+
homepage: https://github.com/mpapis/pluginator
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.9.2
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.25
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Rubygesm plugin system using Gem.find_files.
|
50
|
+
test_files: []
|
51
|
+
has_rdoc:
|