annotaterb icon indicating copy to clipboard operation
annotaterb copied to clipboard

Enhance `--model-dir` to support glob patterns

Open ether-moon opened this issue 8 months ago • 1 comments

This PR enhances the --model-dir option in the models command to support glob patterns, providing more flexibility in specifying model directories.

Problem:

Currently, the --model-dir option only accepts a comma-separated list of explicit directory paths. This can be cumbersome in projects with complex or non-standard directory structures, such as Packwerk-style modular monoliths or other setups using multiple directories under an apps/ or components/ folder. Users cannot easily target multiple model directories scattered across different components using wildcard patterns without listing each one individually.

Solution:

This PR modifies the AnnotateRb::ModelAnnotator::ModelFilesGetter class to:

  1. Treat each entry provided to --model-dir as a potential glob pattern.
  2. Use Dir.glob to find all directories matching the provided patterns.
  3. Search for .rb files within each of the matched directories (respecting the --ignore-model-subdirects option).

This allows users to specify directories like components/*/app/models or apps/**/models to include models from various locations within the project structure, significantly simplifying configuration for modular applications.

Changes Included:

  • Updated lib/annotate_rb/model_annotator/model_files_getter.rb to implement glob pattern handling.
  • Added new RSpec tests in spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb specifically covering various glob pattern scenarios (simple star, recursive glob, combination with regular paths, interaction with --ignore-model-subdirects, handling non-matching patterns, and interaction with specified file arguments).
  • Updated existing RSpec tests to ensure compatibility and correct error handling for invalid/non-existent paths.
  • Updated README.md to document the new glob pattern support for the --model-dir option.

Testing:

All existing and newly added RSpec tests for ModelFilesGetter pass, ensuring the new functionality works as expected and does not break existing behavior.

ether-moon avatar Apr 23 '25 07:04 ether-moon

OMG... I worked on the same task without knowing there was a PR #198 😂

ether-moon avatar Apr 23 '25 08:04 ether-moon

@ether-moon I just merged in #198, and will by deploying a new version soon. Please test it and let me know if this needs to be reviewed after

drwl avatar May 30 '25 19:05 drwl

@ether-moon Sorry that this ended up being duplicate work. I'm going to close this for now, but feel free to re-open if it still hasn't been addressed.

drwl avatar Jun 16 '25 00:06 drwl