r2-oas 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -0
  3. data/GEMSPEC.md +4 -3
  4. data/README.ja.md +54 -37
  5. data/README.md +50 -31
  6. data/lib/r2-oas.rb +5 -7
  7. data/lib/r2-oas/app_configuration.rb +17 -1
  8. data/lib/r2-oas/app_configuration/deprecation.rb +28 -0
  9. data/lib/r2-oas/configuration.rb +52 -0
  10. data/lib/r2-oas/deploy/client.rb +21 -6
  11. data/lib/r2-oas/{plugins/schema/v3/object → dynamic/schema/v3/object/from_routes}/hookable_base_object.rb +7 -7
  12. data/lib/r2-oas/errors.rb +5 -0
  13. data/lib/r2-oas/hooks/hook.rb +7 -5
  14. data/lib/r2-oas/lib/core_ext/hash/deep_merge.rb +44 -0
  15. data/lib/r2-oas/lib/core_ext/object/blank.rb +135 -0
  16. data/lib/r2-oas/pluggable_configuration.rb +8 -5
  17. data/lib/r2-oas/plugin/base.rb +44 -0
  18. data/lib/r2-oas/plugin/executor.rb +148 -0
  19. data/lib/r2-oas/plugin/hookable.rb +42 -0
  20. data/lib/r2-oas/plugin/public.rb +3 -0
  21. data/lib/r2-oas/plugin/transform/transform.rb +10 -0
  22. data/lib/r2-oas/plugin/transform/v3/transform.rb +20 -0
  23. data/lib/r2-oas/plugin/transform/v3/visitable.rb +37 -0
  24. data/lib/r2-oas/public.rb +5 -0
  25. data/lib/r2-oas/schema/base.rb +1 -1
  26. data/lib/r2-oas/schema/builder.rb +1 -1
  27. data/lib/r2-oas/schema/editor.rb +8 -1
  28. data/lib/r2-oas/schema/v3/analyzer/path_analyzer.rb +1 -1
  29. data/lib/r2-oas/schema/v3/builder.rb +1 -1
  30. data/lib/r2-oas/schema/v3/builder/base_builder.rb +14 -2
  31. data/lib/r2-oas/schema/v3/builder/doc_builder.rb +10 -2
  32. data/lib/r2-oas/schema/v3/generator/base_generator.rb +7 -2
  33. data/lib/r2-oas/schema/v3/generator/doc_generator.rb +1 -1
  34. data/lib/r2-oas/schema/v3/manager/file/include_ref_base_file_manager.rb +3 -2
  35. data/lib/r2-oas/schema/v3/object/from_files/base_object.rb +62 -0
  36. data/lib/r2-oas/schema/v3/object/from_files/components/request_body_object.rb +103 -0
  37. data/lib/r2-oas/schema/v3/object/from_files/components/schema_object.rb +102 -0
  38. data/lib/r2-oas/schema/v3/object/from_files/components_object.rb +46 -0
  39. data/lib/r2-oas/schema/v3/object/from_files/external_document_object.rb +23 -0
  40. data/lib/r2-oas/schema/v3/object/from_files/info_object.rb +23 -0
  41. data/lib/r2-oas/schema/v3/object/from_files/openapi_object.rb +61 -0
  42. data/lib/r2-oas/schema/v3/object/from_files/path_item_object.rb +110 -0
  43. data/lib/r2-oas/schema/v3/object/from_files/paths_object.rb +33 -0
  44. data/lib/r2-oas/schema/v3/object/from_files/utils/all.rb +4 -0
  45. data/lib/r2-oas/schema/v3/object/from_files/utils/deep_methods.rb +27 -0
  46. data/lib/r2-oas/schema/v3/object/from_files/utils/refs.rb +151 -0
  47. data/lib/r2-oas/schema/v3/object/{base_object.rb → from_routes/base_object.rb} +11 -1
  48. data/lib/r2-oas/schema/v3/object/{components → from_routes/components}/request_body_object.rb +23 -19
  49. data/lib/r2-oas/schema/v3/object/{components → from_routes/components}/schema_object.rb +16 -12
  50. data/lib/r2-oas/schema/v3/object/{components_object.rb → from_routes/components_object.rb} +11 -11
  51. data/lib/r2-oas/schema/v3/object/from_routes/external_document_object.rb +28 -0
  52. data/lib/r2-oas/schema/v3/object/{info_object.rb → from_routes/info_object.rb} +11 -2
  53. data/lib/r2-oas/schema/v3/object/{openapi_object.rb → from_routes/openapi_object.rb} +12 -11
  54. data/lib/r2-oas/schema/v3/object/{path_item_object.rb → from_routes/path_item_object.rb} +6 -6
  55. data/lib/r2-oas/schema/v3/object/{paths_object.rb → from_routes/paths_object.rb} +11 -8
  56. data/lib/r2-oas/schema/v3/object/{public.rb → from_routes/public.rb} +0 -0
  57. data/lib/r2-oas/schema/v3/object/{server_object.rb → from_routes/server_object.rb} +0 -0
  58. data/lib/r2-oas/schema/v3/object/{tag_object.rb → from_routes/tag_object.rb} +2 -1
  59. data/lib/r2-oas/schema/v3/object/store.rb +54 -0
  60. data/lib/r2-oas/shared/all.rb +1 -0
  61. data/lib/r2-oas/shared/callable.rb +17 -0
  62. data/lib/r2-oas/store.rb +20 -16
  63. data/lib/r2-oas/support/deprecation.rb +24 -0
  64. data/lib/r2-oas/support/deprecation/behavior.rb +21 -0
  65. data/lib/r2-oas/support/deprecation/instance_delegator.rb +42 -0
  66. data/lib/r2-oas/support/deprecation/reporting.rb +91 -0
  67. data/lib/r2-oas/task_logging.rb +3 -7
  68. data/lib/r2-oas/tasks/common.rake +1 -2
  69. data/lib/r2-oas/tasks/main.rake +31 -9
  70. data/lib/r2-oas/tasks/tool.rake +16 -6
  71. data/lib/r2-oas/version.rb +1 -1
  72. data/r2-oas.gemspec +3 -5
  73. metadata +70 -81
  74. data/.github/ISSUE_TEMPLATE.md +0 -12
  75. data/.github/PULL_REQUEST_TEMPLATE.md +0 -12
  76. data/.gitignore +0 -16
  77. data/.rspec +0 -3
  78. data/.rubocop.yml +0 -10
  79. data/.rubocop_todo.yml +0 -282
  80. data/.travis.yml +0 -26
  81. data/Appraisals +0 -13
  82. data/CODE_OF_CONDUCT.md +0 -74
  83. data/Gemfile +0 -12
  84. data/Rakefile +0 -8
  85. data/bin/console +0 -12
  86. data/bin/setup +0 -8
  87. data/devscript/all_support_ruby.sh +0 -43
  88. data/devscript/bundle_for_all_support_ruby.sh +0 -31
  89. data/devscript/rspec_for_all_support_ruby.sh +0 -27
  90. data/docs/.nojekyll +0 -0
  91. data/docs/README.md +0 -173
  92. data/docs/_sidebar.md +0 -25
  93. data/docs/attention/if_clash.md +0 -17
  94. data/docs/index.html +0 -29
  95. data/docs/schema/3.0.0.md +0 -155
  96. data/docs/setting/COC.md +0 -14
  97. data/docs/setting/CORS.md +0 -22
  98. data/docs/setting/configure.md +0 -176
  99. data/docs/trableshouting/runtime_error.md +0 -44
  100. data/docs/usage/analyze_docs.md +0 -875
  101. data/docs/usage/clean_docs.md +0 -19
  102. data/docs/usage/deploy_docs.md +0 -839
  103. data/docs/usage/display_paths_list.md +0 -35
  104. data/docs/usage/display_paths_stats.md +0 -54
  105. data/docs/usage/edit_docs.md +0 -218
  106. data/docs/usage/generate_docs.md +0 -412
  107. data/docs/usage/monitor_docs.md +0 -219
  108. data/docs/usage/use_hook_methods.md +0 -236
  109. data/docs/usage/use_hook_to_generate_docs.md +0 -235
  110. data/docs/usage/use_schema_namespace.md +0 -181
  111. data/docs/usage/use_tag_namespace.md +0 -180
  112. data/docs/usage/view_docs.md +0 -262
  113. data/gemfiles/ruby_2.3.3.gemfile +0 -11
  114. data/gemfiles/ruby_2.4.2.gemfile +0 -11
  115. data/gemfiles/ruby_2.5.8.gemfile +0 -11
  116. data/gemfiles/ruby_2.6.6.gemfile +0 -11
  117. data/gemfiles/ruby_2.7.1.gemfile +0 -11
  118. data/lib/r2-oas/deploy/swagger-ui/dist/favicon-16x16.png +0 -0
  119. data/lib/r2-oas/deploy/swagger-ui/dist/favicon-32x32.png +0 -0
  120. data/lib/r2-oas/deploy/swagger-ui/dist/oauth2-redirect.html +0 -68
  121. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-bundle.js +0 -134
  122. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-bundle.js.map +0 -1
  123. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-standalone-preset.js +0 -22
  124. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-standalone-preset.js.map +0 -1
  125. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.css +0 -4
  126. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.css.map +0 -1
  127. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.js +0 -9
  128. data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.js.map +0 -1
  129. data/lib/r2-oas/schema/v3/object/external_document_object.rb +0 -19
