Skip to content

AppendToTextFile: support glob patterns for the relative file name#7417

Merged
timtebeek merged 1 commit intomainfrom
tim/append-glob
Apr 18, 2026
Merged

AppendToTextFile: support glob patterns for the relative file name#7417
timtebeek merged 1 commit intomainfrom
tim/append-glob

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

  • AppendToTextFile now accepts a glob expression (e.g. **/*.txt) as relativeFileName, letting a single recipe invocation append to many existing files.
  • Matching is delegated to PathMatcher with glob: syntax, aligning with the convention in DeleteSourceFiles, RenameFile, MoveFile, and SetFilePermissions. A literal path still works because it matches itself as a glob.
  • When the pattern contains glob metacharacters no new file is generated (a glob does not specify a concrete path to create); literal paths preserve the existing create-if-missing behavior. The visitor also now guards its PlainText cast with instanceof, avoiding a ClassCastException when a glob matches a non-plaintext file.

Test plan

  • ./gradlew :rewrite-core:test --tests "org.openrewrite.text.AppendToTextFileTest" — new Glob nested class covers multi-file match, non-creation for globs, and skipping non-matching files.

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.
@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(".*[*?\\[{].*")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Apr 17, 2026
@timtebeek timtebeek merged commit 5a3cbe7 into main Apr 18, 2026
1 check passed
@timtebeek timtebeek deleted the tim/append-glob branch April 18, 2026 10:25
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants