Class: Baker

Inherits:
Object
  • Object
show all
Includes:
BakerActions
Defined in:
lib/baker.rb,
lib/baker/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BakerActions

#append_to_file, #format_command, #gsub_file, #inject_into_file, #pty_spawn, #rails_run, #sub_file, #unindent_common_whitespace, #user_for_host

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



34
35
36
# File 'lib/baker.rb', line 34

def debug
  @debug
end

#file_nameObject

Returns the value of attribute file_name.



34
35
36
# File 'lib/baker.rb', line 34

def file_name
  @file_name
end

#interactiveObject

Returns the value of attribute interactive.



34
35
36
# File 'lib/baker.rb', line 34

def interactive
  @interactive
end

#optsObject

Options Parser



37
38
39
# File 'lib/baker.rb', line 37

def opts
  @opts
end

#recipeObject

Returns the value of attribute recipe.



34
35
36
# File 'lib/baker.rb', line 34

def recipe
  @recipe
end

Class Method Details

.pluginObject Also known as: plugins

sig returns(Plugins)



28
29
30
# File 'lib/baker.rb', line 28

def plugin
  @plugin ||= Plugins.new
end

Instance Method Details

#create_options_parserObject



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

def create_options_parser

  @opts = OptionParser.new

  opts.banner = 'Usage: baker [options] [file]'

  opts.on('-v', '--verbose', 'Enable verbose/debug output') do
    @debug = true
    puts "Verbose mode enabled".yellow
  end

  opts.on('-d', '--diff', 'Show diff of the bake file to its template') do
    @diff_mode = true
    puts "Diff mode enabled".yellow if $stdout.tty?
  end

  opts.on('-i', '--interactive', 'Enable interactive mode') do
    @interactive = true
  end

  opts.on('-f', '--fast-forward', 'Skip all completed steps') do
    @fast_forward = true
  end

  opts.on('--no-save', 'Do not save any changes to the bake file') do
    @no_save = true
  end

  opts.on('-h', '--help', 'Displays Help') do
    puts opts
    exit
  end
end

#expand_vars(input) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/baker.rb', line 107

def expand_vars(input)

  return "" if input.nil? || input.empty?

  # Check if we received a ruby string literal, otherwise wrap it in %()
  require 'ripper'
  if Ripper.sexp(input)&.dig(1,0,0) != :string_literal
    input = '%(' + input + ')'
  end

  o = OpenStruct.new(@context)
  o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }

  return o.instance_eval(input)
end

#inspectObject



39
40
41
42
# File 'lib/baker.rb', line 39

def inspect
  # Nothing gained here from seeing instance variables
  return to_s
end

#load_configObject



197
198
199
200
# File 'lib/baker.rb', line 197

def load_config
  Dir.mkdir(File.expand_path('~/.baker')) unless Dir.exist?(File.expand_path('~/.baker'))
  @config = BakerConfig.new(File.expand_path('~/.baker/config.yml'))
end

#load_fileObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/baker.rb', line 173

def load_file

  @context = { file_name: @file_name }

  case Baker.plugins.run(:before_load, baker: self, context: @context)
  when :ask
    puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
    exit(1) if prompt_user_choice(nil) == :abort
  end # Fallthrough: :continue, :skip

  # @file_contents represents the file that baker believes is on disk.
  # It is used to check if there was any concurrent modification.
  @file_contents = File.read(@file_name)

  @recipe = Recipe.from_s(@file_contents)

  case Baker.plugins.run(:after_load, baker: self, context: @context)
  when :ask
    puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
    exit(1) if prompt_user_choice(nil) == :abort
  end # Fallthrough: :continue, :skip

end

#process_argsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/baker.rb', line 82

def process_args
  run_plugins(:before_options)
  
  begin
    opts.parse!
  rescue OptionParser::InvalidOption => e
    puts e
    exit(1)
  end

  @file_name = ARGV.shift || 'template.md'
  @file_name = File.expand_path(@file_name)

  if !File.exist?(@file_name)
    puts "Error: File #{@file_name} does not exist.".red
    exit(1)
  end

  @original_dir = Dir.pwd

  puts "Bake File: #{@file_name}".yellow if @debug

  run_plugins(:after_options)
end

#prompt_user_choice(line, allow_skip: false) ⇒ Object

