|
| 1 | +require_relative './base_processor' |
1 | 2 | require_relative './helpers' |
2 | 3 | require_relative './header_generator' |
3 | 4 |
|
4 | 5 | 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 == '' |
| 6 | + class AnnotationProcessor < BaseProcessor |
| 7 | + # @return [Boolean] |
| 8 | + def update |
| 9 | + header = HeaderGenerator.generate(options) |
| 10 | + content, header_position = Helpers.strip_annotations(existing_text) |
| 11 | + new_content = annotate_routes(header, content, header_position) |
| 12 | + new_text = new_content.join("\n") |
| 13 | + rewrite_contents(new_text) |
| 14 | + end |
28 | 15 |
|
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 == '' |
| 16 | + private |
32 | 17 |
|
33 | | - new_content = magic_comments_map + content + header |
34 | | - end |
| 18 | + def annotate_routes(header, content, header_position) |
| 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 == '' |
35 | 28 |
|
36 | | - # Make sure we end on a trailing newline. |
37 | | - new_content << '' unless new_content.last == '' |
| 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 == '' |
38 | 32 |
|
39 | | - new_content |
| 33 | + new_content = magic_comments_map + content + header |
40 | 34 | end |
41 | 35 |
|
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 |
| 36 | + # Make sure we end on a trailing newline. |
| 37 | + new_content << '' unless new_content.last == '' |
54 | 38 |
|
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 |
| 39 | + new_content |
62 | 40 | end |
63 | 41 | end |
64 | 42 | end |
0 commit comments