Commit 407ace64 by Shu Fujita Committed by Andrew W. Lee

Refactor Annotate::Parser (#742)

# Summary * Removed unnecessary variables * Renamed variables that have same name as reserved word (`p`) * Other tiny fixes
parent 7f8bf46a
...@@ -9,6 +9,11 @@ module Annotate ...@@ -9,6 +9,11 @@ module Annotate
attr_reader :args, :options, :env attr_reader :args, :options, :env
DEFAULT_OPTIONS = {
target_action: :do_annotations,
exit: false
}.freeze
ANNOTATION_POSITIONS = %w[before top after bottom].freeze ANNOTATION_POSITIONS = %w[before top after bottom].freeze
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
...@@ -16,7 +21,7 @@ module Annotate ...@@ -16,7 +21,7 @@ module Annotate
def initialize(args, env) def initialize(args, env)
@args = args @args = args
@options = default_options @options = DEFAULT_OPTIONS.dup
@env = env @env = env
end end
...@@ -45,114 +50,150 @@ module Annotate ...@@ -45,114 +50,150 @@ module Annotate
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
has_set_position = {} has_set_position = {}
positions = ANNOTATION_POSITIONS
option_parser.banner = 'Usage: annotate [options] [model_file]*' option_parser.banner = 'Usage: annotate [options] [model_file]*'
option_parser.on('--additional-file-patterns path1,path2,path3', Array, "Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)") do |additional_file_patterns| option_parser.on('--additional-file-patterns path1,path2,path3',
Array,
"Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)") do |additional_file_patterns|
ENV['additional_file_patterns'] = additional_file_patterns ENV['additional_file_patterns'] = additional_file_patterns
end end
option_parser.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do option_parser.on('-d',
'--delete',
'Remove annotations from all model files or the routes.rb file') do
@options[:target_action] = :remove_annotations @options[:target_action] = :remove_annotations
end end
option_parser.on('-p', '--position [before|top|after|bottom]', positions, option_parser.on('-p',
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p| '--position [before|top|after|bottom]',
env['position'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |position|
env['position'] = position
FILE_TYPE_POSITIONS.each do |key| FILE_TYPE_POSITIONS.each do |key|
env[key] = p unless has_set_position[key] env[key] = position unless has_set_position[key]
end end
end end
option_parser.on('--pc', '--position-in-class [before|top|after|bottom]', positions, option_parser.on('--pc',
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p| '--position-in-class [before|top|after|bottom]',
env['position_in_class'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of the model file') do |position_in_class|
env['position_in_class'] = position_in_class
has_set_position['position_in_class'] = true has_set_position['position_in_class'] = true
end end
option_parser.on('--pf', '--position-in-factory [before|top|after|bottom]', positions, option_parser.on('--pf',
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p| '--position-in-factory [before|top|after|bottom]',
env['position_in_factory'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |position_in_factory|
env['position_in_factory'] = position_in_factory
has_set_position['position_in_factory'] = true has_set_position['position_in_factory'] = true
end end
option_parser.on('--px', '--position-in-fixture [before|top|after|bottom]', positions, option_parser.on('--px',
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p| '--position-in-fixture [before|top|after|bottom]',
env['position_in_fixture'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |position_in_fixture|
env['position_in_fixture'] = position_in_fixture
has_set_position['position_in_fixture'] = true has_set_position['position_in_fixture'] = true
end end
option_parser.on('--pt', '--position-in-test [before|top|after|bottom]', positions, option_parser.on('--pt',
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p| '--position-in-test [before|top|after|bottom]',
env['position_in_test'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of any test files') do |position_in_test|
env['position_in_test'] = position_in_test
has_set_position['position_in_test'] = true has_set_position['position_in_test'] = true
end end
option_parser.on('--pr', '--position-in-routes [before|top|after|bottom]', positions, option_parser.on('--pr',
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p| '--position-in-routes [before|top|after|bottom]',
env['position_in_routes'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |position_in_routes|
env['position_in_routes'] = position_in_routes
has_set_position['position_in_routes'] = true has_set_position['position_in_routes'] = true
end end
option_parser.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions, option_parser.on('--ps',
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p| '--position-in-serializer [before|top|after|bottom]',
env['position_in_serializer'] = p ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |position_in_serializer|
env['position_in_serializer'] = position_in_serializer
has_set_position['position_in_serializer'] = true has_set_position['position_in_serializer'] = true
end end
option_parser.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.', option_parser.on('--w',
'If --w option is used, the same text will be used as opening and closing') do |p| '--wrapper STR',
env['wrapper'] = p 'Wrap annotation with the text passed as parameter.',
'If --w option is used, the same text will be used as opening and closing') do |wrapper|
env['wrapper'] = wrapper
end end
option_parser.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p| option_parser.on('--wo',
env['wrapper_open'] = p '--wrapper-open STR',
'Annotation wrapper opening.') do |wrapper_open|
env['wrapper_open'] = wrapper_open
end end
option_parser.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p| option_parser.on('--wc',
env['wrapper_close'] = p '--wrapper-close STR',
'Annotation wrapper closing') do |wrapper_close|
env['wrapper_close'] = wrapper_close
end end
option_parser.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do option_parser.on('-r',
'--routes',
"Annotate routes.rb with the output of 'rake routes'") do
env['routes'] = 'true' env['routes'] = 'true'
end end
option_parser.on('--models', "Annotate ActiveRecord models") do option_parser.on('--models',
"Annotate ActiveRecord models") do
env['models'] = 'true' env['models'] = 'true'
end end
option_parser.on('-a', '--active-admin', 'Annotate active_admin models') do option_parser.on('-a',
'--active-admin',
'Annotate active_admin models') do
env['active_admin'] = 'true' env['active_admin'] = 'true'
end end
option_parser.on('-v', '--version', 'Show the current version of this gem') do option_parser.on('-v',
'--version',
'Show the current version of this gem') do
puts "annotate v#{Annotate.version}" puts "annotate v#{Annotate.version}"
@options[:exit] = true @options[:exit] = true
end end
option_parser.on('-m', '--show-migration', 'Include the migration version number in the annotation') do option_parser.on('-m',
'--show-migration',
'Include the migration version number in the annotation') do
env['include_version'] = 'yes' env['include_version'] = 'yes'
end end
option_parser.on('-k', '--show-foreign-keys', option_parser.on('-k',
'--show-foreign-keys',
"List the table's foreign key constraints in the annotation") do "List the table's foreign key constraints in the annotation") do
env['show_foreign_keys'] = 'yes' env['show_foreign_keys'] = 'yes'
end end
option_parser.on('--ck', option_parser.on('--ck',
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do '--complete-foreign-keys',
'Complete foreign key names in the annotation') do
env['show_foreign_keys'] = 'yes' env['show_foreign_keys'] = 'yes'
env['show_complete_foreign_keys'] = 'yes' env['show_complete_foreign_keys'] = 'yes'
end end
option_parser.on('-i', '--show-indexes', option_parser.on('-i',
'--show-indexes',
"List the table's database indexes in the annotation") do "List the table's database indexes in the annotation") do
env['show_indexes'] = 'yes' env['show_indexes'] = 'yes'
end end
option_parser.on('-s', '--simple-indexes', option_parser.on('-s',
'--simple-indexes',
"Concat the column's related indexes in the annotation") do "Concat the column's related indexes in the annotation") do
env['simple_indexes'] = 'yes' env['simple_indexes'] = 'yes'
end end
...@@ -168,84 +209,95 @@ module Annotate ...@@ -168,84 +209,95 @@ module Annotate
end end
option_parser.on('--ignore-model-subdirects', option_parser.on('--ignore-model-subdirects',
"Ignore subdirectories of the models directory") do |_dir| "Ignore subdirectories of the models directory") do
env['ignore_model_sub_dir'] = 'yes' env['ignore_model_sub_dir'] = 'yes'
end end
option_parser.on('--sort', option_parser.on('--sort',
"Sort columns alphabetically, rather than in creation order") do |_dir| "Sort columns alphabetically, rather than in creation order") do
env['sort'] = 'yes' env['sort'] = 'yes'
end end
option_parser.on('--classified-sort', option_parser.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir| "Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do
env['classified_sort'] = 'yes' env['classified_sort'] = 'yes'
end end
option_parser.on('-R', '--require path', option_parser.on('-R',
'--require path',
"Additional file to require before loading models, may be used multiple times") do |path| "Additional file to require before loading models, may be used multiple times") do |path|
env['require'] = if env['require'].present? env['require'] = if env['require'].present?
env['require'] + ",#{path}" "#{env['require']},#{path}"
else else
path path
end end
end end
option_parser.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions| option_parser.on('-e',
'--exclude [tests,fixtures,factories,serializers]',
Array,
"Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= EXCLUSION_LIST exclusions ||= EXCLUSION_LIST
exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' } exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' }
end end
option_parser.on('-f', '--format [bare|rdoc|yard|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |fmt| option_parser.on('-f',
env["format_#{fmt}"] = 'yes' '--format [bare|rdoc|yard|markdown]',
FORMAT_TYPES,
'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |format_type|
env["format_#{format_type}"] = 'yes'
end end
option_parser.on('--force', 'Force new annotations even if there are no changes.') do |_force| option_parser.on('--force',
'Force new annotations even if there are no changes.') do
env['force'] = 'yes' env['force'] = 'yes'
end end
option_parser.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do option_parser.on('--frozen',
'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
env['frozen'] = 'yes' env['frozen'] = 'yes'
end end
option_parser.on('--timestamp', 'Include timestamp in (routes) annotation') do option_parser.on('--timestamp',
'Include timestamp in (routes) annotation') do
env['timestamp'] = 'true' env['timestamp'] = 'true'
end end
option_parser.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value| option_parser.on('--trace',
'If unable to annotate a file, print the full stack trace, not just the exception message.') do
env['trace'] = 'yes' env['trace'] = 'yes'
end end
option_parser.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex| option_parser.on('-I',
'--ignore-columns REGEX',
"don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
env['ignore_columns'] = regex env['ignore_columns'] = regex
end end
option_parser.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex| option_parser.on('--ignore-routes REGEX',
"don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
env['ignore_routes'] = regex env['ignore_routes'] = regex
end end
option_parser.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values| option_parser.on('--hide-limit-column-types VALUES',
"don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
env['hide_limit_column_types'] = values.to_s env['hide_limit_column_types'] = values.to_s
end end
option_parser.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values| option_parser.on('--hide-default-column-types VALUES',
"don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
env['hide_default_column_types'] = values.to_s env['hide_default_column_types'] = values.to_s
end end
option_parser.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values| option_parser.on('--ignore-unknown-models',
"don't display warnings for bad model files") do
env['ignore_unknown_models'] = 'true' env['ignore_unknown_models'] = 'true'
end end
option_parser.on('--with-comment', "include database comments in model annotations") do |_values| option_parser.on('--with-comment',
"include database comments in model annotations") do
env['with_comment'] = 'true' env['with_comment'] = 'true'
end end
end end
def default_options
{
target_action: :do_annotations,
exit: false
}
end
end end
end end
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