Prompt the user for input and return their choice.



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/baker.rb', line 716

def prompt_user_choice(line, allow_skip: false)
  begin
    answer = STDIN.gets().strip.downcase
  rescue Interrupt
    answer = ''
  end

  answer = '' if !allow_skip && ['s', '-', 'p'].include?(answer)

  case answer
  when 'y'
    return :continue
  when 's'
    return :skip_temporarily
  when '-', 'p'
    return :skip_permanently
  else
    require 'pathname'
    puts
    puts (' → Aborting as requested. Run `baker ' + Pathname.new(@file_name).relative_path_from(@original_dir).to_s + '` to continue.').green
    puts "   Current task: #{@file_name}:#{line.source_code_line_index}".green if line
    return :abort
  end
end

#runObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/baker.rb', line 202

def run

  load_config

  create_options_parser

  Baker.plugins.init

  process_args

  load_file

  if @diff_mode
    run_diff()

    return
  end

  # Skip stack is used to maintain a list of indentation levels that we need to skip
  @skip_stack = []

  @recipe.steps.each_with_index do |line, index|

    # Skip all completed (incl. strikethrough) steps in fast forward mode
    if line.type != :directive
      if @fast_forward && line.completed?
        next
      end
      @fast_forward = false
    end

    if @debug
      puts "#{line.type} : #{line}"
    else
      puts line
    end

    # Check if we need to skip this line due to skip stack
    if !@skip_stack.empty?
      current_skip_indent = @skip_stack.last

      current_line_indent = line.indentation_level
      if current_line_indent == nil || current_line_indent > current_skip_indent
        # Skip this line
        next
      else
        # We are back to a lower indentation, pop the skip stack
        @skip_stack.pop
      end
    end

    case line.type

    when :directive
      case line.directive_type
      when :var

        # STDERR.puts line.inspect
        if line.attributes == nil || line.attributes.strip.start_with?(/default\s*=/)
          puts ""

          initial_value = expand_vars((line.attributes || "").sub(/^\s*default\s*=\s*/, ''))

          raise "Initial value couldn't be determined for variable '#{line.content}'" if initial_value.nil?

          use_tty = true
          if use_tty
            prompt = TTY::Prompt.new

            history = @config[:history, line.content.to_sym] || []

            history.each { |h| prompt.reader.add_to_history(h) }

            line.attributes = prompt.ask(" → Please enter your value for variable '#{line.content}':\n".yellow, value: initial_value)

            history << line.attributes

            @config[:history, line.content.to_sym] = history.uniq
            @config.flush

          else
            puts " → Please enter your value for variable '#{line.content}':".yellow
            line.attributes = STDIN.gets().strip
          end
          line.lines = ["::var[#{line.content}]{#{line.attributes}}\n"]
          @context[line.content] = line.attributes

          puts ""
        else
          @context[line.content] = line.attributes
          puts " → Variable '#{line.content}' set to '#{line.attributes}'.".yellow
          puts ""
        end

        next

      when :cd
        o = OpenStruct.new(@context)
        o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }

        dir = o.instance_eval(line.content)
        puts " → Changing directory to: #{dir}".yellow

        Dir.chdir(dir)

      when :template

        o = OpenStruct.new(@context)
        o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }

        template_name_suggestion = o.instance_eval(line.content) || File.filename(@file_name)

        if @interactive
          puts " ? About to execute template directive. Press y/Y to continue. Any other key to cancel and exit baker.".yellow
          puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow

          exit(1) if prompt_user_choice(line) == :abort
        end

        puts (" → Executing template directive. Creating file: " + template_name_suggestion).yellow

        template_name_suggestion = File.expand_path(template_name_suggestion)
        if File.exist?(template_name_suggestion)
          puts "Error: The file #{template_name_suggestion} already exists.".red

          puts " ? Do you want to overwrite the file? Enter y/Y to overwrite. Any other key to cancel and exit baker.".yellow

          # Make a copy of the file contents before the user decides to overwrite
          @file_contents = File.read(template_name_suggestion)

          exit(1) if STDIN.gets().strip.downcase != 'y'
        end
        line.lines = ["::template_source[#{@file_name}]"]

        puts ""

        @file_name = template_name_suggestion
        save
        next
      when :template_source
        # Do nothing
      else
        raise "Unknown directive type: #{line.directive_type}"
      end
      next
    when :nop
      # Do nothing
      next

    when :ruby
      #eval line.content

      if line.completed?
        puts "  → Already completed".green if @debug
        next
      end
      @context[:rails_gen_base] = Rails::Generators::Base.new
      @context[:myself] = self
      o = Struct.new(*@context.keys).new(*@context.values)

      o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }
      o.define_singleton_method (:inspect) {
          "<#{self.class}: #{self.instance_variables.map{|v| "#{v}=#{instance_variable_get(v).inspect.truncate(100)}"}.join(", ")}>"
      }

      o.extend Forwardable
      o.def_delegators :rails_gen_base, :template, :inject_into_class, :create_file, :copy_file, :inside, :environment, :gem, :generate, :git, :initializer, :lib, :rails_command, :rake, :route
      o.def_delegators :myself, *BakerActions.instance_methods(false)

      command = line.command

      # Apply indentation and any additional formatting
      to_display = unindent_common_whitespace(format_command(command, max_line_length = 160))
      line_will_break = to_display =~ /\n/ || to_display.length > 80
      to_display = "\n#{to_display}\n" if line_will_break && to_display.scan(/\n/).length == 0

      # Replace all trailing whitespace with ·
      to_display = to_display.gsub(/\s(?=\s*$)/, '·')

      to_display = to_display.indent(1).gsub(/^/, '').indent(3) if line_will_break

      if @interactive
        puts " → About to execute ruby code: #{"\n" if line_will_break}#{to_display}".yellow
      end

      asked_before = false
      case Baker.plugins.run(:before_execution, line: line, baker: self, command: command, context: @context)
      when :skip
        next
      when :ask
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
        exit(1) if prompt_user_choice(line) == :abort
        asked_before = true
      end # Fallthrough: :continue

      if @interactive && !asked_before
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow

        exit(1) if prompt_user_choice(line) == :abort
      end

      puts (" → Executing ruby code: #{"\n" if line_will_break}#{to_display}").yellow

      begin
        if @file_name
          result = o.instance_eval(command, @file_name, line.source_code_line_index)
        else
          result = o.instance_eval(command)
        end
      rescue Exception => e
        result = e
      end

      if result == false
        # We assume the ruby command outputted an error message
        puts "  → Please fix the error or mark the todo as done.".red
        puts "      #{@file_name}:#{line.source_code_line_index}".red
        exit 1
      end

      if result.is_a?(Exception)
        puts "  → Failed with error:".red
        puts result
        puts result.backtrace
        puts "  → Please fix the error or mark the todo as done.".red
        puts "      #{@file_name}:#{line.source_code_line_index}".red
        exit 1
      end

      puts "  → Successfully executed".green
      puts
      line.mark_complete

      case Baker.plugins.run(:after_execution, line: line, baker: self, command: command, context: @context)
      when :skip
        next
      when :ask
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
        exit(1) if prompt_user_choice(line) == :abort
        asked_before = true
      end # Fallthrough: :continue

    when :shell
      # system line.content

      if line.completed?
        next
      end
      require 'ostruct'
      o = OpenStruct.new(@context)
      o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }

      command = o.instance_eval("%(" + line.command + ")")

      if @context.has_key?('WRAP_COMMAND') && @context['WRAP_COMMAND'].to_s.strip != ""
        context = @context.dup

        context['COMMAND'] = command

        o = OpenStruct.new(context)
        o.singleton_class.define_singleton_method(:const_missing) { |name| o[name] }
        command = o.instance_eval("%(" + @context['WRAP_COMMAND'] + ")", @file_name, line.source_code_line_index)
      end

      # Apply indentation and any additional formatting
      to_display = format_command(command, max_line_length = 160)
      line_will_break = to_display =~ /\n/ || to_display.length > 80
      to_display = "\n#{to_display}\n" if line_will_break && to_display.scan(/\n/).length == 1

      # Replace all trailing whitespace with ·
      to_display = to_display.gsub(/\s(?=\s*$)/, '·')

      to_display = to_display.indent(1).gsub(/^/, '').indent(3) if line_will_break

      if @interactive
        puts " → About to execute shell code: #{"\n" if line_will_break}#{to_display}".yellow
      end

      asked_before = false
      case Baker.plugins.run(:before_execution, line: line, baker: self, command: command, context: @context)
      when :skip
        next
      when :ask
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
        exit(1) if prompt_user_choice(line) == :abort
        asked_before = true
      end # Fallthrough: :continue

      if @interactive && !asked_before
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow

        exit(1) if prompt_user_choice(line) == :abort
      end

      puts (" → Executing shell code: #{"\n" if line_will_break}#{to_display}").yellow

      mode = :ptyspawn_with_parser

      case mode

      when :system
        puts ">>>>>>".white
        # Bundler.with_clean_env {
        result = system(command, out: STDOUT)
        #}
        puts ">>>>>>".white

      when :open3

        exit_status = nil
        require 'open3'
        Open3.popen2e(command) do |stdin, stdout_and_stderr, wait_thr|
          stdout_and_stderr.each_line do |line|
            # Remove trailing whitespace and apply formatting
            formatted_line = line.rstrip.indent(2).gsub(/^/, '').indent(3)
            puts formatted_line
          end
          exit_status = wait_thr.value
        end
        result = exit_status&.success?

      when :ptyspawn_with_parser

        require 'pty'
        begin

          line_indent = ''
          line_indent_width = line_indent.length
          line_indent = line_indent.yellow
          first_line_indent = true

          parser = VTParser.new do |action|
            print line_indent if first_line_indent
            first_line_indent = false

            to_output = action.to_ansi

            case action.action_type
            when :print, :execute, :put, :osc_put
              if action.ch == "\r"
                print action.ch
                print line_indent
                next
              end
            when :csi_dispatch
              if to_output == "\e[2K"
                print "\e[2K"
                print line_indent
                next
              else
                if action.ch == 'G'
                  # puts "to_output: #{to_output.inspect} action: #{action} ch: #{ch.inspect}"
                  # && parser.params.size == 1
                  print "\e[#{action.params[0] + line_indent_width}G"

                  next
                end
              end
            end

            print to_output
          end

          PTY.spawn(command) do |stdout_and_stderr, stdin, pid|

            # Input Thread
            input_thread = Thread.new do

              STDIN.raw do |io|
                loop do
                  break if pid.nil?
                  begin
                    if io.wait_readable(0.1)
                      data = io.read_nonblock(1024)
                      stdin.write data
                    end
                  rescue IO::WaitReadable
                    # No input available right now
                  rescue EOFError
                    break
                  rescue Errno::EIO
                    break
                  end
                end
              end
            end

            begin
              stdout_and_stderr.winsize = [$stdout.winsize.first, $stderr.winsize.last - line_indent_width]

              stdout_and_stderr.each_char do |char|

                parser.parse(char)

              end
            rescue Errno::EIO
              # End of output
            end
            Process.wait(pid)
            pid = nil # Signal to the input thread to exit
            input_thread.join # Have to wait for the Thread to finish until we can proceed outputting text to the CLI
            exit_status = $?.exitstatus
            result = exit_status == 0

            # Clear the line, reset the cursor to the start of the line
            print "\e[2K\e[1G"

          end
        rescue PTY::ChildExited
          # Child process has exited
          result = true
        end
      end

      puts ""

      if result
        puts "  → Successfully executed".green
        puts
        line.mark_complete
      else
        error = $?.to_s

        if error.length < 80 && !(error =~ /\n/)
          puts "  → Failed with error: #{error}".red
        else
          puts "  → Failed with error:".red
          to_display = format_command(error, max_line_length = 160)
          to_display = "\n#{to_display}\n " if to_display.scan(/\n/).length == 0
          to_display = to_display.indent(1).gsub(/^/, '').indent(3)
          puts to_display.red
        end
        puts "  → Please fix the error or mark the todo as done:".red
        puts "      #{@file_name}:#{line.source_code_line_index}".red
        exit 1
      end

      case Baker.plugins.run(:after_execution, line: line, baker: self, command: command, context: @context)
      when :skip
        next
      when :ask
        puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
        exit(1) if prompt_user_choice(line) == :abort
        asked_before = true
      end # Fallthrough: :continue

    when :manual

      if line.completed?
        next
      end

      puts " → Perform the following steps manually:".yellow
      puts line.description

      allow_skip_permanently = !@no_save
      skip_message = "s to skip temporarily, #{"- or p to skip permanently, " if allow_skip_permanently}"
      puts
      puts " → Please enter: 'y' to confirm the task was completed, #{skip_message}or any other key to abort and exit baker.".yellow

      next if run_plugins(:before_execution, line: line) == :skip

      user_choice = prompt_user_choice(line, allow_skip: true)
      user_choice = 's' if user_choice == :skip_permanently && !allow_skip_permanently

      case user_choice
      
      when :continue
        line.mark_complete

      when :skip_temporarily
        skip_indent_level = line.indentation_level
        @skip_stack.push(skip_indent_level)

      when :skip_permanently
        
        # Mark this and all sub-task as permanently skipped
        line_indent = line.indentation_level
        
        first_line = true
        @recipe.steps[index..].each { |line|

          # Skip empty lines
          next if line.indentation_level == nil

          # Break if we are back to the same or higher indentation level
          break if !first_line && line.indentation_level <= line_indent
          first_line = false
          
          next if line.completed?

          puts " → Skipping permanently: #{line.single_line_for_display}".yellow

          line.mark_strikethrough
        }

      when :abort
        exit(1)
      end

      next if run_plugins(:after_execution, line: line) == :skip

    end

    save
  end

  puts " → All steps completed.".green

