Skip to content

Commit

Permalink
Support changing of version in pde.bnd files
Browse files Browse the repository at this point in the history
Currently the set-version mojo does not support updating the pde.bnd
file, this now adds support for updating pde.bnd files in similar
fashion like MANIFEST.MF

Fix #2706
  • Loading branch information
laeubi committed Aug 16, 2023
1 parent a9ab27d commit 2fcedec
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<extensions>
<extension>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-build</artifactId>
<version>${tycho-version}</version>
</extension>
</extensions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bundle-Name: Bnd
Bundle-SymbolicName: pde.bnd
Bundle-Vendor:
Bundle-Version: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.eclipse.tycho.its</groupId>
<artifactId>pde-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>bundle</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bundle-Name: Bnd
Bundle-SymbolicName: pde.bnd
Bundle-Vendor:
Bundle-Version: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.tycho.its</groupId>
<artifactId>pde.bnd</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>PDE-BND Bundle</name>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
package org.eclipse.tycho.test.versionsplugin;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

import org.apache.maven.it.Verifier;
import org.apache.maven.model.Model;
Expand All @@ -29,7 +31,7 @@

public class TychoVersionsPluginTest extends AbstractTychoIntegrationTest {

private final String VERSION = TychoVersion.getTychoVersion();
private static final String VERSION = TychoVersion.getTychoVersion();

/**
* <p>
Expand Down Expand Up @@ -80,6 +82,37 @@ public void updateTargetVersionTest() throws Exception {
&& targetContent.contains("sequenceNumber=\"12\""));
}

@Test
public void updateProjectVersionBndTest() throws Exception {
String expectedNewVersion = "1.2.3";

Verifier verifier = getVerifier("tycho-version-plugin/set-version/pde-bnd", true);

verifier.addCliOption("-DnewVersion=" + expectedNewVersion);
verifier.executeGoal("org.eclipse.tycho:tycho-versions-plugin:" + VERSION + ":set-version");

verifier.verifyErrorFreeLog();
Properties properties = new Properties();
properties.load(Files.newInputStream(new File(verifier.getBasedir(), "bundle/pde.bnd").toPath()));
String versionProperty = properties.getProperty("Bundle-Version");
assertNotNull("Bundle-Version is null", versionProperty);
assertEquals("Bundle-Version is not as expected!", expectedNewVersion, versionProperty);
}

@Test
public void updateProjectMetadataVersionBndTest() throws Exception {
String expectedNewVersion = "2.0.0.qualifier";

Verifier verifier = getVerifier("tycho-version-plugin/update-eclipse-metadata/pde-bnd", false, false);
verifier.executeGoal("org.eclipse.tycho:tycho-versions-plugin:" + VERSION + ":update-eclipse-metadata");
verifier.verifyErrorFreeLog();
Properties properties = new Properties();
properties.load(Files.newInputStream(new File(verifier.getBasedir(), "pde.bnd").toPath()));
String versionProperty = properties.getProperty("Bundle-Version");
assertNotNull("Bundle-Version is null", versionProperty);
assertEquals("Bundle-Version is not as expected!", expectedNewVersion, versionProperty);
}

/**
* Verifies that the update-pom goal of the tycho-version plug-in updates the
* version of a pom when the pom file is implicit. The command line for this
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.versions.bundle;

import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import java.util.Set;

class BndLine {

String eol;
String rawstring;
BndLine nextline;
String key;
String value;
String newValue;

boolean isContinuation() {
return rawstring.strip().endsWith("\\");
}

void parse() {
Properties properties = new Properties();
String collect = collect();
try {
properties.load(new StringReader(collect));
} catch (IOException e) {
throw new AssertionError("I/O error while reading a string!", e);
}
Set<String> names = properties.stringPropertyNames();
if (names.isEmpty()) {
return;
}
if (names.size() == 1) {
this.key = names.iterator().next();
this.value = properties.getProperty(key);
return;
}
throw new AssertionError("Line yields more than one property: " + collect);
}

String collect() {
if (nextline == null) {
return rawstring;
}
return rawstring + nextline.collect();
}

@Override
public String toString() {
if (key == null) {
return collect();
}
return key + ": " + value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.versions.bundle;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MutableBndFile {

private final List<BndLine> bndLines = new ArrayList<BndLine>();

public void setValue(String key, String value) {
if (key == null) {
return;
}
for (BndLine bndLine : bndLines) {
if (Objects.equals(key, bndLine.key)) {
bndLine.newValue = value;
return;
}
}
}

public String getValue(String key) {
if (key == null) {
return null;
}
for (BndLine bndLine : bndLines) {
if (Objects.equals(key, bndLine.key)) {
if (bndLine.newValue != null) {
return bndLine.newValue;
}
return bndLine.value;
}
}
return null;
}

public void write(File bndFile) throws IOException {
try (BufferedWriter writer = Files.newBufferedWriter(bndFile.toPath())) {
write(writer);
}
}

public void write(Writer writer) throws IOException {
for (BndLine bndLine : bndLines) {
if (bndLine.newValue == null || bndLine.key == null) {
writer.write(bndLine.collect());
} else {
String value = bndLine.value;
if (value == null) {
writer.write(bndLine.collect() + bndLine.newValue);
} else {
writer.write(bndLine.collect().replace(value, bndLine.newValue));
}
}
}
}

public static MutableBndFile read(File file) throws IOException {
try (InputStream is = new FileInputStream(file)) {
return read(is);
}
}

public static MutableBndFile read(InputStream is) throws IOException {
PushbackReader pushbackReader = new PushbackReader(
new BufferedReader(new InputStreamReader(new BufferedInputStream(is), StandardCharsets.UTF_8)), 1);
MutableBndFile bndFile = new MutableBndFile();
BndLine line;
BndLine last = null;
while ((line = readLine(pushbackReader)) != null) {
if (last != null && last.isContinuation()) {
last.nextline = line;
} else {
bndFile.bndLines.add(line);
}
last = line;
}
bndFile.bndLines.forEach(BndLine::parse);
return bndFile;

}

private static BndLine readLine(PushbackReader reader) throws IOException {
BndLine bndLine = new BndLine();
String str = MutableBundleManifest.readLineWithLineEnding(reader, lineEnding -> bndLine.eol = lineEnding);
if (str == null) {
return null;
}
bndLine.rawstring = str;
return bndLine;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.tycho.versions.engine.Versions;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static MutableBundleManifest read(InputStream is) throws IOException {
ManifestAttribute curr = null;

String str;
while ((str = readLineWithLineEnding(br, mf)) != null) {
while ((str = readLineWithLineEnding(br, mf::setLineEndingWhenFirstLine)) != null) {
if (str.trim().isEmpty()) {
break;
} else if (str.charAt(0) == ' ') {
Expand All @@ -90,26 +91,27 @@ public static MutableBundleManifest read(InputStream is) throws IOException {
return mf;
}

private static String readLineWithLineEnding(PushbackReader reader, MutableBundleManifest mf) throws IOException {
public static String readLineWithLineEnding(PushbackReader reader, Consumer<String> lineEndingConsumer)
throws IOException {
StringBuilder result = new StringBuilder();
int ch, lastch = -1;

while ((ch = reader.read()) != -1) {
if (lastch == '\r') {
if (ch == '\n') {
result.append((char) ch);
mf.setLineEndingWhenFirstLine("\r\n");
lineEndingConsumer.accept("\r\n");
} else {
reader.unread(ch);
mf.setLineEndingWhenFirstLine("\r");
lineEndingConsumer.accept("\r");
}
break;
}

result.append((char) ch);

if (ch == '\n' || ch == '\u2028' || ch == '\u2029' || ch == '\u0085') { // see Scanner#LINE_SEPARATOR_PATTERN
mf.setLineEndingWhenFirstLine(new String(new char[] { (char) ch }));
lineEndingConsumer.accept(new String(new char[] { (char) ch }));
break;
}

Expand Down
Loading

0 comments on commit 2fcedec

Please sign in to comment.