Commit 46dc0846 by Cuong Tran

Merge pull request #226 from datemyschool/annotate_multiple_dir

Add posibility to define model_dir as an array of directorie paths
parents c40faf4a b2fbd90a
...@@ -176,7 +176,7 @@ you can do so with a simple environment variable, instead of editing the ...@@ -176,7 +176,7 @@ you can do so with a simple environment variable, instead of editing the
-m, --show-migration Include the migration version number in the annotation -m, --show-migration Include the migration version number in the annotation
-i, --show-indexes List the table's database indexes in the annotation -i, --show-indexes List the table's database indexes in the annotation
-s, --simple-indexes Concat the column's related indexes in the annotation -s, --simple-indexes Concat the column's related indexes in the annotation
--model-dir dir Annotate model files stored in dir rather than app/models --model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with comas
--ignore-model-subdirects Ignore subdirectories of the models directory --ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order --sort Sort columns alphabetically, rather than in creation order
-R, --require path Additional file to require before loading models, may be used multiple times -R, --require path Additional file to require before loading models, may be used multiple times
......
...@@ -120,7 +120,7 @@ OptionParser.new do |opts| ...@@ -120,7 +120,7 @@ OptionParser.new do |opts|
end end
opts.on('--model-dir dir', opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models") do |dir| "Annotate model files stored in dir rather than app/models, separate multiple dirs with comas") do |dir|
ENV['model_dir'] = dir ENV['model_dir'] = dir
end end
......
...@@ -27,10 +27,10 @@ module Annotate ...@@ -27,10 +27,10 @@ module Annotate
:timestamp, :exclude_serializers :timestamp, :exclude_serializers
] ]
OTHER_OPTIONS=[ OTHER_OPTIONS=[
:model_dir, :ignore_columns :ignore_columns
] ]
PATH_OPTIONS=[ PATH_OPTIONS=[
:require, :require, :model_dir
] ]
...@@ -70,7 +70,7 @@ module Annotate ...@@ -70,7 +70,7 @@ module Annotate
end end
if(!options[:model_dir]) if(!options[:model_dir])
options[:model_dir] = 'app/models' options[:model_dir] = ['app/models']
end end
return options return options
...@@ -111,11 +111,13 @@ module Annotate ...@@ -111,11 +111,13 @@ module Annotate
klass.eager_load! klass.eager_load!
end end
else else
FileList["#{options[:model_dir]}/**/*.rb"].each do |fname| options[:model_dir].each do |dir|
FileList["#{dir}/**/*.rb"].each do |fname|
require File.expand_path(fname) require File.expand_path(fname)
end end
end end
end end
end
def self.bootstrap_rake def self.bootstrap_rake
begin begin
......
...@@ -73,7 +73,7 @@ module AnnotateModels ...@@ -73,7 +73,7 @@ module AnnotateModels
class << self class << self
def model_dir def model_dir
@model_dir || "app/models" @model_dir.is_a?(Array) ? @model_dir : [@model_dir || "app/models"]
end end
def model_dir=(dir) def model_dir=(dir)
...@@ -311,7 +311,7 @@ module AnnotateModels ...@@ -311,7 +311,7 @@ module AnnotateModels
did_annotate = false did_annotate = false
model_name = klass.name.underscore model_name = klass.name.underscore
table_name = klass.table_name table_name = klass.table_name
model_file_name = File.join(model_dir, file) model_file_name = File.join(file)
if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class)) if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
did_annotate = true did_annotate = true
...@@ -357,15 +357,17 @@ module AnnotateModels ...@@ -357,15 +357,17 @@ module AnnotateModels
models.reject!{|m| m.match(/^(.*)=/)} models.reject!{|m| m.match(/^(.*)=/)}
if models.empty? if models.empty?
begin begin
Dir.chdir(model_dir) do model_dir.each do |dir|
models = if options[:ignore_model_sub_dir] Dir.chdir(dir) do
Dir["*.rb"] models.concat( if options[:ignore_model_sub_dir]
Dir["*.rb"].map{ |f| [dir, f] }
else else
Dir["**/*.rb"].reject{ |f| f["concerns/"] } Dir["**/*.rb"].reject{ |f| f["concerns/"] }.map{ |f| [dir, f] }
end)
end end
end end
rescue SystemCallError rescue SystemCallError
puts "No models found in directory '#{model_dir}'." puts "No models found in directory '#{model_dir.join("', '")}'."
puts "Either specify models on the command line, or use the --model-dir option." puts "Either specify models on the command line, or use the --model-dir option."
puts "Call 'annotate --help' for more info." puts "Call 'annotate --help' for more info."
exit 1 exit 1
...@@ -379,11 +381,12 @@ module AnnotateModels ...@@ -379,11 +381,12 @@ module AnnotateModels
# in subdirectories without namespacing. # in subdirectories without namespacing.
def get_model_class(file) def get_model_class(file)
model_path = file.gsub(/\.rb$/, '') model_path = file.gsub(/\.rb$/, '')
model_dir.each { |dir| model_path = model_path.gsub(/^#{dir}/, '').gsub(/^\//, '') }
begin begin
get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}") get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}")
rescue LoadError rescue LoadError
# this is for non-rails projects, which don't get Rails auto-require magic # this is for non-rails projects, which don't get Rails auto-require magic
if Kernel.require(File.expand_path("#{model_dir}/#{model_path}")) if Kernel.require(file)
retry retry
elsif model_path.match(/\//) elsif model_path.match(/\//)
model_path = model_path.split('/')[1..-1].join('/').to_s model_path = model_path.split('/')[1..-1].join('/').to_s
...@@ -428,10 +431,10 @@ module AnnotateModels ...@@ -428,10 +431,10 @@ module AnnotateModels
annotated = [] annotated = []
get_model_files(options).each do |file| get_model_files(options).each do |file|
annotate_model_file(annotated, file, header, options) annotate_model_file(annotated, File.join(file), header, options)
end end
if annotated.empty? if annotated.empty?
puts "Nothing annotated." puts "Nothing to annotate."
else else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}" puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end end
...@@ -456,12 +459,13 @@ module AnnotateModels ...@@ -456,12 +459,13 @@ module AnnotateModels
deannotated = [] deannotated = []
deannotated_klass = false deannotated_klass = false
get_model_files(options).each do |file| get_model_files(options).each do |file|
file = File.join(file)
begin begin
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
model_name = klass.name.underscore model_name = klass.name.underscore
table_name = klass.table_name table_name = klass.table_name
model_file_name = File.join(model_dir, file) model_file_name = file
deannotated_klass = true if(remove_annotation_of_file(model_file_name)) deannotated_klass = true if(remove_annotation_of_file(model_file_name))
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS). (TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
...@@ -475,7 +479,7 @@ module AnnotateModels ...@@ -475,7 +479,7 @@ module AnnotateModels
end end
deannotated << klass if(deannotated_klass) deannotated << klass if(deannotated_klass)
rescue Exception => e rescue Exception => e
puts "Unable to deannotate #{file}: #{e.message}" puts "Unable to deannotate #{File.join(file)}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
end end
......
...@@ -18,7 +18,7 @@ task :annotate_models => :environment do ...@@ -18,7 +18,7 @@ task :annotate_models => :environment do
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position']) options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
options[:show_indexes] = Annotate.true?(ENV['show_indexes']) options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes']) options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
options[:model_dir] = ENV['model_dir'] options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : []
options[:include_version] = Annotate.true?(ENV['include_version']) options[:include_version] = Annotate.true?(ENV['include_version'])
options[:require] = ENV['require'] ? ENV['require'].split(',') : [] options[:require] = ENV['require'] ? ENV['require'].split(',') : []
options[:exclude_tests] = Annotate.true?(ENV['exclude_tests']) options[:exclude_tests] = Annotate.true?(ENV['exclude_tests'])
......
...@@ -136,9 +136,8 @@ EOS ...@@ -136,9 +136,8 @@ EOS
# todo: use 'files' gem instead # todo: use 'files' gem instead
def create(file, body="hi") def create(file, body="hi")
file_path = File.join(AnnotateModels.model_dir, file) file_path = File.join(AnnotateModels.model_dir[0], file)
FileUtils.mkdir_p(File.dirname(file_path)) FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, "wb") do |f| File.open(file_path, "wb") do |f|
f.puts(body) f.puts(body)
end end
...@@ -146,7 +145,7 @@ EOS ...@@ -146,7 +145,7 @@ EOS
end end
def check_class_name(file, class_name) def check_class_name(file, class_name)
klass = AnnotateModels.get_model_class(file) klass = AnnotateModels.get_model_class(File.join(AnnotateModels.model_dir[0], file))
expect(klass).not_to eq(nil) expect(klass).not_to eq(nil)
expect(klass.name).to eq(class_name) expect(klass.name).to eq(class_name)
...@@ -294,7 +293,7 @@ EOS ...@@ -294,7 +293,7 @@ EOS
CONSTANT = 1 CONSTANT = 1
end end
EOS EOS
path = File.expand_path("#{AnnotateModels.model_dir}/loaded_class") path = File.expand_path('loaded_class', AnnotateModels.model_dir[0])
Kernel.load "#{path}.rb" Kernel.load "#{path}.rb"
expect(Kernel).not_to receive(:require).with(path) expect(Kernel).not_to receive(:require).with(path)
...@@ -557,7 +556,7 @@ end ...@@ -557,7 +556,7 @@ end
it "displays an error message" do it "displays an error message" do
expect(capturing(:stdout) { expect(capturing(:stdout) {
AnnotateModels.do_annotations :model_dir => @model_dir, :is_rake => true AnnotateModels.do_annotations :model_dir => @model_dir, :is_rake => true
}).to include("Unable to annotate user.rb: oops") }).to include("Unable to annotate #{@model_dir}/user.rb: oops")
end end
it "displays the full stack trace with --trace" do it "displays the full stack trace with --trace" do
...@@ -587,7 +586,7 @@ end ...@@ -587,7 +586,7 @@ end
it "displays an error message" do it "displays an error message" do
expect(capturing(:stdout) { expect(capturing(:stdout) {
AnnotateModels.remove_annotations :model_dir => @model_dir, :is_rake => true AnnotateModels.remove_annotations :model_dir => @model_dir, :is_rake => true
}).to include("Unable to deannotate user.rb: oops") }).to include("Unable to deannotate #{@model_dir}/user.rb: oops")
end end
it "displays the full stack trace" do it "displays the full stack trace" 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