From 7c2d14bcdd2b844edd2d67f5524e1062659f6a9f Mon Sep 17 00:00:00 2001 From: djsegal Date: Sun, 22 Nov 2015 03:09:42 -0500 Subject: [PATCH 1/2] Add root_dir capability for multiple roots --- bin/annotate | 5 + lib/annotate.rb | 6 +- lib/annotate/annotate_models.rb | 106 +++++++++++------- .../templates/auto_annotate_models.rake | 1 + lib/tasks/annotate_models.rake | 2 + 5 files changed, 76 insertions(+), 44 deletions(-) diff --git a/bin/annotate b/bin/annotate index adab99520..8be355d8f 100755 --- a/bin/annotate +++ b/bin/annotate @@ -129,6 +129,11 @@ OptionParser.new do |opts| ENV['model_dir'] = dir end + opts.on('--root-dir dir', + "Annotate files stored within root dir projects, separate multiple dirs with comas") do |dir| + ENV['root_dir'] = dir + end + opts.on('--ignore-model-subdirects', "Ignore subdirectories of the models directory") do |dir| ENV['ignore_model_sub_dir'] = "yes" diff --git a/lib/annotate.rb b/lib/annotate.rb index cbcabebb9..1c38e4526 100755 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -33,7 +33,7 @@ module Annotate :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper ] PATH_OPTIONS=[ - :require, :model_dir + :require, :model_dir, :root_dir ] @@ -76,6 +76,10 @@ def self.setup_options(options = {}) options[:model_dir] = ['app/models'] end + if(options[:root_dir].empty?) + options[:root_dir] = ['/'] + end + options[:wrapper_open] ||= options[:wrapper] options[:wrapper_close] ||= options[:wrapper] diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index e5e8fe5e1..b085fde96 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -44,46 +44,6 @@ module AnnotateModels SERIALIZERS_TEST_DIR = File.join("test", "serializers") SERIALIZERS_SPEC_DIR = File.join("spec", "serializers") - - TEST_PATTERNS = [ - File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"), - File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"), - File.join(SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"), - ] - - FIXTURE_PATTERNS = [ - File.join(FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"), - File.join(FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"), - File.join(FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"), - File.join(FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml"), - ] - - SCAFFOLD_PATTERNS = [ - File.join(CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"), - File.join(CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"), - File.join(REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"), - File.join(ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb"), - ] - - FACTORY_PATTERNS = [ - File.join(EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"), - File.join(EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"), - File.join(BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"), - File.join(BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"), - File.join(FACTORY_GIRL_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style) - File.join(FACTORY_GIRL_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style) - File.join(FACTORY_GIRL_TEST_DIR, "%TABLE_NAME%.rb"), # (new style) - File.join(FACTORY_GIRL_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style) - File.join(FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"), - File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"), - ] - - SERIALIZER_PATTERNS = [ - File.join(SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"), - File.join(SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"), - File.join(SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb") - ] - # Don't show limit (#) on these column types # Example: show "integer" instead of "integer(4)" NO_LIMIT_COL_TYPES = ["integer", "boolean"] @@ -97,6 +57,64 @@ def model_dir=(dir) @model_dir = dir end + def root_dir + @root_dir.is_a?(Array) ? @root_dir : [@root_dir || "/"] + end + + def root_dir=(dir) + @root_dir = dir + end + + def get_patterns(pattern_types) + current_patterns = [] + root_dir.each do |root_directory| + Array(pattern_types).each do |pattern_type| + current_patterns += case pattern_type + when 'test' + [ + File.join(root_directory, UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"), + File.join(root_directory, MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"), + File.join(root_directory, SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"), + ] + when 'fixture' + [ + File.join(root_directory, FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"), + File.join(root_directory, FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"), + File.join(root_directory, FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"), + File.join(root_directory, FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml"), + ] + when 'scaffold' + [ + File.join(root_directory, CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"), + File.join(root_directory, CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"), + File.join(root_directory, REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"), + File.join(root_directory, ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb"), + ] + when 'factory' + [ + File.join(root_directory, EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"), + File.join(root_directory, EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"), + File.join(root_directory, BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"), + File.join(root_directory, BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"), + File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style) + File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style) + File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%TABLE_NAME%.rb"), # (new style) + File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style) + File.join(root_directory, FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"), + File.join(root_directory, FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"), + ] + when 'serializer' + [ + File.join(root_directory, SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"), + File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"), + File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb") + ] + end + end + end + return current_patterns.map{ |p| p.sub(/^[\/]*/, '') } + end + # Simple quoting for the default column value def quote(value) case value @@ -373,11 +391,11 @@ def annotate(klass, file, header, options={}) %w(test fixture factory serializer scaffold).each do |key| exclusion_key = "exclude_#{key.pluralize}".to_sym - patterns_constant = "#{key.upcase}_PATTERNS".to_sym + patterns_type = "#{key.downcase}" position_key = "position_in_#{key}".to_sym unless options[exclusion_key] - did_annotate = self.const_get(patterns_constant). + did_annotate = self.get_patterns(patterns_type). map { |file| resolve_filename(file, model_name, table_name) }. map { |file| annotate_one_file(file, info, position_key, options_with_position(options, position_key)) }. detect { |result| result } || did_annotate @@ -483,6 +501,7 @@ def do_annotations(options={}) end self.model_dir = options[:model_dir] if options[:model_dir] + self.root_dir = options[:root_dir] if options[:root_dir] annotated = [] get_model_files(options).each do |file| @@ -512,6 +531,7 @@ def annotate_model_file(annotated, file, header, options) def remove_annotations(options={}) self.model_dir = options[:model_dir] if options[:model_dir] + self.root_dir = options[:root_dir] if options[:root_dir] deannotated = [] deannotated_klass = false get_model_files(options).each do |file| @@ -524,7 +544,7 @@ def remove_annotations(options={}) model_file_name = file deannotated_klass = true if(remove_annotation_of_file(model_file_name)) - (TEST_PATTERNS + SCAFFOLD_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS). + (get_patterns(%w[test scaffold fixture factory serializer])). map { |file| resolve_filename(file, model_name, table_name) }. each do |file| if File.exist?(file) diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index 63df1907c..c027f3bb8 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -16,6 +16,7 @@ if Rails.env.development? 'show_indexes' => 'true', 'simple_indexes' => 'false', 'model_dir' => 'app/models', + 'root_dir' => '/', 'include_version' => 'false', 'require' => '', 'exclude_tests' => 'false', diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index ff51b9a34..f79728e79 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -21,6 +21,7 @@ task :annotate_models => :environment do options[:show_indexes] = Annotate.true?(ENV['show_indexes']) options[:simple_indexes] = Annotate.true?(ENV['simple_indexes']) options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models'] + options[:root_dir] = ENV['root_dir'] ? ENV['root_dir'].split(',') : ['/'] options[:include_version] = Annotate.true?(ENV['include_version']) options[:require] = ENV['require'] ? ENV['require'].split(',') : [] options[:exclude_tests] = Annotate.true?(ENV['exclude_tests']) @@ -48,6 +49,7 @@ task :remove_annotation => :environment do options={ :is_rake => true } options[:model_dir] = ENV['model_dir'] + options[:root_dir] = ENV['root_dir'] options[:require] = ENV['require'] ? ENV['require'].split(',') : [] options[:trace] = Annotate.true?(ENV['trace']) AnnotateModels.remove_annotations(options) From 175c2a04012a6ed1e97f68d5ce3e5d83927f7f68 Mon Sep 17 00:00:00 2001 From: djsegal Date: Sun, 22 Nov 2015 04:08:04 -0500 Subject: [PATCH 2/2] Annotate controllers for models: #293 --- lib/annotate.rb | 2 +- lib/annotate/annotate_models.rb | 12 ++++++++++-- .../annotate/templates/auto_annotate_models.rake | 1 + lib/tasks/annotate_models.rake | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/annotate.rb b/lib/annotate.rb index 1c38e4526..1e62c56eb 100755 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -27,7 +27,7 @@ module Annotate :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys, - :exclude_scaffolds + :exclude_scaffolds, :exclude_controllers ] OTHER_OPTIONS=[ :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index b085fde96..8ae1b38be 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -44,6 +44,9 @@ module AnnotateModels SERIALIZERS_TEST_DIR = File.join("test", "serializers") SERIALIZERS_SPEC_DIR = File.join("spec", "serializers") + # Controller files + CONTROLLER_DIR = File.join("app", "controllers") + # Don't show limit (#) on these column types # Example: show "integer" instead of "integer(4)" NO_LIMIT_COL_TYPES = ["integer", "boolean"] @@ -109,6 +112,10 @@ def get_patterns(pattern_types) File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"), File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb") ] + when 'controller' + [ + File.join(root_directory, CONTROLLER_DIR, "%PLURALIZED_MODEL_NAME%_controller.rb") + ] end end end @@ -376,6 +383,7 @@ def remove_annotation_of_file(file_name) # :exclude_factories:: whether to skip modification of factory files # :exclude_serializers:: whether to skip modification of serializer files # :exclude_scaffolds:: whether to skip modification of scaffold files + # :exclude_controllers:: whether to skip modification of controller files # def annotate(klass, file, header, options={}) begin @@ -389,7 +397,7 @@ def annotate(klass, file, header, options={}) did_annotate = true end - %w(test fixture factory serializer scaffold).each do |key| + %w(test fixture factory serializer scaffold controller).each do |key| exclusion_key = "exclude_#{key.pluralize}".to_sym patterns_type = "#{key.downcase}" position_key = "position_in_#{key}".to_sym @@ -544,7 +552,7 @@ def remove_annotations(options={}) model_file_name = file deannotated_klass = true if(remove_annotation_of_file(model_file_name)) - (get_patterns(%w[test scaffold fixture factory serializer])). + (get_patterns(%w[test scaffold fixture factory serializer controller])). map { |file| resolve_filename(file, model_name, table_name) }. each do |file| if File.exist?(file) diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index c027f3bb8..2bb6efb53 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -24,6 +24,7 @@ if Rails.env.development? 'exclude_factories' => 'false', 'exclude_serializers' => 'false', 'exclude_scaffolds' => 'false', + 'exclude_controllers' => 'true', 'ignore_model_sub_dir' => 'false', 'skip_on_db_migrate' => 'false', 'format_bare' => 'true', diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index f79728e79..e93a21f78 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -29,6 +29,7 @@ task :annotate_models => :environment do options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures']) options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers']) options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds']) + options[:exclude_controllers] = Annotate.true?(ENV['exclude_controllers']) options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir']) options[:format_bare] = Annotate.true?(ENV['format_bare']) options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])