rggen-core 0.21.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 135125b25bc30e8eb74fa97644c356afd817cda2a84a810a401efbcde7d2a647
4
- data.tar.gz: c4cb605aaa49801ff0f70458148f1349a220de2c41b6f7ed614e98466e425668
3
+ metadata.gz: d6fde36c04e21476aef2a09a20a3d22467bf6f4a28fa4f831c7aa7208bdb0a9a
4
+ data.tar.gz: 0735722aae6a60569505b06ec3c3c9465c47f201138056752a00cc29b780d5a0
5
5
  SHA512:
6
- metadata.gz: 122340fafae09f08a604c54b3f21e8428a0d03af5ef81b37f4211f881a99186c2f0a45e50cad9367406a75f784041eabc0ef87ebf692946a2dfbec5221135e8f
7
- data.tar.gz: fb87835adc54ec4416f5811126ff65355ada76e05cc46d89eedbe87e2899fd16665d26caf66e41a9919a074824f51ef97fd6dee9abc2aa959b42d83b9c4dd35c
6
+ metadata.gz: 4f1ae60ad3e557b0f18514cb803c635134f93f922deae475db97745571b0ff38a0ce7571c2cef324ed1ac4250fc2e3e5d78d9d48689cfafe827d4ed938425684
7
+ data.tar.gz: 589c74ee6817e7b5aa90595c7d358f2e2db730cf3b06af40ce737da62135680fd55df52259294501c2c43b063d67908b5cd0b1fe8d8fa06d9efdbe7ef05ff1cc
@@ -95,7 +95,7 @@ require_relative 'core/builder/simple_feature_entry'
95
95
  require_relative 'core/builder/list_feature_entry'
96
96
  require_relative 'core/builder/feature_registry'
97
97
  require_relative 'core/builder/layer'
98
- require_relative 'core/builder/plugins'
98
+ require_relative 'core/builder/plugin_manager'
99
99
  require_relative 'core/builder/builder'
100
100
  require_relative 'core/builder'
101
101
 
@@ -4,13 +4,15 @@ module RgGen
4
4
  module Core
5
5
  module Builder
6
6
  class Builder
7
+ extend Forwardable
8
+
7
9
  def initialize
8
10
  initialize_component_registries
9
11
  initialize_layers
10
- @plugins = Plugins.new
12
+ @plugin_manager = PluginManager.new(self)
11
13
  end
12
14
 
13
- attr_reader :plugins
15
+ attr_reader :plugin_manager
14
16
 
15
17
  def input_component_registry(name, &body)
16
18
  component_registry(:input, name, body)
@@ -88,23 +90,9 @@ module RgGen
88
90
  RegisterMap.setup(self)
89
91
  end
90
92
 
91
- def setup(name, module_or_version = nil, &block)
92
- plugins.add(name, module_or_version, &block)
93
- end
94
-
95
- def activate_plugins(**options)
96
- plugins.activate(self, **options)
97
- end
98
-
99
- def load_setup_file(file, activation = true)
100
- (file.nil? || file.empty?) &&
101
- (raise Core::LoadError.new('no setup file is given'))
102
- File.readable?(file) ||
103
- (raise Core::LoadError.new("cannot load such setup file: #{file}"))
104
- RgGen.builder(self)
105
- load(file)
106
- activation && activate_plugins
107
- end
93
+ def_delegator :plugin_manager, :load_plugin
94
+ def_delegator :plugin_manager, :load_plugins
95
+ def_delegator :plugin_manager, :setup
108
96
 
109
97
  private
