Unverified Commit d89ddefa by Shu Fujita Committed by GitHub

Refactor AnnotateRoutes by adding AnnotateRoutes::HeaderGenerator (#790)

I noticed that `AnnotateRoutes` can be more maintainable by refactoring. I am planning to refactor `AnnotateRoutes` in this order. * separate logic of `AnnotateRoutes` into `AnnotateRoutes::HeaderGenerator`. * add methods to `AnnotateRoutes::HeaderGenerator` and refactor methods. * add `AnnotateRoutes::AnnotationProcessor` and `AnnotateRoutes::RemovalProcessor` The final goal of this refactoring is as follows. * https://github.com/nard-tech/annotate_models/blob/feature/refactor_annotate_routes/processors/lib/annotate/annotate_routes.rb * https://github.com/nard-tech/annotate_models/tree/feature/refactor_annotate_routes/processors/lib/annotate/annotate_routes So in the first I added `AnnotateRoutes::HeaderGenerator` in order to separate logic of `AnnotateRoutes` in this PR. When refactor of `AnnotateRoutes` is finished, I would like to refactor `AnnotateModels` in a like way.
parent d9392d9e
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config` # `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 # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
...@@ -163,12 +163,11 @@ Lint/InheritException: ...@@ -163,12 +163,11 @@ Lint/InheritException:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 2 # Offense count: 1
# Configuration parameters: MaximumRangeSize. # Configuration parameters: MaximumRangeSize.
Lint/MissingCopEnableDirective: Lint/MissingCopEnableDirective:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb'
# Offense count: 2 # Offense count: 2
Lint/RescueException: Lint/RescueException:
...@@ -213,7 +212,7 @@ Naming/AccessorMethodName: ...@@ -213,7 +212,7 @@ Naming/AccessorMethodName:
Exclude: Exclude:
- 'lib/annotate.rb' - 'lib/annotate.rb'
# Offense count: 103 # Offense count: 102
# Configuration parameters: Blacklist. # Configuration parameters: Blacklist.
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) # Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
Naming/HeredocDelimiterNaming: Naming/HeredocDelimiterNaming:
...@@ -235,12 +234,13 @@ Naming/UncommunicativeMethodParamName: ...@@ -235,12 +234,13 @@ Naming/UncommunicativeMethodParamName:
Exclude: Exclude:
- 'Rakefile' - 'Rakefile'
# Offense count: 1 # Offense count: 2
# Configuration parameters: EnforcedStyle. # Configuration parameters: EnforcedStyle.
# SupportedStyles: inline, group # SupportedStyles: inline, group
Style/AccessModifierDeclarations: Style/AccessModifierDeclarations:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes/header_generator.rb'
# Offense count: 1 # Offense count: 1
Style/CaseEquality: Style/CaseEquality:
...@@ -266,7 +266,7 @@ Style/Dir: ...@@ -266,7 +266,7 @@ Style/Dir:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
# Offense count: 9 # Offense count: 10
Style/Documentation: Style/Documentation:
Exclude: Exclude:
- 'spec/**/*' - 'spec/**/*'
...@@ -275,6 +275,7 @@ Style/Documentation: ...@@ -275,6 +275,7 @@ Style/Documentation:
- 'lib/annotate/active_record_patch.rb' - 'lib/annotate/active_record_patch.rb'
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
- 'lib/annotate/annotate_routes/header_generator.rb'
- 'lib/annotate/annotate_routes/helpers.rb' - 'lib/annotate/annotate_routes/helpers.rb'
- 'lib/annotate/version.rb' - 'lib/annotate/version.rb'
- 'lib/generators/annotate/install_generator.rb' - 'lib/generators/annotate/install_generator.rb'
...@@ -293,14 +294,13 @@ Style/ExpandPathArguments: ...@@ -293,14 +294,13 @@ Style/ExpandPathArguments:
Exclude: Exclude:
- 'annotate.gemspec' - 'annotate.gemspec'
# Offense count: 10 # Offense count: 9
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle. # Configuration parameters: EnforcedStyle.
# SupportedStyles: format, sprintf, percent # SupportedStyles: format, sprintf, percent
Style/FormatString: Style/FormatString:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb'
# Offense count: 23 # Offense count: 23
# Configuration parameters: EnforcedStyle. # Configuration parameters: EnforcedStyle.
...@@ -309,7 +309,7 @@ Style/FormatStringToken: ...@@ -309,7 +309,7 @@ Style/FormatStringToken:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 27 # Offense count: 28
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle. # Configuration parameters: EnforcedStyle.
# SupportedStyles: when_needed, always, never # SupportedStyles: when_needed, always, never
...@@ -338,7 +338,7 @@ Style/IfUnlessModifier: ...@@ -338,7 +338,7 @@ Style/IfUnlessModifier:
- 'Rakefile' - 'Rakefile'
- 'bin/annotate' - 'bin/annotate'
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes/header_generator.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
...@@ -432,7 +432,7 @@ Style/RedundantSelf: ...@@ -432,7 +432,7 @@ Style/RedundantSelf:
Exclude: Exclude:
- 'lib/tasks/annotate_models_migrate.rake' - 'lib/tasks/annotate_models_migrate.rake'
# Offense count: 13 # Offense count: 12
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, AllowInnerSlashes. # Configuration parameters: EnforcedStyle, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed # SupportedStyles: slashes, percent_r, mixed
...@@ -440,7 +440,6 @@ Style/RegexpLiteral: ...@@ -440,7 +440,6 @@ Style/RegexpLiteral:
Exclude: Exclude:
- 'Rakefile' - 'Rakefile'
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
...@@ -521,7 +520,7 @@ Style/UnneededPercentQ: ...@@ -521,7 +520,7 @@ Style/UnneededPercentQ:
Exclude: Exclude:
- 'annotate.gemspec' - 'annotate.gemspec'
# Offense count: 377 # Offense count: 375
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https # URISchemes: http, https
......
# rubocop:disable Metrics/ModuleLength
# == Annotate Routes # == Annotate Routes
# #
# Based on: # Based on:
...@@ -21,18 +19,15 @@ ...@@ -21,18 +19,15 @@
# #
require_relative './annotate_routes/helpers' require_relative './annotate_routes/helpers'
require_relative './annotate_routes/header_generator'
module AnnotateRoutes module AnnotateRoutes
PREFIX = '== Route Map'.freeze
PREFIX_MD = '## Route Map'.freeze
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
class << self class << self
def do_annotations(options = {}) def do_annotations(options = {})
if routes_file_exist? if routes_file_exist?
existing_text = File.read(routes_file) existing_text = File.read(routes_file)
content, header_position = Helpers.strip_annotations(existing_text) 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") new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text) if rewrite_contents(existing_text, new_text)
...@@ -71,49 +66,6 @@ module AnnotateRoutes ...@@ -71,49 +66,6 @@ module AnnotateRoutes
@routes_rb ||= File.join('config', 'routes.rb') @routes_rb ||= File.join('config', 'routes.rb')
end end
def header(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
def comment(row = '')
if row == ''
'#'
else
"# #{row}"
end
end
def strip_on_removal(content, header_position) def strip_on_removal(content, header_position)
if header_position == :before if header_position == :before
content.shift while content.first == '' content.shift while content.first == ''
...@@ -162,32 +114,5 @@ module AnnotateRoutes ...@@ -162,32 +114,5 @@ module AnnotateRoutes
new_content new_content
end 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]
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 end
require_relative './helpers'
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)
new(options, routes_map).generate
end
private :new
private
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 =~ %r{^\(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
def initialize(options, routes_map)
@options = options
@routes_map = routes_map
end
def generate
magic_comments_map, contents_without_magic_comments = 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 contents_without_magic_comments.size.zero?
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
out << comment(content(HEADER_ROW, maxs))
out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs))
else
out << comment(content(contents_without_magic_comments[0], maxs))
end
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
end
private
attr_reader :options, :routes_map
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
format("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
end.join(' | ')
end
end
end
...@@ -49,7 +49,7 @@ describe AnnotateRoutes do ...@@ -49,7 +49,7 @@ describe AnnotateRoutes do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once 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(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 end
context 'When the result of `rake routes` is present' do context 'When the result of `rake routes` is present' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment