Class: Rook::Kitchen

Inherits:
Object
  • Object
show all
Includes:
Assertion, KitchenHelper
Defined in:
lib/rook/kitchen.rb

Overview

kitchen class

ex.

cookbooks, errors = Cookbook.load_yamlfile('Rookbook.yaml')
kitchen = Kitchen.new
cookbooks.each { |book| kitchen.load_book(book) }
kitchen.start_cooking('product_name')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertion

#assert, #assert_failed

Constructor Details

#initialize(properties = {}, options = {}) ⇒ Kitchen

Returns a new instance of Kitchen.



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/rook/kitchen.rb', line 300

def initialize(properties={}, options={})
  @value_table = ValueTable.new
  @properties = []
  @parameters = []
  properties.each do |name, value|
    add_property({'name'=>name.to_s, 'value'=>value})
  end
  @recipes = []
  @recipe_table = {}
  @materials = []
  @preparations = []
  @forced  = options[:forced]
  #@verbose = options[:verbose]
  @noexec  = options[:noexec]
end

Instance Attribute Details

#materialsObject (readonly)

Returns the value of attribute materials.



316
317
318
# File 'lib/rook/kitchen.rb', line 316

def materials
  @materials
end

#parametersObject (readonly)

Returns the value of attribute parameters.



315
316
317
# File 'lib/rook/kitchen.rb', line 315

def parameters
  @parameters
end

#preparationsObject (readonly)

Returns the value of attribute preparations.



316
317
318
# File 'lib/rook/kitchen.rb', line 316

def preparations
  @preparations
end

#propertiesObject (readonly)

Returns the value of attribute properties.



315
316
317
# File 'lib/rook/kitchen.rb', line 315

def properties
  @properties
end

#recipesObject (readonly)

Returns the value of attribute recipes.



316
317
318
# File 'lib/rook/kitchen.rb', line 316

def recipes
  @recipes
end

Instance Method Details

#_evaluate_expr(expr, linenum = nil) ⇒ Object



360
361
362
# File 'lib/rook/kitchen.rb', line 360

def _evaluate_expr(expr, linenum=nil)
  return @value_table.evaluate_expr(expr, @filename, linenum)
end

#_expand_value(value, linenum = nil) ⇒ Object



356
357
358
# File 'lib/rook/kitchen.rb', line 356

def _expand_value(value, linenum=nil)
  return @value_table.expand_value(value, @filename, linenum)
end

#add_material(material_str) ⇒ Object



460
461
462
463
# File 'lib/rook/kitchen.rb', line 460

def add_material(material_str)
  mat_str = _expand_value(material_str)
  @materials << mat_str
end

#add_parameter(hash, bookname = '') ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/rook/kitchen.rb', line 439

def add_parameter(hash, bookname='')
  name = hash['name']
  if has_parameter?(name)
    @parameters.delete_if { |h| h['name'] == name }
  end
  if hash.key?('value')
    value = _expand_value(hash['value'])
  elsif hash.key?('value*')
    code = hash['value*']
    linenum = hash['linenum']
    code = _expand_value(code)
    value = _evaluate_expr(code, linenum)
  else
    raise RookError.new("parameter '#{name}': no value.")
  end
  set_parameter(name, value)
  hash['value'] = value
  @parameters << hash
end

#add_preparation(code_str, bookname = '', linenum = 1) ⇒ Object



409
410
411
412
# File 'lib/rook/kitchen.rb', line 409

def add_preparation(code_str, bookname='', linenum=1)
  @preparations << [code_str, bookname, linenum]
  Util.evaluate(code_str, bookname, linenum)    # or instance_eval?
end

#add_property(hash, bookname = '') ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/rook/kitchen.rb', line 415

def add_property(hash, bookname='')
  hash['access'] ||= 'public'
  name = hash['name']
  if has_property?(name)
    value = get_property(name)
  else
    if hash.key?('value')
      value = _expand_value(hash['value'])
    elsif hash.key?('expr') || hash.key?('value*')
      code = hash.key?('expr') ? hash['expr'] : hash['value*']
      code = _expand_value(code)
      value = _evaluate_expr(code, hash['linenum'])
    else
      raise RookError.new("property '#{name}': no value.")
    end
    set_property(name, value)
    hash['value'] = value
    @properties << hash
  end
  #eval "$#{name} = value"
  instance_variable_set("@#{name}", value)
end

#add_recipe(recipe) ⇒ Object



