|
| 1 | +package org.openapitools.codegen.cmd; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.time.OffsetDateTime; |
| 6 | +import java.time.format.DateTimeFormatter; |
| 7 | +import java.time.format.DateTimeParseException; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.Properties; |
| 10 | + |
| 11 | +import static org.openapitools.codegen.Constants.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Presents build-time information |
| 15 | + */ |
| 16 | +@SuppressWarnings({"java:S108"}) |
| 17 | +public class BuildInfo { |
| 18 | + private static final String VERSION_PLACEHOLDER = "${project.version}"; |
| 19 | + private static final String UNSET = "unset"; |
| 20 | + private static final String UNKNOWN = "unknown"; |
| 21 | + |
| 22 | + private static final Properties properties = new Properties(); |
| 23 | + |
| 24 | + static { |
| 25 | + try (InputStream is = BuildInfo.class.getResourceAsStream("/version.properties")) { |
| 26 | + Properties versionProps = new Properties(); |
| 27 | + versionProps.load(is); |
| 28 | + properties.putAll(versionProps); |
| 29 | + } catch (IOException ignored) { |
| 30 | + } |
| 31 | + try (InputStream is = BuildInfo.class.getResourceAsStream("/openapi-generator-git.properties")) { |
| 32 | + Properties gitProps = new Properties(); |
| 33 | + gitProps.load(is); |
| 34 | + properties.putAll(gitProps); |
| 35 | + } catch (IOException ignored) { |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets the version of the toolset. |
| 41 | + * |
| 42 | + * @return A semver string |
| 43 | + */ |
| 44 | + public String getVersion() { |
| 45 | + String version = (String) properties.getOrDefault("version", UNKNOWN); |
| 46 | + if (VERSION_PLACEHOLDER.equals(version)) { |
| 47 | + return UNSET; |
| 48 | + } else { |
| 49 | + return version; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the git commit SHA1 hash. Useful for differentiating between SNAPSHOT builds. |
| 55 | + * |
| 56 | + * @return A short git SHA |
| 57 | + */ |
| 58 | + public String getSha() { |
| 59 | + return (String) properties.getOrDefault("git.commit.id.abbrev", UNKNOWN); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the time when this tool was built. |
| 64 | + * |
| 65 | + * @return The time as {@link OffsetDateTime}, or {@link OffsetDateTime#MIN} if metadata cannot be parsed. |
| 66 | + */ |
| 67 | + public OffsetDateTime getBuildTime() { |
| 68 | + try { |
| 69 | + String time = (String) properties.getOrDefault("git.build.time", ""); |
| 70 | + return OffsetDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ROOT)); |
| 71 | + } catch (DateTimeParseException e) { |
| 72 | + return OffsetDateTime.MIN; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets the full version display text, as one would expect from a '--version' CLI option |
| 78 | + * |
| 79 | + * @return Human-readable version display information |
| 80 | + */ |
| 81 | + public String versionDisplayText() { |
| 82 | + StringBuilder sb = new StringBuilder(CLI_NAME); |
| 83 | + sb.append(" ").append(this.getVersion()).append(System.lineSeparator()); |
| 84 | + sb.append(" commit : ").append(this.getSha()).append(System.lineSeparator()); |
| 85 | + sb.append(" built : ").append(this.getBuildTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).append(System.lineSeparator()); |
| 86 | + sb.append(" source : ").append(GIT_REPO).append(System.lineSeparator()); |
| 87 | + sb.append(" docs : ").append(SITE).append(System.lineSeparator()); |
| 88 | + return sb.toString(); |
| 89 | + } |
| 90 | +} |
0 commit comments