Skip to content

Commit

Permalink
Add prompter component
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Oct 5, 2023
1 parent 9d0db44 commit 7e2dc2b
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 183 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.5</version>
<executions>
<execution>
<id>index-project</id>
<goals>
<goal>main-index</goal>
<goal>test-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@
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;
import org.codehaus.plexus.util.cli.CommandLineException;
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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>true</code> when version is valid, <code>false</code>
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
*/
Expand Down Expand Up @@ -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<String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -405,29 +402,6 @@ private String promptBranchName() throws MojoFailureException, CommandLineExcept

String[] branches = hotfixBranches.split("\\r?\\n");

List<String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -112,27 +109,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// add production branch to the list
branches[supportBranches.length] = gitFlowConfig.getProductionBranch();

List<String> 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.");
Expand Down Expand Up @@ -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))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 7e2dc2b

Please sign in to comment.