484
485
486
487
488
# File 'lib/rook/kitchen.rb', line 484

def add_recipe(recipe)
  @recipes.delete_if { |r| r.product == recipe.product } if @recipe_table[recipe.product]
  @recipe_table[recipe.product] = recipe
  @recipes << recipe
end

#cook(product, cooking, level = 0, verbose = nil, parent_fingerprint = false) ⇒ Object

cook product

return true when product recipe is invoked, else return false.



522
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
572
573
574
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/rook/kitchen.rb', line 522

def cook(product, cooking, level=0, verbose=nil, parent_fingerprint=false)
  ## check
  if @materials.include?(product)
    log("skip '#{Util.to_str(product)}': material.", level, verbose)
    return false
  end
  if cooking.cooked?(product)
    log("skip '#{Util.to_str(product)}': already cooked.", level, verbose)
    return false
  end
  if cooking.now_cooking?(product)
    raise RookError.new("'#{Util.to_str(product)}': recipe is recursively looped.")
  end

  ## find recipe
  recipe = find_recipe(product)
  unless recipe
    if product.is_a?(String) && test(?e, product)
      log("skip '#{Util.to_str(product)}': raw ingredient.", level, verbose)
      return false
    else
      raise RookError.new("'#{Util.to_str(product)}': recipe not found.")
    end
  end
  recipe = recipe.to_specific(product)

  ## start cooking
  cooking.start(recipe)
  log("begin '#{Util.to_str(product)}'", level, recipe.verbose?)
  flag_exec = @forced

  ## ingredients
  ingreds = recipe.ingreds ? recipe.ingreds.dup : []
  ingreds.concat(recipe.toppings.find_all { |f| test(?e, f) }) if recipe.toppings
#      ingreds.each do |ingred|
#        flag = cook(ingred, cooking, level+1, recipe.verbose?, recipe.fingerprint)
#        flag_exec = true if flag
#      end

  ## execute when
  ## * command-line option -F specified
  ## * executed at least one recipe of ingredients
  ## * recipe attribute 'forced' is true
  ## * recipe is symbolic and has howoto part
  ## * there isn't product
  ## * recipe has no ingredient
  ## * there is any ingredient which is newer than product
  unless flag_exec
    flag_exec ||= recipe.forced?
    flag_exec ||= recipe.symbolic? && (recipe.method || recipe.block)
    flag_exec ||= !recipe.symbolic? && !test(?e, product)
    flag_exec ||= ingreds.empty? && (recipe.method || recipe.block)
  end
  unless flag_exec || ingreds.empty?
    prod_mtime = File.mtime(product)
    flag_exec = ingreds.any? { |f| f.is_a?(String) && test(?e, f) && prod_mtime < File.mtime(f) }
    #unless flag_exec
    #  log("skip '#{Util.to_str(product)}': newer than all ingredients.", level, recipe.verbose?)
    #end
  end

  ## invoke ingredients' recipe
  #ingreds = recipe.ingreds ? recipe.ingreds.dup : []
  #ingreds.concat(recipe.toppings.find_all { |f| test(?e, f) }) if recipe.toppings
  ingreds.each do |ingred|
    flag = cook(ingred, cooking, level+1, recipe.verbose?, recipe.fingerprint)
    flag_exec = true if flag
  end

  unless flag_exec || ingreds.empty?
    log("skip '#{Util.to_str(product)}': newer than all ingredients.", level, recipe.verbose?)
  end

  ## execute method part
  fingerprint_value = nil
  if flag_exec
    flag_fp = parent_fingerprint == 'ingreds' || recipe.fingerprint == 'product'
    if flag_fp && test(?f, product)
      fingerprint_value = Util.fingerprint(product)
    end
    report(product, level, recipe.verbose?)
    begin
      #recipe.invoke(@value_table)
      recipe.invoke(self)
    rescue => ex
      prods = [ recipe.product ]
      prods += recipe.coprods if recipe.coprods
      prods.each do |file|
        next unless file.is_a?(String) && test(?f, file)
        log("removing '#{file}' because of #{ex.class.name} - #{ex.message}", level, true)
        File.unlink(file)
      end
      raise ex
    end
  end

  ## validate whether product is generated
  unless recipe.symbolic? || @noexec || test(?e, product)
    raise RookError.new("product '#{Util.to_str(product)}': not generated")
  end

  ## fingerprint check
  if fingerprint_value
    fp1 = fingerprint_value
    fp2 = Util.fingerprint(product)
    op = fp1 == fp2 ? '==' : '!='
    log("fingerprint of #{product}: #{fp1} #{op} #{fp2}", level, recipe.verbose?)
    flag_exec = false if fp1 == fp2
  end

  ## finish cooking
  log("end '#{Util.to_str(product)}'", level, recipe.verbose?)
  cooking.finish(recipe)
  return flag_exec

