Skip to content

Commit

Permalink
Preserve the project's version as a semantic version number. Created …
Browse files Browse the repository at this point in the history
…two version of parsing version string, one falls back to generic version, the other returns VersionNumber.UNKNOWN when parsing fails like in previous bld versions.
  • Loading branch information
gbevin committed Jul 20, 2024
1 parent fa0ac43 commit d605ac1
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 60 deletions.
2 changes: 1 addition & 1 deletion core
Binary file modified lib/bld/bld-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion src/bld/java/rife/BldBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import rife.bld.BuildCommand;
import rife.bld.Cli;
import rife.bld.dependencies.VersionNumber;
import rife.bld.extension.ZipOperation;
import rife.bld.operations.*;
import rife.bld.publish.*;
Expand All @@ -28,7 +29,7 @@ public BldBuild()
throws Exception {
name = "bld";
mainClass = "rife.bld.Cli";
version = version(FileUtils.readString(new File(srcMainResourcesDirectory(), "BLD_VERSION")));
version = VersionNumber.parse(FileUtils.readString(new File(srcMainResourcesDirectory(), "BLD_VERSION")));

repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
scope(test)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/rife/bld/BaseProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class BaseProject extends BuildExecutor {
* @see #version()
* @since 1.5
*/
protected Version version = null;
protected VersionNumber version = null;
/**
* The project's main class.
*
Expand Down Expand Up @@ -684,7 +684,7 @@ public VersionNumber version(int major, int minor, int revision, String qualifie
* @since 1.5
*/
public Version version(String description) {
return VersionNumber.parse(description);
return Version.parse(description);
}

/**
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public String name() {
*
* @since 1.5
*/
public Version version() {
public VersionNumber version() {
if (version == null) {
throw new IllegalStateException("The version variable has to be set.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rife/bld/dependencies/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static Dependency parse(String dependency) {

var groupId = matcher.group("groupId");
var artifactId = matcher.group("artifactId");
var version = VersionNumber.parse(matcher.group("version"));
var version = Version.parse(matcher.group("version"));
var classifier = matcher.group("classifier");
var type = matcher.group("type");

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rife/bld/dependencies/PomDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Dependency convertToDependency() {
return new Dependency(
groupId(),
artifactId(),
VersionNumber.parse(version()),
Version.parse(version()),
classifier(),
type(),
exclusions(),
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/rife/bld/dependencies/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,42 @@
*/
package rife.bld.dependencies;

import static rife.bld.dependencies.VersionNumber.parseOrNull;

/**
* Represents the basic functionality of a dependency version.
*
* @since 2.0
*/
public interface Version extends Comparable<Version> {
/**
* Parses a version from a string representation.
* <p>
* If the string can't be successfully parsed as a semantic {@link VersionNumber},
* it will be parsed as a {@link VersionGeneric}.
*
* @param version the version string to parse
* @return the parsed version instance
* @since 2.0
*/
static Version parse(String version) {
if (version == null || version.isEmpty()) {
return VersionNumber.UNKNOWN;
}

var result = parseOrNull(version);
if (result != null) {
return result;
}

// bld doesn't support version ranges at this time
if (version.startsWith("[") || version.startsWith("(")) {
return VersionNumber.UNKNOWN;
}

return new VersionGeneric(version);
}

/**
* Retrieves the qualifier of the version.
*
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/rife/bld/dependencies/VersionGeneric.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public VersionGeneric(String version) {
}

/**
* Returns this instance tokenized representation as unmodifiable list.
*
* @since 2.0
* Visible for testing.
*/
public List<Item> asItems() {
List<Item> asItems() {
return items_;
}

Expand All @@ -72,7 +70,7 @@ private static List<Item> parse(String version) {
/**
* Visible for testing.
*/
public static void trimPadding(List<Item> items) {
static void trimPadding(List<Item> items) {
Boolean number = null;
var end = items.size() - 1;
for (var i = end; i > 0; i--) {
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/rife/bld/dependencies/VersionNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,35 @@ public record VersionNumber(Integer major, Integer minor, Integer revision, Stri
/**
* Parses a version number from a string representation.
* <p>
* If the string can't be successfully parsed, {@link VersionNumber#UNKNOWN} will be returned.
* If the string can't be successfully parsed as a semantic version,
* {@link VersionNumber#UNKNOWN} will be returned.
*
* @param version the version string to parse
* @return a parsed instance of {@code VersionNumber}; or
* {@link VersionNumber#UNKNOWN} when the string couldn't be parsed
* @since 1.5
*/
public static Version parse(String version) {
public static VersionNumber parse(String version) {
if (version == null || version.isEmpty()) {
return UNKNOWN;
}

var result = parseOrNull(version);
if (result == null) {
result = UNKNOWN;
}

return result;
}

static VersionNumber parseOrNull(String version) {
if (version == null) {
return null;
}

var matcher = VERSION_PATTERN.matcher(version);
if (!matcher.matches()) {
// bld doesn't support version ranges at this time
if (version.startsWith("[") || version.startsWith("(")) {
return UNKNOWN;
}
return new VersionGeneric(version);
return null;
}

var major = matcher.group("major");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/rife/bld/dependencies/Xml2MavenMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void startElement(String uri, String localName, String qName, Attributes

public void endElement(String uri, String localName, String qName) {
switch (qName) {
case "latest" -> latest_ = VersionNumber.parse(characterData_.toString());
case "release" -> release_ = VersionNumber.parse(characterData_.toString());
case "version" -> versions_.add(VersionNumber.parse(characterData_.toString()));
case "latest" -> latest_ = Version.parse(characterData_.toString());
case "release" -> release_ = Version.parse(characterData_.toString());
case "version" -> versions_.add(Version.parse(characterData_.toString()));
case "timestamp" -> snapshotTimestamp_ = characterData_.toString();
case "buildNumber" -> snapshotBuildNumber_ = Integer.parseInt(characterData_.toString());
case "snapshot" -> isSnapshot_ = true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rife/bld/dependencies/Xml2MavenPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void endElement(String uri, String localName, String qName) {
switch (qName) {
case "parent" -> {
if (isChildOfProject()) {
var parent_dependency = new Dependency(resolveMavenProperties(lastGroupId_), resolveMavenProperties(lastArtifactId_), VersionNumber.parse(resolveMavenProperties(lastVersion_)));
var parent_dependency = new Dependency(resolveMavenProperties(lastGroupId_), resolveMavenProperties(lastArtifactId_), Version.parse(resolveMavenProperties(lastVersion_)));
var parent = new DependencyResolver(resolution_, retriever_, repositories_, parent_dependency).getMavenPom(parent_);

parent.mavenProperties_.keySet().removeAll(mavenProperties_.keySet());
Expand All @@ -224,7 +224,7 @@ public void endElement(String uri, String localName, String qName) {
var dependency = new PomDependency(lastGroupId_, lastArtifactId_, lastVersion_, lastClassifier_, lastType_, lastScope_, lastOptional_, exclusions_, parent_);
if (collectDependencyManagement_) {
if (dependency.isPomImport()) {
var import_dependency = new Dependency(resolveMavenProperties(lastGroupId_), resolveMavenProperties(lastArtifactId_), VersionNumber.parse(resolveMavenProperties(lastVersion_)));
var import_dependency = new Dependency(resolveMavenProperties(lastGroupId_), resolveMavenProperties(lastArtifactId_), Version.parse(resolveMavenProperties(lastVersion_)));
var imported_pom = new DependencyResolver(resolution_, retriever_, repositories_, import_dependency).getMavenPom(parent_);
imported_pom.dependencyManagement_.keySet().removeAll(dependencyManagement_.keySet());
var resolved_dependencies = new LinkedHashSet<PomDependency>();
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/rife/bld/dependencies/TestDependencySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ void testPopulation() {
@Test
void testAddAll() {
var set1 = new DependencySet()
.include(new Dependency("org.eclipse.jetty", "jetty-server", VersionNumber.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty.toolchain", "jetty-jakarta-servlet-api", VersionNumber.parse("5.0.2")))
.include(new Dependency("org.eclipse.jetty", "jetty-http", VersionNumber.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty", "jetty-io", VersionNumber.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty", "jetty-util", VersionNumber.parse("11.0.14")))
.include(new Dependency("org.slf4j", "slf4j-api", VersionNumber.parse("2.0.5")));
.include(new Dependency("org.eclipse.jetty", "jetty-server", Version.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty.toolchain", "jetty-jakarta-servlet-api", Version.parse("5.0.2")))
.include(new Dependency("org.eclipse.jetty", "jetty-http", Version.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty", "jetty-io", Version.parse("11.0.14")))
.include(new Dependency("org.eclipse.jetty", "jetty-util", Version.parse("11.0.14")))
.include(new Dependency("org.slf4j", "slf4j-api", Version.parse("2.0.5")));

var set2 = new DependencySet()
.include(new Dependency("org.slf4j", "slf4j-simple", VersionNumber.parse("2.0.6")))
.include(new Dependency("org.slf4j", "slf4j-api", VersionNumber.parse("2.0.6")));
.include(new Dependency("org.slf4j", "slf4j-simple", Version.parse("2.0.6")))
.include(new Dependency("org.slf4j", "slf4j-api", Version.parse("2.0.6")));

var set_union1 = new DependencySet(set1);
set_union1.addAll(set2);
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/rife/bld/dependencies/TestVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.dependencies;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TestVersion {
@Test
void testParsing() {
assertEquals(Version.parse("1"), new VersionNumber(1, 0, 0, null));
assertEquals(Version.parse("1.0"), new VersionNumber(1, 0, 0, null));
assertEquals(Version.parse("1.0.0"), new VersionNumber(1, 0, 0, null));

assertEquals(Version.parse("1.2"), new VersionNumber(1, 2, 0, null));
assertEquals(Version.parse("1.2.3"), new VersionNumber(1, 2, 3, null));

assertEquals(Version.parse("1-rc1-SNAPSHOT"), new VersionNumber(1, 0, 0, "rc1-SNAPSHOT"));
assertEquals(Version.parse("1.2-rc1-SNAPSHOT"), new VersionNumber(1, 2, 0, "rc1-SNAPSHOT"));
assertEquals(Version.parse("1.2.3-rc1-SNAPSHOT"), new VersionNumber(1, 2, 3, "rc1-SNAPSHOT"));

assertEquals(Version.parse("11.22"), new VersionNumber(11, 22, 0, null));
assertEquals(Version.parse("11.22.33"), new VersionNumber(11, 22, 33, null));
assertEquals(Version.parse("11.22.33-eap"), new VersionNumber(11, 22, 33, "eap"));

assertEquals(Version.parse("11.fortyfour"), new VersionNumber(11, 0, 0, "fortyfour"));

assertEquals(Version.parse("1.0.0.0"), new VersionNumber(1, 0, 0, "0"));
assertEquals(Version.parse("1.0.0.0.0.0.0"), new VersionNumber(1, 0, 0, "0.0.0.0"));
assertEquals(Version.parse("1.2.3.4-rc1-SNAPSHOT"), new VersionNumber(1, 2, 3, "4-rc1-SNAPSHOT"));
assertEquals(Version.parse("1.2.3.4.rc1-SNAPSHOT"), new VersionNumber(1, 2, 3, "4.rc1-SNAPSHOT"));

assertEquals(Version.parse("1.2.3_4"), new VersionNumber(1, 2, 0, "3_4"));
assertEquals(Version.parse("1.54b"), new VersionNumber(1, 0, 0, "54b"));

assertEquals(Version.parse("2024-02"), new VersionNumber(2024, null, null, "02"));
assertEquals(Version.parse("2.0-05"), new VersionNumber(2, 0, null, "05"));
assertEquals(Version.parse("2024.02"), new VersionNumber(2024, null, null, "02", "."));
assertEquals(Version.parse("2.0.05"), new VersionNumber(2, 0, null, "05", "."));

assertEquals(Version.parse("v3-rev20240514-2.0.0"), new VersionGeneric("v3-rev20240514-2.0.0"));
}

@Test
void testInvalidParsed() {
assertEquals(Version.parse(null), VersionNumber.UNKNOWN);
assertEquals(Version.parse(""), VersionNumber.UNKNOWN);
assertEquals(Version.parse("foo"), new VersionGeneric("foo"));
assertEquals(Version.parse("1."), new VersionGeneric("1."));
assertEquals(Version.parse("1.2.3-"), new VersionGeneric("1.2.3-"));
assertEquals(Version.parse("."), new VersionGeneric("."));
assertEquals(Version.parse("_"), new VersionGeneric("_"));
assertEquals(Version.parse("-"), new VersionGeneric("-"));
assertEquals(Version.parse(".1"), new VersionGeneric(".1"));
assertEquals(Version.parse("a.1"), new VersionGeneric("a.1"));
assertEquals(Version.parse("1_2"), new VersionGeneric("1_2"));
assertEquals(Version.parse("1_2_2"), new VersionGeneric("1_2_2"));
}

@Test
void testStringRepresentation() {
assertEquals(Version.parse("1.0").toString(), "1.0");
assertEquals(Version.parse("1.2.3").toString(), "1.2.3");
assertEquals(Version.parse("1.2.3-4").toString(), "1.2.3-4");
assertEquals(Version.parse("1.2.3.4").toString(), "1.2.3.4");
assertEquals(Version.parse("1-rc-1").toString(), "1-rc-1");
assertEquals(Version.parse("1.2.3-rc-1").toString(), "1.2.3-rc-1");
assertEquals(Version.parse("1.2.3.rc-1").toString(), "1.2.3.rc-1");
}
}
20 changes: 10 additions & 10 deletions src/test/java/rife/bld/dependencies/TestVersionNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ void testIsSnapshot() {
void testInvalidParsed() {
assertEquals(VersionNumber.parse(null), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse(""), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("foo"), new VersionGeneric("foo"));
assertEquals(VersionNumber.parse("1."), new VersionGeneric("1."));
assertEquals(VersionNumber.parse("1.2.3-"), new VersionGeneric("1.2.3-"));
assertEquals(VersionNumber.parse("."), new VersionGeneric("."));
assertEquals(VersionNumber.parse("_"), new VersionGeneric("_"));
assertEquals(VersionNumber.parse("-"), new VersionGeneric("-"));
assertEquals(VersionNumber.parse(".1"), new VersionGeneric(".1"));
assertEquals(VersionNumber.parse("a.1"), new VersionGeneric("a.1"));
assertEquals(VersionNumber.parse("1_2"), new VersionGeneric("1_2"));
assertEquals(VersionNumber.parse("1_2_2"), new VersionGeneric("1_2_2"));
assertEquals(VersionNumber.parse("foo"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("1."), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("1.2.3-"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("."), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("_"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("-"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse(".1"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("a.1"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("1_2"), VersionNumber.UNKNOWN);
assertEquals(VersionNumber.parse("1_2_2"), VersionNumber.UNKNOWN);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ void testExecutionProvidedTest()
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-core", new VersionNumber(10,1,12)))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-jasper", new VersionNumber(10,1,12)))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));
operation.dependencies().scope(Scope.test)
.include(new Dependency("org.jsoup", "jsoup", new VersionNumber(1,16,1)))
.include(new Dependency("jakarta.servlet", "jakarta.servlet-api", new VersionNumber(5,0,0)))
.include(new Dependency("org.eclipse.jetty", "jetty-server", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));

operation.execute();

Expand Down Expand Up @@ -306,13 +306,13 @@ void testFromProjectProvidedTest()
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-core", new VersionNumber(10,1,12)))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-jasper", new VersionNumber(10,1,12)))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));
project.dependencies().scope(Scope.test)
.include(new Dependency("org.jsoup", "jsoup", new VersionNumber(1,16,1)))
.include(new Dependency("jakarta.servlet", "jakarta.servlet-api", new VersionNumber(5,0,0)))
.include(new Dependency("org.eclipse.jetty", "jetty-server", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));

var operation = new DependencyTreeOperation()
.fromProject(project);
Expand Down Expand Up @@ -405,13 +405,13 @@ void testFromProjectExtensions()
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-core", new VersionNumber(10,1,12)))
.include(new Dependency("org.apache.tomcat.embed", "tomcat-embed-jasper", new VersionNumber(10,1,12)))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));
project.dependencies().scope(Scope.test)
.include(new Dependency("org.jsoup", "jsoup", new VersionNumber(1,16,1)))
.include(new Dependency("jakarta.servlet", "jakarta.servlet-api", new VersionNumber(5,0,0)))
.include(new Dependency("org.eclipse.jetty", "jetty-server", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("org.eclipse.jetty", "jetty-servlet", new VersionNumber(11,0,15)).exclude("*", "jetty-jakarta-servlet-api"))
.include(new Dependency("net.imagej", "ij", VersionNumber.parse("1.54d")));
.include(new Dependency("net.imagej", "ij", Version.parse("1.54d")));

var operation = new DependencyTreeOperation()
.fromProject(project);
Expand Down
Loading

0 comments on commit d605ac1

Please sign in to comment.