end

#run_diffObject

Run diff will unmark all completed steps and perform a diff against the original template source

The original template source is searched using the ::template_source directive.

The diff is shown using git diff.



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

def run_diff

  template_source_file = nil

  @recipe.steps.each { |line|
    case line.type
    when :directive
      case line.directive_type
      when :template_source
        template_source_file = line.content
      end
    when :ruby, :shell, :manual
      line.mark_todo if line.completed?
    end
  }

  if template_source_file.nil?
    puts "Error: No template source file found. Please add a ::template_source{path_to_template} directive to the bake file.".red
    exit(1)
  end

  to_write = @recipe.to_s
  Tempfile.open("#{File.basename(@file_name)}_unbaked") do |tempfile|
    tempfile.write(to_write)
    tempfile.flush
    # If attached to a tty, show color diff/word diff
    puts `git diff #{"--word-diff --color" if $stdout.tty?} #{template_source_file} #{tempfile.path}`
  end
end

#run_plugins(block_type, line: nil) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/baker.rb', line 782

def run_plugins(block_type, line: nil)

  puts "Running plugins for block type: #{block_type}" if @debug

  case Baker.plugins.run(block_type, baker: self, line: line)
  when :skip
    return :skip
  when :ask
    puts " ? Press y/Y to continue. Any other key to cancel and exit baker.".yellow
    exit(1) if prompt_user_choice(line) == :abort
  end
