Skip to content

Commit

Permalink
#84 Add PomChangeParentVersion that preserves file formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocarvalho777 committed Feb 3, 2018
1 parent 19e7dd2 commit 4bde043
Show file tree
Hide file tree
Showing 37 changed files with 1,512 additions and 39 deletions.
5 changes: 5 additions & 0 deletions butterfly-utilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>xmlunit</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return hashCode(super.hashCode(),
groupId,
artifactId);
return hashCode(super.hashCode(), groupId, artifactId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.paypal.butterfly.utilities.operations.pom;

import com.paypal.butterfly.extensions.api.exception.TransformationDefinitionException;

import java.util.Objects;

/**
* Abstract Artifact POM operation.
*
* @author facarvalho
*/
public abstract class AbstractStaxArtifactPomOperation<T extends AbstractStaxArtifactPomOperation> extends AbstractStaxPomOperation<T> {

protected String groupId;
protected String artifactId;

public T setGroupId(String groupId) {
checkForBlankString("GroupId", groupId);
this.groupId = groupId;
return (T) this;
}

public T setArtifactId(String artifactId) {
checkForBlankString("ArtifactId",artifactId);
this.artifactId = artifactId;
return (T) this;
}

/**
* Set dependency group id and artifact id based on a String
* whose format is "groupId:artifactId". It throws a
* {@link TransformationDefinitionException} if the specified
* artifact String is invalid
*
* @param artifact the artifact formatted String
* @return this operation instance
*/
public T setArtifact(String artifact) {
checkForBlankString("artifact", artifact);
String[] split = artifact.split(":");
if (split.length != 2) {
throw new TransformationDefinitionException("Invalid artifact " + artifact);
}
groupId = split[0];
artifactId = split[1];
return (T) this;
}

public String getGroupId() {
return groupId;
}

public String getArtifactId() {
return artifactId;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof AbstractStaxArtifactPomOperation)) return false;

AbstractStaxArtifactPomOperation tu = (AbstractStaxArtifactPomOperation) obj;
if (!Objects.equals(tu.groupId, this.groupId)) return false;
if (!Objects.equals(tu.artifactId, this.artifactId)) return false;

return super.equals(obj);
}

