Skip to content

Commit 313df0f

Browse files
luke-hillbarancev
authored andcommitted
Bugfix and Refactor
Add &. safe operator for #zero? call, as sometimes the index method returns nil Begin to refactor out some of the complex iterator check logic into handily named booleans Signed-off-by: Alexei Barantsev <[email protected]>
1 parent cb1001d commit 313df0f

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

rake_tasks/copyright.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,61 @@
33
module Copyright
44
module_function
55

6-
def update(files, options = {})
7-
style = options[:style] || '//'
8-
prefix = options[:prefix] || nil
9-
10-
notice_lines = notice.split(/\n/).map do |line|
11-
"#{style} #{line}".rstrip + "\n"
12-
end
13-
notice_lines = Array(prefix) + notice_lines
14-
notice = notice_lines.join('')
6+
def update(files, style: '//', prefix: nil)
7+
@comment_characters = style
8+
@prefix = prefix
159

1610
files.each do |file|
1711
lines = IO.readlines(file)
1812

1913
index = -1
2014
lines.any? do |line|
2115
done = true
22-
if (line.index(style).zero?) ||
23-
(notice_lines[index + 1] && (line.index(notice_lines[index + 1]).zero?))
16+
if starts_with_comment_character?(line) || valid_copyright_notice_line?(line, index)
2417
index += 1
2518
done = false
2619
end
2720
done
2821
end
2922

3023
if index == -1
31-
write_update_notice(file, lines, notice)
24+
write_update_notice(file, lines, copyright_notice)
3225
else
3326
current = lines.shift(index + 1).join('')
34-
if current != notice
35-
write_update_notice(file, lines, notice)
27+
if current != copyright_notice
28+
write_update_notice(file, lines, copyright_notice)
3629
end
3730
end
3831
end
3932
end
4033

34+
def starts_with_comment_character?(line)
35+
line.index(@comment_characters)&.zero?
36+
end
37+
38+
def valid_copyright_notice_line?(line, index)
39+
copyright_notice_lines[index + 1] &&
40+
line.index(copyright_notice_lines[index + 1])&.zero?
41+
end
42+
43+
def copyright_notice
44+
copyright_notice_lines.join('')
45+
end
46+
47+
def copyright_notice_lines
48+
@copyright_notice_lines ||= Array(@prefix) + commented_notice_lines
49+
end
50+
51+
def commented_notice_lines
52+
notice_lines.map do |line|
53+
"#{@comment_characters} #{line}".rstrip + "\n"
54+
end
55+
end
56+
57+
def notice_lines
58+
notice.split(/\n/)
59+
end
60+
4161
def write_update_notice(file, lines, notice)
4262
puts "Adding notice to #{file}"
4363
File.open(file, 'w') do |f|

0 commit comments

Comments
 (0)