allegro / allegro/allwrite

Add plugin-aware gating to `AddTomlVersionCatalogPlugin`

Open
#178 0 comments 0 reactions 1 assignee Claimed by @PawelBalcerek View on GitHub
Dominant language
Kotlin
Stars
9
Forks
1
Avg merge
1d 3h
Merged PRs (30d)
19

Description

### Context:

`AddTomlVersionCatalogPlugin` currently adds the configured plugin alias to every applicable Gradle build file and updates the shared version catalog without knowing whether a target plugin (e.g. `application` or `library`) is applied. This prevents the recipe from being safely scoped to selected modules in multi-module builds.

OpenRewrite's existing plugin-detection recipes are not suitable here: `FindPlugins` misses Kotlin DSL bare plugin accessors such as `plugins { application }`, while `ModuleHasPlugin` and its fallback rely on Gradle project markers that allwrite does not produce.

### Proposed solution:

1. Add an internal, reusable `AppliedPluginDetector` in `allwrite-recipes` that determines whether a given plugin is applied in a Gradle build file.
2. Support Kotlin DSL bare and backticked accessors, Kotlin and Groovy `id(...)` declarations, and Kotlin and Groovy `apply` declarations.
3. Restrict detection to build files and avoid false positives for extension blocks, similarly prefixed plugin IDs, and unrelated identifiers.
4. Extend `AddTomlVersionCatalogPlugin` with an optional `onlyIfPluginApplied` plugin ID parameter, defaulting to `null` to preserve existing behavior.
5. When the option is configured, collect the paths of matching build files during scanning, add the plugin alias only to those files, and update the version catalog only when at least one matching file exists.
6. Ensure gated execution never creates a missing `plugins {}` block.
7. Add detector tests and regression coverage for ungated behavior, Kotlin and Groovy syntax, single- and multi-module builds, multiple matching modules, non-matching modules, and files without a `plugins {}` block.
8. Update the recipe documentation with the new option and example, and add `AppliedPluginDetector.kt` to the relevant `AGENTS.md` directory structure.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.