@@ -6,22 +6,33 @@ require 'fileutils'
6
6
  module R2OAS
7
7
  module Deploy
8
8
  class Client < Schema::Base
9
+ SWAGGER_UI_DIST_URL = 'https://github.com/swagger-api/swagger-ui/trunk/dist'
10
+
11
+ def initialize(options = {})
12
+ super(options)
13
+ @download_dir = "#{SecureRandom.uuid[0..7]}/dist"
14
+ @dist_path = File.expand_path(Rails.root.join(@download_dir), __FILE__)
15
+ end
16
+
17
+ def download_swagger_ui_dist
18
+ system("svn export #{SWAGGER_UI_DIST_URL} #{@dist_path}")
19
+ end
20
+
9
21
  def deploy
10
22
  copy_swagger_ui_dist
11
23
  copy_swagger_ui_index
12
24
  copy_oas_doc_file
25
+ ensure
26
+ remove_download_dist
13
27
  end
14
28
 
15
29
  private
16
30
 
17
31
  def copy_swagger_ui_dist
18
32
  docs_path = File.expand_path(Rails.root.join(deploy_dir_path), __FILE__)
19
- return if FileTest.exists?(docs_path)
20
-
21
33
  FileUtils.mkdir_p(docs_path) unless FileTest.exists?(docs_path)
