From 3a3b857850fcfe5513c7b6674afcb29ff34102a6 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 30 Apr 2018 04:12:19 +0900 Subject: [PATCH 1/3] Add AnnotateRoutes::Helpers.extract_magic_comments_from_array --- lib/annotate/annotate_routes.rb | 27 +++++-------------------- lib/annotate/annotate_routes/helpers.rb | 25 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 lib/annotate/annotate_routes/helpers.rb diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 5f8bbf357..16fcbed86 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -19,13 +19,14 @@ # # Released under the same license as Ruby. No Support. No Warranty. # + +require_relative './annotate_routes/helpers' + module AnnotateRoutes PREFIX = '== Route Map'.freeze PREFIX_MD = '## Route Map'.freeze HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze - MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze - class << self def do_annotations(options = {}) if routes_file_exist? @@ -73,7 +74,7 @@ def routes_file def header(options = {}) routes_map = app_routes_map(options) - magic_comments_map, routes_map = extract_magic_comments_from_array(routes_map) + magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) out = [] @@ -168,7 +169,7 @@ def rewrite_contents(existing_text, new_text) end def annotate_routes(header, content, header_position, options = {}) - magic_comments_map, content = extract_magic_comments_from_array(content) + 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? @@ -208,24 +209,6 @@ def app_routes_map(options) routes_map end - # @param [Array] content - # @return [Array] all found magic comments - # @return [Array] content without magic comments - def extract_magic_comments_from_array(content_array) - magic_comments = [] - new_content = [] - - content_array.each do |row| - if row =~ MAGIC_COMMENT_MATCHER - magic_comments << row.strip - else - new_content << row - end - end - - [magic_comments, new_content] - end - def content(line, maxs, options = {}) return line.rstrip unless options[:format_markdown] diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb new file mode 100644 index 000000000..79121ad86 --- /dev/null +++ b/lib/annotate/annotate_routes/helpers.rb @@ -0,0 +1,25 @@ +module AnnotateRoutes + module Helpers + 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) + magic_comments = [] + new_content = [] + + content_array.each do |row| + if row =~ MAGIC_COMMENT_MATCHER + magic_comments << row.strip + else + new_content << row + end + end + + [magic_comments, new_content] + end + end + end +end From b1f7342b5ef041783896419ae1f460ce75dcefd5 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 02:30:52 +0900 Subject: [PATCH 2/3] Add AnnotateRoutes::Helpers.strip_annotations and .real_content_and_header_position --- lib/annotate/annotate_routes.rb | 46 ++----------------------- lib/annotate/annotate_routes/helpers.rb | 44 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 16fcbed86..d1fed3353 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -31,7 +31,7 @@ class << self def do_annotations(options = {}) if routes_file_exist? existing_text = File.read(routes_file) - content, header_position = strip_annotations(existing_text) + content, header_position = Helpers.strip_annotations(existing_text) new_content = annotate_routes(header(options), content, header_position, options) new_text = new_content.join("\n") @@ -48,7 +48,7 @@ def do_annotations(options = {}) def remove_annotations(_options={}) if routes_file_exist? existing_text = File.read(routes_file) - content, header_position = strip_annotations(existing_text) + 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) @@ -114,35 +114,6 @@ def comment(row = '') end 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 strip_on_removal(content, header_position) if header_position == :before content.shift while content.first == '' @@ -218,18 +189,5 @@ def content(line, maxs, options = {}) sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) end.join(' | ') 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 - return real_content, header_position - end end end diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb index 79121ad86..1dba65bbe 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/helpers.rb @@ -3,6 +3,35 @@ 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 @@ -20,6 +49,21 @@ 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 From 9cb8fc5904805a217e71ea667a3137d5dbb3e79d Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 1 Mar 2020 03:15:59 +0900 Subject: [PATCH 3/3] Execute `rubocop --auto-gen-config` --- .rubocop_todo.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fc7c1e779..ababb76a5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-02-13 20:05:34 +0900 using RuboCop version 0.68.1. +# on 2020-03-01 03:15:48 +0900 using RuboCop version 0.68.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -266,7 +266,7 @@ Style/Dir: Exclude: - 'bin/annotate' -# Offense count: 7 +# Offense count: 9 Style/Documentation: Exclude: - 'spec/**/*' @@ -274,6 +274,8 @@ Style/Documentation: - 'lib/annotate.rb' - 'lib/annotate/active_record_patch.rb' - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' + - 'lib/annotate/annotate_routes/helpers.rb' - 'lib/annotate/version.rb' - 'lib/generators/annotate/install_generator.rb' - 'lib/tasks/annotate_models_migrate.rake' @@ -307,7 +309,7 @@ Style/FormatStringToken: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 26 +# Offense count: 27 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: when_needed, always, never @@ -422,7 +424,7 @@ Style/RedundantParentheses: # Configuration parameters: AllowMultipleReturnValues. Style/RedundantReturn: Exclude: - - 'lib/annotate/annotate_routes.rb' + - 'lib/annotate/annotate_routes/helpers.rb' # Offense count: 2 # Cop supports --auto-correct.