110
98
 
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module Core
5
+ module Builder
6
+ class Plugin
7
+ DEFAULT_PLUGIN_VERSION = '0.0.0'
8
+
9
+ def initialize(plugin_module, &block)
10
+ @plugin_module = plugin_module
11
+ @block = block
12
+ plugin_name # check if plugin name is given
13
+ end
14
+
15
+ def default_setup(builder)
16
+ @plugin_module.respond_to?(:default_setup) &&
17
+ @plugin_module.default_setup(builder)
18
+ end
19
+
20
+ def optional_setup(builder)
21
+ @block && @plugin_module.instance_exec(builder, &@block)
22
+ end
23
+
24
+ def plugin_name
25
+ if @plugin_module.const_defined?(:PLUGIN_NAME)
26
+ @plugin_module.const_get(:PLUGIN_NAME)
27
+ elsif @plugin_module.respond_to?(:plugin_name)
28
+ @plugin_module.plugin_name
29
+ else
30
+ raise Core::PluginError.new('no plugin name is given')
31
+ end
32
+ end
33
+
34
+ def version
35
+ if @plugin_module.const_defined?(:VERSION)
36
+ @plugin_module.const_get(:VERSION)
37
+ elsif @plugin_module.respond_to?(:version)
38
+ @plugin_module.version
39
+ else
40
+ DEFAULT_PLUGIN_VERSION
41
+ end
42
+ end
43
+
44
+ def version_info
45
+ "#{plugin_name} #{version}"
46
+ end
47
+ end
48
+
49
+ class PluginManager
50
+ def initialize(builder)
51
+ @builder = builder
52
+ @plugins = []
53
+ end
54
+
55
+ def load_plugin(setup_path_or_name)
56
+ setup_path_or_name = setup_path_or_name.to_s.strip
57
+ setup_path =
58
+ if setup_file_directly_given?(setup_path_or_name)
59
+ setup_path_or_name
60
+ else
61
+ get_setup_path(setup_path_or_name)
62
+ end
63
+ read_setup_file(setup_path, setup_path_or_name)
64
+ end
65
+
66
+ def load_plugins(plugins, no_default_plugins, activation = true)
67
+ RgGen.builder(@builder)
68
+ merge_plugins(plugins, no_default_plugins)
69
+ .each { |plugin| load_plugin(*plugin) }
70
+ activation && activate_plugins
71
+ end
72
+
73
+ def setup(plugin_module, &block)
74
+ @plugins << Plugin.new(plugin_module, &block)
75
+ end
76
+
77
+ def activate_plugins(**options)
78
+ options[:no_default_setup] ||
79
+ @plugins.each { |plugin| plugin.default_setup(@builder) }
80
+ options[:no_optional_setup] ||
81
+ @plugins.each { |plugin| plugin.optional_setup(@builder) }
82
+ end
83
+
84
+ def version_info
85
+ @plugins.map(&:version_info)
86
+ end
87
+
88
+ private
89
+
90
+ def setup_file_directly_given?(setup_path_or_name)
91
+ File.ext(setup_path_or_name) == 'rb' ||
92
+ File.basename(setup_path_or_name, '.*') == 'setup'
93
+ end
94
+
95
+ def get_setup_path(name)
96
+ base, sub_directory = name.split('/', 2)
97
+ base = base.sub(/^rggen[-_]/, '').tr('-', '_')
98
+ File.join(*[
99
+ 'rggen', base, sub_directory || '', 'setup'
100
+ ].reject(&:empty?))
101
+ end
102
+
103
+ def read_setup_file(setup_path, setup_path_or_name)
104
+ require setup_path
105
+ rescue ::LoadError
106
+ raise Core::PluginError.new([
107
+ "cannot load such plugin: #{setup_path_or_name}",
108
+ setup_path_or_name != setup_path && " (#{setup_path})" || ''
109
+ ].join)
110
+ end
111
+
112
+ def merge_plugins(plugins, no_default_plugins)
113
+ [
114
+ *default_plugins(no_default_plugins),
115
+ *plugins_from_env,
116
+ *plugins
117
+ ]
118
+ end
119
+
120
+ DEFAULT_PLUGSINS = 'rggen/setup'
121
+
122
+ def default_plugins(no_default_plugins)
123
+ load_default_plugins?(no_default_plugins) && DEFAULT_PLUGSINS || nil
124
+ end
125
+
126
+ def load_default_plugins?(no_default_plugins)
127
+ return false if no_default_plugins
128
+ return false if ENV.key?('RGGEN_NO_DEFAULT_PLUGINS')
129
+ return false if Gem.find_files(DEFAULT_PLUGSINS).empty?
130
+ true
131
+ end
132
+
133
+ def plugins_from_env
134
+ ENV['RGGEN_PLUGINS']
135
+ &.split(':')&.map(&:strip)&.reject(&:empty?)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -15,11 +15,13 @@ module RgGen
15
15
  :define_simple_feature,