22
-
23
- dist_path = File.expand_path('swagger-ui/dist', __dir__)
24
- FileUtils.cp_r(dist_path, docs_path)
34
+ FileUtils.mkdir_p(@dist_path) unless FileTest.exists?(@dist_path)
35
+ FileUtils.cp_r(@dist_path, docs_path)
25
36
  end
26
37
 
27
38
  def copy_swagger_ui_index
@@ -35,10 +46,14 @@ module R2OAS
35
46
 
36
47
  def copy_oas_doc_file
37
48
  swagger_file_path = File.expand_path(Rails.root.join(deploy_dir_path, doc_save_file_name), __FILE__)
38
- oas_doc_file_path = File.expand_path("#{root_dir_path}/#{doc_save_file_name}")
49
+ oas_doc_file_path = File.expand_path(output_path)
39
50
  FileUtils.cp_r(oas_doc_file_path, swagger_file_path)
40
51
  end
41
52
 
53
+ def remove_download_dist
54
+ FileUtils.rm_rf(File.expand_path('..', @dist_path))
55
+ end
56
+
42
57
  # [ref]
43
58
  # https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/ErbNewArguments
44
59
  def make_index(template)
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'r2-oas/schema/v3/object/base_object'
3
+ require 'r2-oas/schema/v3/object/from_routes/base_object'
4
4
  require 'r2-oas/hooks/hook'