@Override
public int hashCode() {
return hashCode(super.hashCode(), groupId, artifactId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.paypal.butterfly.utilities.operations.pom;

import com.paypal.butterfly.extensions.api.TOExecutionResult;
import com.paypal.butterfly.extensions.api.TransformationContext;
import com.paypal.butterfly.utilities.operations.pom.stax.AbstractStaxOperation;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
* Abstract POM operation.
*
* @author facarvalho
*/
abstract class AbstractStaxPomOperation<T extends AbstractStaxPomOperation> extends AbstractStaxOperation<T> {

/*
* Read the Maven pom file and returns an in-memory model of it
*/
protected Model getModel(File pomFile) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
FileInputStream fileInputStream = new FileInputStream(pomFile);
Model model = reader.read(fileInputStream);
fileInputStream.close();

return model;
}

@Override
protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) {
return super.execution(transformedAppFolder, transformationContext);
}

@Override
protected TOExecutionResult xmlExecution(File transformedAppFolder, TransformationContext transformationContext) throws XmlPullParserException, XMLStreamException, IOException {
return pomExecution(transformedAppFolder, transformationContext);
}

/*
* This abstract method is supposed to be developed by the subclasses executing POM file operations
*/
protected abstract TOExecutionResult pomExecution(File transformedAppFolder, TransformationContext transformationContext) throws XmlPullParserException, XMLStreamException, IOException;

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.paypal.butterfly.utilities.operations.pom;

import com.paypal.butterfly.extensions.api.TOExecutionResult;
import com.paypal.butterfly.extensions.api.TransformationContext;
import com.paypal.butterfly.extensions.api.exception.TransformationOperationException;
import com.paypal.butterfly.extensions.api.operations.AddElement;
import org.apache.maven.model.Model;
import com.paypal.butterfly.utilities.operations.pom.stax.EndElementEventCondition;
import com.paypal.butterfly.utilities.operations.pom.stax.StartElementEventCondition;
import org.apache.maven.model.Parent;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;

/**
* Add a parent artifact in a Maven POM file.
Expand All @@ -13,7 +22,7 @@
*
* @author facarvalho
*/
public class PomAddParent extends AbstractArtifactPomOperation<PomAddParent> implements AddElement<PomAddParent> {
public class PomAddParent extends AbstractStaxArtifactPomOperation<PomAddParent> implements AddElement<PomAddParent> {

private static final String DESCRIPTION = "Add parent artifact %s:%s in POM file %s";

Expand Down Expand Up @@ -88,44 +97,78 @@ public String getDescription() {
}

@Override
protected TOExecutionResult pomExecution(String relativePomFile, Model model) {
protected TOExecutionResult pomExecution(File transformedAppFolder, TransformationContext transformationContext) throws XmlPullParserException, XMLStreamException, IOException {

File pomFile = getAbsoluteFile(transformedAppFolder, transformationContext);
Parent existingParent = getModel(pomFile).getParent();
String details;
Parent existingParent = model.getParent();

String relativePomFile = getRelativePath(transformedAppFolder, pomFile);

if (existingParent != null) {
String message = String.format("Pom file %s already has a parent", getRelativePath());
String message = String.format("Pom file %s already has a parent", relativePomFile);

switch (ifPresent) {
case WarnNotAdd:
return TOExecutionResult.warning(this, new TransformationOperationException(message));
return TOExecutionResult.warning(this, message);
case NoOp:
return TOExecutionResult.noOp(this, message);
case Fail:
// Fail is the default
default:
return TOExecutionResult.error(this, new TransformationOperationException(message));
}
}

Parent newParent;
if(groupId != null && artifactId != null && version != null) {
newParent = new Parent();
newParent.setGroupId(groupId);
newParent.setArtifactId(artifactId);
newParent.setVersion(version);
model.setParent(newParent);
} else {
// FIXME this should be in a pre-validation
// FIXME this should be in a pre-validation
if(groupId == null && artifactId == null && version == null) {
throw new IllegalStateException("Invalid POM parent transformation operation");
}

XMLEventReader reader = getReader(transformedAppFolder, transformationContext);
XMLEventWriter writer = getWriter(transformedAppFolder, transformationContext);

if (existingParent != null && ifPresent.equals(IfPresent.WarnButAdd)) {
copyUntil(reader, writer, new StartElementEventCondition("parent"), true);
skipUntil(reader, new EndElementEventCondition("parent"));
writeNewParent(writer);
writer.add(eventFactory.createEndElement("", "","parent"));
} else {
copyUntil(reader, writer, new StartElementEventCondition("project"), true);
writer.add(eventFactory.createStartElement("", "", "parent"));
writeNewParent(writer);
writer.add(eventFactory.createEndElement("", "","parent"));
}

writer.add(reader);

Parent newParent = new Parent();
newParent.setGroupId(groupId);
newParent.setArtifactId(artifactId);
newParent.setVersion(version);

if (ifPresent.equals(IfPresent.Overwrite)) {
details = String.format("Parent for POM file (%s) has been set to %s", relativePomFile, newParent);
details = String.format("Parent for POM file %s has been set to %s", relativePomFile, newParent);
return TOExecutionResult.success(this, details);
} else {
details = String.format("Parent for POM file (%s) has been overwritten to %s", relativePomFile, newParent);
details = String.format("Parent for POM file %s has been overwritten to %s", relativePomFile, newParent);
return TOExecutionResult.warning(this, details);
}
}

private void writeNewParent(XMLEventWriter writer) throws XMLStreamException {

// FIXME
// Fix indentation

writer.add(eventFactory.createStartElement("", "", "groupId"));
writer.add(eventFactory.createCharacters(groupId));
writer.add(eventFactory.createEndElement("", "", "groupId"));
writer.add(eventFactory.createStartElement("", "", "artifactId"));
writer.add(eventFactory.createCharacters(artifactId));
writer.add(eventFactory.createEndElement("", "", "artifactId"));
writer.add(eventFactory.createStartElement("", "", "version"));
writer.add(eventFactory.createCharacters(version));
writer.add(eventFactory.createEndElement("", "", "version"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.apache.maven.model.Parent;

/**
* Changes the parent, or its version, in a Maven POM file.
* This transformation utility is deprecated.
* Please use {@link PomAddParent} or {@link PomChangeParentVersion} instead.
*
* @author facarvalho
*/
@Deprecated
public class PomChangeParent extends AbstractArtifactPomOperation<PomChangeParent> implements ChangeOrRemoveElement<PomChangeParent> {

private static final String DESCRIPTION = "Change parent artifact in POM file %s";
Expand Down
Loading

0 comments on commit 4bde043

Please sign in to comment.