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
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
189
190
191
192
193
194
# File 'lib/baker/bakerlib.rb', line 15

def self.from_s(s)

  j = Recipe.new
  j.steps = []
  
  next_day = []
  multiline = nil
  s.each_line.with_index { |line, index|

    if multiline
      multiline[:lines] << line

      case multiline[:multiline_type]
      when :ruby, :shell

        if line =~ /^\s*```\s*$/
          
          multiline[:command] << $1

          j.steps << RecipeStep.new(
            multiline[:lines], 
            type: multiline[:multiline_type], 
            task_marker: multiline[:task_marker], 
            command: multiline[:command].join, 
            description: multiline[:description],
            line_index: multiline[:line_index])
          
          multiline = nil
          next
        end

        # Multiline continues just collect items.
        multiline[:command] << line
        next

      when :unknown

        # If line starts with ``` we have a new block
        if line =~ /^\s*```(.*\z)/m

          # If this block starts with ```bash or ```sh we have a shell block
          command = $1
          if command.strip == "bash" || command.strip == "sh"
            multiline[:multiline_type] = :shell
            next
          end

          # All other blocks are considered ruby blocks
          multiline[:multiline_type] = :ruby

          if command =~ /^(.*)```\s*$/
            # Ruby block terminated on same line
            command = $1
            raise if multiline[:command].size > 0
            
            j.steps << RecipeStep.new(
              multiline[:lines],
              type: multiline[:multiline_type],
              task_marker: multiline[:task_marker],
              command: command,
              description: multiline[:description],
              line_index: multiline[:line_index])

            multiline = nil
            next
          end 

          if command.strip != "ruby" && command.strip.size > 0
            multiline[:command] << command
          end
          next
        end
          
        # Line did not start with ```

        j.steps << RecipeStep.new(
          multiline[:lines],
          type: :manual,
          task_marker: multiline[:task_marker],
          description: multiline[:description],
          line_index: multiline[:line_index])

        multiline = nil
      
        # IMPORTANT: Don't call next here, we continue processing this line as a normal line

      else
        raise "Unknown or missing multiline type: #{multiline[:multiline_type]}"
      end
    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, line_index: index)
      when "template_source"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :template_source, content: content, attributes: attributes, line_index: index)
      when "var"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :var, content: content, attributes: attributes, line_index: index)
      when "cd"
        j.steps << RecipeStep.new(line, type: :directive, directive_type: :cd, content: content, line_index: index)
      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, line_index: index)

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

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

    # General case in which a multiline task might be started
    # We will try to find the last colon to separate description and command unless there is a backtick after a colon
    elsif line =~ /^\s*(-\s+)?\[(.)\]\s(?:(.*?):(?=\s*`)|(.*):(?=[ \n]))?\s*(.*\z)/m

      task_marker = $2
      description = $3 || $4
      command = $5

      # Command starts with ``` => Ruby command block
      if command =~ /^```(.*\z)/m
        command_block_type = :ruby
        command = $1
        if command.strip == "ruby"
          command = "" # Ignore markdown block designator for ruby
        end
        if command =~ /^(.*)```\s*$/
          # Ruby block terminated on same line
          command = $1
          j.steps << RecipeStep.new(line, type: :ruby, 
            task_marker: task_marker, command: command, description: description, line_index: index)
        else # Multiline ruby command started
          commands = []
          if command.strip.size > 0
            commands << command
          end
          multiline = {
            lines: [line], 
            multiline_type: :ruby,
            task_marker: task_marker, description: description, command: commands, line_index: index
          }
        end
      else # Block doesn't start with ````
        if command.strip.size > 0
          # Not a ``` block but just other text after a colon

          description = (description ? [description, command].join(": ") : command).chomp
          j.steps << RecipeStep.new(line, type: :manual, task_marker: task_marker, description: description, line_index: index)
        else
          # Empty line after colon, we don't know yet what comes next
          multiline = {
            lines: [line], 
            multiline_type: :unknown,
            task_marker: task_marker, description: description, command: [], line_index: index
          }
        end
      end
      
    elsif line =~ /^\s*(-\s+)?\[(.)\]\s*(.*\z)/m

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

    end
  }
  if multiline
    raise "Unterminated multiline block. Are you missing closing triple backticks ``` on an empty line?" 
  end
  return j
end

Instance Method Details

#to_sObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/baker/bakerlib.rb', line 196

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