Class: StructureConflictResolver::Resolver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Resolver

Returns a new instance of Resolver.



21
22
23
24
25
# File 'lib/structure_conflict_resolver.rb', line 21

def initialize(filename)
  @filename           = filename
  @conflicts          = []
  @potential_conflict = MergeConflict.new
end

Instance Attribute Details

#conflictsObject

Returns the value of attribute conflicts.



19
20
21
# File 'lib/structure_conflict_resolver.rb', line 19

def conflicts
  @conflicts
end

#filenameObject

Returns the value of attribute filename.



19
20
21
# File 'lib/structure_conflict_resolver.rb', line 19

def filename
  @filename
end

#potential_conflictObject

Returns the value of attribute potential_conflict.



19
20
21
# File 'lib/structure_conflict_resolver.rb', line 19

def potential_conflict
  @potential_conflict
end

Instance Method Details

#check_contentObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/structure_conflict_resolver.rb', line 57

def check_content
  end_with :error, "Unparsable conflicts!"    if conflicts.any?(&:parse_error?)
  end_with :error, "No resolvable conflicts!" if conflicts.all? { |c| !c.resolvable? }
  end_with :error, "Nothing changed!"         if content == new_content

  if new_content =~ /\<{7} |\={7}|\<{7}/ || conflicts.any? { |c| !c.resolvable? }
    puts Rainbow("Warning!").yellow
    puts ""
    puts "There are conflicts remaining.  You'll have to fix those manually"
    puts ""
  end
end

#resolve!Object



27
28
29
30
31
32
33
# File 'lib/structure_conflict_resolver.rb', line 27

def resolve!
  validate_file
  scan_for_conflicts
  substitute_content
  check_content
  write_content
end

#scan_for_conflictsObject



41
42
43
44
45
46
47
48
49
# File 'lib/structure_conflict_resolver.rb', line 41

def scan_for_conflicts
  content.each_line do |line|
    potential_conflict.parse! line
    if potential_conflict.scanning_completed?
      conflicts << potential_conflict
      potential_conflict = MergeConflict.new
    end
  end
end

#substitute_contentObject



51
52
53
54
55
# File 'lib/structure_conflict_resolver.rb', line 51

def substitute_content
  conflicts
    .select(&:scanning_completed?)
    .each { |c| new_content.gsub! c.original_blob, c.resolved_text }
end

#validate_fileObject



35
36
37
38
39
# File 'lib/structure_conflict_resolver.rb', line 35

def validate_file
  end_with :error, "No filename provided!" if filename.nil?
  end_with :error, "db/schema.rb is not currently supported" if filename =~ /schema\.rb/
  end_with :error, "#{filename} not found" unless File.exist?(filename)
end

#write_contentObject



70
71
72
73
# File 'lib/structure_conflict_resolver.rb', line 70

def write_content
  File.open(filename, "w") { |file| file.write new_content }
  end_with :success, "✅  Version conflicts resolved!\n\n You'll probably want to \`git add #{filename}\`,\n and continue your rebase/merge.\n\n"
end