end

#run_safeObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/baker.rb', line 158

def run_safe

  run

rescue SystemExit
  # fall through
rescue Exception => e
  puts
  puts e.full_message(highlight: true, order: :bottom)

  puts
  require 'pathname'
  puts " → Please fix the error and run `baker #{Pathname.new(@file_name).relative_path_from(@original_dir) }` to continue.".yellow
end

#saveObject



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/baker.rb', line 741

def save

  return if @no_save

  to_write = @recipe.to_s

  if File.exist?(@file_name) && @file_contents != nil
    if File.read(@file_name) != @file_contents
      puts " → File has been modified by another program, while baker processed todos.".red

      while true
        puts " ? Do you want to overwrite the file? Enter y/Y to overwrite or d/D to show diff. Any other key to cancel and exit baker.".yellow
        input = STDIN.gets()
        case input.strip.downcase
        when 'y'
          puts " → Exiting without saving. Please manually mark the successfully executed steps as done.".red
          exit 1
        when 'd'
          puts " → Diff:".red
          Tempfile.open('assumed_file_contents') do |tempfile|
            tempfile.write(@file_contents)
            tempfile.flush
            puts `git diff --color #{tempfile.path} #{@file_name}`
          end
          puts
          next # Repeat
        end
        break
      end
    end
  end

  if !File.exist?(@file_name) || File.read(@file_name) != to_write
    File.write(@file_name, to_write)
    @file_contents = to_write
  else
    puts " → No changes to save.".yellow
  end

end

#to_hashObject



44
45
46
# File 'lib/baker.rb', line 44

def to_hash
  (self.instance_variables - [:@file_contents, :@recipe]).inject({}){ |cont, attr| cont[attr] = instance_variable_get(attr); cont }
end