16
16
  :define_list_feature,
17
17
  :define_list_item_feature,
18
+ :define_value_extractor,
18
19
  :enable,
19
20
  :disable_all,
20
21
  :disable,
21
22
  :delete,
22
- :setup
23
+ :setup,
24
+ :load_plugin
23
25
  ].each do |method_name|
24
26
  def_delegator :'RgGen.builder', method_name
25
27
  end
@@ -20,6 +20,9 @@ module RgGen
20
20
  class BuilderError < RgGenError
21
21
  end
22
22
 
23
+ class PluginError < RgGenError
24
+ end
25
+
23
26
  class RuntimeError < RgGenError
24
27
  end
25
28
 
@@ -4,7 +4,7 @@ module RgGen
4
4
  module Core
5
5
  class Generator
6
6
  def run(builder, options)
7
- load_setup_file(builder, options[:setup])
7
+ load_plugins(builder, options)
8
8
  load_configuration(builder, options[:configuration])
9
9
  load_register_map(builder, options.register_map_files)
10
10
  write_files(builder, options)
@@ -15,8 +15,8 @@ module RgGen
15
15
  attr_reader :configuration
16
16
  attr_reader :register_map
17
17
 
18
- def load_setup_file(builder, file)
19
- builder.load_setup_file(file)
18
+ def load_plugins(builder, options)
19
+ builder.load_plugins(options[:plugins], options[:no_default_plugins])
20
20
  end
21
21
 
22
22
  def load_configuration(builder, file)
@@ -101,21 +101,19 @@ module RgGen
101
101
  end
102
102
  end
103
103
 
104
- Options.add_option(:setup) do |option|
105
- option.long_option '--setup FILE'
106
- option.default { default_steup_file }
107
- option.description 'Specify a Ruby file to set up RgGen tool'
108
-
109
- def option.default_steup_file
110
- ENV['RGGEN_DEFAULT_SETUP_FILE'] || define_setup_file_from_const
111
- end
104
+ Options.add_option(:no_default_plugins) do |option|
105
+ option.long_option '--no-default-plugins'
106
+ option.default false
107
+ option.action { |_, options| options[:no_default_plugins] = true }
108
+ option.description 'Do not load default plugins'
109
+ end
112
110
 
113
- def option.define_setup_file_from_const
114
- require 'rggen/default_setup_file'
115
- RgGen::DEFAULT_SETUP_FILE
116
- rescue ::LoadError
117
- nil
118
- end
111
+ Options.add_option(:plugins) do |option|
112
+ option.long_option '--plugin PLUGIN'
113
+ option.default { [] }
114
+ option.action { |value, options| options[:plugins] << value }
115
+ option.description 'Load a RgGen plugin ' \
116
+ "(name of plugin/path to 'setup.rb' file)"
119
117
  end
120
118
 
121
119
  Options.add_option(:configuration) do |option|
@@ -18,18 +18,20 @@ module RgGen
18
18
  end
19
19
 
20
20
  def run(builder, options)
21
- verbose? && load_setup_file(builder, options[:setup])
21
+ verbose? && load_plugins(builder, options)
22
22
  puts version_message(builder)
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def verbose?
28
- @verbose
27
+ def load_plugins(builder, options)
28
+ plugins = options[:plugins]
29
+ no_default_plugins = options[:no_default_plugins]
30
+ builder.load_plugins(plugins, no_default_plugins, false)
29
31
  end
30
32
 