5
5
 
6
6
  module R2OAS
7
- module Plugins
7
+ module Dynamic
8
8
  module Schema
9
9
  module V3
10
10
  class HookableBaseObject < R2OAS::Schema::V3::BaseObject
@@ -22,11 +22,11 @@ module R2OAS
22
22
 
23
23
  def self.inherited(base)
24
24
  base.extend ClassMethods
25
- self.hook = Hooks::Hook.register(base)
25
+ self.hook = Hooks::Hook.register(:dynamic, base)
26
26
  end
27
27
 
28
28
  def self.hooks
29
- superclass.hook.repository[self].global_hooks_data
29
+ superclass.hook.repository[:dynamic][self].global_hooks_data
30
30
  end
31
31
 
32
32
  def self.hook=(value)
@@ -58,8 +58,8 @@ module R2OAS
58
58
 
59
59
  attr_accessor :doc
60
60
 
61
- def initialize
62
- super
61
+ def initialize(opts = {})
62
+ super(opts)
63
63
  self.doc = {}
64
64
  end
65
65
 
@@ -72,7 +72,7 @@ module R2OAS
72
72
  end
73
73
 
74
74
  def use_superclass_hook
75
- self.class.hook.repository[self.class] = self.class.hook.repository[self.class.superclass]
75
+ self.class.hook.repository[:dynamic][self.class] = self.class.hook.repository[:dynamic][self.class.superclass]
76
76
  end
77
77
 
78
78
  private
@@ -5,4 +5,9 @@ module R2OAS
5
5
  class NoFileExistsError < StandardError; end
6
6
  class NoSupportError < StandardError; end
7
7
  class ChecksumError < StandardError; end
8
+ class PluginNameError < StandardError; end
9
+ class PluginDuplicationError < StandardError; end
10
+ class PluginLoadError < StandardError; end
11
+ class DepulicateSchemaNameError < StandardError; end
12
+ class RefInvalidAssignment < StandardError; end
8
13
  end
@@ -12,19 +12,21 @@ module R2OAS
12
12
  class << self
13
13
  attr_accessor :repository
14
14
 
15
- def register(target_class)
15
+ def register(type, target_class)
16
16
  @repository ||= {}
17
+ @repository[type] ||= {}
18
+ @type = type
17
19
  @hooks ||= {}
18
- @repository[target_class] = Repository.new(target_class)
20
+ @repository[type][target_class] = Repository.new(target_class)
19
21
  self
20
22
  end
21
23
 
22
24
  def on(on, callback, target_class, once = false)
23
- target_repository = @repository[target_class]
25
+ target_repository = @repository[@type][target_class]
24
26
  uid = target_repository.last_hook_id + 1
25
27
  target_repository.last_hook_id = uid
26
28
 
27
- @repository[target_class].global_hooks_data[on] ||= []
29
+ target_repository.global_hooks_data[on] ||= []
28
30
  global_hook = GlobalHook.new(callback, once, uid, target_class)
29
31
 
30
32
  target_repository.global_hooks_data[on].push(global_hook)
@@ -34,7 +36,7 @@ module R2OAS
34
36
 
35
37
  # MEMO: Do not Use
36
38
  def off(uid, target_class)
37
- target_repository = @repository[target_class]
39
+ target_repository = @repository[@type][target_class]
38
40
  result = uid
39
41
 
