Skip to content

Commit

Permalink
Merge pull request #11 from ruffkat/feature/custom-configurator
Browse files Browse the repository at this point in the history
Extract CiVersionSource interface
  • Loading branch information
Jeff-Glatz authored Apr 4, 2022
2 parents 519dc6c + 3a63722 commit 364b7cb
Show file tree
Hide file tree
Showing 25 changed files with 626 additions and 186 deletions.
18 changes: 18 additions & 0 deletions src/main/java/tools/bestquality/maven/ci/CiMojoConfigurator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tools.bestquality.maven.ci;

import org.codehaus.plexus.component.configurator.BasicComponentConfigurator;
import tools.bestquality.maven.versioning.IncrementorConverter;

import javax.annotation.PostConstruct;
import javax.inject.Named;

@Named("ci-mojo-configurator")
public class CiMojoConfigurator
extends BasicComponentConfigurator {

@PostConstruct
public void initialize() {
converterLookup.registerConverter(new IncrementorConverter());
converterLookup.registerConverter(new CiVersionSourceConverter());
}
}
52 changes: 43 additions & 9 deletions src/main/java/tools/bestquality/maven/ci/CiVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.StringUtils;
import tools.bestquality.maven.versioning.Incrementor;
import tools.bestquality.util.Strings;

import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import static java.lang.Boolean.parseBoolean;
import static java.lang.String.format;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
import static tools.bestquality.maven.versioning.Version.parseVersion;
import static tools.bestquality.util.Strings.trim;

