Skip to content

Commit a0a8cc4

Browse files
committed
Add AnnotateRoutes::AnnotationProcessor
1 parent 5d01c41 commit a0a8cc4

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@
1919
#
2020

2121
require_relative './annotate_routes/helpers'
22-
require_relative './annotate_routes/header_generator'
22+
require_relative './annotate_routes/annotation_processor'
2323

2424
module AnnotateRoutes
2525
class << self
2626
def do_annotations(options = {})
2727
if routes_file_exist?
2828
existing_text = File.read(routes_file)
29-
content, header_position = Helpers.strip_annotations(existing_text)
30-
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
31-
new_text = new_content.join("\n")
32-
if rewrite_contents(existing_text, new_text, options[:frozen])
29+
if AnnotationProcessor.update(routes_file, existing_text, options)
3330
puts "#{routes_file} was annotated."
3431
else
3532
puts "#{routes_file} was not changed."
@@ -91,29 +88,5 @@ def rewrite_contents(existing_text, new_text, frozen)
9188

9289
content_changed
9390
end
94-
95-
def annotate_routes(header, content, header_position, options = {})
96-
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
97-
if %w(before top).include?(options[:position_in_routes])
98-
header = header << '' if content.first != ''
99-
magic_comments_map << '' if magic_comments_map.any?
100-
new_content = magic_comments_map + header + content
101-
else
102-
# Ensure we have adequate trailing newlines at the end of the file to
103-
# ensure a blank line separating the content from the annotation.
104-
content << '' unless content.last == ''
105-
106-
# We're moving something from the top of the file to the bottom, so ditch
107-
# the spacer we put in the first time around.
108-
content.shift if header_position == :before && content.first == ''
109-
110-
new_content = magic_comments_map + content + header
111-
end
112-
113-
# Make sure we end on a trailing newline.
114-
new_content << '' unless new_content.last == ''
115-
116-
new_content
117-
end
11891
end
11992
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require_relative './helpers'
2+
require_relative './header_generator'
3+
4+
module AnnotateRoutes
5+
module AnnotationProcessor
6+
class << self
7+
# @param [Boolean]
8+
def update(routes_file, existing_text, options = {})
9+
header = HeaderGenerator.generate(options)
10+
content, header_position = Helpers.strip_annotations(existing_text)
11+
new_content = annotate_routes(header, content, header_position, options)
12+
new_text = new_content.join("\n")
13+
rewrite_contents(routes_file, existing_text, new_text, options)
14+
end
15+
16+
private
17+
18+
def annotate_routes(header, content, header_position, options = {})
19+
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
20+
if %w(before top).include?(options[:position_in_routes])
21+
header = header << '' if content.first != ''
22+
magic_comments_map << '' if magic_comments_map.any?
23+
new_content = magic_comments_map + header + content
24+
else
25+
# Ensure we have adequate trailing newlines at the end of the file to
26+
# ensure a blank line separating the content from the annotation.
27+
content << '' unless content.last == ''
28+
29+
# We're moving something from the top of the file to the bottom, so ditch
30+
# the spacer we put in the first time around.
31+
content.shift if header_position == :before && content.first == ''
32+
33+
new_content = magic_comments_map + content + header
34+
end
35+
36+
# Make sure we end on a trailing newline.
37+
new_content << '' unless new_content.last == ''
38+
39+
new_content
40+
end
41+
42+
# @param routes_file [String]
43+
# @param existing_text [String]
44+
# @param new_text [String]
45+
# @param options [Hash]
46+
# @return [Boolean]
47+
def rewrite_contents(routes_file, existing_text, new_text, options)
48+
content_changed = existing_text != new_text
49+
frozen = options[:frozen]
50+
51+
if content_changed && frozen
52+
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`."
53+
end
54+
55+
if content_changed
56+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
57+
true
58+
else
59+
false
60+
end
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)