Skip to content

Commit

Permalink
Merge pull request #85 from ADI10HERO/write-version
Browse files Browse the repository at this point in the history
feat: add writeVersion for maven project
  • Loading branch information
garethjevans authored Aug 6, 2021
2 parents 6b5fc12 + aca6185 commit f426350
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.FilePath;
import hudson.model.TaskListener;
import io.jenkins.plugins.conventionalcommits.utils.CurrentVersion;
import io.jenkins.plugins.conventionalcommits.utils.WriteVersion;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,6 +33,7 @@ public class NextVersionStep extends Step {
private String outputFormat;
private String startTag;
private String buildMetadata;
private boolean writeVersion;

@DataBoundConstructor
public NextVersionStep() {
Expand Down Expand Up @@ -95,9 +97,14 @@ public void setBuildMetadata(String buildMetadata) {
this.buildMetadata = buildMetadata;
}

@DataBoundSetter
public void setWriteVersion(boolean writeVersion) {
this.writeVersion = writeVersion;
}

@Override
public StepExecution start(StepContext stepContext) throws Exception {
return new Execution(outputFormat, startTag, buildMetadata, stepContext);
return new Execution(outputFormat, startTag, buildMetadata, writeVersion, stepContext);
}

/** This class extends Step Execution class, contains the run method. */
Expand All @@ -120,15 +127,22 @@ public static class Execution extends SynchronousStepExecution<String> {
justification = "Only used when starting.")
private final transient String buildMetadata;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient boolean writeVersion;

protected Execution(
String outputFormat,
String startTag,
String buildMetadata,
boolean writeVersion,
@Nonnull org.jenkinsci.plugins.workflow.steps.StepContext context) {
super(context);
this.outputFormat = outputFormat;
this.startTag = startTag;
this.buildMetadata = buildMetadata;
this.writeVersion = writeVersion;
}

@Override
Expand Down Expand Up @@ -175,7 +189,11 @@ protected String run() throws Exception {
nextVersion = nextVersion.setBuildMetadata(buildMetadata);
}

// TODO write the version using the output template
if (writeVersion) {
WriteVersion writer = new WriteVersion();
writer.write(nextVersion, dir);
}

getContext().get(TaskListener.class).getLogger().println(nextVersion);

return nextVersion.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)
}
return Version.valueOf(version);
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper) th
HelmChart.class);
return Version.valueOf(chart.getVersion());
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)

return Version.valueOf(results);
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)

return Version.valueOf(results);
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {

String os = System.getProperty("os.name");
String commandName = "mvn";

if (os.contains("Windows")) {
commandName += ".cmd";
}

List<String> command =
Arrays.asList(
commandName, "versions:set", "-DnewVersion=" + nextVersion.toString());
processHelper.runProcessBuilder(directory, command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper) th

return Version.valueOf((String) map.get("version"));
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ abstract class ProjectType {

public abstract Version getCurrentVersion(File directory, ProcessHelper processHelper)
throws IOException, InterruptedException;

public abstract void writeVersion(
File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)

return Version.valueOf(result);
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.jenkins.plugins.conventionalcommits.utils;

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.DefaultProcessHelper;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

/** Class to write back the calculated next semantic version into the config file of a project. */
public class WriteVersion {
private ProcessHelper processHelper;

public void setProcessHelper(ProcessHelper processHelper) {
this.processHelper = processHelper;
}

/**
* Writes next semantic version in a file.
*
* @param nextVersion The project's calculated next semantic version.
* @param directory Directory of the project
* @throws IOException If an error occurs while reading/writing files.
* @throws InterruptedException If an error occurs while executing command using processHelper.
*/
public void write(Version nextVersion, File directory) throws IOException, InterruptedException {

ProjectType projectType = ProjectTypeFactory.getProjectType(directory);

if (projectType != null) {
if (processHelper == null) {
processHelper = new DefaultProcessHelper();
}
projectType.writeVersion(directory, nextVersion, processHelper);
} else {
LogUtils logger = new LogUtils();
logger.log(Level.INFO, Level.INFO, Level.FINE, Level.FINE, true, "Could not write to file");
}
}
}

0 comments on commit f426350

Please sign in to comment.