Skip to content

Commit

Permalink
Added support for karaf feature files #41
Browse files Browse the repository at this point in the history
  • Loading branch information
b8 committed Aug 25, 2016
1 parent 2d2570c commit 3cb7a44
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.inventage.tools.versiontiger.internal.impl;

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Set;

import com.inventage.tools.versiontiger.MavenVersion;
import com.inventage.tools.versiontiger.util.FileHandler;
import com.inventage.tools.versiontiger.util.XmlHandler;

import de.pdark.decentxml.Element;

class KarafFeature {

private static final String ELEMENT_ROOT = "features";
private static final String ELEMENT_BUNDLE = "bundle";
private static final String BUNDLE_MVN_PREFIX = "mvn:";
private static final String GAV_SEPARATOR = "/";

private final File filePath;

private String content;

KarafFeature(File filePath) {
this.filePath = filePath;
}

Set<String> updateReferencesFor(String id, MavenVersion oldVersion, MavenVersion newVersion) {
XmlHandler xmlHandler = new XmlHandler();
Set<String> result = new LinkedHashSet<String>();

Element features = xmlHandler.getElement(getFileContent(), ELEMENT_ROOT);
for (Element feature : features.getChildren()) {
for (Element bundle : feature.getChildren()) {
if (ELEMENT_BUNDLE.equals(bundle.getName())) {
String bundleReference = updateBundleReferencesFor(id, oldVersion, newVersion, bundle);
if (bundleReference != null) {
result.add(bundleReference);
}
}
}
}

if (!result.isEmpty()) {
content = features.getDocument().toXML();
new FileHandler().writeFileContent(filePath, content);
}

return result;
}

private String updateBundleReferencesFor(String id, MavenVersion oldVersion, MavenVersion newVersion, Element bundle) {
String bundleReference = bundle.getTrimmedText();
if (bundleReference.startsWith(BUNDLE_MVN_PREFIX)) {
bundleReference = bundleReference.substring(4);
String[] gav = bundleReference.split(GAV_SEPARATOR, 3);
if (gav != null && gav.length == 3) {
if (id.equals(gav[1]) && (oldVersion == null || gav[2].equals(oldVersion.toString()))) {
bundleReference = BUNDLE_MVN_PREFIX + gav[0] + GAV_SEPARATOR + gav[1] + GAV_SEPARATOR + newVersion;
bundle.setText(bundleReference);
return bundle.getChildPath() + " = " + bundleReference;
}
}
}
return null;
}

private String getFileContent() {
if (content == null) {
content = new FileHandler().readFileContent(filePath);
}

return content;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.inventage.tools.versiontiger.internal.impl;

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Set;

import com.inventage.tools.versiontiger.MavenProject;
import com.inventage.tools.versiontiger.MavenVersion;
Expand Down Expand Up @@ -85,6 +87,7 @@ public void setVersion(MavenVersion newVersion) {
version = newVersion;

setProjectVersionInPom(newVersion);
setVersionInKarafFiles(oldVersion, newVersion);
}

private void setProjectVersionInPom(MavenVersion newVersion) {
Expand Down Expand Up @@ -151,6 +154,13 @@ public void updateReferencesFor(String id, MavenVersion oldVersion, MavenVersion
pomContent = projectElement.getDocument().toXML();
new FileHandler().writeFileContent(getPomXmlFile(), pomContent);
}

for (File karafFile : getKarafFiles()) {
Set<String> updatedReferences = new KarafFeature(karafFile).updateReferencesFor(id, oldVersion, newVersion);
for (String updatedKarafReference : updatedReferences) {
logReferenceSuccess(karafFile + ": " + updatedKarafReference, oldVersion, newVersion, id);
}
}
}
}

Expand Down Expand Up @@ -199,6 +209,36 @@ private boolean updateParent(Element projectElement, String id, MavenVersion old

return hasModifications;
}

private Set<File> getKarafFiles() {
Set<File> result = new LinkedHashSet<File>();

Element propertyElement = new XmlHandler().getElement(getPomContent(), "project/properties/versionTigerFiles");
if (propertyElement != null) {
String propertyValue = propertyElement.getText();
if (propertyValue != null) {
for (String fileDef : propertyValue.trim().split("\\s*,\\s*")) {
if (fileDef.startsWith("karaf:")) {
File karafFile = new FileHandler().createFileFromPath(projectPath, fileDef.substring(6));
if (karafFile.exists()) {
result.add(karafFile);
}
}
}
}
}

return result;
}

private void setVersionInKarafFiles(MavenVersion oldVersion, MavenVersion newVersion) {
for (File karafFile : getKarafFiles()) {
Set<String> updatedReferences = new KarafFeature(karafFile).updateReferencesFor(id, oldVersion, newVersion);
for (String updatedKarafReference : updatedReferences) {
logSuccess(karafFile + ": " + updatedKarafReference, oldVersion, newVersion);
}
}
}

private File getPomXmlFile() {
return new File(projectPath, "pom.xml");
Expand Down

0 comments on commit 3cb7a44

Please sign in to comment.