-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AnnotateRoutes::RemovalProcessor
- Loading branch information
Showing
4 changed files
with
119 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,42 @@ | ||
require_relative './base_processor' | ||
require_relative './helpers' | ||
require_relative './header_generator' | ||
|
||
module AnnotateRoutes | ||
module AnnotationProcessor | ||
class << self | ||
# @param [Boolean] | ||
def update(routes_file, existing_text, options = {}) | ||
header = HeaderGenerator.generate(options) | ||
content, header_position = Helpers.strip_annotations(existing_text) | ||
new_content = annotate_routes(header, content, header_position, options) | ||
new_text = new_content.join("\n") | ||
rewrite_contents(routes_file, existing_text, new_text, options) | ||
end | ||
|
||
private | ||
|
||
def annotate_routes(header, content, header_position, options = {}) | ||
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content) | ||
if %w(before top).include?(options[:position_in_routes]) | ||
header = header << '' if content.first != '' | ||
magic_comments_map << '' if magic_comments_map.any? | ||
new_content = magic_comments_map + header + content | ||
else | ||
# Ensure we have adequate trailing newlines at the end of the file to | ||
# ensure a blank line separating the content from the annotation. | ||
content << '' unless content.last == '' | ||
class AnnotationProcessor < BaseProcessor | ||
# @return [Boolean] | ||
def update | ||
header = HeaderGenerator.generate(options) | ||
content, header_position = Helpers.strip_annotations(existing_text) | ||
new_content = annotate_routes(header, content, header_position) | ||
new_text = new_content.join("\n") | ||
rewrite_contents(new_text) | ||
end | ||
|
||
# We're moving something from the top of the file to the bottom, so ditch | ||
# the spacer we put in the first time around. | ||
content.shift if header_position == :before && content.first == '' | ||
private | ||
|
||
new_content = magic_comments_map + content + header | ||
end | ||
def annotate_routes(header, content, header_position) | ||
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content) | ||
if %w(before top).include?(options[:position_in_routes]) | ||
header = header << '' if content.first != '' | ||
magic_comments_map << '' if magic_comments_map.any? | ||
new_content = magic_comments_map + header + content | ||
else | ||
# Ensure we have adequate trailing newlines at the end of the file to | ||
# ensure a blank line separating the content from the annotation. | ||
content << '' unless content.last == '' | ||
|
||
# Make sure we end on a trailing newline. | ||
new_content << '' unless new_content.last == '' | ||
# We're moving something from the top of the file to the bottom, so ditch | ||
# the spacer we put in the first time around. | ||
content.shift if header_position == :before && content.first == '' | ||
|
||
new_content | ||
new_content = magic_comments_map + content + header | ||
end | ||
|
||
# @param routes_file [String] | ||
# @param existing_text [String] | ||
# @param new_text [String] | ||
# @param options [Hash] | ||
# @return [Boolean] | ||
def rewrite_contents(routes_file, existing_text, new_text, options) | ||
content_changed = existing_text != new_text | ||
frozen = options[:frozen] | ||
|
||
if content_changed && frozen | ||
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." | ||
end | ||
# Make sure we end on a trailing newline. | ||
new_content << '' unless new_content.last == '' | ||
|
||
if content_changed | ||
File.open(routes_file, 'wb') { |f| f.puts(new_text) } | ||
true | ||
else | ||
false | ||
end | ||
end | ||
new_content | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module AnnotateRoutes | ||
class BaseProcessor | ||
def initialize(options, routes_file) | ||
@options = options | ||
@routes_file = routes_file | ||
end | ||
|
||
def routes_file_exist? | ||
File.exist?(routes_file) | ||
end | ||
|
||
private | ||
|
||
attr_reader :options, :routes_file | ||
|
||
def existing_text | ||
@existing_text ||= File.read(routes_file) | ||
end | ||
|
||
# @param new_text [String] | ||
# @return [Boolean] | ||
def rewrite_contents(new_text) | ||
content_changed = content_changed?(new_text) | ||
|
||
if content_changed && frozen? | ||
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." | ||
end | ||
|
||
if content_changed | ||
write(new_text) | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
def write(text) | ||
File.open(routes_file, 'wb') { |f| f.puts(text) } | ||
end | ||
|
||
def content_changed?(new_text) | ||
!(existing_text == new_text) | ||
end | ||
|
||
def frozen? | ||
options[:frozen] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require_relative './base_processor' | ||
require_relative './helpers' | ||
|
||
module AnnotateRoutes | ||
class RemovalProcessor < BaseProcessor | ||
# @return [Boolean] | ||
def update | ||
content, header_position = Helpers.strip_annotations(existing_text) | ||
new_content = strip_on_removal(content, header_position) | ||
new_text = new_content.join("\n") | ||
rewrite_contents(new_text) | ||
end | ||
|
||
private | ||
|
||
def strip_on_removal(content, header_position) | ||
if header_position == :before | ||
content.shift while content.first == '' | ||
elsif header_position == :after | ||
content.pop while content.last == '' | ||
end | ||
|
||
# Make sure we end on a trailing newline. | ||
content << '' unless content.last == '' | ||
|
||
# TODO: If the user buried it in the middle, we should probably see about | ||
# TODO: preserving a single line of space between the content above and | ||
# TODO: below... | ||
content | ||
end | ||
end | ||
end |