Skip to content

Enhance --model-dir to support glob patterns #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ether-moon
Copy link

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.

Allows the `model_dir` option to accept glob patterns, enabling more flexible model directory specification.
It also improves error messages when specified files are not found in the matched directories.
The changes ensure correct file path resolution when using glob patterns and relative paths.
@ether-moon
Copy link
Author

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

@drwl
Copy link
Owner

drwl commented May 30, 2025

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants