Browse Source

adopt semantic versioning for plugin

pull/4/head
Decebal Suiu 11 years ago
parent
commit
5c223d79b2
  1. 126
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginVersion.java
  2. 4
      pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java

126
pf4j/src/main/java/ro/fortsoft/pf4j/PluginVersion.java

@ -12,19 +12,20 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import java.util.ArrayList; import ro.fortsoft.pf4j.util.StringUtils;
import java.util.List;
import java.util.StringTokenizer; import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Represents the version of a Plugin and allows versions to be compared. * Represents the version of a Plugin and allows versions to be compared.
* Version identifiers have five components. * Version following semantic defined by <a href="http://semver.org/">Semantic Versioning</a> document.
* Version identifiers have four components.
* *
* 1. Major version. A non-negative integer. * 1. Major version. A non-negative integer.
* 2. Minor version. A non-negative integer. * 2. Minor version. A non-negative integer.
* 3. Release version. A non-negative integer. * 3. Patch version. A non-negative integer.
* 4. Build version. A non-negative integer. * 4. Qualifier. A text string.
* 5. Qualifier. A text string.
* *
* This class is immutable. * This class is immutable.
* *
@ -32,74 +33,50 @@ import java.util.StringTokenizer;
*/ */
public class PluginVersion implements Comparable<PluginVersion> { public class PluginVersion implements Comparable<PluginVersion> {
private static final String FORMAT = "(\\d+)\\.(\\d+)(?:\\.)?(\\d*)(\\.|-|\\+)?([0-9A-Za-z-.]*)?";
private static final Pattern PATTERN = Pattern.compile(FORMAT);
private int major; private int major;
private int minor; private int minor;
private int release; private int patch;
private int build; private String separator;
private String qualifier; private String qualifier;
private PluginVersion() { public PluginVersion(int major, int minor, int patch) {
}
public PluginVersion(int major, int minor, int release) {
this.major = major; this.major = major;
this.minor = minor; this.minor = minor;
this.release = release; this.patch = patch;
} }
public PluginVersion(int major, int minor, int release, int build) { public PluginVersion(int major, int minor, int patch, String separator, String qualifier) {
this.major = major; this.major = major;
this.minor = minor; this.minor = minor;
this.release = release; this.patch = patch;
this.build = build; this.separator = separator;
}
public PluginVersion(int major, int minor, int release, int build, String qualifier) {
this.major = major;
this.minor = minor;
this.release = release;
this.build = build;
this.qualifier = qualifier; this.qualifier = qualifier;
} }
public static PluginVersion createVersion(String version) { public static PluginVersion createVersion(String version) {
if (version == null) { Matcher matcher = PATTERN.matcher(version);
return new PluginVersion(); if (!matcher.matches()) {
throw new IllegalArgumentException("'" + version + "' does not match format '" + FORMAT + "'");
} }
PluginVersion v = new PluginVersion();
StringTokenizer st = new StringTokenizer(version, ".");
List<String> tmp = new ArrayList<String>();
for (int i = 0; st.hasMoreTokens() && i < 4; i++) {
tmp.add(st.nextToken());
}
int n = tmp.size(); int major = Integer.valueOf(matcher.group(1));
switch (n) { int minor = Integer.valueOf(matcher.group(2));
case 0 : int patch;
break; String patchMatch = matcher.group(3);
case 1 : if (StringUtils.isNotEmpty(patchMatch)) {
v.major = Integer.parseInt(tmp.get(0)); patch = Integer.valueOf(patchMatch);
break; } else {
case 2 : patch = 0;
v.major = Integer.parseInt(tmp.get(0));
v.minor = Integer.parseInt(tmp.get(1));
break;
case 3 :
v.major = Integer.parseInt(tmp.get(0));
v.minor = Integer.parseInt(tmp.get(1));
v.release = Integer.parseInt(tmp.get(2));
break;
case 4 :
v.major = Integer.parseInt(tmp.get(0));
v.minor = Integer.parseInt(tmp.get(1));
v.release = Integer.parseInt(tmp.get(2));
v.build = Integer.parseInt(tmp.get(3));
break;
} }
String separator = matcher.group(4);
String qualifier = matcher.group(5);
return v; return new PluginVersion(major, minor, patch, separator, "".equals(qualifier) ? null : qualifier);
} }
public int getMajor() { public int getMajor() {
@ -110,12 +87,8 @@ public class PluginVersion implements Comparable<PluginVersion> {
return this.minor; return this.minor;
} }
public int getRelease() { public int getPatch() {
return this.release; return this.patch;
}
public int getBuild() {
return this.build;
} }
public String getQualifier() { public String getQualifier() {
@ -128,9 +101,10 @@ public class PluginVersion implements Comparable<PluginVersion> {
sb.append('.'); sb.append('.');
sb.append(minor); sb.append(minor);
sb.append('.'); sb.append('.');
sb.append(release); sb.append(patch);
sb.append('.'); if (separator != null) {
sb.append(build); sb.append(separator);
}
if (qualifier != null) { if (qualifier != null) {
sb.append(qualifier); sb.append(qualifier);
} }
@ -138,6 +112,7 @@ public class PluginVersion implements Comparable<PluginVersion> {
return sb.toString(); return sb.toString();
} }
@Override
public int compareTo(PluginVersion version) { public int compareTo(PluginVersion version) {
if (version.major > major) { if (version.major > major) {
return 1; return 1;
@ -151,36 +126,19 @@ public class PluginVersion implements Comparable<PluginVersion> {
return -1; return -1;
} }
if (version.release > release) { if (version.patch > patch) {
return 1;
} else if (version.release < release) {
return -1;
}
if (version.build > build) {
return 1; return 1;
} else if (version.build < build) { } else if (version.patch < patch) {
return -1; return -1;
} }
return 0; return 0;
} }
/*
private String extractQualifier(String token) {
StringTokenizer st = new StringTokenizer(token, "-");
if (st.countTokens() == 2) {
return st.
}
}
*/
// for test only // for test only
public static void main(String[] args) { public static void main(String[] args) {
PluginVersion v = PluginVersion.createVersion("4.0.0.123"); PluginVersion v = PluginVersion.createVersion("1.2.3-SNAPSHOT");
System.out.println(v.toString()); System.out.println(v.toString());
// v = PluginVersion.createVersion("4.0.0.123-alpha");
// System.out.println(v.toString());
PluginVersion v1 = PluginVersion.createVersion("4.1.0"); PluginVersion v1 = PluginVersion.createVersion("4.1.0");
System.out.println(v1.toString()); System.out.println(v1.toString());
PluginVersion v2 = PluginVersion.createVersion("4.0.32"); PluginVersion v2 = PluginVersion.createVersion("4.0.32");

4
pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java

@ -21,4 +21,8 @@ public class StringUtils {
return (str == null) || str.isEmpty(); return (str == null) || str.isEmpty();
} }
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
} }

Loading…
Cancel
Save