Skip to content

Commit

Permalink
Support the glob pattern in root_dir and model_dir
Browse files Browse the repository at this point in the history
We often want to specify multiple directories in `root_dir` and `model_dir`.
For example, when using engines, packs-rails, etc.

* https://github.com/rubyatscale/packs-rails
* https://guides.rubyonrails.org/engines.html

This commit supports the glob pattern with these options, making it easy
to specify multiple directories.

fixes #99
  • Loading branch information
sinsoku committed Feb 27, 2025
1 parent efc56fb commit f6d4773
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/annotate_rb/eager_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ def call(options)
klass.eager_load!
end
else
options[:model_dir].each do |dir|
::Rake::FileList["#{dir}/**/*.rb"].each do |fname|
require File.expand_path(fname)
end
model_files = ModelAnnotator::ModelFilesGetter.call(options)
model_files&.each do |model_file|
require model_file
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions lib/annotate_rb/model_annotator/model_files_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def call(options)

return model_files if model_files.any?

options[:model_dir].each do |dir|
model_dirs = options[:model_dir].flat_map { |model_dir| Dir[model_dir] }
model_dirs.each do |dir|
Dir.chdir(dir) do
list = if options[:ignore_model_sub_dir]
Dir["*.rb"].map { |f| [dir, f] }
Expand All @@ -26,12 +27,14 @@ def call(options)
end
end

model_files
rescue SystemCallError
warn "No models found in directory '#{options[:model_dir].join("', '")}'."
warn "Either specify models on the command line, or use the --model-dir option."
warn "Call 'annotaterb --help' for more info."
# exit 1 # TODO: Return exit code back to caller. Right now it messes up RSpec being able to run
if model_files.empty?
warn "No models found in directory '#{options[:model_dir].join("', '")}'."
warn "Either specify models on the command line, or use the --model-dir option."
warn "Call 'annotaterb --help' for more info."
# exit 1 # TODO: Return exit code back to caller. Right now it messes up RSpec being able to run
else
model_files
end
end

private
Expand Down
5 changes: 4 additions & 1 deletion lib/annotate_rb/model_annotator/pattern_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def initialize(options, pattern_types = [])
def get
current_patterns = []

@options[:root_dir].each do |root_directory|
root_dirs = @options[:root_dir].flat_map do |root_dir|
root_dir.empty? ? root_dir : Dir[root_dir]
end
root_dirs.each do |root_directory|
Array(@pattern_types).each do |pattern_type|
patterns = generate(root_directory, pattern_type)

Expand Down
28 changes: 28 additions & 0 deletions spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,33 @@
expect($stderr.string).to include("No models found in directory")
end
end

context "when `model_dir` is the glob pattern" do
let(:base_options) { {model_dir: ["app/models", "packs/*/app/models"]} }
let(:options) { AnnotateRb::Options.new(base_options, {working_args: []}) }

around do |example|
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
FileUtils.mkdir_p(File.join("app", "models"))
FileUtils.touch(File.join("app", "models", "x.rb"))
FileUtils.mkdir_p(File.join("packs", "foo", "app", "models"))
FileUtils.touch(File.join("packs", "foo", "app", "models", "y.rb"))
FileUtils.mkdir_p(File.join("packs", "bar", "app", "models"))
FileUtils.touch(File.join("packs", "bar", "app", "models", "z.rb"))

example.run
end
end
end

it "returns all model files under directories that matches the glob pattern" do
is_expected.to contain_exactly(
["app/models", "x.rb"],
["packs/bar/app/models", "z.rb"],
["packs/foo/app/models", "y.rb"]
)
end
end
end
end
30 changes: 30 additions & 0 deletions spec/lib/annotate_rb/model_annotator/pattern_getter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,35 @@
])
end
end

context 'when root_dir is the glob pattern' do
let(:base_options) { {root_dir: ["", "packs/*"]} }
let(:pattern_type) { "test" }

around do |example|
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
FileUtils.mkdir_p("packs/foo")
FileUtils.mkdir_p("packs/bar")
FileUtils.touch(File.join(dir, "foo.rb"))
example.run
end
end
end

it "returns patterns with the glob pattern expanded" do
is_expected.to eq([
"test/unit/%MODEL_NAME%_test.rb",
"test/models/%MODEL_NAME%_test.rb",
"spec/models/%MODEL_NAME%_spec.rb",
"packs/bar/test/unit/%MODEL_NAME%_test.rb",
"packs/bar/test/models/%MODEL_NAME%_test.rb",
"packs/bar/spec/models/%MODEL_NAME%_spec.rb",
"packs/foo/test/unit/%MODEL_NAME%_test.rb",
"packs/foo/test/models/%MODEL_NAME%_test.rb",
"packs/foo/spec/models/%MODEL_NAME%_spec.rb"
])
end
end
end
end

0 comments on commit f6d4773

Please sign in to comment.