openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.28k stars 339 forks source link

Allow partial update of maven plugin configuration #3810

Open gauee opened 11 months ago

gauee commented 11 months ago

What problem are you trying to solve?

Partial update of maven plugin configuration

What precondition(s) should be checked before applying this recipe?

N/A

Describe the situation before applying the recipe

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>${swagger-codegen-maven-plugin-version}</version>
  <configuration>
    <inputSpec>${project.basedir}/src/main/resources/yaml/yamlfilename.yaml</inputSpec>
    <language>com.my.package.for.v1.GeneratorLanguage</language>
    <templateDirectory>myTemplateDir</templateDirectory>
    <output>${project.build.directory}/generated-sources</output>
    <apiPackage>${default.package}.handler</apiPackage>
    <modelPackage>${default.package}.model</modelPackage>
    <invokerPackage>${default.package}.handler</invokerPackage>
  </configuration>
</plugin>

Describe the situation after applying the recipe

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>${swagger-codegen-maven-plugin-version}</version>
  <configuration>
    <inputSpec>${project.basedir}/src/main/resources/yaml/yamlfilename.yaml</inputSpec>
    <language>com.my.package.for.v2.GeneratorLanguage</language>
    <templateDirectory>myTemplateDir</templateDirectory>
    <output>${project.build.directory}/generated-sources</output>
    <apiPackage>${default.package}.handler</apiPackage>
    <modelPackage>${default.package}.model</modelPackage>
    <invokerPackage>${default.package}.handler</invokerPackage>
  </configuration>
</plugin>

Have you considered any alternatives or workarounds?

Reuse existing ChangePluginConfiguration recipe for straightforward/static configuration.

Any additional context

The tag hierarchy could change between plugin releases or when migrating from one to another plugin. So configuration elements could be customized by the teams and should be untouched. Looking for simple add/remove capabilities for tags under plugin configuration, so partial xml can be injected/removed.

Are you interested in contributing this recipe to OpenRewrite?

yes

timtebeek commented 11 months ago

Hi @gauee, good seeing you here! Do I understand correctly you'd want to alter, or provide an alternative to, the existing ChangePluginConfiguration recipe, such that you can alter rather than completely override the plugin configuration?

Such that in your example above you end up with this change without having to specify all other configuration tags?

  <plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>${swagger-codegen-maven-plugin-version}</version>
    <configuration>
      <inputSpec>${project.basedir}/src/main/resources/yaml/yamlfilename.yaml</inputSpec>
-     <language>com.my.package.for.v1.GeneratorLanguage</language>
+     <language>com.my.package.for.v2.GeneratorLanguage</language>
      <templateDirectory>myTemplateDir</templateDirectory>
      <output>${project.build.directory}/generated-sources</output>
      <apiPackage>${default.package}.handler</apiPackage>
      <modelPackage>${default.package}.model</modelPackage>
      <invokerPackage>${default.package}.handler</invokerPackage>
    </configuration>
  </plugin>
timtebeek commented 11 months ago

Implementation wise I'd think we can add a Boolean merge option to ChangePluginConfiguration that when set to TRUE instead of overwriting all existing configuration, merges in any new values, and only overrides when there's a match. Does that sound doable and as something you'd like to pick up @gauee ?

shanman190 commented 11 months ago

A short term workaround might be to just use the org.openrewrite.xml.ChangeTagValue recipe which should be able to handle this particular change already.

gauee commented 11 months ago

@timtebeek @shanman190 thank for an update! I will explore the recipes for xml transformations (https://docs.openrewrite.org/recipes/xml).

I have also the case when with maven plugin migration I had to change the configuration tag names like below.

                 <configuration>
-                    <environmentVariables>
+                    <configOptions>
                         <interfaceOnly>true</interfaceOnly>
-                    </environmentVariables>
+                    </configOptions>
                     <configurationFile>config.json</configurationFile>
                     <inputSpec>inputSpec.yaml</inputSpec>
                 </configuration>

I will check on multimodule project how org.openrewrite.xml.ChangeTagName handles it.