Class: Rubtools::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubtools.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Initialize the rubtool

  • Find all recipes

  • Parse options



15
16
17
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
# File 'lib/rubtools.rb', line 15

def initialize(args)
  @args = args
  @options = Hash.new
  @tools = Hash.new
  @matches = Array.new

  @tool_libs = Dir.glob(File.join(File.dirname(__FILE__), 'tools', '**', '*.rb'))
  @tool_libs.each {|lib| require lib }

  option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: rt constant:method [options]"

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      @options[:verbose] = v
    end
    
    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("-h", "--help", "Show help") do
      Logger.info opts
      exit
    end

    opts.on_tail("--version", "Show rubTools version") do
      Logger.info Rubtools::VERSION
      exit
    end
  end

  begin
    option_parser.parse!(args)
    raise ArgumentError, "missing arguments" unless @args.any?
  rescue OptionParser::InvalidOption, ArgumentError => e
    Logger.info e.message
    Logger.info option_parser
    exit
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



9
10
11
# File 'lib/rubtools.rb', line 9

def args
  @args
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/rubtools.rb', line 9

def options
  @options
end

#tool_libsObject

Returns the value of attribute tool_libs.



9
10
11
# File 'lib/rubtools.rb', line 9

def tool_libs
  @tool_libs
end

#toolsObject

Returns the value of attribute tools.



9
10
11
# File 'lib/rubtools.rb', line 9

def tools
  @tools
end

Instance Method Details

#run!Object

Run!

  • Find the best constant method passed in parameter

  • Execute the method



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/rubtools.rb', line 59

def run!
  req_const_method = @args.shift.downcase
  values = req_const_method.split(/\W/)

  if values.size == 1
    req_method = values.flatten[0]
  elsif values.size == 2
    req_constant = values.flatten[0]
    req_method = values.flatten[1]
  end

  # Searching in tools libraries the method to call
  for constant in Rubtools::Tools.constants
    @tools[constant] = Array.new

    for method in Rubtools::Tools.const_get(constant).instance_methods(false)
      @tools[constant] << method
    end
  end

  for constant in @tools.keys
    for method in @tools[constant]
      if (req_method == method.to_s.downcase && req_constant == constant.to_s.downcase) || req_method == method.to_s.downcase
        @matches << [constant, method]
      end
    end
  end

  if @matches.any?
    constant = @matches.first[0]
    method = @matches.first[1]

    Logger.info "Calling #{constant}::#{method}..."
    object = Rubtools::Tools.const_get(constant).new
    object.options = @options
    
    begin
      if @args.any?
        object.method(method).call @args
      else
        object.method(method).call
      end
    rescue ArgumentError => e
      Logger.error "Method '#{method}' takes arguments: " + e.message
    end
  else
    Logger.error "No recipe found..."
  end
end