Class: Snaptoken::Commands::Undiff

Inherits:
BaseCommand show all
Defined in:
lib/snaptoken/commands/undiff.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::ERROR_MSG

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

inherited, #initialize, #latest_step, #needs!, #parseopts!, #step_path, #steps

Constructor Details

This class inherits a constructor from Snaptoken::Commands::BaseCommand

Class Method Details

.nameObject



2
3
4
# File 'lib/snaptoken/commands/undiff.rb', line 2

def self.name
  "undiff"
end

.summaryObject



6
7
8
9
# File 'lib/snaptoken/commands/undiff.rb', line 6

def self.summary
  "Convert steps.diff to steps/. Doesn't\n" +
  "overwrite steps/ unless forced."
end

.usageObject



11
12
13
# File 'lib/snaptoken/commands/undiff.rb', line 11

def self.usage
  "[-f] [-q]"
end

Instance Method Details

#runObject



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
# File 'lib/snaptoken/commands/undiff.rb', line 25

def run
  needs! :config, :diff

  FileUtils.cd(@config[:path]) do
    if @opts[:force]
      FileUtils.rm_rf("steps")
    else
      needs! not: :steps_folder
    end

    FileUtils.mkdir("steps")
    FileUtils.cd("steps") do
      File.open("../steps.diff", "r") do |f|
        step_num = 0
        step = Snaptoken::Step.new(0, nil, [])
        prev_step = nil
        cur_diff = nil
        while line = f.gets
          if line =~ /^~~~ step: ([\s\w-]+)$/
            if cur_diff
              apply_diff(step, cur_diff)
              cur_diff = nil
            end

            prev_step = step
            step = Snaptoken::Step.from_commit_msg(prev_step.number + 1, $1)

            print "\r\e[K[steps.diff -> steps/] #{step.folder_name}" unless @opts[:quiet]

            if step.number == 1
              FileUtils.mkdir(step.folder_name)
            else
              FileUtils.cp_r(prev_step.folder_name, step.folder_name)
            end
          elsif line =~ /^diff --git/
            apply_diff(step, cur_diff) if cur_diff
            cur_diff = line
          elsif cur_diff
            cur_diff << line
          end
        end
        apply_diff(step, cur_diff) if cur_diff
        print "\n" unless @opts[:quiet]
      end
    end
  end
end

#setopts!(o) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/snaptoken/commands/undiff.rb', line 15

def setopts!(o)
  o.on("-f", "--force", "Overwrite steps/ folder") do |f|
    @opts[:force] = f
  end

  o.on("-q", "--quiet", "Don't output progress") do |q|
    @opts[:quiet] = q
  end
end