lib/core/plugin/action.rb in nucleon-0.2.0 vs lib/core/plugin/action.rb in nucleon-0.2.1

- old
+ new

@@ -7,10 +7,14 @@ #----------------------------------------------------------------------------- # Info def self.describe(group = nil, action = 'unknown', weight = -1000, description = nil, help = nil) + describe_base(group, action, weight, description, help) + end + + def self.describe_base(group = nil, action = 'unknown', weight = -1000, description = nil, help = nil) if group group_name = Util::Data.array(group).join('.') description_id = "#{namespace}.action.#{group_name}.#{action}.description" help_id = "#{namespace}.action.#{group_name}.#{action}.help" else @@ -41,11 +45,11 @@ def initialize(namespace, provider, name, type, default, locale = nil, &validator) @provider = provider @name = name @type = type @default = default - @locale = locale.nil? ? "#{namespace}.actions.#{provider}.options.#{name}" : locale + @locale = locale.nil? ? "#{namespace}.action.#{provider.to_s.gsub('_', '.')}.options.#{name}" : locale @validator = validator if validator end #--- @@ -74,11 +78,11 @@ logger.debug("Running nucleon action #{provider} with #{options.inspect}") action = Nucleon.action(provider, options) exit_status = action.execute action_result = action.result - + rescue => error logger.error("Nucleon action #{provider} experienced an error:") logger.error(error.inspect) logger.error(error.message) logger.error(Nucleon::Util::Data.to_yaml(error.backtrace)) @@ -103,11 +107,11 @@ #--- def normalize(reload) args = array(delete(:args, [])) - + @action_interface = Util::Liquid.new do |method, method_args| options = {} options = method_args[0] if method_args.length > 0 quiet = true @@ -134,10 +138,16 @@ end #----------------------------------------------------------------------------- # Checks + def strict? + true # Override in providers if needed (allow extra options if false) + end + + #--- + def processed? get(:processed, false) end #----------------------------------------------------------------------------- @@ -178,16 +188,16 @@ get(:settings) end #--- - def register(name, type, default, locale = nil) + def register(name, type, default = nil, locale = nil, &code) name = name.to_sym - if block_given? + if code option = Option.new(namespace, plugin_provider, name, type, default, locale) do |value, success| - yield(value, success) + code.call(value, success) end else option = Option.new(namespace, plugin_provider, name, type, default, locale) end @@ -195,10 +205,32 @@ settings[name] = option.default if settings[name].nil? end #--- + def register_bool(name, default = false, locale = nil, &code) + register(name, :bool, default, locale, &code) + end + + def register_int(name, default = nil, locale = nil, &code) + register(name, :int, default, locale, &code) + end + + def register_float(name, default = nil, locale = nil, &code) + register(name, :float, default, locale, &code) + end + + def register_str(name, default = '', locale = nil, &code) + register(name, :str, default, locale, &code) + end + + def register_array(name, default = [], locale = nil, &code) + register(name, :array, default, locale, &code) + end + + #--- + def remove(names) Util::Data.rm_keys(config, names) Util::Data.rm_keys(settings, names) end @@ -288,18 +320,20 @@ action_info[:description][:help].split("\n").each do |line| help_text << ' ' + line + "\n" end @parser = Util::CLI::Parser.new(args, usage, "\n#{help_text}\n") do |parser| + parser.strict = strict? + parse(parser) extension(:parse, { :parser => parser, :config => config }) end if @parser if @parser.processed set(:processed, true) - settings.import(Util::Data.merge([ @parser.options, @parser.arguments ], true)) + settings.import(Util::Data.merge([ @parser.extra, @parser.options, @parser.arguments ], true)) logger.debug("Parse successful") elsif @parser.options[:help] && ! quiet? executable = delete(:executable, '') puts I18n.t('nucleon.core.exec.help.usage') + ": " + executable.to_s + ' ' + help + "\n" @@ -314,15 +348,19 @@ end end #--- + def parse_types + [ :bool, :int, :float, :str, :array ] + end + def parse(parser) generate = lambda do |format, name| formats = [ :option, :arg ] - types = [ :bool, :int, :float, :str, :array ] + types = parse_types name = name.to_sym if config.export.has_key?(name) && formats.include?(format.to_sym) option_config = config[name] type = option_config.type @@ -412,11 +450,11 @@ myself.status = code.unknown_status unless status.is_a?(Integer) if processed? && status != code.success logger.warn("Execution failed for #{plugin_provider} with status #{status}") - alert(Codes.render_index(status)) + warn(Codes.render_index(status), { :i18n => false }) end status end @@ -445,10 +483,28 @@ end #----------------------------------------------------------------------------- # Utilities + def validate_plugins(namespace, type, name, values) + plugin_class = Nucleon.plugin_class(namespace, type) + loaded_plugins = Nucleon.loaded_plugins(namespace, type) + success = true + + array(values).each do |value| + if info = plugin_class.translate_reference(value) + if ! loaded_plugins.keys.include?(info[:provider].to_sym) + warn("corl.action.#{plugin_name.to_s.gsub('_', '.')}.errors.#{name}", Util::Data.merge([ info, { :value => value } ])) + success = false + end + end + end + success + end + + #--- + def self.components(search) components = [] array(search).each do |element| break if element.match(/^\-+/) @@ -521,10 +577,10 @@ unless components.empty? action_id = components.is_a?(Array) ? components.flatten.join('::') : components action_id_pattern = action_id.gsub('::', ':.*:') action_index.each do |loaded_action_id, loaded_action_info| - if loaded_action_id.match(/#{action_id_pattern}/) + if loaded_action_id.match(/(^|\:)#{action_id_pattern.gsub(/\-/, '\-')}(\:|$)/) loaded_action_info[:action_id] = loaded_action_id actions_found << loaded_action_info end end end