Class: Nucleon::Plugin::Action
- Inherits:
-
Object
- Object
- Nucleon::Plugin::Action
- Extended by:
- Mixin::Colors
- Includes:
- Mixin::Action::Registration
- Defined in:
- lib/core/plugin/action.rb
Defined Under Namespace
Class Method Summary collapse
-
.action_help(action = nil, extended_help = false) ⇒ Object
—.
-
.action_index(tree = true) ⇒ Object
—.
-
.components(search) ⇒ Object
—————————————————————————– Utilities.
-
.describe(group = nil, action = 'unknown', weight = -1000,, description = nil, help = nil) ⇒ Object
—————————————————————————– Info.
- .describe_base(group = nil, action = 'unknown', weight = -1000,, description = nil, help = nil, provider_override = nil) ⇒ Object
- .exec(provider, options, quiet = true, display_errors = true, state = nil) ⇒ Object
- .exec_cli(provider, args, quiet = false, name = :nucleon, display_errors = true, state = nil) ⇒ Object
-
.exec_safe(provider, options, display_errors = true, state = nil) ⇒ Object
—————————————————————————– Action plugin interface.
-
.namespace ⇒ Object
—.
-
.render_colorized(text, length = 0) ⇒ Object
—.
-
.search_actions(search_components) ⇒ Object
—.
Instance Method Summary collapse
- #arguments ⇒ Object
-
#cleanup ⇒ Object
—.
-
#config ⇒ Object
—.
-
#config_subset(names) ⇒ Object
—.
-
#configure {|action_info| ... } ⇒ Object
—.
-
#execute(skip_validate = false, skip_hooks = false) ⇒ Object
—.
-
#finalize_execution(skip_hooks = false) ⇒ Object
—.
-
#help ⇒ Object
—.
-
#ignore ⇒ Object
—.
-
#index_config ⇒ Object
—————————————————————————– Property accessor / modifiers.
-
#namespace ⇒ Object
—.
-
#normalize(reload) ⇒ Object
—.
- #options ⇒ Object
- #parse(parser) ⇒ Object
-
#parse_base(args) ⇒ Object
—————————————————————————– Operations.
-
#parse_types ⇒ Object
—.
-
#processed? ⇒ Boolean
—.
- #register(name, type, default = nil, locale = nil, &code) ⇒ Object
-
#registration_provider ⇒ Object
—.
-
#remove(names) ⇒ Object
—.
-
#render_options ⇒ Object
—————————————————————————– Output.
- #result ⇒ Object
-
#result=(result) ⇒ Object
—.
-
#run ⇒ Object
—.
-
#settings ⇒ Object
—.
-
#strict? ⇒ Boolean
—————————————————————————– Checks.
- #usage ⇒ Object
-
#usage=(usage) ⇒ Object
—.
-
#validate(*args) ⇒ Object
—.
Methods included from Mixin::Colors
black, blue, cyan, green, grey, purple, red, yellow
Methods included from Mixin::Action::Registration
#register_array, #register_bool, #register_directories, #register_directory, #register_file, #register_files, #register_float, #register_int, #register_plugin, #register_plugin_provider, #register_plugin_providers, #register_plugin_type, #register_plugin_types, #register_plugins, #register_project, #register_project_provider, #register_project_providers, #register_projects, #register_str, #register_translator, #register_translator_provider, #register_translator_providers, #register_translators, #split_locales, #validate_directories, #validate_directory, #validate_file, #validate_files, #validate_plugin_providers, #validate_plugin_types, #validate_plugins
Class Method Details
.action_help(action = nil, extended_help = false) ⇒ Object
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
# File 'lib/core/plugin/action.rb', line 624 def self.action_help(action = nil, extended_help = false) action_index = action_index(false).export provider_index = {} processed_actions = {} last_namespace = nil last_group = nil multiple_found = false command_width = 0 namespace_command_width = {} output = '' if action if action.empty? output << cyan(sprintf("\n%s\n", I18n.t('nucleon.core.exec.help.no_actions_found'))) else multiple_found = true output << cyan(sprintf("\n%s\n", I18n.t('nucleon.core.exec.help.multiple_actions_found'))) action.each do |info| provider_index[info[:provider]] = true end end end action_index.each do |action_id, info| if ! multiple_found || provider_index.has_key?(info[:provider]) action = Nucleon.action(info[:provider], { :settings => {}, :quiet => true, :help => true }) command_text = action.help command_size = command_text.gsub(/\e\[(\d+)m/, '').size command_width = [ command_width, command_size + 2 ].max namespace = info[:description][:namespace] namespace_command_width[namespace] = 0 unless namespace_command_width.has_key?(namespace) namespace_command_width[namespace] = [ namespace_command_width[namespace], command_size + 2 ].max if extended_help help_text = '' info[:description][:help].split("\n").each do |line| break if ! help_text.empty? && line.empty? help_text << ' ' + line + "\n" end else help_text = nil end processed_actions[action_id] = { :info => info, :command => command_text, :help => help_text } end end processed_actions.each do |action_id, info| command_text = info[:command] help_text = info[:help] info = info[:info] namespace = info[:description][:namespace] group = info[:description][:group] group_id = group.is_a?(Array) ? group.flatten.join('::') : group.to_s group_id = '' unless group_id output << "\n" if group_id != last_group if namespace != last_namespace output << "\n----------------------------------------------------\n" if help_text output << sprintf("\n %s:\n\n", I18n.t('nucleon.core.exec.help.action_group', { :namespace => purple(namespace) })) end if help_text output << " " + render_colorized(command_text, namespace_command_width[namespace]) + " -- " + blue(info[:description][:description]) + "\n" output << "\n#{help_text}\n" else output << " " + render_colorized(command_text, command_width) + " -- " + blue(info[:description][:description]) + "\n" end last_namespace = namespace last_group = group_id end output end |
.action_index(tree = true) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/core/plugin/action.rb', line 523 def self.action_index(tree = true) action_config = Config.new action_index = Config.new generate_index = lambda do |info, parents = nil| groups = info.keys - [ :_weight, :_weights ] groups = groups.sort do |a, b| info[b][:_weight] <=> info[a][:_weight] end groups.each do |group| data = info[group] if data.is_a?(Hash) && data.has_key?(:_weights) sub_parents = parents.nil? ? [ group ] : [ parents, group ].flatten generate_index.call(data, sub_parents) else keys = tree ? [ parents, group ] : [ parents, group ].flatten.join('::') action_index.set(keys, data) end end end Nucleon.loaded_plugins(:nucleon, :action).each do |provider, data| description = data[:class].describe data[:description] = description data[:_weight] = description[:weight] keys = [ description[:namespace], description[:group], description[:action] ].flatten.compact action_config.set(keys, data) keys.pop while ! keys.empty? group_config = action_config.get(keys) if group_config.has_key?(:_weights) group_config[:_weights].push(description[:weight]) else action_config.set([ keys, :_weights ], [ description[:weight] ]) end action_config.set([ keys, :_weight ], group_config[:_weights].inject(0.0) { |sum, el| sum + el } / group_config[:_weights].size) keys.pop end end generate_index.call(action_config.export) action_index end |
.components(search) ⇒ Object
Utilities
511 512 513 514 515 516 517 518 519 |
# File 'lib/core/plugin/action.rb', line 511 def self.components(search) components = [] array(search).each do |element| break if element.match(/^\-+/) components << element end components end |
.describe(group = nil, action = 'unknown', weight = -1000,, description = nil, help = nil) ⇒ Object
Info
15 16 17 |
# File 'lib/core/plugin/action.rb', line 15 def self.describe(group = nil, action = 'unknown', weight = -1000, description = nil, help = nil) describe_base(group, action, weight, description, help) end |
.describe_base(group = nil, action = 'unknown', weight = -1000,, description = nil, help = nil, provider_override = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/core/plugin/action.rb', line 19 def self.describe_base(group = nil, action = 'unknown', weight = -1000, description = nil, help = nil, provider_override = nil) if provider_override provider_override = provider_override.to_s.gsub('_', '.') description_id = "#{namespace}.action.#{provider_override}.description" help_id = "#{namespace}.action.#{provider_override}.help" else 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 description_id = "#{namespace}.action.#{action}.description" help_id = "#{namespace}.action.#{action}.help" end end { :namespace => namespace, :weight => weight, :group => group, :action => action, :description => description ? description : I18n.t(description_id), :help => help ? help : I18n.t(help_id) } end |
.exec(provider, options, quiet = true, display_errors = true, state = nil) ⇒ Object
129 130 131 |
# File 'lib/core/plugin/action.rb', line 129 def self.exec(provider, , quiet = true, display_errors = true, state = nil) exec_safe(provider, { :settings => Config.ensure(), :quiet => quiet }, display_errors, state) end |
.exec_cli(provider, args, quiet = false, name = :nucleon, display_errors = true, state = nil) ⇒ Object
133 134 135 |
# File 'lib/core/plugin/action.rb', line 133 def self.exec_cli(provider, args, quiet = false, name = :nucleon, display_errors = true, state = nil) exec_safe(provider, { :args => args, :quiet => quiet, :executable => name }, display_errors, state) end |
.exec_safe(provider, options, display_errors = true, state = nil) ⇒ Object
Action plugin interface
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/core/plugin/action.rb', line 99 def self.exec_safe(provider, , display_errors = true, state = nil) begin state = State.new(Nucleon.code.unknown_status) unless state logger = Nucleon.logger logger.info("Running nucleon action #{provider} with #{.inspect}") state.action = Nucleon.action(provider, ) state.action.execute state.status = state.action.status if state.action.status.is_a?(Integer) state.result = state.action.result rescue => error # This does NOT catch interrupts state.error = error if display_errors logger.error("Nucleon action #{provider} experienced an error:") logger.error(state.error.inspect) logger.error(state.error.) logger.error(Nucleon::Util::Data.to_yaml(state.error.backtrace)) Nucleon.ui.error(state.error., { :prefix => false }) if state.error. end state.action.finalize_execution(false) if state.action end state end |
.namespace ⇒ Object
47 48 49 |
# File 'lib/core/plugin/action.rb', line 47 def self.namespace :nucleon end |
.render_colorized(text, length = 0) ⇒ Object
714 715 716 717 718 |
# File 'lib/core/plugin/action.rb', line 714 def self.render_colorized(text, length = 0) command_size = text.gsub(/\e\[(\d+)m/, '').size remaining = [ length - command_size, 0 ].max text + sprintf("%#{remaining}s", ' ') end |
.search_actions(search_components) ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/core/plugin/action.rb', line 575 def self.search_actions(search_components) action_components = components(search_components) action_index = action_index(false).export actions_found = [] final_components = [] search_action = lambda do |components| unless components.empty? index = action_index action_id = components.is_a?(Array) ? components.flatten.join('::') : components action_id_pattern = action_id.gsub('::', ':.*:') # Check for exact matches index.each do |loaded_action_id, loaded_action_info| if loaded_action_id.match(/^[^\:]+\:\:#{action_id.gsub(/\-/, '\-')}$/) loaded_action_info[:action_id] = loaded_action_id actions_found << loaded_action_info break end end if actions_found.empty? # Check for similarly named actions index.each do |loaded_action_id, loaded_action_info| 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 end if components.is_a?(Array) && ! components.empty? && actions_found.empty? components.pop final_components = components search_action.call(components) else final_components = components end end search_action.call(action_components) unless action_components.empty? { :actions => actions_found.size == 1 ? actions_found[0] : actions_found, :components => final_components } end |
Instance Method Details
#arguments ⇒ Object
263 264 265 |
# File 'lib/core/plugin/action.rb', line 263 def arguments [] end |
#cleanup ⇒ Object
491 492 493 494 495 496 497 498 |
# File 'lib/core/plugin/action.rb', line 491 def cleanup logger.info("Running cleanup for action #{plugin_provider}") yield if block_given? # Nothing to do right now extension(:cleanup) end |
#config ⇒ Object
209 210 211 |
# File 'lib/core/plugin/action.rb', line 209 def config get(:config) end |
#config_subset(names) ⇒ Object
215 216 217 |
# File 'lib/core/plugin/action.rb', line 215 def config_subset(names) Util::Data.subset(config, names) end |
#configure {|action_info| ... } ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/core/plugin/action.rb', line 269 def configure action_info = index_config yield(action_info) if block_given? group = array(action_info[:description][:group]) action = cyan(action_info[:description][:action]) if ! group.empty? group = green(group.join(' ').strip) usage = "#{group} #{action} " else usage = "#{action} " end arguments.each do |arg| arg_config = config[arg.to_sym] arg_prefix = arg_config.default ? '[' : '' arg_suffix = arg_config.default ? ']' : '' if arg_config.type == :array usage << "#{arg_prefix}<#{arg}> ...#{arg_suffix}" else usage << "#{arg_prefix}<#{arg}>#{arg_suffix} " end end myself.usage = yellow(usage) myself end |
#execute(skip_validate = false, skip_hooks = false) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/core/plugin/action.rb', line 441 def execute(skip_validate = false, skip_hooks = false) logger.info("Executing action #{plugin_provider}") myself.status = code.success myself.result = nil if processed? if skip_validate || validate yield if block_given? && ( skip_hooks || extension_check(:exec_init) ) else puts "\n" + I18n.t('nucleon.core.exec.help.usage') + ': ' + help + "\n" unless quiet? myself.status = code.validation_failed skip_hooks = true end finalize_execution(skip_hooks) else if @parser.[:help] myself.status = code.help_wanted else myself.status = code.action_unprocessed end finalize_execution(true) end end |
#finalize_execution(skip_hooks = false) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/core/plugin/action.rb', line 468 def finalize_execution(skip_hooks = false) begin myself.status = extension_set(:exec_exit, status) unless skip_hooks ensure cleanup end myself.status = code.unknown_status unless myself.status.is_a?(Integer) if processed? && myself.status != code.success logger.warn("Execution failed for #{plugin_provider} with status #{status}") warn(Codes.render_status(status), { :i18n => false }) end end |
#help ⇒ Object
312 313 314 315 |
# File 'lib/core/plugin/action.rb', line 312 def help return @parser.help if @parser usage end |
#ignore ⇒ Object
255 256 257 |
# File 'lib/core/plugin/action.rb', line 255 def ignore [] end |
#index_config ⇒ Object
Property accessor / modifiers
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/core/plugin/action.rb', line 190 def index_config action_info = nil self.class.action_index(false).export.each do |action_id, info| if info[:provider] == plugin_provider action_info = info break end end Config.ensure(action_info) end |
#namespace ⇒ Object
203 204 205 |
# File 'lib/core/plugin/action.rb', line 203 def namespace self.class.namespace end |
#normalize(reload) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/core/plugin/action.rb', line 139 def normalize(reload) args = array(delete(:args, [])) help = delete(:help, false) unless reload @action_interface = Util::Liquid.new do |method, method_args| = {} = method_args[0] if method_args.length > 0 quiet = true quiet = method_args[1] if method_args.length > 1 myself.class.exec(method, , quiet) end set(:config, Config.new) if get(:settings, nil) # Internal processing configure set(:processed, true) set(:settings, Config.ensure(get(:settings))) Nucleon.log_level = settings[:log_level] if settings.has_key?(:log_level) else # External processing set(:settings, Config.new) configure parse_base(args) end yield if block_given? && ! help end end |
#options ⇒ Object
259 260 261 |
# File 'lib/core/plugin/action.rb', line 259 def config.keys - arguments - ignore end |
#parse(parser) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/core/plugin/action.rb', line 373 def parse(parser) generate = lambda do |format, name| formats = [ :option, :arg ] 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 default = option_config.default locale = option_config.locale if types.include?(type.to_sym) value_label = "#{type.to_s.upcase}" if type == :bool parser.send("option_#{type}", name, default, "--[no-]#{name}", locale) elsif format == :arg parser.send("#{format}_#{type}", name, default, locale) else if type == :array parser.send("option_#{type}", name, default, "--#{name} #{value_label},...", locale) else parser.send("option_#{type}", name, default, "--#{name} #{value_label}", locale) end end end end end #--- .each do |name| generate.call(:option, name) end arguments.each do |name| generate.call(:arg, name) end end |
#parse_base(args) ⇒ Object
Operations
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/core/plugin/action.rb', line 330 def parse_base(args) logger.info("Parsing action #{plugin_provider} with: #{args.inspect}") action_info = index_config help_text = '' action_info[:description][:help].split("\n").each do |line| help_text << ' ' + green(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.extra, @parser., @parser.arguments ], true)) logger.debug("Parse successful") elsif @parser.[:help] && ! quiet? executable = delete(:executable, '') puts I18n.t('nucleon.core.exec.help.usage') + ": " + executable.to_s + ' ' + help + "\n" else if @parser.[:help] logger.debug("Help wanted but running in silent mode") else logger.warn("Parse failed for unknown reasons") end end end end |
#parse_types ⇒ Object
369 370 371 |
# File 'lib/core/plugin/action.rb', line 369 def parse_types [ :bool, :int, :float, :str, :array ] end |
#processed? ⇒ Boolean
183 184 185 |
# File 'lib/core/plugin/action.rb', line 183 def processed? get(:processed, false) end |
#register(name, type, default = nil, locale = nil, &code) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/core/plugin/action.rb', line 231 def register(name, type, default = nil, locale = nil, &code) name = name.to_sym if code option = Option.new(namespace, registration_provider, name, type, default, locale) do |value, success| code.call(value, success) end else option = Option.new(namespace, registration_provider, name, type, default, locale) end config[name] = option settings[name] = option.default if settings[name].nil? end |
#registration_provider ⇒ Object
227 228 229 |
# File 'lib/core/plugin/action.rb', line 227 def registration_provider plugin_provider end |
#remove(names) ⇒ Object
248 249 250 251 |
# File 'lib/core/plugin/action.rb', line 248 def remove(names) Util::Data.rm_keys(config, names) Util::Data.rm_keys(settings, names) end |
#render_options ⇒ Object
Output
503 504 505 506 |
# File 'lib/core/plugin/action.rb', line 503 def = super .merge(settings.export) end |
#result ⇒ Object
323 324 325 |
# File 'lib/core/plugin/action.rb', line 323 def result get(:result, nil) end |
#result=(result) ⇒ Object
319 320 321 |
# File 'lib/core/plugin/action.rb', line 319 def result=result set(:result, result) end |
#run ⇒ Object
485 486 487 |
# File 'lib/core/plugin/action.rb', line 485 def run @action_interface end |
#settings ⇒ Object
221 222 223 |
# File 'lib/core/plugin/action.rb', line 221 def settings get(:settings) end |
#strict? ⇒ Boolean
Checks
177 178 179 |
# File 'lib/core/plugin/action.rb', line 177 def strict? true # Override in providers if needed (allow extra options if false) end |
#usage ⇒ Object
306 307 308 |
# File 'lib/core/plugin/action.rb', line 306 def usage get(:usage, '') end |
#usage=(usage) ⇒ Object
302 303 304 |
# File 'lib/core/plugin/action.rb', line 302 def usage=usage set(:usage, usage) end |
#validate(*args) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/core/plugin/action.rb', line 417 def validate(*args) # TODO: Add extension hooks and logging # Validate all of the configurations success = true config.export.each do |name, option| unless ignore.include?(name) success = false unless option.validate(settings[name], *args) end end if success # Check for missing arguments (in case of internal execution mode) arguments.each do |name| if settings[name.to_sym].nil? warn('nucleon.core.exec.errors.missing_argument', { :name => name }) success = false end end end success end |