Skip to content

Commit

Permalink
Merge d5b7abd into fb144bb
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaapo committed May 31, 2023
2 parents fb144bb + d5b7abd commit 4c28834
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,137 @@

package org.apache.eventmesh.runtime.constants;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.Objects;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EventMeshVersion {

public static final String CURRENT_VERSION = Version.V3_0_0.name();
private static String CURRENT_VERSION = "";

private static final String VERSION_KEY = "Implementation-Version";

private static final String SPEC_VERSION_KEY = "Specification-Version";

/**
* The version pattern in build.gradle. In general, it is like version='0.0.1' or version="0.0.1" if exists.
*/
private static final String VERSION_PATTERN = "version\\s*=\\s*['\"](.+)['\"]";

/**
* @return Eg: "v0.0.1-release"
*/
public static String getCurrentVersionDesc() {
return CURRENT_VERSION.replace("V", "")
.replace("_", ".")
.replace("_SNAPSHOT", "-SNAPSHOT");
if (StringUtils.isNotBlank(getCurrentVersion())) {
return "v" + CURRENT_VERSION;

Check warning on line 59 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L59

Added line #L59 was not covered by tests
}
return "";
}

/**
* @return Eg: "0.0.1-release"
*/
public static String getCurrentVersion() {
if (StringUtils.isNotBlank(CURRENT_VERSION)) {
return CURRENT_VERSION;

Check warning on line 69 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L69

Added line #L69 was not covered by tests
}
// get version from jar.
getVersionFromJarFile();
// get version from build file.
if (StringUtils.isBlank(CURRENT_VERSION)) {
getVersionFromBuildFile();
}
return CURRENT_VERSION;
}

private static void getVersionFromJarFile() {
// get version from MANIFEST.MF in jar.
CodeSource codeSource = EventMeshVersion.class.getProtectionDomain().getCodeSource();
if (Objects.isNull(codeSource)) {
log.warn("Failed to get CodeSource for EventMeshVersion.class");
return;

Check warning on line 85 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L84-L85

Added lines #L84 - L85 were not covered by tests
}
URL url = codeSource.getLocation();
if (Objects.isNull(url)) {
log.warn("Failed to get URL for EventMeshVersion.class");
return;

Check warning on line 90 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L89-L90

Added lines #L89 - L90 were not covered by tests
}
try (JarFile jarFile = new JarFile(url.getPath())) {
Manifest manifest = jarFile.getManifest();
Attributes attributes = manifest.getMainAttributes();

Check warning on line 94 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L92-L94

Added lines #L92 - L94 were not covered by tests
CURRENT_VERSION = StringUtils.isBlank(attributes.getValue(VERSION_KEY))
? attributes.getValue(SPEC_VERSION_KEY) : attributes.getValue(VERSION_KEY);

Check warning on line 96 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L96

Added line #L96 was not covered by tests

// get version from the file name of jar.
if (StringUtils.isBlank(CURRENT_VERSION)) {
getVersionFromJarFileName(url.getFile());

Check warning on line 100 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L100

Added line #L100 was not covered by tests
}
} catch (IOException e) {
log.error("Failed to load project version from MANIFEST.MF due to IOException {}.", e.getMessage());
}

Check warning on line 104 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L104

Added line #L104 was not covered by tests
}

private static void getVersionFromBuildFile() {
String projectDir = System.getProperty("user.dir");

String gradlePropertiesPath = projectDir + File.separator + "gradle.properties";
Properties properties = new Properties();
try (FileInputStream fis = new FileInputStream(gradlePropertiesPath)) {
properties.load(fis);
CURRENT_VERSION = properties.getProperty("version");
} catch (IOException e) {
log.error("Failed to load version from gradle.properties due to IOException {}.", e.getMessage());

Check warning on line 116 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L115-L116

Added lines #L115 - L116 were not covered by tests
}

if (StringUtils.isBlank(CURRENT_VERSION)) {
String buildGradlePath = projectDir + File.separator + "build.gradle";
try {
File buildFile = new File(buildGradlePath);
String content = new String(Files.readAllBytes(buildFile.toPath()));
Pattern pattern = Pattern.compile(VERSION_PATTERN);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
CURRENT_VERSION = matcher.group(1);

Check warning on line 127 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L127

Added line #L127 was not covered by tests
} else {
log.warn("Failed to load version from build.gradle due to missing the configuration of \"version='xxx'\".");
}
} catch (IOException e) {
log.error("Failed to load version from build.gradle due to IOException {}.", e.getMessage());

Check warning on line 132 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L131-L132

Added lines #L131 - L132 were not covered by tests
}
}
}

public enum Version {
V3_0_0,
V3_0_1,
V3_1_0,
V3_2_0,
V3_3_0
// path: /.../.../eventmesh-runtime-0.0.1-xxx.jar, version: 0.0.1-xxx
private static void getVersionFromJarFileName(String path) {
if (!StringUtils.isEmpty(path) && path.endsWith(".jar")) {
Path filePath = Paths.get(path);
String fileName = filePath.getFileName().toString();
fileName = fileName.replace(".jar", "");
Pattern pattern = Pattern.compile("-\\d");
Matcher matcher = pattern.matcher(fileName);

Check warning on line 144 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L140-L144

Added lines #L140 - L144 were not covered by tests
if (matcher.find()) {
int index = matcher.start();
CURRENT_VERSION = fileName.substring(index + 1);
} else {
log.info("Failed to load version from jar name due to missing related info.");

Check warning on line 149 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshVersion.java#L146-L149

Added lines #L146 - L149 were not covered by tests
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.eventmesh.runtime.util;

import static org.apache.eventmesh.runtime.constants.EventMeshVersion.getCurrentVersionDesc;

import org.apache.commons.lang3.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -51,7 +55,8 @@ public class BannerUtil {
+ " / ____|_ _____ _ __ | |_| \\/ | ___ ___| |__ " + System.lineSeparator()
+ " | __|\\ \\ / / _ | '_ \\| __| |\\/| |/ _ |/ __| '_ \\ " + System.lineSeparator()
+ " | |___ \\ V / __| | | | |_| | | | __|\\__ \\ | | |" + System.lineSeparator()
+ " \\ ____| \\_/ \\___|_| |_|\\__|_| |_|\\___||___/_| |_|";
+ " \\ ____| \\_/ \\___|_| |_|\\__|_| |_|\\___||___/_| |_| "
+ (StringUtils.isNotBlank(getCurrentVersionDesc()) ? "(" + getCurrentVersionDesc() + ")" : "");

public static void generateBanner() {
String banner =
Expand Down

0 comments on commit 4c28834

Please sign in to comment.