From 78d0ddc3457dd7603c2192e7331f10e5f537a8cb Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 3 Feb 2019 18:24:09 +0900 Subject: [PATCH 01/15] Add AnnotateRoutes::AnnotationProcessor --- lib/annotate/annotate_routes.rb | 31 +-------- .../annotate_routes/annotation_processor.rb | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 lib/annotate/annotate_routes/annotation_processor.rb diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index c9a2218ac..13151dba8 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -19,17 +19,14 @@ # require_relative './annotate_routes/helpers' -require_relative './annotate_routes/header_generator' +require_relative './annotate_routes/annotation_processor' module AnnotateRoutes class << self def do_annotations(options = {}) if routes_file_exist? existing_text = File.read(routes_file) - content, header_position = Helpers.strip_annotations(existing_text) - new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options) - new_text = new_content.join("\n") - if rewrite_contents(existing_text, new_text, options[:frozen]) + if AnnotationProcessor.update(routes_file, existing_text, options) puts "#{routes_file} was annotated." else puts "#{routes_file} was not changed." @@ -91,29 +88,5 @@ def rewrite_contents(existing_text, new_text, frozen) content_changed end - - 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 == '' - - # 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 = magic_comments_map + content + header - end - - # Make sure we end on a trailing newline. - new_content << '' unless new_content.last == '' - - new_content - end end end diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb new file mode 100644 index 000000000..ca325bc29 --- /dev/null +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -0,0 +1,64 @@ +require_relative './helpers' +require_relative './header_generator' + +# This module provides methods for annotating config/routes.rb. +module AnnotateRoutes + # This module provides methods for adding annotation to config/routes.rb. + 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 == '' + + # 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 = magic_comments_map + content + header + end + + # Make sure we end on a trailing newline. + new_content << '' unless new_content.last == '' + + new_content + 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] + + abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen + + if content_changed + File.open(routes_file, 'wb') { |f| f.puts(new_text) } + true + else + false + end + end + end + end +end From 6c2b5f6afb9dca3c1cad93386b3551ac392a5d0a Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 3 Feb 2019 18:31:16 +0900 Subject: [PATCH 02/15] Add AnnotateRoutes::RemovalProcessor --- lib/annotate/annotate_routes.rb | 36 +----------- .../annotate_routes/removal_processor.rb | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 lib/annotate/annotate_routes/removal_processor.rb diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 13151dba8..146ddf394 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -18,8 +18,8 @@ # Released under the same license as Ruby. No Support. No Warranty. # -require_relative './annotate_routes/helpers' require_relative './annotate_routes/annotation_processor' +require_relative './annotate_routes/removal_processor' module AnnotateRoutes class << self @@ -36,13 +36,10 @@ def do_annotations(options = {}) end end - def remove_annotations(options={}) + def remove_annotations(options = {}) if routes_file_exist? existing_text = File.read(routes_file) - content, header_position = Helpers.strip_annotations(existing_text) - new_content = strip_on_removal(content, header_position) - new_text = new_content.join("\n") - if rewrite_contents(existing_text, new_text, options[:frozen]) + if RemovalProcessor.update(routes_file, existing_text, options) puts "Annotations were removed from #{routes_file}." else puts "#{routes_file} was not changed (Annotation did not exist)." @@ -61,32 +58,5 @@ def routes_file_exist? def routes_file @routes_rb ||= File.join('config', 'routes.rb') end - - 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 - - def rewrite_contents(existing_text, new_text, frozen) - content_changed = (existing_text != new_text) - - if content_changed - abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if frozen - File.open(routes_file, 'wb') { |f| f.puts(new_text) } - end - - content_changed - end end end diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb new file mode 100644 index 000000000..dc07634eb --- /dev/null +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -0,0 +1,57 @@ +require_relative './helpers' + +# This module provides methods for annotating config/routes.rb. +module AnnotateRoutes + # This module provides methods for removing annotation from config/routes.rb. + module RemovalProcessor + class << self + # @param routes_file [String] + # @param existing_text [String] + # @param options [Hash] + def update(routes_file, existing_text, options) + content, header_position = Helpers.strip_annotations(existing_text) + new_content = strip_on_removal(content, header_position) + new_text = new_content.join("\n") + rewrite_contents(routes_file, existing_text, new_text, options) + end + + private + + def strip_on_removal(content, header_position) + case header_position + when :before + content.shift while content.first == '' + when :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 + + # @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] + + abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen + + if content_changed + File.open(routes_file, 'wb') { |f| f.puts(new_text) } + true + else + false + end + end + end + end +end From e15df344b53206ffd42674bf5731b86d145150c8 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 3 Feb 2019 18:31:16 +0900 Subject: [PATCH 03/15] Add AnnotateRoutes::BaseProcessor --- lib/annotate/annotate_routes.rb | 24 ++---- .../annotate_routes/annotation_processor.rb | 78 +++++++------------ .../annotate_routes/base_processor.rb | 49 ++++++++++++ .../annotate_routes/removal_processor.rb | 68 ++++++---------- 4 files changed, 109 insertions(+), 110 deletions(-) create mode 100644 lib/annotate/annotate_routes/base_processor.rb diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 146ddf394..7d3f6d3c6 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -24,9 +24,10 @@ module AnnotateRoutes class << self def do_annotations(options = {}) - if routes_file_exist? - existing_text = File.read(routes_file) - if AnnotationProcessor.update(routes_file, existing_text, options) + routes_file = File.join('config', 'routes.rb') + processor = AnnotationProcessor.new(options, routes_file) + if processor.routes_file_exist? + if processor.update puts "#{routes_file} was annotated." else puts "#{routes_file} was not changed." @@ -37,9 +38,10 @@ def do_annotations(options = {}) end def remove_annotations(options = {}) - if routes_file_exist? - existing_text = File.read(routes_file) - if RemovalProcessor.update(routes_file, existing_text, options) + routes_file = File.join('config', 'routes.rb') + processor = RemovalProcessor.new(options, routes_file) + if processor.routes_file_exist? + if processor.update puts "Annotations were removed from #{routes_file}." else puts "#{routes_file} was not changed (Annotation did not exist)." @@ -48,15 +50,5 @@ def remove_annotations(options = {}) puts "#{routes_file} could not be found." end end - - private - - def routes_file_exist? - File.exist?(routes_file) - end - - def routes_file - @routes_rb ||= File.join('config', 'routes.rb') - end end end diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index ca325bc29..98546d1f0 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -1,64 +1,44 @@ +require_relative './base_processor' require_relative './helpers' require_relative './header_generator' # This module provides methods for annotating config/routes.rb. module AnnotateRoutes - # This module provides methods for adding annotation to config/routes.rb. - 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 == '' + # This class provides methods for adding annotation to config/routes.rb. + 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] - - abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen + # 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 diff --git a/lib/annotate/annotate_routes/base_processor.rb b/lib/annotate/annotate_routes/base_processor.rb new file mode 100644 index 000000000..619c4e7bf --- /dev/null +++ b/lib/annotate/annotate_routes/base_processor.rb @@ -0,0 +1,49 @@ +# This module provides methods for annotating config/routes.rb. +module AnnotateRoutes + # This class is abstract class of classes adding and removing annotation to config/routes.rb. + 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) + + abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen? + + 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 diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb index dc07634eb..f6586f548 100644 --- a/lib/annotate/annotate_routes/removal_processor.rb +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -1,57 +1,35 @@ +require_relative './base_processor' require_relative './helpers' # This module provides methods for annotating config/routes.rb. module AnnotateRoutes - # This module provides methods for removing annotation from config/routes.rb. - module RemovalProcessor - class << self - # @param routes_file [String] - # @param existing_text [String] - # @param options [Hash] - def update(routes_file, existing_text, options) - content, header_position = Helpers.strip_annotations(existing_text) - new_content = strip_on_removal(content, header_position) - new_text = new_content.join("\n") - rewrite_contents(routes_file, existing_text, new_text, options) - end - - private - - def strip_on_removal(content, header_position) - case header_position - when :before - content.shift while content.first == '' - when :after - content.pop while content.last == '' - end + # This class provides methods for removing annotation from config/routes.rb. + 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 - # Make sure we end on a trailing newline. - content << '' unless content.last == '' + private - # 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 + def strip_on_removal(content, header_position) + case header_position + when :before + content.shift while content.first == '' + when :after + content.pop while content.last == '' 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] - - abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen + # Make sure we end on a trailing newline. + content << '' unless content.last == '' - if content_changed - File.open(routes_file, 'wb') { |f| f.puts(new_text) } - true - else - false - end - end + # 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 From 87dced6231bc264743531320eb71037059c091be Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:24:48 +0900 Subject: [PATCH 04/15] Add AnnotateRoutes::BaseProcessor#strip_annotations and #real_content_and_header_position / Remove them from AnnotateRoutes::Helpers --- .../annotate_routes/annotation_processor.rb | 2 +- .../annotate_routes/base_processor.rb | 42 ++++++++++++++++++ lib/annotate/annotate_routes/helpers.rb | 44 ------------------- .../annotate_routes/removal_processor.rb | 3 +- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index 98546d1f0..6bb1af88a 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -9,7 +9,7 @@ class AnnotationProcessor < BaseProcessor # @return [Boolean] def update header = HeaderGenerator.generate(options) - content, header_position = Helpers.strip_annotations(existing_text) + content, header_position = strip_annotations(existing_text) new_content = annotate_routes(header, content, header_position) new_text = new_content.join("\n") rewrite_contents(new_text) diff --git a/lib/annotate/annotate_routes/base_processor.rb b/lib/annotate/annotate_routes/base_processor.rb index 619c4e7bf..189ea8cf8 100644 --- a/lib/annotate/annotate_routes/base_processor.rb +++ b/lib/annotate/annotate_routes/base_processor.rb @@ -45,5 +45,47 @@ def content_changed?(new_text) def frozen? options[:frozen] end + + # TODO: write the method doc using ruby rdoc formats + # This method returns an array of 'real_content' and 'header_position'. + # 'header_position' will either be :before, :after, or + # a number. If the number is > 0, the + # annotation was found somewhere in the + # middle of the file. If the number is + # zero, no annotation was found. + def strip_annotations(content) + real_content = [] + mode = :content + header_position = 0 + + content.split(/\n/, -1).each_with_index do |line, line_number| + if mode == :header && line !~ /\s*#/ + mode = :content + real_content << line unless line.blank? + elsif mode == :content + if line =~ /^\s*#\s*== Route.*$/ + header_position = line_number + 1 # index start's at 0 + mode = :header + else + real_content << line + end + end + end + + real_content_and_header_position(real_content, header_position) + end + + def real_content_and_header_position(real_content, header_position) + # By default assume the annotation was found in the middle of the file + + # ... unless we have evidence it was at the beginning ... + return real_content, :before if header_position == 1 + + # ... or that it was at the end. + return real_content, :after if header_position >= real_content.count + + # and the default + [real_content, header_position] + end end end diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb index 1dba65bbe..79121ad86 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/helpers.rb @@ -3,35 +3,6 @@ module Helpers MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze class << self - # TODO: write the method doc using ruby rdoc formats - # This method returns an array of 'real_content' and 'header_position'. - # 'header_position' will either be :before, :after, or - # a number. If the number is > 0, the - # annotation was found somewhere in the - # middle of the file. If the number is - # zero, no annotation was found. - def strip_annotations(content) - real_content = [] - mode = :content - header_position = 0 - - content.split(/\n/, -1).each_with_index do |line, line_number| - if mode == :header && line !~ /\s*#/ - mode = :content - real_content << line unless line.blank? - elsif mode == :content - if line =~ /^\s*#\s*== Route.*$/ - header_position = line_number + 1 # index start's at 0 - mode = :header - else - real_content << line - end - end - end - - real_content_and_header_position(real_content, header_position) - end - # @param [Array] content # @return [Array] all found magic comments # @return [Array] content without magic comments @@ -49,21 +20,6 @@ def extract_magic_comments_from_array(content_array) [magic_comments, new_content] end - - private - - def real_content_and_header_position(real_content, header_position) - # By default assume the annotation was found in the middle of the file - - # ... unless we have evidence it was at the beginning ... - return real_content, :before if header_position == 1 - - # ... or that it was at the end. - return real_content, :after if header_position >= real_content.count - - # and the default - return real_content, header_position - end end end end diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb index f6586f548..f6b9ca98a 100644 --- a/lib/annotate/annotate_routes/removal_processor.rb +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -1,5 +1,4 @@ require_relative './base_processor' -require_relative './helpers' # This module provides methods for annotating config/routes.rb. module AnnotateRoutes @@ -7,7 +6,7 @@ module AnnotateRoutes class RemovalProcessor < BaseProcessor # @return [Boolean] def update - content, header_position = Helpers.strip_annotations(existing_text) + content, header_position = strip_annotations(existing_text) new_content = strip_on_removal(content, header_position) new_text = new_content.join("\n") rewrite_contents(new_text) From dd1a53e5b95d76c7d7d424de7542354895b6f621 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:26:39 +0900 Subject: [PATCH 05/15] Refactor AnnotateRoutes::AnnotationProcessor#annotate_routes --- lib/annotate/annotate_routes/annotation_processor.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index 6bb1af88a..918e59124 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -8,16 +8,16 @@ module AnnotateRoutes class AnnotationProcessor < BaseProcessor # @return [Boolean] def update - header = HeaderGenerator.generate(options) content, header_position = strip_annotations(existing_text) - new_content = annotate_routes(header, content, header_position) + new_content = annotate_routes(content, header_position) new_text = new_content.join("\n") rewrite_contents(new_text) end private - def annotate_routes(header, content, header_position) + def annotate_routes(content, header_position) + header = HeaderGenerator.generate(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 != '' From 0765bccf358ec5fdec8be285f0521b2fbc458ffd Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:28:27 +0900 Subject: [PATCH 06/15] Rename AnnotateRoutes::AnnotationProcessor#annotate_routes to #generate_new_content_array --- lib/annotate/annotate_routes/annotation_processor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index 918e59124..d6d427ad1 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -9,14 +9,14 @@ class AnnotationProcessor < BaseProcessor # @return [Boolean] def update content, header_position = strip_annotations(existing_text) - new_content = annotate_routes(content, header_position) + new_content = generate_new_content_array(content, header_position) new_text = new_content.join("\n") rewrite_contents(new_text) end private - def annotate_routes(content, header_position) + def generate_new_content_array(content, header_position) header = HeaderGenerator.generate(options) magic_comments_map, content = Helpers.extract_magic_comments_from_array(content) if %w[before top].include?(options[:position_in_routes]) From 67b96ba9c4168bf0e1f2f7437d7b4f6b686214c7 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:28:42 +0900 Subject: [PATCH 07/15] Rename AnnotateRoutes::RemovalProcessor#strip_on_removal to #generate_new_content_array --- lib/annotate/annotate_routes/removal_processor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb index f6b9ca98a..b1fb94a64 100644 --- a/lib/annotate/annotate_routes/removal_processor.rb +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -7,14 +7,14 @@ class RemovalProcessor < BaseProcessor # @return [Boolean] def update content, header_position = strip_annotations(existing_text) - new_content = strip_on_removal(content, header_position) + new_content = generate_new_content_array(content, header_position) new_text = new_content.join("\n") rewrite_contents(new_text) end private - def strip_on_removal(content, header_position) + def generate_new_content_array(content, header_position) case header_position when :before content.shift while content.first == '' From e56ebad73afbffe166ca06e53a19e9e7026af694 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:47:30 +0900 Subject: [PATCH 08/15] Add AnnotateRoutes::BaseProcessor#update --- lib/annotate/annotate_routes/annotation_processor.rb | 8 -------- lib/annotate/annotate_routes/base_processor.rb | 8 ++++++++ lib/annotate/annotate_routes/removal_processor.rb | 8 -------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index d6d427ad1..e92fb4795 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -6,14 +6,6 @@ module AnnotateRoutes # This class provides methods for adding annotation to config/routes.rb. class AnnotationProcessor < BaseProcessor - # @return [Boolean] - def update - content, header_position = strip_annotations(existing_text) - new_content = generate_new_content_array(content, header_position) - new_text = new_content.join("\n") - rewrite_contents(new_text) - end - private def generate_new_content_array(content, header_position) diff --git a/lib/annotate/annotate_routes/base_processor.rb b/lib/annotate/annotate_routes/base_processor.rb index 189ea8cf8..f0435ce5f 100644 --- a/lib/annotate/annotate_routes/base_processor.rb +++ b/lib/annotate/annotate_routes/base_processor.rb @@ -7,6 +7,14 @@ def initialize(options, routes_file) @routes_file = routes_file end + # @return [Boolean] + def update + content, header_position = strip_annotations(existing_text) + new_content = generate_new_content_array(content, header_position) + new_text = new_content.join("\n") + rewrite_contents(new_text) + end + def routes_file_exist? File.exist?(routes_file) end diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb index b1fb94a64..62b2ebba5 100644 --- a/lib/annotate/annotate_routes/removal_processor.rb +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -4,14 +4,6 @@ module AnnotateRoutes # This class provides methods for removing annotation from config/routes.rb. class RemovalProcessor < BaseProcessor - # @return [Boolean] - def update - content, header_position = strip_annotations(existing_text) - new_content = generate_new_content_array(content, header_position) - new_text = new_content.join("\n") - rewrite_contents(new_text) - end - private def generate_new_content_array(content, header_position) From ad3b25bd4d6be56db822d33c87234343498fda55 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:41:15 +0900 Subject: [PATCH 09/15] Add AnnotateRoutes::MagicCommentsExtractor#execute as a module method --- lib/annotate/annotate_routes/annotation_processor.rb | 4 ++-- lib/annotate/annotate_routes/header_generator.rb | 4 ++-- .../{helpers.rb => magic_comments_extractor.rb} | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) rename lib/annotate/annotate_routes/{helpers.rb => magic_comments_extractor.rb} (84%) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index e92fb4795..df1c330c4 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -1,6 +1,6 @@ require_relative './base_processor' -require_relative './helpers' require_relative './header_generator' +require_relative './magic_comments_extractor' # This module provides methods for annotating config/routes.rb. module AnnotateRoutes @@ -10,7 +10,7 @@ class AnnotationProcessor < BaseProcessor def generate_new_content_array(content, header_position) header = HeaderGenerator.generate(options) - magic_comments_map, content = Helpers.extract_magic_comments_from_array(content) + magic_comments_map, content = MagicCommentsExtractor.execute(content) if %w[before top].include?(options[:position_in_routes]) header = header << '' if content.first != '' magic_comments_map << '' if magic_comments_map.any? diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index b1c93acf7..91d7c5420 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -1,4 +1,4 @@ -require_relative './helpers' +require_relative './magic_comments_extractor' module AnnotateRoutes class HeaderGenerator @@ -42,7 +42,7 @@ def initialize(options, routes_map) end def generate - magic_comments_map, contents_without_magic_comments = Helpers.extract_magic_comments_from_array(routes_map) + magic_comments_map, contents_without_magic_comments = MagicCommentsExtractor.execute(routes_map) out = [] diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/magic_comments_extractor.rb similarity index 84% rename from lib/annotate/annotate_routes/helpers.rb rename to lib/annotate/annotate_routes/magic_comments_extractor.rb index 79121ad86..8188c4542 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/magic_comments_extractor.rb @@ -1,12 +1,13 @@ module AnnotateRoutes - module Helpers + # namespace to contain module function to extract magic comments + module MagicCommentsExtractor MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze class << self # @param [Array] content # @return [Array] all found magic comments # @return [Array] content without magic comments - def extract_magic_comments_from_array(content_array) + def execute(content_array) magic_comments = [] new_content = [] From 683f4d680f3b44193ac5d24c51d1a641b3336c58 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 04:06:00 +0900 Subject: [PATCH 10/15] Refactor AnnotateRoutes::AnnotationProcessor#generate_new_content_array --- .../annotate_routes/annotation_processor.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index df1c330c4..2f6de3196 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -12,9 +12,12 @@ def generate_new_content_array(content, header_position) header = HeaderGenerator.generate(options) magic_comments_map, content = MagicCommentsExtractor.execute(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 + new_content_array = [] + new_content_array += magic_comments_map + new_content_array << '' if magic_comments_map.any? + new_content_array += header + new_content_array << '' if content.first != '' + new_content_array += 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. @@ -24,13 +27,13 @@ def generate_new_content_array(content, header_position) # the spacer we put in the first time around. content.shift if header_position == :before && content.first == '' - new_content = magic_comments_map + content + header + new_content_array = magic_comments_map + content + header end # Make sure we end on a trailing newline. - new_content << '' unless new_content.last == '' + new_content_array << '' unless new_content_array.last == '' - new_content + new_content_array end end end From fadd88ae4a30a73636035a52616a09622ddad724 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:59:13 +0900 Subject: [PATCH 11/15] Add AnnotateRoutes::AnnotationProcessor#header --- lib/annotate/annotate_routes/annotation_processor.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index 2f6de3196..256a96579 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -8,8 +8,11 @@ module AnnotateRoutes class AnnotationProcessor < BaseProcessor private + def header + @header ||= HeaderGenerator.generate(options) + end + def generate_new_content_array(content, header_position) - header = HeaderGenerator.generate(options) magic_comments_map, content = MagicCommentsExtractor.execute(content) if %w[before top].include?(options[:position_in_routes]) new_content_array = [] From 25667c6fafa66f2f95f8ca80dfd2c616ec4b9ca8 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 04:27:18 +0900 Subject: [PATCH 12/15] Add AnnotateRoutes::BaseProcessor#new_text and remove #rewrite_contents --- .../annotate_routes/base_processor.rb | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/annotate/annotate_routes/base_processor.rb b/lib/annotate/annotate_routes/base_processor.rb index f0435ce5f..9ad8bb1ce 100644 --- a/lib/annotate/annotate_routes/base_processor.rb +++ b/lib/annotate/annotate_routes/base_processor.rb @@ -9,10 +9,16 @@ def initialize(options, routes_file) # @return [Boolean] def update - content, header_position = strip_annotations(existing_text) - new_content = generate_new_content_array(content, header_position) - new_text = new_content.join("\n") - rewrite_contents(new_text) + content_changed = content_changed?(new_text) + + abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen? + + if content_changed + write(new_text) + true + else + false + end end def routes_file_exist? @@ -27,19 +33,11 @@ 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) - - abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen? - - if content_changed - write(new_text) - true - else - false - end + # @return [String] + def new_text + content, header_position = strip_annotations(existing_text) + new_content = generate_new_content_array(content, header_position) + new_content.join("\n") end def write(text) From d9f6713149a4c8355afc446187da4094a7ad0da1 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 03:43:35 +0900 Subject: [PATCH 13/15] Add AnnotateRoutes::AnnotationProcessor#execute and AnnotateRoutes::RemovalProcessor#execute --- lib/annotate/annotate_routes.rb | 24 ++++--------------- .../annotate_routes/annotation_processor.rb | 13 ++++++++++ .../annotate_routes/base_processor.rb | 11 +++++++++ .../annotate_routes/removal_processor.rb | 13 ++++++++++ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 7d3f6d3c6..5ed67b21b 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -25,30 +25,14 @@ module AnnotateRoutes class << self def do_annotations(options = {}) routes_file = File.join('config', 'routes.rb') - processor = AnnotationProcessor.new(options, routes_file) - if processor.routes_file_exist? - if processor.update - puts "#{routes_file} was annotated." - else - puts "#{routes_file} was not changed." - end - else - puts "#{routes_file} could not be found." - end + result = AnnotationProcessor.execute(options, routes_file) + puts result end def remove_annotations(options = {}) routes_file = File.join('config', 'routes.rb') - processor = RemovalProcessor.new(options, routes_file) - if processor.routes_file_exist? - if processor.update - puts "Annotations were removed from #{routes_file}." - else - puts "#{routes_file} was not changed (Annotation did not exist)." - end - else - puts "#{routes_file} could not be found." - end + result = RemovalProcessor.execute(options, routes_file) + puts result end end end diff --git a/lib/annotate/annotate_routes/annotation_processor.rb b/lib/annotate/annotate_routes/annotation_processor.rb index 256a96579..e22635ca0 100644 --- a/lib/annotate/annotate_routes/annotation_processor.rb +++ b/lib/annotate/annotate_routes/annotation_processor.rb @@ -6,6 +6,19 @@ module AnnotateRoutes # This class provides methods for adding annotation to config/routes.rb. class AnnotationProcessor < BaseProcessor + # @return [String] + def execute + if routes_file_exist? + if update + "#{routes_file} was annotated." + else + "#{routes_file} was not changed." + end + else + "#{routes_file} could not be found." + end + end + private def header diff --git a/lib/annotate/annotate_routes/base_processor.rb b/lib/annotate/annotate_routes/base_processor.rb index 9ad8bb1ce..ec7044951 100644 --- a/lib/annotate/annotate_routes/base_processor.rb +++ b/lib/annotate/annotate_routes/base_processor.rb @@ -2,6 +2,17 @@ module AnnotateRoutes # This class is abstract class of classes adding and removing annotation to config/routes.rb. class BaseProcessor + class << self + # @param options [Hash] + # @param routes_file [String] + # @return [String] + def execute(options, routes_file) + new(options, routes_file).execute + end + + private :new + end + def initialize(options, routes_file) @options = options @routes_file = routes_file diff --git a/lib/annotate/annotate_routes/removal_processor.rb b/lib/annotate/annotate_routes/removal_processor.rb index 62b2ebba5..c9aafb255 100644 --- a/lib/annotate/annotate_routes/removal_processor.rb +++ b/lib/annotate/annotate_routes/removal_processor.rb @@ -4,6 +4,19 @@ module AnnotateRoutes # This class provides methods for removing annotation from config/routes.rb. class RemovalProcessor < BaseProcessor + # @return [String] + def execute + if routes_file_exist? + if update + "Annotations were removed from #{routes_file}." + else + "#{routes_file} was not changed (Annotation did not exist)." + end + else + "#{routes_file} could not be found." + end + end + private def generate_new_content_array(content, header_position) From 31b705de139ee9d7ca0bc663195d3e43e88b0833 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 12 Jan 2020 03:29:42 +0900 Subject: [PATCH 14/15] Fix AnnotateRoutes.do_annotations and .remove_annotations so as to return message --- lib/annotate/annotate_routes.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 5ed67b21b..47958921c 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -23,16 +23,22 @@ module AnnotateRoutes class << self + # @param options [Hash] + # @return [String] def do_annotations(options = {}) routes_file = File.join('config', 'routes.rb') - result = AnnotationProcessor.execute(options, routes_file) - puts result + AnnotationProcessor.execute(options, routes_file).tap do |result| + puts result + end end + # @param options [Hash] + # @return [String] def remove_annotations(options = {}) routes_file = File.join('config', 'routes.rb') - result = RemovalProcessor.execute(options, routes_file) - puts result + RemovalProcessor.execute(options, routes_file).tap do |result| + puts result + end end end end From 50b50bb3ba4b19ca02f08034d4afad8f01d6bff9 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 12 Jan 2020 04:31:50 +0900 Subject: [PATCH 15/15] Add comments to module AnnotateRoutes and nested classes --- lib/annotate/annotate_routes.rb | 1 + lib/annotate/annotate_routes/header_generator.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 47958921c..b0eb89f45 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -21,6 +21,7 @@ require_relative './annotate_routes/annotation_processor' require_relative './annotate_routes/removal_processor' +# This module provides methods for annotating config/routes.rb. module AnnotateRoutes class << self # @param options [Hash] diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 91d7c5420..e8b4c26e6 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -1,6 +1,8 @@ require_relative './magic_comments_extractor' +# This module provides methods for annotating config/routes.rb. module AnnotateRoutes + # This class processes result of `rake routes` and generate content enbeded in config/routes.rb. class HeaderGenerator PREFIX = '== Route Map'.freeze PREFIX_MD = '## Route Map'.freeze