-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
replace_lines.rb
55 lines (48 loc) · 1.36 KB
/
replace_lines.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# frozen_string_literal: true
require 'json'
module BuildTools
class << self
# @option [required, String] :filename
# @option [required, String] :start
# @option [required, String] :stop
# @option [required, String] :new_lines
# @option [required, Boolean] :padding (false)
# @return [Boolean] Returns `true` if the target file was changed.
# Returns `false` otherwise.
def replace_lines(options = {})
filename = options.fetch(:filename)
start = options.fetch(:start)
stop = options.fetch(:stop)
new_lines = options.fetch(:new_lines)
padding = options.fetch(:padding, false)
orig_file = File.open(filename, 'rb') { |file| file.read }
skip = false
lines = []
orig_file.lines.each do |line|
if line.match(start)
lines << line
lines << "\n" if padding
if Array === new_lines
lines += new_lines
else
lines << new_lines
end
skip = true
elsif line.match(stop)
lines << "\n" if padding
lines << line
skip = false
elsif !skip
lines << line
end
end
new_file = lines.join
if new_file != orig_file
File.open(filename, 'wb') { |file| file.write(new_file) }
true
else
false
end
end
end
end