end

#find_generic_recipe(product) ⇒ Object



491
492
493
# File 'lib/rook/kitchen.rb', line 491

def find_generic_recipe(product)
  return @recipes.find { |r| r.kind == 'generic' && r.match?(product) }
end

#find_recipe(product) ⇒ Object



501
502
503
# File 'lib/rook/kitchen.rb', line 501

def find_recipe(product)
  return find_specific_recipe(product) || find_generic_recipe(product)
end

#find_specific_recipe(product) ⇒ Object



496
497
498
# File 'lib/rook/kitchen.rb', line 496

def find_specific_recipe(product)
  return @recipes.find { |r| r.kind == 'specific' && r.match?(product) }
end

#get_parameter(name) ⇒ Object



332
333
334
# File 'lib/rook/kitchen.rb', line 332

def get_parameter(name)
  return @value_table.get_parameter(name)
end

#get_property(name) ⇒ Object



328
329
330
# File 'lib/rook/kitchen.rb', line 328

def get_property(name)
  return @value_table.get_property(name)
end

#get_value(name) ⇒ Object



344
345
346
# File 'lib/rook/kitchen.rb', line 344

def get_value(name)
  return @value_table.get_value(name)
end

#has_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/rook/kitchen.rb', line 324

def has_parameter?(name)
  return @value_table.has_parameter?(name)
end

#has_property?(name) ⇒ Boolean

Returns:

  • (Boolean)


320
321
322
# File 'lib/rook/kitchen.rb', line 320

def has_property?(name)
  return @value_table.has_property?(name)
end

#load_book(cookbook) ⇒ Object



370
371
372
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
# File 'lib/rook/kitchen.rb', line 370

def load_book(cookbook)
  b = cookbook
  # preparation
  b.preparations.each do |hash|
    add_preparation(hash['code'], b.bookname, hash['linenum'])
  end if b.preparation
  # properties
  b.properties.each do |hash|
    hash['bookname'] = b.bookname
    add_property(hash, b.bookname)
  end if b.properties
  # parameters
  b.parameters.each do |hash|
    hash['bookname'] = b.bookname
    add_parameter(hash, b.bookname)
  end if b.parameters
  # materials
  b.materials.each do |nonprod|
    add_material(nonprod)
  end if b.materials
  # recipes
  b.recipes.each do |hash|
    hash = @value_table.expand_value(hash)
    hash['bookname'] = b.bookname
    recipe = Recipe.new(hash)
    add_recipe(recipe)
  end if b.recipes
end

#load_books(cookbooks) ⇒ Object



365
366
367
# File 'lib/rook/kitchen.rb', line 365

def load_books(cookbooks)
  cookboos.each do |cookbook| load_book(cookbook) end
end

#load_script(filename) ⇒ Object



400
401
402
# File 'lib/rook/kitchen.rb', line 400

def load_script(filename)
  self.instance_eval File.read(filename), filename, 1
end

#load_script!(script, filename) ⇒ Object



404
405
406
# File 'lib/rook/kitchen.rb', line 404

def load_script!(script, filename)
  self.instance_eval script, filename, 1
end

#parameter_namesObject



352
353
354
# File 'lib/rook/kitchen.rb', line 352

def parameter_names
  return @value_table.parameter_names
end

#property_namesObject



348
349
350
# File 'lib/rook/kitchen.rb', line 348

def property_names
  return @value_table.property_names
end

#set_parameter(name, value) ⇒ Object



340
341
342
# File 'lib/rook/kitchen.rb', line 340

def set_parameter(name, value)
  @value_table.set_parameter(name, value)
end

#set_property(name, value) ⇒ Object



336
337
338
# File 'lib/rook/kitchen.rb', line 336

def set_property(name, value)
  @value_table.set_property(name, value)
end

#start_cooking(product) ⇒ Object



506
507
508
509
510
511
512
513
514
# File 'lib/rook/kitchen.rb', line 506

def start_cooking(product)
  cooking = Cooking.new(product)
  begin
    cook(product, cooking, 0)
  rescue Exception => ex
    log(ex.message, 0, nil, '*** ')
    raise ex
  end
end