| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class CMockPluginManager |
| 8 |
attr_accessor :plugins |
| 9 |
|
| 10 |
def initialize(config, utils) |
| 11 |
@plugins = [] |
| 12 |
plugins_to_load = [:expect, config.plugins].flatten.uniq.compact |
| 13 |
plugins_to_load.each do |plugin| |
| 14 |
plugin_name = plugin.to_s |
| 15 |
object_name = 'CMockGeneratorPlugin' + camelize(plugin_name) |
| 16 |
self.class.mutex.synchronize { load_plugin(plugin_name, object_name, config, utils) } |
| 17 |
end |
| 18 |
@plugins.sort! { |a, b| a.priority <=> b.priority } |
| 19 |
end |
| 20 |
|
| 21 |
def run(method, args = nil) |
| 22 |
if args.nil? |
| 23 |
@plugins.collect { |plugin| plugin.send(method) if plugin.respond_to?(method) }.flatten.join |
| 24 |
else |
| 25 |
@plugins.collect { |plugin| plugin.send(method, args) if plugin.respond_to?(method) }.flatten.join |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
def camelize(lower_case_and_underscored_word) |
| 30 |
lower_case_and_underscored_word.gsub(/\/(.?)/) { '::' + Regexp.last_match(1).upcase }.gsub(/(^|_)(.)/) { Regexp.last_match(2).upcase } |
| 31 |
end |
| 32 |
|
| 33 |
def self.mutex |
| 34 |
@mutex ||= Mutex.new |
| 35 |
end |
| 36 |
|
| 37 |
private |
| 38 |
|
| 39 |
def load_plugin(plugin_name, object_name, config, utils) |
| 40 |
unless Object.const_defined? object_name |
| 41 |
file_name = "#{__dir__}/cmock_generator_plugin_#{plugin_name.downcase}.rb" |
| 42 |
require file_name |
| 43 |
end |
| 44 |
class_name = Object.const_get(object_name) |
| 45 |
@plugins << class_name.new(config, utils) |
| 46 |
rescue StandardError |
| 47 |
file_name = "#{__dir__}/cmock_generator_plugin_#{plugin_name.downcase}.rb" |
| 48 |
raise "ERROR: CMock unable to load plugin '#{plugin_name}' '#{object_name}' #{file_name}" |
| 49 |
end |
| 50 |
end |