public class CiVersion {
private Optional<String> revision;
Expand Down Expand Up @@ -98,21 +100,21 @@ public String expand(String template) {
*/
public String replace(String pom) {
if (revision.isPresent()) {
String element = revision.filter(StringUtils::isNotEmpty)
String element = revision.filter(Strings::isNotBlank)
.map(r -> format("<revision>%s</revision>", r))
.orElse("<revision/>");
pom = pom.replaceAll("(?s)(<properties.*)<revision\\s*>.*</revision\\s*>|<revision\\s*/>(.*properties>)",
format("$1%s$2", element));
}
if (sha1.isPresent()) {
String element = sha1.filter(StringUtils::isNotEmpty)
String element = sha1.filter(Strings::isNotBlank)
.map(s -> format("<sha1>%s</sha1>", s))
.orElse("<sha1/>");
pom = pom.replaceAll("(?s)(<properties.*)<sha1\\s*>.*</sha1\\s*>|<sha1\\s*/>(.*properties>)",
format("$1%s$2", element));
}
if (changelist.isPresent()) {
String element = changelist.filter(StringUtils::isNotEmpty)
String element = changelist.filter(Strings::isNotBlank)
.map(c -> format("<changelist>%s</changelist>", c))
.orElse("<changelist/>");
pom = pom.replaceAll("(?s)(<properties.*)<changelist\\s*>.*</changelist\\s*>|<changelist\\s*/>(.*properties>)",
Expand All @@ -121,15 +123,28 @@ public String replace(String pom) {
return pom;
}

public CiVersion withMissingFrom(Properties properties) {
public CiVersion withMissingFrom(Properties properties)
throws MojoFailureException {
if (!revision.isPresent() && properties.containsKey("revision")) {
withRevision(properties.getProperty("revision"));
String revision = trim(properties.getProperty("revision"));
if (parseBoolean(revision)) {
throw new MojoFailureException("The revision property must be specified as a string value");
}
withRevision(revision);
}
if (!sha1.isPresent() && properties.containsKey("sha1")) {
withSha1(properties.getProperty("sha1"));
String sha1 = trim(properties.getProperty("sha1"));
if (parseBoolean(sha1)) {
throw new MojoFailureException("The sha1 property must be specified as a string value");
}
withSha1(sha1);
}
if (!changelist.isPresent() && properties.containsKey("changelist")) {
withChangelist(properties.getProperty("changelist"));
String changelist = trim(properties.getProperty("changelist"));
if (parseBoolean(changelist)) {
throw new MojoFailureException("The changelist property must be specified as a string value");
}
withChangelist(changelist);
}
return this;
}
Expand Down Expand Up @@ -160,6 +175,24 @@ public String toExternalForm() {
return builder.toString();
}

public String toComponentForm() {
StringBuilder builder = new StringBuilder();
revision.ifPresent(value -> builder
.append("revision:")
.append(value)
.append(" "));
sha1.ifPresent(value -> builder
.append("sha1:")
.append(value)
.append(" "));
changelist.ifPresent(value -> builder
.append("changelist:")
.append(value)
.append(" "));
return builder.toString()
.trim();
}

public void applyTo(Model model) {
model.setVersion(toExternalForm());
Properties properties = model.getProperties();
Expand Down Expand Up @@ -200,7 +233,8 @@ private String nextRevision(Incrementor incrementor)
}
}

public static CiVersion versionFrom(Properties properties) {
public static CiVersion versionFrom(Properties properties)
throws MojoFailureException {
return new CiVersion().withMissingFrom(properties);
}
}
48 changes: 5 additions & 43 deletions src/main/java/tools/bestquality/maven/ci/CiVersionSource.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,11 @@
package tools.bestquality.maven.ci;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static tools.bestquality.maven.ci.CiVersion.versionFrom;

enum CiVersionSource {
PROJECT() {
@Override
public CiVersion from(MavenProject project, MavenSession session) {
return versionFrom(project.getProperties());
}
},
SYSTEM() {
@Override
public CiVersion from(MavenProject project, MavenSession session) {
return versionFrom(session.getSystemProperties());
}
},
MERGE_SYSTEM_FIRST() {
@Override
public CiVersion from(MavenProject project, MavenSession session) {
return versionFrom(session.getSystemProperties())
.withMissingFrom(project.getProperties());
}
},
MERGE_PROJECT_FIRST() {
@Override
public CiVersion from(MavenProject project, MavenSession session) {
return versionFrom(project.getProperties())
.withMissingFrom(session.getSystemProperties());
}
};

public abstract CiVersion from(MavenProject project, MavenSession session);

public static CiVersionSource source(String name) {
return stream(values())
.filter(item -> item.name()
.equalsIgnoreCase(name.replace("-", "_")))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
format("No enum constant in %s matching %s",
CiVersionSource.class.getCanonicalName(), name)));
}
@FunctionalInterface
public interface CiVersionSource {
CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tools.bestquality.maven.ci;

import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.converters.basic.AbstractBasicConverter;

import static tools.bestquality.maven.ci.CiVersionSources.source;

public class CiVersionSourceConverter
extends AbstractBasicConverter {

@Override
public boolean canConvert(Class<?> type) {
return CiVersionSource.class.equals(type);
}

@Override
protected Object fromString(String value)
throws ComponentConfigurationException {
return source(value);
}
}
56 changes: 56 additions & 0 deletions src/main/java/tools/bestquality/maven/ci/CiVersionSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tools.bestquality.maven.ci;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static tools.bestquality.maven.ci.CiVersion.versionFrom;

enum CiVersionSources
implements CiVersionSource {
PROJECT() {
@Override
public CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException {
return versionFrom(project.getProperties());
}
},
SYSTEM() {
@Override
public CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException {
return versionFrom(session.getSystemProperties());
}
},
MERGE_SYSTEM_FIRST() {
@Override
public CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException {
return versionFrom(session.getSystemProperties())
.withMissingFrom(project.getProperties());
}
},
MERGE_PROJECT_FIRST() {
@Override
public CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException {
return versionFrom(project.getProperties())
.withMissingFrom(session.getSystemProperties());
}
};

public abstract CiVersion from(MavenProject project, MavenSession session)
throws MojoFailureException;

public static CiVersionSource source(String name) {
return stream(values())
.filter(item -> item.name()
.equalsIgnoreCase(name.replace("-", "_")))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
format("No enum constant in %s matching %s",
CiVersionSources.class.getCanonicalName(), name)));
}
}
45 changes: 39 additions & 6 deletions src/main/java/tools/bestquality/maven/ci/ExpandPomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.maven.execution.MavenSession;
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.apache.maven.project.MavenProject;
Expand All @@ -13,10 +14,9 @@
import static java.lang.String.format;
import static java.nio.file.Files.createDirectories;
import static org.apache.maven.plugins.annotations.LifecyclePhase.VALIDATE;
import static tools.bestquality.maven.ci.CiVersionSource.MERGE_SYSTEM_FIRST;


@Mojo(name = "expand-pom",
configurator = "ci-mojo-configurator",
threadSafe = true,
defaultPhase = VALIDATE)
public class ExpandPomMojo
Expand All @@ -29,6 +29,28 @@ public class ExpandPomMojo
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

/**
* The source of the properties used to compute the CI Version. There are 2
* primary sources of properties from which the CI version is built:
* <p/>
* <ul>
* <li>The maven project's properties</li>
* <li>The maven session's system properties</li>
* </ul>
* <p/>
* This parameter determines which set of properties is used, there are options
* for merging the properties as well. The possible values are:
* <p/>
* <ul>
* <li><code>project</code></li>
* <li><code>system</code></li>
* <li><code>merge-system-first</code></li>
* <li><code>merge-project-first</code></li>
* </ul>
*/
@Parameter(alias = "source", property = "source", defaultValue = "merge-system-first")
private CiVersionSource source;

public ExpandPomMojo() {
this(new Content());
}
Expand All @@ -47,8 +69,13 @@ public ExpandPomMojo withSession(MavenSession session) {
return this;
}

public ExpandPomMojo withSource(CiVersionSource source) {
this.source = source;
return this;
}

public void execute()
throws MojoExecutionException {
throws MojoExecutionException, MojoFailureException {
String projectPom = readProjectPom();
String expandedPom = expandProjectPom(projectPom);
if (projectPom.equals(expandedPom)) {
Expand All @@ -73,12 +100,18 @@ private String readProjectPom()
}
}

private CiVersion current()
throws MojoFailureException {
return source.from(project, session);
}

private String expandProjectPom(String projectPom)
throws MojoExecutionException {
throws MojoExecutionException, MojoFailureException {
info("Expanding contents of project POM file");
CiVersion version = current();
try {
CiVersionSource source = MERGE_SYSTEM_FIRST;
CiVersion version = source.from(project, session);
info(format("Expanding POM file with %s [%s]",
version.toExternalForm(), version.toComponentForm()));
return version.replace(version.expand(projectPom));
} catch (Exception e) {
error("Failure expanding template POM file", e);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/tools/bestquality/maven/ci/ExportVersionMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import tools.bestquality.io.Content;
Expand All @@ -13,7 +14,6 @@
import static java.lang.System.out;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.file.Files.createDirectories;
import static tools.bestquality.maven.ci.CiVersionSource.source;

public abstract class ExportVersionMojo<M extends ExportVersionMojo<M>>
extends CiMojo {
Expand Down Expand Up @@ -42,7 +42,7 @@ public abstract class ExportVersionMojo<M extends ExportVersionMojo<M>>
* </ul>
*/
@Parameter(alias = "source", property = "source", defaultValue = "merge-system-first")
protected String source;
protected CiVersionSource source;

/**
* The directory containing exported version information
Expand Down Expand Up @@ -77,7 +77,7 @@ public M withSession(MavenSession session) {
}

@SuppressWarnings("unchecked")
public M withSource(String source) {
public M withSource(CiVersionSource source) {
this.source = source;
return (M) this;
}
Expand All @@ -94,8 +94,8 @@ public M withScriptable(boolean scriptable) {
return (M) this;
}

protected CiVersion current() {
CiVersionSource source = source(this.source);
protected CiVersion current()
throws MojoFailureException {
return source.from(project, session);
}

Expand Down
Loading

0 comments on commit 364b7cb

Please sign in to comment.