Class: Rubtools::Runner
- Inherits:
-
Object
- Object
- Rubtools::Runner
- Defined in:
- lib/rubtools.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#config ⇒ Object
Returns the value of attribute config.
-
#option_parser ⇒ Object
Returns the value of attribute option_parser.
-
#options ⇒ Object
Returns the value of attribute options.
-
#tools ⇒ Object
Returns the value of attribute tools.
Instance Method Summary collapse
-
#initialize(args) ⇒ Runner
constructor
Initialize the rubtool - Find all recipes - Parse options.
-
#run! ⇒ Object
Run! - Find the best constant method passed in parameter - Execute the method.
Constructor Details
#initialize(args) ⇒ Runner
Initialize the rubtool
-
Find all recipes
-
Parse options
18 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rubtools.rb', line 18 def initialize(args) @args = args @options = Hash.new @tools = Hash.new @matches = Array.new rubtools_config_path = File.join(Dir.home, '.rubtools.yml') config_hash = YAML::load_file(rubtools_config_path) if File.exists? rubtools_config_path @config = RecursiveOpenStruct.new config_hash, recurse_over_arrays: true if config_hash if @config tool_libs = [] if @config.recipes && @config.recipes.any? for recipes_folder in @config.recipes if File.exists?(recipes_folder) && File.directory?(recipes_folder) tool_libs << Dir.glob(File.join(recipes_folder, '**', '*.rb')) end end end # Require recipes from paths in the config file require_all tool_libs.flatten end @option_parser = OptionParser.new do |opts| opts. = "Usage: rt constant:method [options]" opts.separator "" opts.separator "Common options:" opts.on_tail("-L", "--list", "List available commands") do |l| @options[:list] = l end opts.on_tail("-h", "--help", "Show help") do Logger.verbose opts exit end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| @options[:verbose] = v end opts.on_tail("--version", "Show rubTools version") do Logger.verbose Rubtools::VERSION exit end end begin @option_parser.parse!(args) rescue OptionParser::InvalidOption Logger.verbose @option_parser exit end end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
12 13 14 |
# File 'lib/rubtools.rb', line 12 def args @args end |
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/rubtools.rb', line 12 def config @config end |
#option_parser ⇒ Object
Returns the value of attribute option_parser.
12 13 14 |
# File 'lib/rubtools.rb', line 12 def option_parser @option_parser end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/rubtools.rb', line 12 def @options end |
#tools ⇒ Object
Returns the value of attribute tools.
12 13 14 |
# File 'lib/rubtools.rb', line 12 def tools @tools end |
Instance Method Details
#run! ⇒ Object
Run!
-
Find the best constant method passed in parameter
-
Execute the method
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rubtools.rb', line 79 def run! # Searching in tools libraries the method to call for constant in Rubtools::Tools.constants @tools[constant.downcase] = Array.new const = Rubtools::Tools.const_get(constant) unless const.are_methods_hidden? for method in const.available_methods method_hash = Hash.new method_hash[method] = Array.new for param in Rubtools::Tools.const_get(constant).instance_method(method).parameters method_hash[method] << param.last end @tools[constant.downcase] << method_hash end end end if @options[:list] Logger.verbose "Available commands: \n\n" for constant in @tools.keys for method_hash in @tools[constant] params = method_hash.first.last.any? ? "(#{method_hash.first.last.join(", ")})" : nil Logger.success "#{constant}:#{method_hash.keys.first}#{params}" end if @tools[constant].any? && @tools.keys.last != constant Logger.verbose "\n" end end exit end begin raise ArgumentError, "missing arguments" unless @args.any? rescue ArgumentError => e Logger.verbose e. Logger.verbose @option_parser exit end req_const_method = @args.shift.downcase values = req_const_method.split(/\W/) constant = values.flatten.first.capitalize method = values.flatten.last begin Logger.verbose "Calling #{constant}::#{method}..." object = Rubtools::Tools.const_get(constant).new @config, @options if @args.any? object.method(method).call @args else object.method(method).call end rescue Interrupt Logger.verbose "Interrupted" rescue NameError => e Logger.error e. rescue ArgumentError => e Logger.error "#{e.class} => #{e.}" for backtrace in e.backtrace Logger.error backtrace end end end |