40
42
  target_repository.global_hooks_data.each do |on|
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copy From https://github.com/rails/rails/blob/v4.2.5/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
4
+
5
+ # rubocop:disable all
6
+ class Hash
7
+ # Returns a new hash with +self+ and +other_hash+ merged recursively.
8
+ #
9
+ # h1 = { a: true, b: { c: [1, 2, 3] } }
10
+ # h2 = { a: false, b: { x: [3, 4, 5] } }
11
+ #
12
+ # h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
13
+ #
14
+ # Like with Hash#merge in the standard library, a block can be provided
15
+ # to merge values:
16
+ #
17
+ # h1 = { a: 100, b: 200, c: { c1: 100 } }
18
+ # h2 = { b: 250, c: { c1: 200 } }
19
+ # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
20
+ # # => { a: 100, b: 450, c: { c1: 300 } }
21
+ def deep_merge(other_hash, &block)
22
+ dup.deep_merge!(other_hash, &block)
23
+ end
24
+
25
+ # Same as +deep_merge+, but modifies +self+.
26
+ def deep_merge!(other_hash, &block)
27
+ other_hash.each_pair do |current_key, other_value|
28
+ this_value = self[current_key]
29
+
30
+ self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
31
+ this_value.deep_merge(other_value, &block)
32
+ else
33
+ if block_given? && key?(current_key)
34
+ block.call(current_key, this_value, other_value)
35
+ else
36
+ other_value
37
+ end
38
+ end
39
+ end
40
+
41
+ self
42
+ end
43
+ end
44
+ # rubocop:enable all
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copy From https://github.com/rails/rails/blob/v4.2.5/activesupport/lib/active_support/core_ext/object/blank.rb
4
+
5
+ # rubocop:disable all
6
+ class Object
7
+ # An object is blank if it's false, empty, or a whitespace string.
8
+ # For example, +false+, '', ' ', +nil+, [], and {} are all blank.
9
+ #
10
+ # This simplifies
11
+ #
12
+ # !address || address.empty?
13
+ #
14
+ # to
15
+ #
16
+ # address.blank?
17
+ #
18
+ # @return [true, false]
19
+ def blank?
20
+ respond_to?(:empty?) ? !!empty? : !self
21
+ end
22
+
23
+ # An object is present if it's not blank.
24
+ #
25
+ # @return [true, false]
26
+ def present?
27
+ !blank?
28
+ end
29
+
30
+ # Returns the receiver if it's present otherwise returns +nil+.
31
+ # <tt>object.presence</tt> is equivalent to
32
+ #
33
+ # object.present? ? object : nil
34
+ #
35
+ # For example, something like
36
+ #
37
+ # state = params[:state] if params[:state].present?
38
+ # country = params[:country] if params[:country].present?
39
+ # region = state || country || 'US'
40
+ #
41
+ # becomes
42
+ #
43
+ # region = params[:state].presence || params[:country].presence || 'US'
44
+ #
45
+ # @return [Object]
46
+ def presence
47
+ self if present?
48
+ end
49
+ end
50
+
51
+ class NilClass
52
+ # +nil+ is blank:
53
+ #
54
+ # nil.blank? # => true
55
+ #
56
+ # @return [true]
57
+ def blank?
58
+ true
59
+ end
60
+ end
61
+
62
+ class FalseClass
63
+ # +false+ is blank:
64
+ #
65
+ # false.blank? # => true
66
+ #
67
+ # @return [true]
68
+ def blank?
69
+ true
70
+ end
71
+ end
72
+
73
+ class TrueClass
74
+ # +true+ is not blank:
75
+ #
76
+ # true.blank? # => false
77
+ #
78
+ # @return [false]
79
+ def blank?
80
+ false
81
+ end
82
+ end
83
+
84
+ class Array
85
+ # An array is blank if it's empty:
86
+ #
87
+ # [].blank? # => true
88
+ # [1,2,3].blank? # => false
89
+ #
90
+ # @return [true, false]
91
+ alias_method :blank?, :empty?
92
+ end
93
+
94
+ class Hash
95
+ # A hash is blank if it's empty:
96
+ #
97
+ # {}.blank? # => true
98
+ # { key: 'value' }.blank? # => false
99
+ #
100
+ # @return [true, false]
101
+ alias_method :blank?, :empty?
102
+ end
103
+
104
+ class String
105
+ BLANK_REGEXP = /\A[[:space:]]*\z/
106
+
107
+ # A string is blank if it's empty or contains whitespaces only:
108
+ #
109
+ # ''.blank? # => true
110
+ # ' '.blank? # => true
111
+ # "\t\n\r".blank? # => true
112
+ # ' blah '.blank? # => false
113
+ #
114
+ # Unicode whitespace is supported:
115
+ #
116
+ # "\u00a0".blank? # => true
117
+ #
118
+ # @return [true, false]
119
+ def blank?
120
+ BLANK_REGEXP === self
121
+ end
122
+ end
123
+
124
+ class Numeric #:nodoc:
125
+ # No number is blank:
126
+ #
127
+ # 1.blank? # => false
128
+ # 0.blank? # => false
129
+ #
130
+ # @return [false]
131
+ def blank?
132
+ false
133
+ end
134
+ end
135
+ # rubocop:enable all
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'schema/v3/object/public'
3
+ require 'r2-oas/schema/v3/object/from_routes/public'
4
+ require_relative 'support/deprecation'
4
5
 
