Class: XDry::Patcher

Inherits:
Object
  • Object
show all
Defined in:
lib/xdry/patching/patcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePatcher

Returns a new instance of Patcher.



8
9
10
11
# File 'lib/xdry/patching/patcher.rb', line 8

def initialize
  @patched = {}
  @dry_run = true
end

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



6
7
8
# File 'lib/xdry/patching/patcher.rb', line 6

def dry_run
  @dry_run
end

#verboseObject

Returns the value of attribute verbose.



6
7
8
# File 'lib/xdry/patching/patcher.rb', line 6

def verbose
  @verbose
end

Instance Method Details

#delete_line(pos) ⇒ Object



21
22
23
# File 'lib/xdry/patching/patcher.rb', line 21

def delete_line pos
  do_delete_lines pos.file_ref, pos.line_no - 1, 1
end

#do_delete_lines(file_ref, line_index, line_count) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xdry/patching/patcher.rb', line 32

def do_delete_lines file_ref, line_index, line_count
  lines = patched_lines_of(file_ref)

  if @verbose
    puts "DELETING #{line_count} LINE(S) FROM LINE NO.#{line_index+1}:"
    lines[line_index .. line_index+line_count-1].each { |line| puts "    #{line}" }
  end

  file_ref.fixup_positions! line_index+line_count, -line_count
  lines[line_index .. line_index+line_count-1] = []
end

#do_insert_after(file_ref, start_scope, line_index, new_lines, indent, parse) ⇒ Object



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
# File 'lib/xdry/patching/patcher.rb', line 44

def do_insert_after file_ref, start_scope, line_index, new_lines, indent, parse
  new_lines = new_lines.collect { |line| line.blank? ? line : indent + line }
  new_lines = new_lines.collect { |line| line.gsub("\t", INDENT_STEP) }
  lines = patched_lines_of(file_ref)

  if @verbose
    puts "INSERTING LINES AFTER LINE NO.#{line_index+1}:"
    new_lines.each { |line| puts "    #{line}" }
    puts "  AFTER LINE:"
    puts "    #{lines[line_index]}"
  end

  # collapse leading/trailing empty lines with the empty lines that already exist
  # in the source code

  # when line_index == -1 (insert at the beginning of the file), there are no leading lines
  if line_index >= 0
    desired_leading_empty_lines = new_lines.prefix_while(&:blank?).length
    actual_leading_empty_lines  = lines[0..line_index].suffix_while(&:blank?).length
    leading_lines_to_remove     = [actual_leading_empty_lines, desired_leading_empty_lines].min
    new_lines = new_lines[leading_lines_to_remove .. -1]
  end

  # if all lines were empty, the number of trailing empty lines might have changed
  # after removal of some leading lines, so we compute this after the removal
  desired_trailing_empty_lines = new_lines.suffix_while(&:blank?).length
  actual_trailing_empty_lines  = lines[line_index+1..-1].prefix_while(&:blank?).length
  trailing_lines_to_remove     = [actual_trailing_empty_lines, desired_trailing_empty_lines].min
  new_lines = new_lines[0 .. -(trailing_lines_to_remove+1)]

  file_ref.fixup_positions! line_index+1, new_lines.size

  lines[line_index+1 .. line_index+1] = new_lines.collect { |line| "#{line}\n" } + [lines[line_index+1]]

  if parse
    driver = ParsingDriver.new(nil)
    driver.verbose = @verbose
    driver.parse_fragment file_ref, new_lines, line_index+1+1, start_scope
  end
end

#insert_after(pos, new_lines, indent = '', parse = true) ⇒ Object



13
14
15
# File 'lib/xdry/patching/patcher.rb', line 13

def insert_after pos, new_lines, indent = '', parse = true
  do_insert_after pos.file_ref, pos.scope_after, pos.line_no - 1, new_lines, indent || '', parse
end

#insert_before(pos, new_lines, indent = '', parse = true) ⇒ Object



17
18
19
# File 'lib/xdry/patching/patcher.rb', line 17

def insert_before pos, new_lines, indent = '', parse = true
  do_insert_after pos.file_ref, pos.scope_before, pos.line_no - 2, new_lines, indent || '', parse
end

#remove_marker!(marker) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/xdry/patching/patcher.rb', line 117

def remove_marker! marker
  if marker.is_a? NFullLineMarker
    delete_line marker.pos
  else
    replace_line marker.pos do |old_line|
      old_line.gsub(marker.text, '').gsub(/ +$/, '')
    end
  end
end

#replace_line(pos) ⇒ Object



25
26
27
28
29
30
# File 'lib/xdry/patching/patcher.rb', line 25

def replace_line pos
  old_line = patched_lines_of(pos.file_ref)[pos.line_no-1].rstrip
  new_line = yield(old_line)
  delete_line pos
  insert_before pos, [new_line], '', false
end

#retrieve!Object



108
109
110
111
112
113
114
115
# File 'lib/xdry/patching/patcher.rb', line 108

def retrieve!
  result = {}
  for file_ref, lines in @patched
    result[file_ref.path] = lines.join("")
  end
  @patched = {}
  return result
end

#save!Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/xdry/patching/patcher.rb', line 85

def save!
  changed_file_refs = []
  for file_ref, lines in @patched
    original_path = file_ref.path

    text = lines.join("")
    next if text == file_ref.read

    changed_file_refs << file_ref

    new_path = if @dry_run
      ext = File.extname(original_path)
      File.join(File.dirname(original_path), File.basename(original_path, ext) + '.xdry' + ext)
    else
      original_path
    end

    open(new_path, 'w') { |f| f.write text }
  end
  @patched = {}
  return changed_file_refs
end