From deb7f462af828ca1da72f45ed0eca49281e94af7 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Wed, 26 Feb 2025 11:07:54 +0100 Subject: [PATCH 01/13] add: add help information for the rewrite command --- cli/src/main/resources/nls/Help.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/src/main/resources/nls/Help.properties b/cli/src/main/resources/nls/Help.properties index 3c7ac3fa87..1b6ea67b06 100644 --- a/cli/src/main/resources/nls/Help.properties +++ b/cli/src/main/resources/nls/Help.properties @@ -83,6 +83,8 @@ cmd.quarkus.detail=Quarkus is a Kubernetes-native Java framework for building cl cmd.repository=Set up pre-configured git repositories using 'ide repository setup ' cmd.repository.detail=Without further arguments this will set up all pre-configured git repositories.\nAlso, you can provide an explicit git repo as `` argument and IDEasy will automatically clone, build and set up your project based on the existing property file.\nRepositories are configured in 'settings/repository/.properties' and can therefore be shared with your project team for automatic or optional setup. cmd.repository.val.repository=The name of the properties file of the pre-configured git repository to set up, omit to set up all active repositories. +cmd.rewrite=Refactor existing code base with specific recipes provided by OpenRewrite +cmd.rewrite.detail=OpenRewrite is a popular tool for refactoring (meta-programming). Detailed documentation can be found at https://docs.openrewrite.org/ cmd.set-edition=Set the edition of the selected tool. cmd.set-edition.detail=This will set the according tool edition variable in your configuration file. If you want to roll out such change and share it with your team, you can commit and push your settings git repository.\nBy default these changes are saved in the project specific settings. Use --conf --home or --workspace to specify otherwise. cmd.set-edition.opt.--cfg=Selection of the configuration file (settings | home | conf | workspace). From 0b18f92fdf9c5021c8c7bb15113e7550d73cf4bf Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Mon, 3 Mar 2025 08:56:30 +0100 Subject: [PATCH 02/13] add: add help information for the rewrite command --- .../ide/commandlet/CommandletManagerImpl.java | 2 + .../tools/ide/tool/rewrite/Rewrite.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java index 3083ae283e..6a3ae994b4 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java @@ -40,6 +40,7 @@ import com.devonfw.tools.ide.tool.oc.Oc; import com.devonfw.tools.ide.tool.pgadmin.PgAdmin; import com.devonfw.tools.ide.tool.quarkus.Quarkus; +import com.devonfw.tools.ide.tool.rewrite.Rewrite; import com.devonfw.tools.ide.tool.sonar.Sonar; import com.devonfw.tools.ide.tool.terraform.Terraform; import com.devonfw.tools.ide.tool.tomcat.Tomcat; @@ -101,6 +102,7 @@ public CommandletManagerImpl(IdeContext context) { add(new Node(context)); add(new Npm(context)); add(new Mvn(context)); + add(new Rewrite(context)); add(new GcViewer(context)); add(new Gradle(context)); add(new Eclipse(context)); diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java b/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java new file mode 100644 index 0000000000..019adcd9f5 --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java @@ -0,0 +1,47 @@ +package com.devonfw.tools.ide.tool.rewrite; + +import com.devonfw.tools.ide.common.Tag; +import com.devonfw.tools.ide.context.IdeContext; +import com.devonfw.tools.ide.step.Step; +import com.devonfw.tools.ide.tool.ToolCommandlet; +import com.devonfw.tools.ide.tool.plugin.PluginBasedCommandlet; +import com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor; + +import java.util.Set; + +/** + * {@link ToolCommandlet} for Rewrite. + */ +public class Rewrite extends PluginBasedCommandlet { + + /** + * The constructor. + * + * @param context the {@link IdeContext}. + * @param tool the {@link #getName() tool name}. + * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method. + */ + public Rewrite(IdeContext context, String tool, Set tags) { + super(context, tool, tags); + } + + /** + * we do not need to do anything because right now we will only call OpenRewrite in the command line form + * @param plugin the {@link ToolPluginDescriptor} to install. + * @param step the {@link Step} for the plugin installation. + */ + @Override + public void installPlugin(ToolPluginDescriptor plugin, Step step) { + //do nothing + } + + /** + * The constructor. + * + * @param context the {@link IdeContext}. + */ + public Rewrite(IdeContext context) { + + super(context, "rewrite", Set.of(Tag.JAVA)); + } +} From 2eecccc2d423476096251de15ced2eec8b08bc3a Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Thu, 6 Mar 2025 09:54:32 +0100 Subject: [PATCH 03/13] add: add help information for the rewrite command --- .../devonfw/tools/ide/commandlet/CommandletManagerImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java index 6a3ae994b4..258bc7a83a 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java @@ -40,7 +40,6 @@ import com.devonfw.tools.ide.tool.oc.Oc; import com.devonfw.tools.ide.tool.pgadmin.PgAdmin; import com.devonfw.tools.ide.tool.quarkus.Quarkus; -import com.devonfw.tools.ide.tool.rewrite.Rewrite; import com.devonfw.tools.ide.tool.sonar.Sonar; import com.devonfw.tools.ide.tool.terraform.Terraform; import com.devonfw.tools.ide.tool.tomcat.Tomcat; @@ -102,7 +101,7 @@ public CommandletManagerImpl(IdeContext context) { add(new Node(context)); add(new Npm(context)); add(new Mvn(context)); - add(new Rewrite(context)); + add(new RefactorCommandlet(context)); add(new GcViewer(context)); add(new Gradle(context)); add(new Eclipse(context)); From d746c6a3e6dac57a369ecf9b3076ac32736fb642 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Thu, 6 Mar 2025 09:54:42 +0100 Subject: [PATCH 04/13] add: add help information for the rewrite command --- cli/src/main/resources/nls/Help.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/main/resources/nls/Help.properties b/cli/src/main/resources/nls/Help.properties index 1b6ea67b06..afed28bba1 100644 --- a/cli/src/main/resources/nls/Help.properties +++ b/cli/src/main/resources/nls/Help.properties @@ -83,8 +83,8 @@ cmd.quarkus.detail=Quarkus is a Kubernetes-native Java framework for building cl cmd.repository=Set up pre-configured git repositories using 'ide repository setup ' cmd.repository.detail=Without further arguments this will set up all pre-configured git repositories.\nAlso, you can provide an explicit git repo as `` argument and IDEasy will automatically clone, build and set up your project based on the existing property file.\nRepositories are configured in 'settings/repository/.properties' and can therefore be shared with your project team for automatic or optional setup. cmd.repository.val.repository=The name of the properties file of the pre-configured git repository to set up, omit to set up all active repositories. -cmd.rewrite=Refactor existing code base with specific recipes provided by OpenRewrite -cmd.rewrite.detail=OpenRewrite is a popular tool for refactoring (meta-programming). Detailed documentation can be found at https://docs.openrewrite.org/ +cmd.refactor=Refactor existing code base with specific recipes provided by OpenRewrite +cmd.refactor.detail=OpenRewrite is a popular tool for refactoring (meta-programming). Detailed documentation can be found at https://docs.openrewrite.org/ cmd.set-edition=Set the edition of the selected tool. cmd.set-edition.detail=This will set the according tool edition variable in your configuration file. If you want to roll out such change and share it with your team, you can commit and push your settings git repository.\nBy default these changes are saved in the project specific settings. Use --conf --home or --workspace to specify otherwise. cmd.set-edition.opt.--cfg=Selection of the configuration file (settings | home | conf | workspace). From a7a0312a21fccec95f4d0d155467be577dbb3213 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Thu, 6 Mar 2025 09:57:45 +0100 Subject: [PATCH 05/13] update: add necessary commandlet and property --- .../ide/commandlet/RefactorCommandlet.java | 41 +++++++++++++++ .../ide/property/RefactorRecipeProperty.java | 50 +++++++++++++++++++ .../tools/ide/tool/rewrite/Rewrite.java | 47 ----------------- 3 files changed, 91 insertions(+), 47 deletions(-) create mode 100644 cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java create mode 100644 cli/src/main/java/com/devonfw/tools/ide/property/RefactorRecipeProperty.java delete mode 100644 cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java new file mode 100644 index 0000000000..751cc9c42a --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java @@ -0,0 +1,41 @@ +package com.devonfw.tools.ide.commandlet; + +import com.devonfw.tools.ide.context.IdeContext; +import com.devonfw.tools.ide.property.StringProperty; +import com.devonfw.tools.ide.tool.ToolCommandlet; + +/** + * {@link ToolCommandlet} for Refactor. + */ +public class RefactorCommandlet extends Commandlet { + + /** + * The constructor. + * + * @param context the {@link IdeContext}. + */ + public RefactorCommandlet(IdeContext context) { + + super(context); + addKeyword(getName()); + add(new StringProperty("recipe", true, true, "")); + } + + @Override + public String getName() { + return "refactor"; + } + + @Override + public void run() { + //3 branches + //1. list available recipes + //2. execute the exact recipe + } + + @Override + public boolean isIdeHomeRequired() { + + return false; + } +} diff --git a/cli/src/main/java/com/devonfw/tools/ide/property/RefactorRecipeProperty.java b/cli/src/main/java/com/devonfw/tools/ide/property/RefactorRecipeProperty.java new file mode 100644 index 0000000000..04a1321aa8 --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/property/RefactorRecipeProperty.java @@ -0,0 +1,50 @@ +package com.devonfw.tools.ide.property; + +import com.devonfw.tools.ide.commandlet.Commandlet; +import com.devonfw.tools.ide.completion.CompletionCandidateCollector; +import com.devonfw.tools.ide.context.IdeContext; +import com.devonfw.tools.ide.tool.ToolCommandlet; +import com.devonfw.tools.ide.tool.plugin.PluginBasedCommandlet; +import com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor; +import com.devonfw.tools.ide.tool.plugin.ToolPlugins; +import com.devonfw.tools.ide.validation.PropertyValidator; + +public class RefactorRecipeProperty extends Property { + + public RefactorRecipeProperty(String name) { + + this(name, null); + } + + public RefactorRecipeProperty(String name, PropertyValidator validator) { + + super(name, true, null, true, validator); + } + + @Override + public Class getValueType() { + + return String.class; + } + + @Override + public String parse(String valueAsString, IdeContext context) { + + return valueAsString; + } + + @Override + protected void completeValue(String arg, IdeContext context, Commandlet commandlet, CompletionCandidateCollector collector) { + + ToolCommandlet cmd = commandlet.getToolForCompletion(); + if (cmd instanceof PluginBasedCommandlet pbc) { + ToolPlugins plugins = pbc.getPlugins(); + for (ToolPluginDescriptor pluginDescriptor : plugins.getPlugins()) { + if (pluginDescriptor.name().toLowerCase().startsWith(arg.toLowerCase())) { + collector.add(pluginDescriptor.name(), null, null, commandlet); + } + } + } + } + +} diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java b/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java deleted file mode 100644 index 019adcd9f5..0000000000 --- a/cli/src/main/java/com/devonfw/tools/ide/tool/rewrite/Rewrite.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.devonfw.tools.ide.tool.rewrite; - -import com.devonfw.tools.ide.common.Tag; -import com.devonfw.tools.ide.context.IdeContext; -import com.devonfw.tools.ide.step.Step; -import com.devonfw.tools.ide.tool.ToolCommandlet; -import com.devonfw.tools.ide.tool.plugin.PluginBasedCommandlet; -import com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor; - -import java.util.Set; - -/** - * {@link ToolCommandlet} for Rewrite. - */ -public class Rewrite extends PluginBasedCommandlet { - - /** - * The constructor. - * - * @param context the {@link IdeContext}. - * @param tool the {@link #getName() tool name}. - * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method. - */ - public Rewrite(IdeContext context, String tool, Set tags) { - super(context, tool, tags); - } - - /** - * we do not need to do anything because right now we will only call OpenRewrite in the command line form - * @param plugin the {@link ToolPluginDescriptor} to install. - * @param step the {@link Step} for the plugin installation. - */ - @Override - public void installPlugin(ToolPluginDescriptor plugin, Step step) { - //do nothing - } - - /** - * The constructor. - * - * @param context the {@link IdeContext}. - */ - public Rewrite(IdeContext context) { - - super(context, "rewrite", Set.of(Tag.JAVA)); - } -} From 6970443801a004517af49f3b57bf2c810fbea8e9 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:33:29 +0200 Subject: [PATCH 06/13] add: help message for RefactorCommandlet --- cli/src/main/resources/nls/Help.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/src/main/resources/nls/Help.properties b/cli/src/main/resources/nls/Help.properties index fd58bb84d7..d6db316d89 100644 --- a/cli/src/main/resources/nls/Help.properties +++ b/cli/src/main/resources/nls/Help.properties @@ -89,6 +89,7 @@ cmd.repository.detail=Without further arguments this will set up all pre-configu cmd.repository.val.repository=The name of the properties file of the pre-configured git repository to set up, omit to set up all active repositories. cmd.refactor=Refactor existing code base with specific recipes provided by OpenRewrite cmd.refactor.detail=OpenRewrite is a popular tool for refactoring (meta-programming). Detailed documentation can be found at https://docs.openrewrite.org/ +cmd.refactor.val.recipe_name=Refactor recipe names (RECIPE_1|RECIPE_2|RECIPE_3) cmd.set-edition=Set the edition of the selected tool. cmd.set-edition.detail=This will set the according tool edition variable in your configuration file. If you want to roll out such change and share it with your team, you can commit and push your settings git repository.\nBy default these changes are saved in the project specific settings. Use --conf --home or --workspace to specify otherwise. cmd.set-edition.opt.--cfg=Selection of the configuration file (settings | home | conf | workspace). @@ -153,5 +154,6 @@ val.plugin=The plugin to select val.settingsRepository=The settings git repository with the IDEasy configuration for the project. val.tool=The tool commandlet to select. val.version=The tool version. +val.recipe_extra_arguments=possible additional arguments for the recipe values=Values: version-banner=Current version of IDE is {} From 10f82d70b32e34f4faaa7e1aa970dd89af8e8a64 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:35:09 +0200 Subject: [PATCH 07/13] add: wrapper of the configurable recipe --- .../ide/tool/openrewrite/RecipeWrapper.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeWrapper.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeWrapper.java b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeWrapper.java new file mode 100644 index 0000000000..c13de47c0b --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeWrapper.java @@ -0,0 +1,17 @@ +package com.devonfw.tools.ide.tool.openrewrite; + +import java.util.Locale; + +public class RecipeWrapper { + public String description; + public String origin_name; + public String url; + public RefactorRecipeEnum ideasy_command; + public String raw_cmd; + + //in case of future need + public String getName() { + return this.origin_name; + } + +} From 5e29a3aa0130faad48f4e58087bd626e40eb8a5d Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:35:43 +0200 Subject: [PATCH 08/13] update: two configuration files (more recipes can be added in this way) --- cli/src/main/resources/refactor/openrewrite.json | 16 ++++++++++++++++ cli/src/test/resources/refactor/openrewrite.json | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 cli/src/main/resources/refactor/openrewrite.json create mode 100644 cli/src/test/resources/refactor/openrewrite.json diff --git a/cli/src/main/resources/refactor/openrewrite.json b/cli/src/main/resources/refactor/openrewrite.json new file mode 100644 index 0000000000..4ed6375340 --- /dev/null +++ b/cli/src/main/resources/refactor/openrewrite.json @@ -0,0 +1,16 @@ +[ + { + "origin_name": "java.format_java_code", + "ideasy_command": "FORMAT_JAVA_CODE", + "description": "Format Java code using a standard comprehensive set of Java formatting recipes.", + "url": "https://docs.openrewrite.org/recipes/java/format/autoformat", + "raw_cmd": "mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.activeRecipes=org.openrewrite.java.format.AutoFormat -Drewrite.exportDatatables=true" + }, + { + "origin_name": "java.remove_blank_lines", + "ideasy_command": "REMOVE_BLANK_LINES", + "description": "Add and/or remove blank lines.", + "url": "https://docs.openrewrite.org/recipes/java/format/blanklines", + "raw_cmd": "mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.activeRecipes=org.openrewrite.java.format.BlankLines -Drewrite.exportDatatables=true" + } +] diff --git a/cli/src/test/resources/refactor/openrewrite.json b/cli/src/test/resources/refactor/openrewrite.json new file mode 100644 index 0000000000..4ed6375340 --- /dev/null +++ b/cli/src/test/resources/refactor/openrewrite.json @@ -0,0 +1,16 @@ +[ + { + "origin_name": "java.format_java_code", + "ideasy_command": "FORMAT_JAVA_CODE", + "description": "Format Java code using a standard comprehensive set of Java formatting recipes.", + "url": "https://docs.openrewrite.org/recipes/java/format/autoformat", + "raw_cmd": "mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.activeRecipes=org.openrewrite.java.format.AutoFormat -Drewrite.exportDatatables=true" + }, + { + "origin_name": "java.remove_blank_lines", + "ideasy_command": "REMOVE_BLANK_LINES", + "description": "Add and/or remove blank lines.", + "url": "https://docs.openrewrite.org/recipes/java/format/blanklines", + "raw_cmd": "mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.activeRecipes=org.openrewrite.java.format.BlankLines -Drewrite.exportDatatables=true" + } +] From 3f843e3325580c4d8fd14f99f5df9f6ce8729391 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:37:03 +0200 Subject: [PATCH 09/13] update: keep names consistent with the names in the config file --- .../tools/ide/tool/openrewrite/RefactorRecipeEnum.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java new file mode 100644 index 0000000000..e2761b32bd --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java @@ -0,0 +1,5 @@ +package com.devonfw.tools.ide.tool.openrewrite; + +public enum RefactorRecipeEnum { + FORMAT_JAVA_CODE, REMOVE_BLANK_LINES +} From faa40be4bb159d931dd0867b116238eb87f9e5a1 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:55:38 +0200 Subject: [PATCH 10/13] add: placeholder enum --- .../devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java index e2761b32bd..c72c299526 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RefactorRecipeEnum.java @@ -1,5 +1,5 @@ package com.devonfw.tools.ide.tool.openrewrite; public enum RefactorRecipeEnum { - FORMAT_JAVA_CODE, REMOVE_BLANK_LINES + FORMAT_JAVA_CODE, REMOVE_BLANK_LINES, UNRECOGNIZED_RECIPE } From 35eb98c32c80f06210569d5135f036a788d84b08 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:57:01 +0200 Subject: [PATCH 11/13] update: finish the implementation --- .../ide/commandlet/RefactorCommandlet.java | 76 ++++++++++++++++++- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java index 751cc9c42a..c7028470d7 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java @@ -1,14 +1,27 @@ package com.devonfw.tools.ide.commandlet; import com.devonfw.tools.ide.context.IdeContext; +import com.devonfw.tools.ide.property.EnumProperty; import com.devonfw.tools.ide.property.StringProperty; import com.devonfw.tools.ide.tool.ToolCommandlet; +import com.devonfw.tools.ide.tool.mvn.Mvn; +import com.devonfw.tools.ide.tool.openrewrite.RecipeManager; +import com.devonfw.tools.ide.tool.openrewrite.RecipeWrapper; +import com.devonfw.tools.ide.tool.openrewrite.RefactorRecipeEnum; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.Scanner; /** * {@link ToolCommandlet} for Refactor. */ public class RefactorCommandlet extends Commandlet { + public final EnumProperty command; + public final StringProperty arguments; + private RecipeManager recipeManager; /** * The constructor. * @@ -18,19 +31,74 @@ public RefactorCommandlet(IdeContext context) { super(context); addKeyword(getName()); - add(new StringProperty("recipe", true, true, "")); + this.command = add(new EnumProperty<>("", true, "recipe_name", RefactorRecipeEnum.class)); + this.arguments = new StringProperty("", false, true, "recipe_extra_arguments"); + recipeManager = new RecipeManager(); + add(this.arguments); + //this.recipe = context. } @Override public String getName() { + //this indicates the command name return "refactor"; } + private String[] adaptMVNCommand(String recipeRawCommands) { + if(recipeRawCommands.startsWith("mvn")) { + return recipeRawCommands.replaceFirst("\\Qmvn\\E", "").split("\\s+"); + } else { + return recipeRawCommands.split("\\s+"); + } + } + + private String changeToDryRunCommand(String recipeRawCommands) { + return recipeRawCommands.replaceAll(":run\\b", ":dryrun"); + } + + private void showInfo(RecipeWrapper wrapper) { + context.info("Recipe [{}], {} ", wrapper.ideasy_command.name(), wrapper.description); + context.info("Reference {}", wrapper.url); + context.info("Raw command: {}", wrapper.raw_cmd); + } + + private boolean confirmApplyChange() { + context.info("***Before making actual changes to the code, please confirm it seriously. It is strongly recommended to perform a DRY-RUN first***"); + context.info("Type yes to apply changes, or press other keys to perform DRY-RUN: "); + + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + + return (input.equalsIgnoreCase("yes")); + } + @Override public void run() { - //3 branches - //1. list available recipes - //2. execute the exact recipe + + context.info("{} called", getClass().getSimpleName()); + + RefactorRecipeEnum command = this.command.getValue(); + String option = this.arguments.getValue(); + + if(!recipeManager.isValidRecipeEnum(command)) { + context.error("INVALID recipe name: {}", command); + return; + } + + RecipeWrapper wrapper = recipeManager.getRecipeWrapper(command); + + showInfo(wrapper); + + String commandLine = wrapper.raw_cmd; + + if(!confirmApplyChange()) { + commandLine = changeToDryRunCommand(commandLine); + } + + context.info("Actual command line: {}", commandLine); + + getCommandlet(Mvn.class).runTool(adaptMVNCommand(commandLine)); + } @Override From 7bffb92dcca8f4787bb268f373213ddd7053e63b Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Tue, 26 Aug 2025 11:57:25 +0200 Subject: [PATCH 12/13] update: finish the feature --- .../ide/tool/openrewrite/RecipeManager.java | 62 +++++++++++++++++++ .../tool/openrewrite/RecipeManagerTest.java | 37 +++++++++++ 2 files changed, 99 insertions(+) create mode 100644 cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManager.java create mode 100644 cli/src/test/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManagerTest.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManager.java b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManager.java new file mode 100644 index 0000000000..530606dcde --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManager.java @@ -0,0 +1,62 @@ +package com.devonfw.tools.ide.tool.openrewrite; + +import com.devonfw.tools.ide.json.JsonMapping; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + + +public class RecipeManager { + + private static final String OPEN_REWRITE_CONFIG_JSON_PATH = "refactor/openrewrite.json"; + private final Map recipes = new HashMap<>(); + + + public RecipeManager() { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader( + Objects.requireNonNull(RecipeManager.class.getClassLoader().getResourceAsStream(OPEN_REWRITE_CONFIG_JSON_PATH)), StandardCharsets.UTF_8)); + ObjectMapper objectMapper = JsonMapping.create(); + + List wrapperList = objectMapper.readValue(reader, objectMapper.getTypeFactory().constructCollectionType(List.class, RecipeWrapper.class)); + + for(RecipeWrapper one: wrapperList) { + recipes.put(one.ideasy_command, one); + } + + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public List listAvailableRecipes() { + return Collections.unmodifiableList(recipes.values().stream().toList()); + } + + private Optional findRecipeByName(String rawName) { + return recipes.values().stream().filter(x -> x.origin_name.equals(rawName)).findAny(); + } + + public boolean isValidRecipeNameRawName(String rawName) { + return findRecipeByName(rawName).isPresent(); + } + + public boolean isValidRecipeEnum(RefactorRecipeEnum recipeEnum) { + return recipes.containsKey(recipeEnum); + } + + public RecipeWrapper getRecipeWrapper(RefactorRecipeEnum recipeEnum) { + return recipes.get(recipeEnum); + } + +} diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManagerTest.java b/cli/src/test/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManagerTest.java new file mode 100644 index 0000000000..6710f0b767 --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/openrewrite/RecipeManagerTest.java @@ -0,0 +1,37 @@ +package com.devonfw.tools.ide.tool.openrewrite; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +class RecipeManagerTest { + + static RecipeManager manager; + + @BeforeAll + static void init() { + manager = new RecipeManager(); + } + + @Test + public void testCreation() { + assertFalse(manager.listAvailableRecipes().isEmpty()); + } + + @Test + public void testStringValidation() { + assertFalse(manager.isValidRecipeNameRawName("NONSENSE")); + assertTrue(manager.isValidRecipeNameRawName(manager.listAvailableRecipes().stream().findAny().get().origin_name)); + } + + @Test + public void testEnumValidation() { + assertFalse(manager.isValidRecipeEnum(RefactorRecipeEnum.UNRECOGNIZED_RECIPE)); + assertTrue(manager.isValidRecipeEnum( + Arrays.stream(RefactorRecipeEnum.values()) + .filter(x -> !x.equals(RefactorRecipeEnum.UNRECOGNIZED_RECIPE)).findAny().get())); + } +} From c32d2a193c1d44ecb45bdba71fa02ab14bf97aa3 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Fri, 24 Oct 2025 13:03:25 +0200 Subject: [PATCH 13/13] fix: fix failed errors by correcting the text --- .../devonfw/tools/ide/commandlet/RefactorCommandlet.java | 2 +- cli/src/main/resources/nls/Help.properties | 8 ++++---- cli/src/main/resources/nls/Help_de.properties | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java index c7028470d7..77e1d14355 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/RefactorCommandlet.java @@ -32,7 +32,7 @@ public RefactorCommandlet(IdeContext context) { super(context); addKeyword(getName()); this.command = add(new EnumProperty<>("", true, "recipe_name", RefactorRecipeEnum.class)); - this.arguments = new StringProperty("", false, true, "recipe_extra_arguments"); + this.arguments = new StringProperty("", false, true, "recipe-extra-arguments"); recipeManager = new RecipeManager(); add(this.arguments); //this.recipe = context. diff --git a/cli/src/main/resources/nls/Help.properties b/cli/src/main/resources/nls/Help.properties index ace9c2609c..22c97039ee 100644 --- a/cli/src/main/resources/nls/Help.properties +++ b/cli/src/main/resources/nls/Help.properties @@ -88,12 +88,12 @@ cmd.python=Tool commandlet for Python. cmd.python.detail=Python is an object-oriented programming language, comparable to Perl, Ruby, Scheme, or Java. Detailed documentation can be found at https://www.python.org/doc/ cmd.quarkus=Tool commandlet for Quarkus (framework for cloud-native apps). cmd.quarkus.detail=Quarkus is a Kubernetes-native Java framework for building cloud-native applications. Detailed documentation can be found at https://quarkus.io/ -cmd.repository=Set up pre-configured git repositories using 'ide repository setup ' -cmd.repository.detail=Without further arguments this will set up all pre-configured git repositories.\nAlso, you can provide an explicit git repo as `` argument and IDEasy will automatically clone, build and set up your project based on the existing property file.\nRepositories are configured in 'settings/repository/.properties' and can therefore be shared with your project team for automatic or optional setup. -cmd.repository.val.repository=The name of the properties file of the pre-configured git repository to set up, omit to set up all active repositories. cmd.refactor=Refactor existing code base with specific recipes provided by OpenRewrite cmd.refactor.detail=OpenRewrite is a popular tool for refactoring (meta-programming). Detailed documentation can be found at https://docs.openrewrite.org/ cmd.refactor.val.recipe_name=Refactor recipe names (RECIPE_1|RECIPE_2|RECIPE_3) +cmd.repository=Set up pre-configured git repositories using 'ide repository setup ' +cmd.repository.detail=Without further arguments this will set up all pre-configured git repositories.\nAlso, you can provide an explicit git repo as `` argument and IDEasy will automatically clone, build and set up your project based on the existing property file.\nRepositories are configured in 'settings/repository/.properties' and can therefore be shared with your project team for automatic or optional setup. +cmd.repository.val.repository=The name of the properties file of the pre-configured git repository to set up, omit to set up all active repositories. cmd.set-edition=Set the edition of the selected tool. cmd.set-edition.detail=This will set the according tool edition variable in your configuration file. If you want to roll out such change and share it with your team, you can commit and push your settings git repository.\nBy default these changes are saved in the project specific settings. Use --conf --home or --workspace to specify otherwise. cmd.set-edition.opt.--cfg=Selection of the configuration file (settings | home | conf | workspace). @@ -158,9 +158,9 @@ val.cfg=Selection of the configuration file (settings | home | conf | workspace) val.commandlet=The selected commandlet (use 'ide help' to list all commandlets). val.edition=The tool edition. val.plugin=The plugin to select +val.recipe-extra-arguments=possible additional arguments for the recipe val.settingsRepository=The settings git repository with the IDEasy configuration for the project. val.tool=The tool commandlet to select. val.version=The tool version. -val.recipe_extra_arguments=possible additional arguments for the recipe values=Values: version-banner=Current version of IDE is {} diff --git a/cli/src/main/resources/nls/Help_de.properties b/cli/src/main/resources/nls/Help_de.properties index 7944c5f23d..8abaa54cac 100644 --- a/cli/src/main/resources/nls/Help_de.properties +++ b/cli/src/main/resources/nls/Help_de.properties @@ -88,6 +88,9 @@ cmd.python=Werkzeug Kommando für Python. cmd.python.detail=Python ist eine objektorientierte Programmiersprache, vergleichbar mit Perl, Ruby, Scheme oder Java. Detaillierte Dokumentation ist zu finden unter https://www.python.org/doc/ cmd.quarkus=Werkzeug Kommando für Quarkus (Framework für Cloud-native Anwendungen). cmd.quarkus.detail=Quarkus ist ein Kubernetes-native Java-Framework zur Entwicklung von Cloud-native Anwendungen. Detaillierte Dokumentation ist zu finden unter https://quarkus.io/ +cmd.refactor=Refaktorieren Sie die vorhandene Codebasis mit spezifischen Rezepten von OpenRewrite +cmd.refactor.detail=OpenRewrite ist ein beliebtes Tool für Refactoring (Metaprogrammierung). Eine ausführliche Dokumentation finden Sie unter https://docs.openrewrite.org/ +cmd.refactor.val.recipe_name=Rezeptnamen umgestalten (RECIPE_1|RECIPE_2|RECIPE_3) cmd.repository=Richtet das vorkonfigurierte Git Repository ein mittels 'ide repository setup '. cmd.repository.detail=Dies wird alle vorkonfigurierten Repositories einrichten. Rufen Sie einfach 'ide repository setup ' auf, ersetzen Sie durch den Namen Ihrer Projektkonfigurationsdatei, die sich in 'settings/repository/your_project_name' befindet und IDEasy wird Ihr Projekt basierend auf der vorhandenen Eigenschaftsdatei automatisch klonen, bauen und einrichten.\nWenn Sie den Projektnamen weglassen, werden alle im Repository-Verzeichnis gefundenen Projekte vorkonfiguriert. cmd.repository.val.repository=Der Name der Properties-Datei des vorkonfigurierten Git Repositories zum Einrichten. Falls nicht angegeben, werden alle aktiven Projekte eingerichtet. @@ -155,6 +158,7 @@ val.cfg=Auswahl der Konfigurationsdatei (settings | home | conf | workspace). val.commandlet=Das ausgewählte Commandlet ("ide help" verwenden, um alle Commandlets aufzulisten). val.edition=Die Werkzeug Edition. val.plugin=Die zu selektierende Erweiterung. +val.recipe-extra-arguments=Mögliche zusätzliche Argumente für das Rezept. val.settingsRepository=Das settings git Repository mit den IDEasy Einstellungen für das Projekt. val.tool=Das zu selektierende Werkzeug Kommando. val.version=Die Werkzeug Version.