From 7e2dc2bf2c7759e8813e69b6174694519298d250 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Thu, 5 Oct 2023 21:54:52 +0300 Subject: [PATCH] Add prompter component --- pom.xml | 14 +++ .../plugin/gitflow/AbstractGitFlowMojo.java | 25 +++- .../gitflow/GitFlowFeatureFinishMojo.java | 37 +----- .../gitflow/GitFlowFeatureStartMojo.java | 15 +-- .../gitflow/GitFlowHotfixFinishMojo.java | 28 +---- .../gitflow/GitFlowHotfixStartMojo.java | 38 +----- .../plugin/gitflow/GitFlowReleaseMojo.java | 15 +-- .../gitflow/GitFlowReleaseStartMojo.java | 15 +-- .../gitflow/GitFlowSupportStartMojo.java | 7 +- .../gitflow/GitFlowVersionUpdateMojo.java | 38 +----- .../gitflow/prompter/ConsolePrompter.java | 119 ++++++++++++++++++ .../gitflow/prompter/GitFlowPrompter.java | 72 +++++++++++ .../gitflow/prompter/PromptValidation.java | 42 +++++++ 13 files changed, 282 insertions(+), 183 deletions(-) create mode 100644 src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/ConsolePrompter.java create mode 100644 src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/GitFlowPrompter.java create mode 100644 src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/PromptValidation.java diff --git a/pom.xml b/pom.xml index e38a74b4..2eb27927 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,20 @@ + + org.eclipse.sisu + sisu-maven-plugin + 0.3.5 + + + index-project + + main-index + test-index + + + + diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java index d98b59d9..0bf478f2 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -44,7 +44,6 @@ import org.apache.maven.project.ProjectBuildingResult; import org.apache.maven.settings.Settings; import org.apache.maven.shared.release.policy.version.VersionPolicy; -import org.codehaus.plexus.components.interactivity.Prompter; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.Os; import org.codehaus.plexus.util.StringUtils; @@ -52,6 +51,8 @@ import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; +import com.amashchenko.maven.plugin.gitflow.prompter.GitFlowPrompter; + /** * Abstract git flow mojo. * @@ -261,7 +262,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** Default prompter. */ @Component - protected Prompter prompter; + protected GitFlowPrompter prompter; /** Maven settings. */ @Parameter(defaultValue = "${settings}", readonly = true) protected Settings settings; @@ -473,6 +474,26 @@ protected boolean validBranchName(final String branchName) throws MojoFailureExc return res.getExitCode() == SUCCESS_EXIT_CODE; } + /** + * Checks if version is valid. + * + * @param version + * Version to validate. + * @return true when version is valid, false + * otherwise. + * @throws MojoFailureException + * Shouldn't happen, actually. + * @throws CommandLineException + * If command line execution fails. + */ + protected boolean validVersion(final String version) throws MojoFailureException, CommandLineException { + boolean valid = "".equals(version) || (GitFlowVersionInfo.isValidVersion(version) && validBranchName(version)); + if (!valid) { + getLog().info("The version is not valid."); + } + return valid; + } + /** * Executes git commands to check for uncommitted changes. * diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java index c197c093..25480a1a 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java @@ -15,16 +15,13 @@ */ package com.amashchenko.maven.plugin.gitflow; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -48,8 +45,7 @@ public class GitFlowFeatureFinishMojo extends AbstractGitFlowMojo { private boolean skipTestProject = false; /** - * Whether to squash feature branch commits into a single commit upon - * merging. + * Whether to squash feature branch commits into a single commit upon merging. * * @since 1.2.3 */ @@ -255,41 +251,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { private String promptBranchName() throws MojoFailureException, CommandLineException { final String featureBranches = gitFindBranches(gitFlowConfig.getFeatureBranchPrefix(), false); - final String currentBranch = gitCurrentBranch(); - if (StringUtils.isBlank(featureBranches)) { throw new MojoFailureException("There are no feature branches."); } final String[] branches = featureBranches.split("\\r?\\n"); - List numberedList = new ArrayList<>(); - String defaultChoice = null; - StringBuilder str = new StringBuilder("Feature branches:").append(LS); - for (int i = 0; i < branches.length; i++) { - str.append((i + 1) + ". " + branches[i] + LS); - numberedList.add(String.valueOf(i + 1)); - if (branches[i].equals(currentBranch)) { - defaultChoice = String.valueOf(i + 1); - } - } - str.append("Choose feature branch to finish"); - - String featureNumber = null; - try { - while (StringUtils.isBlank(featureNumber)) { - featureNumber = prompter.prompt(str.toString(), numberedList, defaultChoice); - } - } catch (PrompterException e) { - throw new MojoFailureException("feature-finish", e); - } - - String featureBranchName = null; - if (featureNumber != null) { - int num = Integer.parseInt(featureNumber); - featureBranchName = branches[num - 1]; - } - - return featureBranchName; + return prompter.prompt(branches, gitCurrentBranch(), "Feature branches:", "Choose feature branch to finish"); } } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java index 74735172..72728b7a 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java @@ -23,7 +23,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.shared.release.versions.VersionParseException; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -87,18 +86,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { String featureBranchName = null; if (settings.isInteractiveMode()) { - try { - while (StringUtils.isBlank(featureBranchName)) { - featureBranchName = prompter - .prompt("What is a name of feature branch? " + gitFlowConfig.getFeatureBranchPrefix()); - - if (!validateBranchName(featureBranchName, featureNamePattern, false)) { - featureBranchName = null; - } - } - } catch (PrompterException e) { - throw new MojoFailureException("feature-start", e); - } + featureBranchName = prompter.prompt("What is a name of feature branch? " + gitFlowConfig.getFeatureBranchPrefix(), + res -> StringUtils.isNotBlank(res) && validateBranchName(res, featureNamePattern, false)); } else { validateBranchName(featureName, featureNamePattern, true); featureBranchName = featureName; diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java index 8b76de46..f3fbd5d1 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java @@ -15,9 +15,7 @@ */ package com.amashchenko.maven.plugin.gitflow; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.maven.artifact.Artifact; @@ -26,7 +24,6 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -405,29 +402,6 @@ private String promptBranchName() throws MojoFailureException, CommandLineExcept String[] branches = hotfixBranches.split("\\r?\\n"); - List numberedList = new ArrayList<>(); - StringBuilder str = new StringBuilder("Hotfix branches:").append(LS); - for (int i = 0; i < branches.length; i++) { - str.append((i + 1) + ". " + branches[i] + LS); - numberedList.add(String.valueOf(i + 1)); - } - str.append("Choose hotfix branch to finish"); - - String hotfixNumber = null; - try { - while (StringUtils.isBlank(hotfixNumber)) { - hotfixNumber = prompter.prompt(str.toString(), numberedList); - } - } catch (PrompterException e) { - throw new MojoFailureException("hotfix-finish", e); - } - - String hotfixBranchName = null; - if (hotfixNumber != null) { - int num = Integer.parseInt(hotfixNumber); - hotfixBranchName = branches[num - 1]; - } - - return hotfixBranchName; + return prompter.prompt(branches, null, "Hotfix branches:", "Choose hotfix branch to finish"); } } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java index 74ec20c0..96d097a1 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java @@ -15,9 +15,7 @@ */ package com.amashchenko.maven.plugin.gitflow; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.maven.artifact.Artifact; @@ -27,7 +25,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.shared.release.versions.VersionParseException; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -112,27 +109,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { // add production branch to the list branches[supportBranches.length] = gitFlowConfig.getProductionBranch(); - List numberedList = new ArrayList<>(); - StringBuilder str = new StringBuilder("Branches:").append(LS); - for (int i = 0; i < branches.length; i++) { - str.append((i + 1) + ". " + branches[i] + LS); - numberedList.add(String.valueOf(i + 1)); - } - str.append("Choose branch to hotfix"); - - String branchNumber = null; - try { - while (StringUtils.isBlank(branchNumber)) { - branchNumber = prompter.prompt(str.toString(), numberedList); - } - } catch (PrompterException e) { - throw new MojoFailureException("hotfix-start", e); - } - - if (branchNumber != null) { - int num = Integer.parseInt(branchNumber); - branchName = branches[num - 1]; - } + branchName = prompter.prompt(branches, null, "Branches:", "Choose branch to hotfix"); if (StringUtils.isBlank(branchName)) { throw new MojoFailureException("Branch name is blank."); @@ -167,18 +144,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { String version = null; if (settings.isInteractiveMode()) { - try { - while (version == null) { - version = prompter.prompt("What is the hotfix version? [" + defaultVersion + "]"); - - if (!"".equals(version) && (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) { - getLog().info("The version is not valid."); - version = null; - } - } - } catch (PrompterException e) { - throw new MojoFailureException("hotfix-start", e); - } + version = prompter.prompt("What is the hotfix version? [" + defaultVersion + "]", this::validVersion); } else { if (StringUtils.isNotBlank(hotfixVersion) && (!GitFlowVersionInfo.isValidVersion(hotfixVersion) || !validBranchName(hotfixVersion))) { diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java index 2831bd69..5b7314f5 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java @@ -24,7 +24,6 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; /** @@ -223,19 +222,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { String version = null; if (settings.isInteractiveMode()) { - try { - while (version == null) { - version = prompter.prompt("What is release version? [" + defaultVersion + "]"); - - if (!"".equals(version) - && (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) { - getLog().info("The version is not valid."); - version = null; - } - } - } catch (PrompterException e) { - throw new MojoFailureException("release", e); - } + version = prompter.prompt("What is release version? [" + defaultVersion + "]", this::validVersion); } else { version = releaseVersion; } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java index f3717adc..a4684eca 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java @@ -25,7 +25,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.shared.release.versions.VersionParseException; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -278,19 +277,7 @@ private String getReleaseVersion() throws MojoFailureException, VersionParseExce String version = null; if (settings.isInteractiveMode()) { - try { - while (version == null) { - version = prompter.prompt("What is release version? [" + defaultVersion + "]"); - - if (!"".equals(version) - && (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) { - getLog().info("The version is not valid."); - version = null; - } - } - } catch (PrompterException e) { - throw new MojoFailureException("release-start", e); - } + version = prompter.prompt("What is release version? [" + defaultVersion + "]", this::validVersion); } else { version = releaseVersion; } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java index 800f3b45..70c124e0 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java @@ -25,7 +25,6 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -90,11 +89,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoFailureException("There are no tags."); } - try { - tag = prompter.prompt("Choose tag to start support branch", Arrays.asList(tagsStr.split("\\r?\\n"))); - } catch (PrompterException e) { - throw new MojoFailureException("support-start", e); - } + tag = prompter.prompt("Choose tag to start support branch", Arrays.asList(tagsStr.split("\\r?\\n"))); } else if (StringUtils.isNotBlank(tagName)) { if (gitCheckTagExists(tagName)) { tag = tagName; diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionUpdateMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionUpdateMojo.java index 205e36cc..2468cb34 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionUpdateMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionUpdateMojo.java @@ -15,9 +15,7 @@ */ package com.amashchenko.maven.plugin.gitflow; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.maven.plugin.MojoExecutionException; @@ -25,7 +23,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.shared.release.versions.VersionParseException; -import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; @@ -125,27 +122,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { branches[supportBranches.length] = releaseBranch; } - List numberedList = new ArrayList<>(); - StringBuilder str = new StringBuilder("Branches:").append(LS); - for (int i = 0; i < branches.length; i++) { - str.append((i + 1) + ". " + branches[i] + LS); - numberedList.add(String.valueOf(i + 1)); - } - str.append("Choose branch to update"); - - String branchNumber = null; - try { - while (StringUtils.isBlank(branchNumber)) { - branchNumber = prompter.prompt(str.toString(), numberedList); - } - } catch (PrompterException e) { - throw new MojoFailureException("version-update", e); - } - - if (branchNumber != null) { - int num = Integer.parseInt(branchNumber); - branchName = branches[num - 1]; - } + branchName = prompter.prompt(branches, null, "Branches:", "Choose branch to update"); } } else if (StringUtils.isNotBlank(fromBranch)) { if (fromBranch.equals(releaseBranch) || contains(supportBranches, fromBranch)) { @@ -178,18 +155,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { String version = null; if (settings.isInteractiveMode()) { - try { - while (version == null) { - version = prompter.prompt("What is the update version? [" + defaultVersion + "]"); - - if (!"".equals(version) && (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) { - getLog().info("The version is not valid."); - version = null; - } - } - } catch (PrompterException e) { - throw new MojoFailureException("version-update", e); - } + version = prompter.prompt("What is the update version? [" + defaultVersion + "]", this::validVersion); } else { if (StringUtils.isNotBlank(updateVersion) && (!GitFlowVersionInfo.isValidVersion(updateVersion) || !validBranchName(updateVersion))) { diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/ConsolePrompter.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/ConsolePrompter.java new file mode 100644 index 00000000..dfff91a8 --- /dev/null +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/ConsolePrompter.java @@ -0,0 +1,119 @@ +/* + * Copyright 2014-2023 Aleksandr Mashchenko. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.amashchenko.maven.plugin.gitflow.prompter; + +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.components.interactivity.Prompter; +import org.codehaus.plexus.components.interactivity.PrompterException; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.cli.CommandLineException; + +/** + * Default prompter which uses console for input. + * + */ +@Named +@Singleton +public class ConsolePrompter implements GitFlowPrompter { + private static final String LS = System.getProperty("line.separator"); + + private final Prompter prompter; + + @Inject + public ConsolePrompter(Prompter prompter) { + this.prompter = prompter; + } + + /** + * {@inheritDoc} + */ + @Override + public String prompt(String[] choices, final String defaultChoice, String preMessage, String postMessage) throws MojoFailureException { + List numberedList = new ArrayList<>(); + String defChoice = null; + StringBuilder str = new StringBuilder(preMessage).append(LS); + for (int i = 0; i < choices.length; i++) { + str.append(i + 1).append(". ").append(choices[i]).append(LS); + numberedList.add(String.valueOf(i + 1)); + if (choices[i].equals(defaultChoice)) { + defChoice = String.valueOf(i + 1); + } + } + str.append(postMessage); + + String response = null; + try { + while (StringUtils.isBlank(response)) { + if (defaultChoice == null || defChoice == null) { + response = prompter.prompt(str.toString(), numberedList); + } else { + response = prompter.prompt(str.toString(), numberedList, defChoice); + } + } + } catch (PrompterException e) { + throw new MojoFailureException("prompter error", e); + } + + String result = null; + if (response != null) { + int num = Integer.parseInt(response); + result = choices[num - 1]; + } + + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public String prompt(String message, PromptValidation validation) throws MojoFailureException, CommandLineException { + String response = null; + try { + while (response == null) { + response = prompter.prompt(message); + + if (!validation.valid(response)) { + response = null; + } + } + } catch (PrompterException e) { + throw new MojoFailureException("prompter error", e); + } + return response; + } + + /** + * {@inheritDoc} + */ + @Override + public String prompt(String message, List choices) throws MojoFailureException { + String response = null; + try { + response = prompter.prompt(message, choices); + } catch (PrompterException e) { + throw new MojoFailureException("prompter error", e); + } + return response; + } +} diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/GitFlowPrompter.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/GitFlowPrompter.java new file mode 100644 index 00000000..f7994ca3 --- /dev/null +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/GitFlowPrompter.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014-2023 Aleksandr Mashchenko. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.amashchenko.maven.plugin.gitflow.prompter; + +import java.util.List; + +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.cli.CommandLineException; + +/** + * Git flow prompter interface. + * + */ +public interface GitFlowPrompter { + /** + * Prompts with predefined choices. + * + * @param choices + * Predefined list of choices. + * @param defaultChoice + * The default choice to use. + * @param preMessage + * Text to display before prompt. + * @param postMessage + * Text to display after prompt. + * @return Response obtained from prompting. + * @throws MojoFailureException + * If error happens during prompting. + */ + String prompt(String[] choices, String defaultChoice, String preMessage, String postMessage) throws MojoFailureException; + + /** + * Prompts and validates the response. + * + * @param message + * Text to display on prompt. + * @param validation + * Validation function to validate prompt response. + * @return Response obtained from prompting. + * @throws MojoFailureException + * If error happens during prompting. + * @throws CommandLineException + * If error happens during validation. + */ + String prompt(String message, PromptValidation validation) throws MojoFailureException, CommandLineException; + + /** + * Prompts with predefined choices. + * + * @param message + * Text to display on prompt. + * @param choices + * Predefined list of choices. + * @return Response obtained from prompting. + * @throws MojoFailureException + * If error happens during prompting. + */ + String prompt(String message, List choices) throws MojoFailureException; +} diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/PromptValidation.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/PromptValidation.java new file mode 100644 index 00000000..fa1d4271 --- /dev/null +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/prompter/PromptValidation.java @@ -0,0 +1,42 @@ +/* + * Copyright 2014-2023 Aleksandr Mashchenko. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.amashchenko.maven.plugin.gitflow.prompter; + +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.cli.CommandLineException; + +/** + * Functional interface to validate prompt response. + * + * @param + * Type of the response to validate. + */ +@FunctionalInterface +public interface PromptValidation { + /** + * Validates prompt response. + * + * @param t + * Response to validate. + * @return true when response is valid, false + * otherwise. + * @throws MojoFailureException + * If validation fails. + * @throws CommandLineException + * If validation fails. + */ + boolean valid(T t) throws MojoFailureException, CommandLineException; +}