Class: Recipe

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

Overview

Recipe is the datastore class for the task list managed by baker

Responsible for:

- Parsing a md file into a Recipe
- Serialization back into a md file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stepsObject

Array of ‘RecipeStep`s



13
14
15
# File 'lib/baker/bakerlib.rb', line 13

def steps
  @steps
end

Class Method Details

.from_s(s) ⇒ Object



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
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
# File 'lib/baker/bakerlib.rb', line 15

def self.from_s(s)

  j = Recipe.new
  j.steps = []
  
  next_day = []
  multiline_ruby = nil
  s.each_line { |line|

    if multiline_ruby
      if line =~ /^\s*```/
        
        multiline_ruby[:lines] << line

        j.steps << RecipeStep.new(
          multiline_ruby[:lines], 
          type: :ruby, 
          task_marker: multiline_ruby[:task_marker], 
          command: multiline_ruby[:command].join, 
          description: multiline_ruby[:description])
        
        multiline_ruby = nil

      else
        multiline_ruby[:lines] << line
        multiline_ruby[:command] << line
      end
      next
    end

    if line =~ /^::(\w*?)(?:\[(.*?)\])?(?:\{(.*)\})?\s*$/
      directive = $1.downcase
      content = $2
      attributes = $3

      # puts "Directive: '#{directive}' - Content: '#{content}' Attributes: '#{attributes}'"

      case directive
      when "template"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :template, content: content, attributes: attributes)
      when "template_source"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :template_source, content: content, attributes: attributes)
      when "var"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :var, content: content, attributes: attributes)
      when "cd"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :cd, content: content)
      else
        raise "Unknown directive: #{directive}"
      end

    elsif line =~ /^\s*(-\s+)?\[(.)\]\s*(?:(.*?):)?\s*`([^`]+)`/

      j.steps << RecipeStep.new(line, type: :shell, task_marker: $2, command: $4, description: $3)

    elsif line =~ /^\s*(-\s+)?\[(.)\]\s*(?:(.*?):)?\s*``([^`]+)``/

      j.steps << RecipeStep.new(line, type: :ruby, task_marker: $2, command: $4, description: $3)

    elsif line =~ /^\s*(-\s+)?\[(.)\]\s(?:(.*?):)?\s*```(.*$)/m
    
      multiline_ruby = {lines: [line], task_marker: $2, description: $3, command: [$4]}
      
    elsif line =~ /^\s*(-\s+)?\[(.)\]\s*(.*)/

      j.steps << RecipeStep.new(line, type: :manual, task_marker: $2, description: $3)
    
    else
      j.steps << RecipeStep.new(line, type: :nop)
    end
  }

  return j
end

Instance Method Details

#to_sObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/baker/bakerlib.rb', line 89

def to_s
  result = []

  empty = false
  steps.each { |step| 
    if step.lines.empty? 
      empty = true
    else
      # if we encountered a deleted line (empty array), we skip all blank lines until we encounter a non-blank line
      if empty && step.lines.all? { |line| line.strip.size == 0 }
        # nothing
      else
        empty = false
        result << step.to_s
      end
    end
  }

  return result.join # + (empty ? "" : "\n")
end