From 74dcb4ac3126afc7f98f50029152399a25532df3 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 02:36:11 +0900 Subject: [PATCH 1/7] Add AnnotateRoutes::HeaderGenerator.app_routes_map --- lib/annotate/annotate_routes.rb | 20 ++-------------- .../annotate_routes/header_generator.rb | 24 +++++++++++++++++++ spec/lib/annotate/annotate_routes_spec.rb | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 lib/annotate/annotate_routes/header_generator.rb diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index d1fed3353..3e5cecee9 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -21,6 +21,7 @@ # require_relative './annotate_routes/helpers' +require_relative './annotate_routes/header_generator' module AnnotateRoutes PREFIX = '== Route Map'.freeze @@ -72,7 +73,7 @@ def routes_file end def header(options = {}) - routes_map = app_routes_map(options) + routes_map = HeaderGenerator.app_routes_map(options) magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) @@ -163,23 +164,6 @@ def annotate_routes(header, content, header_position, options = {}) new_content end - def app_routes_map(options) - routes_map = `rake routes`.chomp("\n").split(/\n/, -1) - - # In old versions of Rake, the first line of output was the cwd. Not so - # much in newer ones. We ditch that line if it exists, and if not, we - # keep the line around. - routes_map.shift if routes_map.first =~ /^\(in \// - - # Skip routes which match given regex - # Note: it matches the complete line (route_name, path, controller/action) - if options[:ignore_routes] - routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ } - end - - routes_map - end - def content(line, maxs, options = {}) return line.rstrip unless options[:format_markdown] diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb new file mode 100644 index 000000000..ea37737f1 --- /dev/null +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -0,0 +1,24 @@ +require_relative './helpers' + +module AnnotateRoutes + class HeaderGenerator + class << self + def app_routes_map(options) + routes_map = `rake routes`.chomp("\n").split(/\n/, -1) + + # In old versions of Rake, the first line of output was the cwd. Not so + # much in newer ones. We ditch that line if it exists, and if not, we + # keep the line around. + routes_map.shift if routes_map.first =~ /^\(in \// + + # Skip routes which match given regex + # Note: it matches the complete line (route_name, path, controller/action) + if options[:ignore_routes] + routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ } + end + + routes_map + end + end + end +end diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index dc4400a35..a0ed118cc 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -49,7 +49,7 @@ expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once + expect(AnnotateRoutes::HeaderGenerator).to receive(:`).with('rake routes').and_return(rake_routes_result).once end context 'When the result of `rake routes` is present' do From 29d62cdb453647f45f34740bbbeb4ebbae5fb784 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Wed, 8 Jan 2020 17:52:36 +0900 Subject: [PATCH 2/7] Add AnnotateRoutes::HeaderGenerator.generate --- lib/annotate/annotate_routes.rb | 59 +------------------ .../annotate_routes/header_generator.rb | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 58 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 3e5cecee9..7bd5ebb17 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -24,16 +24,12 @@ require_relative './annotate_routes/header_generator' module AnnotateRoutes - PREFIX = '== Route Map'.freeze - PREFIX_MD = '## Route Map'.freeze - HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze - 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(header(options), content, header_position, options) + new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options) new_text = new_content.join("\n") if rewrite_contents(existing_text, new_text) @@ -72,49 +68,6 @@ def routes_file @routes_rb ||= File.join('config', 'routes.rb') end - def header(options = {}) - routes_map = HeaderGenerator.app_routes_map(options) - - magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) - - out = [] - - magic_comments_map.each do |magic_comment| - out << magic_comment - end - out << '' if magic_comments_map.any? - - out << comment(options[:wrapper_open]) if options[:wrapper_open] - - out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') - out << comment - return out if routes_map.size.zero? - - maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } - - if options[:format_markdown] - max = maxs.map(&:max).compact.max - - out << comment(content(HEADER_ROW, maxs, options)) - out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)) - else - out << comment(content(routes_map[0], maxs, options)) - end - - out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) } - out << comment(options[:wrapper_close]) if options[:wrapper_close] - - out - end - - def comment(row = '') - if row == '' - '#' - else - "# #{row}" - end - end - def strip_on_removal(content, header_position) if header_position == :before content.shift while content.first == '' @@ -163,15 +116,5 @@ def annotate_routes(header, content, header_position, options = {}) new_content end - - def content(line, maxs, options = {}) - return line.rstrip unless options[:format_markdown] - - line.each_with_index.map do |elem, index| - min_length = maxs.map { |arr| arr[index] }.max || 0 - - sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) - end.join(' | ') - end end end diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index ea37737f1..9b71168cd 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -2,7 +2,48 @@ module AnnotateRoutes class HeaderGenerator + PREFIX = '== Route Map'.freeze + PREFIX_MD = '## Route Map'.freeze + HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze + class << self + def generate(options = {}) + routes_map = app_routes_map(options) + + magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) + + out = [] + + magic_comments_map.each do |magic_comment| + out << magic_comment + end + out << '' if magic_comments_map.any? + + out << comment(options[:wrapper_open]) if options[:wrapper_open] + + out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') + out << comment + return out if routes_map.size.zero? + + maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } + + if options[:format_markdown] + max = maxs.map(&:max).compact.max + + out << comment(content(HEADER_ROW, maxs, options)) + out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)) + else + out << comment(content(routes_map[0], maxs, options)) + end + + out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) } + out << comment(options[:wrapper_close]) if options[:wrapper_close] + + out + end + + private + def app_routes_map(options) routes_map = `rake routes`.chomp("\n").split(/\n/, -1) @@ -19,6 +60,24 @@ def app_routes_map(options) routes_map end + + def comment(row = '') + if row == '' + '#' + else + "# #{row}" + end + end + + def content(line, maxs, options = {}) + return line.rstrip unless options[:format_markdown] + + line.each_with_index.map do |elem, index| + min_length = maxs.map { |arr| arr[index] }.max || 0 + + sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) + end.join(' | ') + end end end end From 26f77bd1cd1861d78d5c57d2e6f1dffc4a1c4322 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Wed, 8 Jan 2020 18:05:41 +0900 Subject: [PATCH 3/7] Define methods of AnnotateRoutes::HeaderGenerator as instance methods [skip CI] --- .../annotate_routes/header_generator.rb | 100 ++++++++++-------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 9b71168cd..cea612bcf 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -9,39 +9,11 @@ class HeaderGenerator class << self def generate(options = {}) routes_map = app_routes_map(options) - - magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) - - out = [] - - magic_comments_map.each do |magic_comment| - out << magic_comment - end - out << '' if magic_comments_map.any? - - out << comment(options[:wrapper_open]) if options[:wrapper_open] - - out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') - out << comment - return out if routes_map.size.zero? - - maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } - - if options[:format_markdown] - max = maxs.map(&:max).compact.max - - out << comment(content(HEADER_ROW, maxs, options)) - out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)) - else - out << comment(content(routes_map[0], maxs, options)) - end - - out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) } - out << comment(options[:wrapper_close]) if options[:wrapper_close] - - out + new(options, routes_map).generate end + private :new + private def app_routes_map(options) @@ -60,24 +32,66 @@ def app_routes_map(options) routes_map end + end - def comment(row = '') - if row == '' - '#' - else - "# #{row}" - end + def initialize(options, routes_map) + @options = options + @routes_map = routes_map + end + + def generate + magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) + + out = [] + + magic_comments_map.each do |magic_comment| + out << magic_comment + end + out << '' if magic_comments_map.any? + + out << comment(options[:wrapper_open]) if options[:wrapper_open] + + out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') + out << comment + return out if routes_map.size.zero? + + maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } + + if options[:format_markdown] + max = maxs.map(&:max).compact.max + + out << comment(content(HEADER_ROW, maxs)) + out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs)) + else + out << comment(content(routes_map[0], maxs)) end - def content(line, maxs, options = {}) - return line.rstrip unless options[:format_markdown] + out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) } + out << comment(options[:wrapper_close]) if options[:wrapper_close] + + out + end + + private - line.each_with_index.map do |elem, index| - min_length = maxs.map { |arr| arr[index] }.max || 0 + attr_reader :options, :routes_map - sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) - end.join(' | ') + def comment(row = '') + if row == '' + '#' + else + "# #{row}" end end + + def content(line, maxs) + return line.rstrip unless options[:format_markdown] + + line.each_with_index.map do |elem, index| + min_length = maxs.map { |arr| arr[index] }.max || 0 + + sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) + end.join(' | ') + end end end From ba22f2ee8996374a69a9f2d170e30a2c8b291c87 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Wed, 8 Jan 2020 18:07:31 +0900 Subject: [PATCH 4/7] Rename variable in AnnotateRoutes::HeaderGenerator#generate --- lib/annotate/annotate_routes/header_generator.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index cea612bcf..9a0b10b0f 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -40,7 +40,7 @@ def initialize(options, routes_map) end def generate - magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map) + magic_comments_map, contents_without_magic_comments = Helpers.extract_magic_comments_from_array(routes_map) out = [] @@ -53,9 +53,9 @@ def generate out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') out << comment - return out if routes_map.size.zero? + return out if contents_without_magic_comments.size.zero? - maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } + maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) } if options[:format_markdown] max = maxs.map(&:max).compact.max @@ -63,10 +63,10 @@ def generate out << comment(content(HEADER_ROW, maxs)) out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs)) else - out << comment(content(routes_map[0], maxs)) + out << comment(content(contents_without_magic_comments[0], maxs)) end - out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) } + out += contents_without_magic_comments[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) } out << comment(options[:wrapper_close]) if options[:wrapper_close] out From 6931e34c7dbc6c4f42e30ddc2e6cee2b7a51c30c Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Fri, 3 Apr 2020 00:47:20 +0900 Subject: [PATCH 5/7] Remove Rubocop comment from AnnotateRoutes --- lib/annotate/annotate_routes.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 7bd5ebb17..75cc421ed 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -1,5 +1,3 @@ -# rubocop:disable Metrics/ModuleLength - # == Annotate Routes # # Based on: From d7ab4a2d9bfb2c5cb88a6e458dae8e33605c5b20 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Fri, 3 Apr 2020 00:49:55 +0900 Subject: [PATCH 6/7] Fix Rubocop violation in AnnotateRoutes::HeaderGenerator --- lib/annotate/annotate_routes/header_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 9a0b10b0f..941f9bb99 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -22,7 +22,7 @@ def app_routes_map(options) # In old versions of Rake, the first line of output was the cwd. Not so # much in newer ones. We ditch that line if it exists, and if not, we # keep the line around. - routes_map.shift if routes_map.first =~ /^\(in \// + routes_map.shift if routes_map.first =~ %r{^\(in \/} # Skip routes which match given regex # Note: it matches the complete line (route_name, path, controller/action) @@ -90,7 +90,7 @@ def content(line, maxs) line.each_with_index.map do |elem, index| min_length = maxs.map { |arr| arr[index] }.max || 0 - sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) + format("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) end.join(' | ') end end From 337ea5baa0a42039165207cb7ebc0435bb456609 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Fri, 3 Apr 2020 00:52:09 +0900 Subject: [PATCH 7/7] Execute `bundle exec rubocop --auto-gen-config` --- .rubocop_todo.yml | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ababb76a5..4d802d81c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-03-01 03:15:48 +0900 using RuboCop version 0.68.1. +# on 2020-04-03 00:51:53 +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 @@ -163,12 +163,11 @@ Lint/InheritException: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 2 +# Offense count: 1 # Configuration parameters: MaximumRangeSize. Lint/MissingCopEnableDirective: Exclude: - 'lib/annotate/annotate_models.rb' - - 'lib/annotate/annotate_routes.rb' # Offense count: 2 Lint/RescueException: @@ -213,7 +212,7 @@ Naming/AccessorMethodName: Exclude: - 'lib/annotate.rb' -# Offense count: 103 +# Offense count: 102 # Configuration parameters: Blacklist. # Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) Naming/HeredocDelimiterNaming: @@ -235,12 +234,13 @@ Naming/UncommunicativeMethodParamName: Exclude: - 'Rakefile' -# Offense count: 1 +# Offense count: 2 # Configuration parameters: EnforcedStyle. # SupportedStyles: inline, group Style/AccessModifierDeclarations: Exclude: - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes/header_generator.rb' # Offense count: 1 Style/CaseEquality: @@ -266,7 +266,7 @@ Style/Dir: Exclude: - 'bin/annotate' -# Offense count: 9 +# Offense count: 10 Style/Documentation: Exclude: - 'spec/**/*' @@ -275,6 +275,7 @@ Style/Documentation: - 'lib/annotate/active_record_patch.rb' - 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_routes.rb' + - 'lib/annotate/annotate_routes/header_generator.rb' - 'lib/annotate/annotate_routes/helpers.rb' - 'lib/annotate/version.rb' - 'lib/generators/annotate/install_generator.rb' @@ -293,14 +294,13 @@ Style/ExpandPathArguments: Exclude: - 'annotate.gemspec' -# Offense count: 10 +# Offense count: 9 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: format, sprintf, percent Style/FormatString: Exclude: - 'lib/annotate/annotate_models.rb' - - 'lib/annotate/annotate_routes.rb' # Offense count: 23 # Configuration parameters: EnforcedStyle. @@ -309,7 +309,7 @@ Style/FormatStringToken: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 27 +# Offense count: 28 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: when_needed, always, never @@ -338,7 +338,7 @@ Style/IfUnlessModifier: - 'Rakefile' - 'bin/annotate' - 'lib/annotate/annotate_models.rb' - - 'lib/annotate/annotate_routes.rb' + - 'lib/annotate/annotate_routes/header_generator.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -432,7 +432,7 @@ Style/RedundantSelf: Exclude: - 'lib/tasks/annotate_models_migrate.rake' -# Offense count: 13 +# Offense count: 12 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, AllowInnerSlashes. # SupportedStyles: slashes, percent_r, mixed @@ -440,7 +440,6 @@ Style/RegexpLiteral: Exclude: - 'Rakefile' - 'lib/annotate/annotate_models.rb' - - 'lib/annotate/annotate_routes.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -521,7 +520,7 @@ Style/UnneededPercentQ: Exclude: - 'annotate.gemspec' -# Offense count: 377 +# Offense count: 375 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https