5
6
  module R2OAS
6
7
  module PluggableConfiguration
@@ -16,18 +17,20 @@ module R2OAS
16
17
  }
17
18
  # rubocop:enable Style/MutableConstant
18
19
 
19
- VALID_OPTIONS_KEYS = [
20
- :use_object_classes
20
+ VALID_OPTIONS_KEYS = %i[
21
+ use_object_classes
21
22
  ].freeze
22
23
 
23
- attr_accessor *VALID_OPTIONS_KEYS
24
+ attr_reader *VALID_OPTIONS_KEYS
24
25
 
25
26
  private
26
27
 
27
28
  module_function
28
29
 
29
30
  def set_default(target)
30
- target.use_object_classes = DEFAULT_USE_OBJECT_CLASSES
31
+ Deprecation.silence do
32
+ target.use_object_classes = DEFAULT_USE_OBJECT_CLASSES
33
+ end
31
34
  end
32
35
  end
33
36
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'r2-oas/base'
4
+ require 'r2-oas/hooks/hook'
5
+ require_relative 'hookable'
6
+
7
+ module R2OAS
8
+ module Plugin
9
+ class Base < ::R2OAS::Base
10
+ extend ::R2OAS::Plugin::Hookable
11
+
12
+ class << self
13
+ attr_accessor :plugin_name, :type, :opts
14
+
15
+ def setup(&block)
16
+ return unless block_given?
17
+
18
+ callback = proc { |*args| block.call(*args) }
19
+ on(:setup, callback)
20
+ end
21
+
22
+ def teardown(&block)
23
+ return unless block_given?
24
+
25
+ callback = proc { |*args| block.call(*args) }
26
+ on(:teardown, callback)
27
+ end
28
+
29
+ def execute_setup(*args)
30
+ execute_hook(:setup, *args)
31
+ end
32
+
33
+ def execute_teardown(*args)
34
+ execute_hook(:teardown, *args)
35
+ end
36
+
37
+ def inherited(base)
38
+ super
39
+ base.opts = {}
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hookable'
4
+ require 'r2-oas/errors'
5
+
6
+ module R2OAS
7
+ module Plugin
8
+ class Executor
9
+ extend ::R2OAS::Plugin::Hookable
10
+
11
+ def initialize(plugins, opts)
12
+ @plugins = plugins
13
+ @use_plugin = !!opts[:use_plugin]
14
+ end
15
+
16
+ def execute_transform_plugins(hook_method, *args)
17
+ return unless @use_plugin
18
+ return unless @plugins.present?
19
+
20
+ @plugin_map ||= self.class.plugin_map(@plugins)
21
+
22
+ self.class.execute_transform_plugins(@plugin_map, hook_method, *args)
23
+ end
24
+
25
+ class << self
26
+ attr_accessor :plugin_map
27
+
28
+ def execute_transform_plugins(plugin_map, hook_method, *args)
29
+ execute_plugins(plugin_map, :transform, hook_method, *args)
30
+ end
31
+
32
+ def execute_plugins(plugin_map, type, hook_method, *args)
33
+ return unless plugin_map.present?
34
+
35
+ plugins_info = plugin_map[type.to_sym][hook_method.to_sym]
36
+
37
+ return if plugins_info.nil?
38
+
39
+ plugins_info.each do |info|
40
+ opts = info[:plugin_opts]
41
+ klass = info[:plugin_klass]
42
+ klass.send(:opts=, opts) if opts.present?
43
+ klass.send(info[:execute_hook_method], *args)
44
+ end
45
+ end
46
+
47
+ # e.g.)
48
+ # {
49
+ # transform: {
50
+ # setup: [
51
+ # { plugin_name: 'r2oas-transform-plugin-sample1', plugin_klass: Sample1Plugin, hook_method: :setup },
52
+ # { plugin_name: 'r2oas-transform-plugin-sample2', plugin_klass: Sample2Plugin, hook_method: :setup },
53
+ # ],
54
+ # info: [
55
+ # { plugin_name: 'r2oas-transform-plugin-sample1', plugin_klass: Sample1Plugin, hook_method: :info },
56
+ # ]
57
+ # }
58
+ # }
59
+ #
60
+ def plugin_map(plugins)
61
+ @plugin_map ||= {}
62
+ return @plugin_map if @plugin_map.present?
63
+
64
+ @will_plugins = []
65
+
66
+ use_plugins_info = create_use_plugins_info(plugins)
67
+
68
+ loaded_plugins_hooks_map = hooks_map.each_with_object({}) do |(klass, repo), result|
69
+ # MEMO:
70
+ # klass may be DynamicSchema or Plugin or Other Class
71
+ next unless klass.respond_to?(:plugin_name)
72
+ # Plugins that are loaded but not used
73
+ next unless use_plugins_info.keys.include?(klass.plugin_name)
74
+
75
+ result[klass.plugin_name] = { klass: klass, repo: repo }
76
+ end
77
+
78
+ unknown_plugins = use_plugins_info.keys - loaded_plugins_hooks_map.keys
79
+
80
+ raise R2OAS::PluginLoadError, "The '#{unknown_plugins.join(', ')}' plugin doesn't exist or can't be loaded" if unknown_plugins.present?
81
+
82
+ loaded_plugins_hooks_map.each do |plugin_name, info|
83
+ plugin_klass = info[:klass]
84
+ repo = info[:repo]
85
+
86
+ repo.global_hooks_data.each do |hook_method, callbacks|
87
+ callbacks.each do
88
+ plugin_type = plugin_klass.type
89
+
90
+ data = {
91
+ plugin_name: plugin_name,
92
+ plugin_klass: plugin_klass,
93
+ plugin_opts: use_plugins_info[plugin_name],
94
+ execute_hook_method: "execute_#{hook_method}"
95
+ }
96
+
97
+ if @plugin_map[plugin_type].present?
98
+ if @plugin_map[plugin_type][hook_method].present?
99
+ @plugin_map[plugin_type][hook_method].push(data)
100
+ else
101
+ @plugin_map[plugin_type][hook_method] = [data]
102
+ end
103
+ else
104
+ @plugin_map[plugin_type] = {}
105
+ @plugin_map[plugin_type][hook_method] = [data]
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ @plugin_map
112
+ end
113
+
114
+ def create_use_plugins_info(plugins)
115
+ will_use_plugins = []
116
+ result = {}
117
+
118
+ plugins.each do |plugin_info|
119
+ case plugin_info
120
+ when Array
121
+ plugin_name = plugin_info[0]
122
+ plugin_opts = plugin_info[1]
123
+
124
+ raise PluginNameError, 'Missing plugin name' if plugin_name.blank?
125
+
126
+ result[plugin_name] = plugin_opts
127
+ when String
128
+ plugin_name = plugin_info
129
+ plugin_opts = nil
130
+
131
+ result[plugin_name] = plugin_opts
132
+ else
133
+ raise NoSupportError, "The plugin loading format '#{plugin_info.class}' is incorrect"
134
+ end
135
+
136
+ if will_use_plugins.include?(plugin_name)
137
+ raise PluginDuplicationError, "Plugin: duplicate '#{plugin_name}'"
138
+ else
139
+ will_use_plugins.push(plugin_name)
140
+ end
141
+ end
142
+
143
+ result
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end