Skip to content

Commit

Permalink
Bugfix and Refactor
Browse files Browse the repository at this point in the history
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 <barancev@gmail.com>
  • Loading branch information
luke-hill authored and barancev committed Nov 1, 2019
1 parent cb1001d commit 313df0f
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions rake_tasks/copyright.rb
Expand Up @@ -3,41 +3,61 @@
module Copyright
module_function

def update(files, options = {})
style = options[:style] || '//'
prefix = options[:prefix] || nil

notice_lines = notice.split(/\n/).map do |line|
"#{style} #{line}".rstrip + "\n"
end
notice_lines = Array(prefix) + notice_lines
notice = notice_lines.join('')
def update(files, style: '//', prefix: nil)
@comment_characters = style
@prefix = prefix

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

index = -1
lines.any? do |line|
done = true
if (line.index(style).zero?) ||
(notice_lines[index + 1] && (line.index(notice_lines[index + 1]).zero?))
if starts_with_comment_character?(line) || valid_copyright_notice_line?(line, index)
index += 1
done = false
end
done
end

if index == -1
write_update_notice(file, lines, notice)
write_update_notice(file, lines, copyright_notice)
else
current = lines.shift(index + 1).join('')
if current != notice
write_update_notice(file, lines, notice)
if current != copyright_notice
write_update_notice(file, lines, copyright_notice)
end
end
end
end

def starts_with_comment_character?(line)
line.index(@comment_characters)&.zero?
end

def valid_copyright_notice_line?(line, index)
copyright_notice_lines[index + 1] &&
line.index(copyright_notice_lines[index + 1])&.zero?
end

def copyright_notice
copyright_notice_lines.join('')
end

def copyright_notice_lines
@copyright_notice_lines ||= Array(@prefix) + commented_notice_lines
end

def commented_notice_lines
notice_lines.map do |line|
"#{@comment_characters} #{line}".rstrip + "\n"
end
end

def notice_lines
notice.split(/\n/)
end

def write_update_notice(file, lines, notice)
puts "Adding notice to #{file}"
File.open(file, 'w') do |f|
Expand Down

0 comments on commit 313df0f

Please sign in to comment.