Class: Rook::Main

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ Main

Returns a new instance of Main.



30
31
32
33
34
35
# File 'lib/rook/main.rb', line 30

def initialize(argv=ARGV)
  @argv   = argv
  @options, @properties = parse_argv(@argv)
  @options[?h] = true if @properties['help'] == true
  @products = argv
end

Class Method Details

.main(argv = ARGV) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rook/main.rb', line 38

def self.main(argv=ARGV)
  status = 0
  begin
    main = self.new(argv)
    output = main.execute()
    #print output if output
  rescue RookError => ex
    status = 1
    raise ex if $rook_debug
    $stderr.puts "*** error: #{ex.message}"
  end
  exit status
end

Instance Method Details

#executeObject



53
54
55
56
57
58
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
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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rook/main.rb', line 53

def execute
  options    = @options
  properties = @properties
  products   = @products

  ##
  $rook_debug = true if @options[?D]    # or $DEBUG
  $KCODE = @options[?k] if @options[?k]

  ## help and version
  if @options[?h] || @options[?v]
    cmdname = File.basename($0)
    #desc = "#{cmdname} - a SCM (software configuration management) tool like Make, Rake, Ant, or Cook.\n"
    desc = "#{cmdname} - a SCM (Software Cooking Management) tool like Make, Rake, Ant, or Cook.\n"
    puts version() if @options[?v]
    puts desc      if @options[?h]
    puts help()    if @options[?h]
    return
  end

  ## require libraries
  @options[?r].split(/,/).each { |lib| require lib } if @options[?r]

  ## bookname
  bookname = @options[?f] || @options[?b]
  if bookname
    test(?f, bookname) or raise _error("cookbook '#{bookname}' not found.")
  else
    ['.yaml', '.yml', '.rb', ''].each do |suffix|
      break if test(?f, bookname = "Rookbook#{suffix}")
      break if test(?f, bookname = "rookbook#{suffix}")
      bookname = nil
    end
    bookname  or raise _error("cookbook 'Rookbook.yaml' or 'Rookbook.rb' not found.")
  end

  ## properties filename
  propfile = @options[?P]
  if propfile
    test(?f, propfile) or raise _error("file '#{propfile}' not found.")
  else
    %w[Rookbook.props properties.yaml].each do |f|
      break propfile = f if test(?f, f)
    end
  end
  if propfile
    props = YAML.load_file(propfile)
    props.each do |k, v|
      @properties[k] = v unless @properties.key?(k)
    end
  end

  ## global options
  $rook_verbose = @options[?V] ? true : (@options[?q] ? false : nil)
  $rook_forced  = @options[?F]
  $rook_noexec  = @options[?n] ? 1 : (@options[?N] ? 2 : nil)

  ## cookbook
  style = bookname =~ /\.ya?ml\z/ ? :yaml : :ruby
  if style == :yaml
    flag_expand_tab = !options[?T]
    begin
      cookbook = Cookbook.new(bookname, true, flag_expand_tab)
    rescue CookbookError => ex
      $stderr.puts "validation error:"
      ex.errors.each do |error|
        e = error
        $stderr.puts "#{bookname}:#{e.linenum}: [#{e.path}] #{e.message}"
      end
      return
    end
  end

  ## check
  if @options[?c]
    style == :yaml or raise _error("-c: cannot check Ruby-DSL style cookbook.")
    cookbook.validate_rubycode()
    return
  end

  ## kitchen
  props = { :verbose=>$rook_verbose, :forced=>$rook_forced, :noexec=>$rook_noexec }
  kitchen = Kitchen.new(@properties, props)
  if style == :yaml
    kitchen.load_book(cookbook)
  else
    kitchen.load_script(bookname)
  end

  ## list recipes
  if @options[?l] || @options[?L]
    show_all   = @options[?L] ? true : false
    list_properties(kitchen, show_all)
    puts
    list_recipes(kitchen, @options[?L])
    return
  end

  ## show property/parameter value
  if @options[?p]
    @options[?p].split(/,/).each do |name|
      if kitchen.has_parameter?(name)
        pp kitchen.get_parameter(name)
      elsif kitchen.has_property?(name)
        pp kitchen.get_property(name)
      else
        raise _error("-p #{name}: no such parameter or property.")
      end
    end
    return
  end

  ## target product
  if @products && !@products.empty?
    products = @products
  elsif kitchen.find_specific_recipe(:default)
    products = [ :default ]
  elsif prod = kitchen.get_parameter('rook_product')
    products = [ prod ]
  elsif prod = kitchen.get_property('rook_product')
    products = [ prod ]
  else
    $stderr.puts "*** no product specified."
    list_recipes(kitchen)
    return
  end

  ## start cooking
  products.each do |product|
    product = product.sub(/\A:/, '').intern if product.is_a?(String) && product[0] == ?:
    kitchen.start_cooking(product)
  end

  return nil

end