@@ -503,8 +503,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
503
503
old_columns = old_header && old_header . scan ( column_pattern ) . sort
504
504
new_columns = new_header && new_header . scan ( column_pattern ) . sort
505
505
506
- magic_comment_matcher = Regexp . new ( /(^#\s *encoding:.*\n )|(^# coding:.*\n )|(^# -\* - coding:.*\n )|(^# -\* - encoding\s ?:.*\n )|(^#\s *frozen_string_literal:.+\n )|(^# -\* - frozen_string_literal\s *:.+-\* -\n )/ )
507
- magic_comments = old_content . scan ( magic_comment_matcher ) . flatten . compact
506
+ magic_comments_block = magic_comments_as_string ( old_content )
508
507
509
508
if old_columns == new_columns && !options [ :force ]
510
509
return false
@@ -522,13 +521,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
522
521
# if there *was* no old schema info (no substitution happened) or :force was passed,
523
522
# we simply need to insert it in correct position
524
523
if new_content == old_content || options [ :force ]
525
- old_content . sub !( magic_comment_matcher , '' )
524
+ old_content . gsub !( magic_comment_matcher , '' )
526
525
old_content . sub! ( annotate_pattern ( options ) , '' )
527
526
528
527
new_content = if %w( after bottom ) . include? ( options [ position ] . to_s )
529
- magic_comments . join + ( old_content . rstrip + "\n \n " + wrapped_info_block )
528
+ magic_comments_block + ( old_content . rstrip + "\n \n " + wrapped_info_block )
530
529
else
531
- magic_comments . join + wrapped_info_block + "\n " + old_content
530
+ magic_comments_block + wrapped_info_block + "\n " + old_content
532
531
end
533
532
end
534
533
@@ -540,6 +539,20 @@ def annotate_one_file(file_name, info_block, position, options = {})
540
539
end
541
540
end
542
541
542
+ def magic_comment_matcher
543
+ Regexp . new ( /(^#\s *encoding:.*(?:\n |r\n ))|(^# coding:.*(?:\n |\r \n ))|(^# -\* - coding:.*(?:\n |\r \n ))|(^# -\* - encoding\s ?:.*(?:\n |\r \n ))|(^#\s *frozen_string_literal:.+(?:\n |\r \n ))|(^# -\* - frozen_string_literal\s *:.+-\* -(?:\n |\r \n ))/ )
544
+ end
545
+
546
+ def magic_comments_as_string ( content )
547
+ magic_comments = content . scan ( magic_comment_matcher ) . flatten . compact
548
+
549
+ if magic_comments . any?
550
+ magic_comments . join + "\n "
551
+ else
552
+ ''
553
+ end
554
+ end
555
+
543
556
def remove_annotation_of_file ( file_name , options = { } )
544
557
if File . exist? ( file_name )
545
558
content = File . read ( file_name )
0 commit comments