Skip to content
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

Suggestion: run recipes with arguments #345

Closed
ferblaca opened this issue Apr 29, 2022 · 4 comments
Closed

Suggestion: run recipes with arguments #345

ferblaca opened this issue Apr 29, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@ferblaca
Copy link

ferblaca commented Apr 29, 2022

It would be great to be able to run recipes that accept parameters by giving them value with arguments.

For example the recipe org.openrewrite.java.ChangePackage that accepts oldPackageName, newPackageName and recursive as parameters could be executed for example:

mvn org.openrewrite.maven:rewrite-maven-plugin:4.23.0:run-Drewrite.recipeArtifactCoordinates=org.openrewrite:rewrite-java:7.22.0 -Drewrite.activeRecipes=org.openrewrite.java.ChangePackage -Drewrite.oldPackageName='org.package1' -Drewrite.newPackageName='org.package2' -Drewrite.recursive=true

Is there a way to add value to parameters dynamically without having to do it declaratively?

thank you in advance!

@tkvangorder
Copy link
Contributor

hi @ferblaca

Thanks for the suggestion. I think the problem with using system properties to pass arguments is that they would still need to enforce some sort of recipe namespace because many times the active recipe may actually be a composite (which could then include other composite recipes). It is also possible to use the same recipe multiple times with different parameters as with this example

We would be interested in hearing any troubles you are having with the declarative approach.

@tkvangorder tkvangorder added the question Further information is requested label Apr 29, 2022
@ferblaca
Copy link
Author

ferblaca commented May 2, 2022

hi @tkvangorder!! thank you very much for your quick reply 👍

For example I would like to configure a main recipe to call other recipes depending on an input parameter.

For example, if I have a recipe like this:

public class MainRecipe extends Recipe {

  private static final ComparableVersion version400 = new ComparableVersion("4.0.0");

  private static final ComparableVersion version410 = new ComparableVersion("4.1.0");

  private static final ComparableVersion version430 = new ComparableVersion("4.3.0");

  @Option(displayName = "version from", description = "version to migrate from.", required = false,
      example = "4.0.0")
  private final String versionFrom;

  @JsonCreator
  public MainRecipe(
      @JsonProperty("versionFrom") final String versionFrom) {

    ComparableVersion fromVersion = new ComparableVersion(versionFrom);


    if (fromVersion.compareTo(version400) < 0) {
      this.doNext(new Migration_to_400_Recipe(true));
    }

    if (fromVersion.compareTo(version410) < 0) {
      this.doNext(new Migration_to_410_Recipe(true));
    }

    if (fromVersion.compareTo(version430) < 0) {
      this.doNext(new Migration_to_430_Recipe(true));
    }

  }
}

Depending on the value of the parameter versionFrom some recipes will be executed or others, but it would be interesting that this value could be configured dynamically with the execution of the recipe through the plugin, without having to set this value in a declarative way in a file.

@StWitte
Copy link

StWitte commented Sep 15, 2023

I have a slightly different use case for using command line arguments.

Given a ChangeTagValue (as an alternative way of changing the pom project version)

  - org.openrewrite.xml.ChangeTagValue:
      elementName: /project/version
      newValue: 8.0.1-SNAPSHOT

I would like to be able to pass the newVersion as a command line parameter.
Something like: mvn .... -DnewValue=8.0.1-SNAPSHOT

  - org.openrewrite.xml.ChangeTagValue:
      elementName: /project/version
      newValue: ${newValue}

@timtebeek
Copy link
Contributor

We now recommend folks use the Moderne CLI when there's a desire to set run recipe options.

Option name Description
-P, --recipe-option Recipe options, if any. If a recipe accepts more than one option, you can include this argument multiple times.

Folks that use the Maven and Gradle plugins can continue to use declarative yaml recipes in rewrite.yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

4 participants