Skip to content

Commit 78d90f2

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

File tree

2 files changed

+65
-29
lines changed

2 files changed

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

0 commit comments

Comments
 (0)