31
- def load_setup_file(builder, file)
32
- file.nil? || file.empty? || builder.load_setup_file(file, false)
33
+ def verbose?
34
+ @verbose
33
35
  end
34
36
 
35
37
  def version_message(builder)
@@ -44,8 +46,10 @@ module RgGen
44
46
  end
45
47
 
46
48
  def verbose_version(builder)
47
- ["rggen-core #{Core::VERSION}", *builder.plugins.version_info]
48
- .map { |version_info| " - #{version_info}" }
49
+ [
50
+ "rggen-core #{Core::VERSION}",
51
+ *builder.plugin_manager.version_info
52
+ ].map { |version_info| " - #{version_info}" }
49
53
  end
50
54
  end
51
55
  end
@@ -3,7 +3,7 @@
3
3
  module RgGen
4
4
  module Core
5
5
  MAJOR = 0
6
- MINOR = 21
6
+ MINOR = 22
7
7
  PATCH = 0
8
8
  VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rggen-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taichi Ishitani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -98,7 +98,7 @@ files:
98
98
  - lib/rggen/core/builder/list_feature_entry.rb
99
99
  - lib/rggen/core/builder/loader_registry.rb
100
100
  - lib/rggen/core/builder/output_component_registry.rb
101
- - lib/rggen/core/builder/plugins.rb
101
+ - lib/rggen/core/builder/plugin_manager.rb
102
102
  - lib/rggen/core/builder/simple_feature_entry.rb
103
103
  - lib/rggen/core/cli.rb
104
104
  - lib/rggen/core/configuration.rb
@@ -189,5 +189,5 @@ requirements: []
189
189
  rubygems_version: 3.1.2
190
190
  signing_key:
191
191
  specification_version: 4
192
- summary: rggen-core-0.21.0
192
+ summary: rggen-core-0.22.0
193
193
  test_files: []
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RgGen
4
- module Core
5
- module Builder
6
- class Plugin
7
- DEFAULT_PLUGIN_VERSION = '0.0.0'
8
-
9
- def initialize(name, module_or_version, &block)
10
- @name = name
11
- @block = block
12
- @version, @plugin_module =
13
- if module_or_version.is_a?(Module)
14
- [extract_version(module_or_version), module_or_version]
15
- else
16
- [module_or_version || DEFAULT_PLUGIN_VERSION]
17
- end
18
- end
19
-
20
- attr_reader :name
21
- attr_reader :version
22
-
23
- def default_setup(builder)
24
- @plugin_module &&
25
- begin
26
- @plugin_module.respond_to?(:default_setup) &&
27
- @plugin_module.default_setup(builder)
28
- end
29
- end
30
-
31
- def optional_setup(builder)
32
- @block &&
33
- if @plugin_module
34
- @plugin_module.instance_exec(builder, &@block)
35
- else
36
- @block.call(builder)
37
- end
38
- end
39
-
40
- def version_info
41
- "#{name} #{version}"
42
- end
43
-
44
- private
45
-
46
- def extract_version(plugin_module)
47
- if plugin_module.const_defined?(:VERSION)
48
- plugin_module.const_get(:VERSION)
49
- elsif plugin_module.respond_to?(:version)
50
- plugin_module.version
51
- else
52
- DEFAULT_PLUGIN_VERSION
53
- end
54
- end
55
- end
56
-
57
- class Plugins
58
- def initialize
59
- @plugins = []
60
- end
61
-
62
- def add(name, module_or_version, &block)
63
- @plugins << Plugin.new(name, module_or_version, &block)
64
- end
65
-
66
- def activate(builder, **options)
67
- options[:no_default_setup] ||
68
- @plugins.each { |plugin| plugin.default_setup(builder) }
69
- options[:no_optional_setup] ||
70
- @plugins.each { |plugin| plugin.optional_setup(builder) }
71
- end
72
-
73
- def version_info
74
- @plugins.map(&:version_info)
75
- end
76
- end
77
- end
78
- end
79
- end