everyday-plugins 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aef7ff8b3547cb61a4fe29ba134fc62a33899dea
4
+ data.tar.gz: 5ac025923337b937c39c2ae97aa76c725549619b
5
+ SHA512:
6
+ metadata.gz: 1c51fe1fc1b868bf1300d7955b073fe5e73da16845b20fe038fe9d5a8fb4dcec4ec1c27a77fa458d5455a701e0447d42702e45f4a41f456829e8aaa67087e51b
7
+ data.tar.gz: cba503a3718084028bc52d40eb44892bd490cd2809b79889209e35cbf7c30c3026ef57a74025e3ef64625c1ade6339bb8666f6c1d1373559605cd862b723da39
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in everyday-plugins.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eric Henderson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # EverydayPlugins
2
+
3
+ A simple gem plugin system. Extracted from `mvn2`. Plugins have to be defined in gems for them to be able to be picked up on automatically.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'everyday-plugins'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install everyday-plugins
18
+
19
+ ## Usage
20
+
21
+ ###For supporting plugins:
22
+ See below for info on defining and using plugin types. As for including plugins, use
23
+
24
+ ```ruby
25
+ EverydayPlugins::Plugins.load_plugins '{folder}'
26
+ ```
27
+
28
+ where `{folder}` is the folder name you specify for your plugins to be put in. For example, my `mvn2` gem uses
29
+
30
+ ```ruby
31
+ EverydayPlugins::Plugins.load_plugins 'mvn2'
32
+ ```
33
+
34
+ ###For creating plugins:
35
+
36
+ The files have to match the pattern `{folder}/plugin/*.plugin.rb` in your gem's `lib/` folder, where `{folder}` is whatever folder path the gem you are adding a plugin to requires. If you do that, when the gem is installed, the gem accepting plugins will automatically pick up on it and load it.
37
+
38
+ Two examples of this are my gems `colorconfig` and `mvn2-say`.
39
+
40
+ The `EverydayPlugins::Plugin` module is used for registering a plugin of a defined type. It defines the `register` method. You should use `extend` so that you can define things statically. There is no need for a `build` method because it automatically adds the plugins at the time you call the method.
41
+
42
+ Here is an example:
43
+
44
+ ```ruby
45
+ register :option, sym: :timer, names: %w(-t --timer), desc: 'display a timer while the build is in progress'
46
+ ```
47
+
48
+ And another:
49
+
50
+ ```ruby
51
+ register(:before_run, order: 2) { |options|
52
+ EverydayPlugins::Plugins.set_var :time1, Time.now
53
+ EverydayPlugins::Plugins.set_var :thread, options[:timer] ? Thread.new {
54
+ start_time = EverydayPlugins::Plugins.get_var :time1
55
+ while true
56
+ print "\r#{get_timer_message(start_time, Time.now)}"
57
+ sleep(0.05)
58
+ end
59
+ } : nil
60
+ }
61
+ ```
62
+
63
+ The `register` method takes a first parameter of the plugin type identifier, followed by an optional (but probably necessary) hash of options, and an optional block (usable with most built-in plugin types, required by some).
64
+
65
+ -----
66
+
67
+ If you want to define a new plugin type, you can extend the `EverydayPlugins::PluginType`. This defines the `register_type` and `register_variable` methods. Technically, you don't really need to register a variable, but if you want to give it a certain starting value, you will need to register it and pass over the value. `register_type` takes a first parameter of the plugin type identifier, followed by a block. The block will always take a first parameter of the list of instances, followed by any additional parameters passed to the plugin when it is called. The block has the task of taking the list of plugin instances and turning it into whatever result the plugin is supposed to have.
68
+
69
+ Here is an example:
70
+
71
+ ```ruby
72
+ register_type(:command_flag) { |list|
73
+ options = EverydayPlugins::Plugins.get_var :options
74
+ flags = []
75
+ list.each { |flag|
76
+ if flag[:block].nil?
77
+ flags << " #{flag[:options][:flag]}" if flag[:options].has_key?(:option) && options[flag[:options][:option]] == (flag[:options].has_key?(:value) ? flag[:options][:value] : true)
78
+ else
79
+ flag[:block].call(options, flags)
80
+ end
81
+ }
82
+ flags.join
83
+ }
84
+ ```
85
+
86
+ -----
87
+
88
+ You might notice in the second plugin example, as well as the plugin type example, methods are called on `EverydayPlugins::Plugins`. This is the class that stores all of the plugins and types. It has various methods, but the ones involved in making a plugin are:
89
+
90
+ * `EverydayPlugins::Plugins.get(type, *args)`
91
+ * Gets the result of a plugin type. The first parameter is the plugin type identifier, followed by any additional arguments the plugin type takes.
92
+ * `EverydayPlugins::Plugins.get_var(name)`
93
+ * Gets a single variable by name
94
+ * `EverydayPlugins::Plugins.get_vars(*names)`
95
+ * Gets a list of variables by name. Useful for getting a lot of variables in one line.
96
+ * `EverydayPlugins::Plugins.set_var(name, value)`
97
+ * Sets a single variable of the given name to the given value.
98
+ * `EverydayPlugins::Plugins.set_vars(vars = {})`
99
+ * Sets a list of variables to the values specified. It use a hash format, so you can do something like `EverydayPlugins::Plugins.set_vars found: true, info_line_last: false`
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it ( http://github.com/henderea/everyday-plugins/fork )
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ #noinspection RubyResolve
5
+ require 'everyday-plugins/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'everyday-plugins'
9
+ spec.version = EverydayPlugins::VERSION
10
+ spec.authors = ['Eric Henderson']
11
+ spec.email = ['henderea@gmail.com']
12
+ spec.summary = %q{A simple gem plugin system}
13
+ spec.description = %q{A simple gem plugin system. Extracted from the mvn2 gem.}
14
+ spec.homepage = 'https://github.com/henderea/everyday-plugins'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ end
@@ -0,0 +1,2 @@
1
+ require 'everyday-plugins/version'
2
+ require 'everyday-plugins/plugin'
@@ -0,0 +1,103 @@
1
+ module EverydayPlugins
2
+ class Plugins
3
+ def self.instance
4
+ @instance ||= Plugins.new
5
+ end
6
+
7
+ def self.load_plugins(path, error_fatal = false)
8
+ begin
9
+ Gem.find_latest_files("/#{path}/plugin/*.plugin.rb").each { |plugin|
10
+ #noinspection RubyResolve
11
+ begin
12
+ require plugin
13
+ rescue LoadError => e
14
+ puts "Error in loading plugin '#{plugin}'"
15
+ puts e.inspect
16
+ exit 1 if error_fatal
17
+ end
18
+ }
19
+ rescue Exception => e
20
+ puts 'Error in loading plugins'
21
+ puts e.inspect
22
+ exit 1 if error_fatal
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ @ext = {}
28
+ @types = {}
29
+ @vars = {}
30
+ end
31
+
32
+ def register(type, options = {}, &block)
33
+ @ext[type] ||= []
34
+ @ext[type] << { options: options, block: block }
35
+ end
36
+
37
+ def register_type(type, &block)
38
+ @types[type] = block
39
+ end
40
+
41
+ def register_variable(name, value = nil)
42
+ @vars[name] = value
43
+ end
44
+
45
+ def [](type)
46
+ @ext[type] || []
47
+ end
48
+
49
+ def get(type, *args)
50
+ @types[type].call(self[type], *args)
51
+ end
52
+
53
+ def self.get(type, *args)
54
+ instance.get(type, *args)
55
+ end
56
+
57
+ def get_var(name)
58
+ @vars[name] || nil
59
+ end
60
+
61
+ def get_vars(*names)
62
+ names.map { |name| get_var(name) }
63
+ end
64
+
65
+ def set_var(name, value)
66
+ @vars[name] = value
67
+ end
68
+
69
+ def set_vars(vars = {})
70
+ vars.each { |v| set_var(*v) }
71
+ end
72
+
73
+ def self.get_var(name)
74
+ instance.get_var(name)
75
+ end
76
+
77
+ def self.get_vars(*names)
78
+ instance.get_vars(*names)
79
+ end
80
+
81
+ def self.set_var(name, value)
82
+ instance.set_var(name, value)
83
+ end
84
+
85
+ def self.set_vars(vars = {})
86
+ instance.set_vars(vars)
87
+ end
88
+ end
89
+ module Plugin
90
+ def register(type, options = {}, &block)
91
+ EverydayPlugins::Plugins.instance.register(type, options, &block)
92
+ end
93
+ end
94
+ module PluginType
95
+ def register_type(type, &block)
96
+ EverydayPlugins::Plugins.instance.register_type(type, &block)
97
+ end
98
+
99
+ def register_variable(name, value = nil)
100
+ EverydayPlugins::Plugins.instance.register_variable(name, value)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module EverydayPlugins
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: everyday-plugins
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Henderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A simple gem plugin system. Extracted from the mvn2 gem.
42
+ email:
43
+ - henderea@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - everyday-plugins.gemspec
54
+ - lib/everyday-plugins.rb
55
+ - lib/everyday-plugins/plugin.rb
56
+ - lib/everyday-plugins/version.rb
57
+ homepage: https://github.com/henderea/everyday-plugins
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.0
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: A simple gem plugin system
81
+ test_files: []
82
+ has_rdoc: