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

#232 Support interactive input for next development version (optional) #233

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.release.versions.VersionParseException;
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;
import org.codehaus.plexus.util.cli.CommandLineUtils;
Expand Down Expand Up @@ -1223,4 +1225,77 @@ public String getError() {
public void setArgLine(String argLine) {
this.argLine = argLine;
}

/**
* Prompts the user to enter a new version (or hit enter to accept the default version). Use only in interactive mode.
* @param promptMessage Prompt message
* @param defaultVersion Default version
* @return Version
* @throws MojoFailureException
* @throws CommandLineException
*/
protected String promptForVersion(String promptMessage, String defaultVersion) throws MojoFailureException, CommandLineException {
String version = null;
try {
while (version == null) {
version = prompter.prompt(promptMessage + " [" + defaultVersion + "]");

if (!"".equals(version)
&& (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) {
getLog().info("The version is not valid.");
version = null;
}
}
} catch (PrompterException e) {
throw new MojoFailureException("promptForVersion", e);
}
return version;
}

/**
* Gets next development snapshot version.
* @param version Release version
* @param developmentVersion Development version to use instead of the default next development
* @param digitsOnlyDevVersion Whether to remove qualifiers from the next development version.
* @param versionDigitToIncrement Which digit to increment in the next development version. Starts from zero.
* @param developmentVersionPrompt Whether to prompt for next development version
* @return Next development snapshot version
* @throws MojoFailureException
* @throws VersionParseException
*/
protected String getNextSnapshotVersion(String currentVersion,
String developmentVersion, boolean digitsOnlyDevVersion, Integer versionDigitToIncrement, boolean developmentVersionPrompt)
throws MojoFailureException, VersionParseException, CommandLineException {
// get next snapshot version
String nextSnapshotVersion;
if (!settings.isInteractiveMode()
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
currentVersion);
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}

nextSnapshotVersion = versionInfo
.nextSnapshotVersion(versionDigitToIncrement);
}

if (settings.isInteractiveMode() && developmentVersionPrompt) {
String defaultVersion = nextSnapshotVersion;
nextSnapshotVersion = promptForVersion("What is next snapshot version?", defaultVersion);
if (StringUtils.isBlank(nextSnapshotVersion)) {
getLog().info("Version is blank. Using default version.");
nextSnapshotVersion = defaultVersion;
}
}

if (StringUtils.isBlank(nextSnapshotVersion)) {
throw new MojoFailureException(
"Next snapshot version is blank.");
}
return nextSnapshotVersion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,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 = promptForVersion("What is the hotfix version?", defaultVersion);
} else {
if (StringUtils.isNotBlank(hotfixVersion)
&& (!GitFlowVersionInfo.isValidVersion(hotfixVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public class GitFlowReleaseFinishMojo extends AbstractGitFlowMojo {
@Parameter(property = "developmentVersion", defaultValue = "")
private String developmentVersion = "";

/**
* Whether to prompt for the next development version in interactive mode (with a default value).
*
* @since 1.15.0
*/
@Parameter(property = "developmentVersionPrompt", defaultValue = "false")
private boolean developmentVersionPrompt = false;

/**
* Which digit to increment in the next development version. Starts from
* zero.
Expand Down Expand Up @@ -168,6 +176,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// check uncommitted changes
checkUncommittedChanges();

// get current project version from pom
final String currentVersion = getCurrentProjectVersion();

// get next development snapshot version
final String nextSnapshotVersion;
if (!commitDevelopmentVersionAtStart) {
nextSnapshotVersion = getNextSnapshotVersion(currentVersion,
developmentVersion, digitsOnlyDevVersion, versionDigitToIncrement, developmentVersionPrompt);
}
else {
nextSnapshotVersion = null;
}

// git for-each-ref --format='%(refname:short)' refs/heads/release/*
String releaseBranch = gitFindBranches(gitFlowConfig.getReleaseBranchPrefix(), false).trim();

Expand Down Expand Up @@ -258,9 +279,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitMerge(releaseBranch, releaseRebase, releaseMergeNoFF, releaseMergeFFOnly,
commitMessages.getReleaseFinishMergeMessage(), messageProperties);

// get current project version from pom
final String currentVersion = getCurrentProjectVersion();

if (!skipTag) {
String tagVersion = currentVersion;
if ((tychoBuild || useSnapshotInRelease) && ArtifactUtils.isSnapshot(currentVersion)) {
Expand Down Expand Up @@ -314,27 +332,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (!commitDevelopmentVersionAtStart) {
// get next snapshot version
final String nextSnapshotVersion;
if (!settings.isInteractiveMode()
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
currentVersion);
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}

nextSnapshotVersion = versionInfo
.nextSnapshotVersion(versionDigitToIncrement);
}

if (StringUtils.isBlank(nextSnapshotVersion)) {
throw new MojoFailureException(
"Next snapshot version is blank.");
}

// mvn versions:set -DnewVersion=... -DgenerateBackupPoms=false
mvnSetVersions(nextSnapshotVersion);

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 @@ -114,6 +113,14 @@ public class GitFlowReleaseMojo extends AbstractGitFlowMojo {
@Parameter(property = "developmentVersion", defaultValue = "")
private String developmentVersion = "";

/**
* Whether to prompt for the next development version in interactive mode (with a default value).
*
* @since 1.15.0
*/
@Parameter(property = "developmentVersionPrompt", defaultValue = "false")
private boolean developmentVersionPrompt = false;

/**
* Which digit to increment in the next development version. Starts from
* zero.
Expand Down Expand Up @@ -217,22 +224,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"Cannot get default project version.");
}

// get release version
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 = promptForVersion("What is release version?", defaultVersion);
} else {
version = releaseVersion;
}
Expand All @@ -242,6 +237,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
version = defaultVersion;
}

// get next development snapshot version
final String nextSnapshotVersion = getNextSnapshotVersion(version, developmentVersion,
digitsOnlyDevVersion, versionDigitToIncrement, developmentVersionPrompt);

// maven goals before release
if (StringUtils.isNotBlank(preReleaseGoals)) {
mvnRun(preReleaseGoals);
Expand Down Expand Up @@ -290,26 +289,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitCheckout(gitFlowConfig.getDevelopmentBranch());
}

// get next snapshot version
final String nextSnapshotVersion;
if (!settings.isInteractiveMode()
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version);
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}

nextSnapshotVersion = versionInfo
.nextSnapshotVersion(versionDigitToIncrement);
}

if (StringUtils.isBlank(nextSnapshotVersion)) {
throw new MojoFailureException(
"Next snapshot version is blank.");
}

// mvn set version
mvnSetVersions(nextSnapshotVersion);

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 @@ -104,6 +103,15 @@ public class GitFlowReleaseStartMojo extends AbstractGitFlowMojo {
@Parameter(property = "developmentVersion", defaultValue = "")
private String developmentVersion = "";

/**
* Whether to prompt for the next development version in interactive mode (with a default value).
*
* @since 1.15.0
*/
@Parameter(property = "developmentVersionPrompt", defaultValue = "false")
private boolean developmentVersionPrompt = false;


/**
* Which digit to increment in the next development version. Starts from
* zero.
Expand Down Expand Up @@ -193,6 +201,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// get release version
final String releaseVersion = getReleaseVersion();

// get next development snapshot version
final String nextSnapshotVersion;
if (commitDevelopmentVersionAtStart) {
nextSnapshotVersion = getNextSnapshotVersion(releaseVersion,
developmentVersion, digitsOnlyDevVersion, versionDigitToIncrement, developmentVersionPrompt);
}
else {
nextSnapshotVersion = null;
}

// get release branch
String fullBranchName = gitFlowConfig.getReleaseBranchPrefix();
if (StringUtils.isNotBlank(branchName)) {
Expand Down Expand Up @@ -221,9 +239,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// git branch release/... develop
gitCreateBranch(fullBranchName, startPoint);

final String nextSnapshotVersion =
getNextSnapshotVersion(releaseVersion);

// mvn versions:set ...
// git commit -a -m ...
commitProjectVersion(nextSnapshotVersion, commitMessages.getReleaseVersionUpdateMessage());
Expand Down Expand Up @@ -259,30 +274,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private String getNextSnapshotVersion(String currentVersion) throws MojoFailureException, VersionParseException {
// get next snapshot version
final String nextSnapshotVersion;
if (!settings.isInteractiveMode()
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
currentVersion);
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}

nextSnapshotVersion = versionInfo
.nextSnapshotVersion(versionDigitToIncrement);
}

if (StringUtils.isBlank(nextSnapshotVersion)) {
throw new MojoFailureException(
"Next snapshot version is blank.");
}
return nextSnapshotVersion;
}

private String getReleaseVersion() throws MojoFailureException, VersionParseException, CommandLineException {
// get current project version from pom
final String currentVersion = getCurrentProjectVersion();
Expand All @@ -303,20 +294,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 = promptForVersion("What is release version?", defaultVersion);
} else {
version = releaseVersion;
}
Expand Down