AppendToTextFile: support glob patterns for the relative file name#7417
Merged
AppendToTextFile: support glob patterns for the relative file name#7417
AppendToTextFile: support glob patterns for the relative file name#7417Conversation
Matching is now delegated to `PathMatcher` with `glob:` syntax, consistent with `DeleteSourceFiles`, `RenameFile`, `MoveFile`, and `SetFilePermissions`. When the pattern contains glob metacharacters no new file is generated, since a glob does not specify a concrete path to create.
Jenson3210
approved these changes
Apr 17, 2026
| @Override | ||
| public Collection<PlainText> generate(AtomicBoolean fileExists, Collection<SourceFile> generatedInThisCycle, ExecutionContext ctx) { | ||
| // A glob can match multiple existing files but doesn't specify a concrete path to create. | ||
| if (relativeFileName.matches(".*[*?\\[{].*")) { |
Contributor
There was a problem hiding this comment.
this might not match on files like backstage nunjuck templates as these contain {{varname}}.
Also not sure if the [] characters might not be misleading here. But probably overthinking this and will be fine in 99% of the cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AppendToTextFilenow accepts a glob expression (e.g.**/*.txt) asrelativeFileName, letting a single recipe invocation append to many existing files.PathMatcherwithglob:syntax, aligning with the convention inDeleteSourceFiles,RenameFile,MoveFile, andSetFilePermissions. A literal path still works because it matches itself as a glob.PlainTextcast withinstanceof, avoiding aClassCastExceptionwhen a glob matches a non-plaintext file.Test plan
./gradlew :rewrite-core:test --tests "org.openrewrite.text.AppendToTextFileTest"— newGlobnested class covers multi-file match, non-creation for globs, and skipping non-matching files.