From 1a9018cbbf45ab242d5eb7376e4564f03921b51b Mon Sep 17 00:00:00 2001 From: Simon Brown <1009874+simonbrowndotje@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:25:56 +0000 Subject: [PATCH] Wraps up version information. --- .../com/structurizr/cli/VersionCommand.java | 37 +------------- .../com/structurizr/cli/util/DateUtils.java | 43 +++++++++++++++++ .../com/structurizr/cli/util/Version.java | 48 +++++++++++++++++++ 3 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/structurizr/cli/util/DateUtils.java create mode 100644 src/main/java/com/structurizr/cli/util/Version.java diff --git a/src/main/java/com/structurizr/cli/VersionCommand.java b/src/main/java/com/structurizr/cli/VersionCommand.java index 66f3506..f0e54ac 100644 --- a/src/main/java/com/structurizr/cli/VersionCommand.java +++ b/src/main/java/com/structurizr/cli/VersionCommand.java @@ -1,18 +1,10 @@ package com.structurizr.cli; import com.structurizr.Workspace; -import com.structurizr.dsl.StructurizrDslParser; -import com.structurizr.export.plantuml.StructurizrPlantUMLExporter; -import com.structurizr.importer.documentation.DefaultDocumentationImporter; +import com.structurizr.cli.util.Version; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.io.InputStream; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Properties; - class VersionCommand extends AbstractCommand { private static final Log log = LogFactory.getLog(VersionCommand.class); @@ -20,33 +12,8 @@ class VersionCommand extends AbstractCommand { VersionCommand() { } - private static final String BUILD_VERSION_KEY = "build.number"; - private static final String BUILD_TIMESTAMP_KEY = "build.timestamp"; - private static final String GIT_COMMIT_KEY = "git.commit"; - - private static final String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - public void run(String... args) throws Exception { - String version = ""; - String buildTimestamp = ""; - String gitCommit = ""; - - try { - Properties buildProperties = new Properties(); - InputStream in = VersionCommand.class.getClassLoader().getResourceAsStream("build.properties"); - DateFormat format = new SimpleDateFormat(ISO_DATE_TIME_FORMAT); - if (in != null) { - buildProperties.load(in); - version = buildProperties.getProperty(BUILD_VERSION_KEY); - buildTimestamp = buildProperties.getProperty(BUILD_TIMESTAMP_KEY); - gitCommit = buildProperties.getProperty(GIT_COMMIT_KEY); - in.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - log.info("structurizr-cli: " + version); + log.info("structurizr-cli: " + new Version().getBuildNumber()); try { log.info("structurizr-java: " + Class.forName(Workspace.class.getCanonicalName()).getPackage().getImplementationVersion()); diff --git a/src/main/java/com/structurizr/cli/util/DateUtils.java b/src/main/java/com/structurizr/cli/util/DateUtils.java new file mode 100644 index 0000000..7cc8e29 --- /dev/null +++ b/src/main/java/com/structurizr/cli/util/DateUtils.java @@ -0,0 +1,43 @@ +package com.structurizr.cli.util; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +/** + * Utilities for calculating various points in time. + */ +public class DateUtils { + + public static final String UTC_TIME_ZONE = "UTC"; + public static final String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + public static Calendar getCalendar() { + return Calendar.getInstance(TimeZone.getTimeZone(UTC_TIME_ZONE)); + } + + public static Date getNow() { + return getCalendar().getTime(); + } + + public static Date removeMilliseconds(Date date) { + Calendar cal = getCalendar(); + cal.setTime(date); + cal.set(Calendar.MILLISECOND, 0); + + return cal.getTime(); + } + + public static String formatIsoDate(Date d) { + if (d != null) { + SimpleDateFormat sdf = new SimpleDateFormat(ISO_DATE_TIME_FORMAT); + sdf.setTimeZone(TimeZone.getTimeZone(UTC_TIME_ZONE)); + + return sdf.format(d); + } else { + return ""; + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/structurizr/cli/util/Version.java b/src/main/java/com/structurizr/cli/util/Version.java new file mode 100644 index 0000000..ecb1ea8 --- /dev/null +++ b/src/main/java/com/structurizr/cli/util/Version.java @@ -0,0 +1,48 @@ +package com.structurizr.cli.util; + +import java.io.InputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +public class Version { + + private static final String BUILD_VERSION_KEY = "build.number"; + private static final String BUILD_TIMESTAMP_KEY = "build.timestamp"; + private static final String GIT_COMMIT_KEY = "git.commit"; + + private static String version = ""; + private static Date buildTimestamp = new Date(); + private static String gitCommit; + + static { + try { + Properties buildProperties = new Properties(); + InputStream in = Version.class.getClassLoader().getResourceAsStream("build.properties"); + DateFormat format = new SimpleDateFormat(DateUtils.ISO_DATE_TIME_FORMAT); + if (in != null) { + buildProperties.load(in); + version = buildProperties.getProperty(BUILD_VERSION_KEY); + buildTimestamp = format.parse(buildProperties.getProperty(BUILD_TIMESTAMP_KEY)); + gitCommit = buildProperties.getProperty(GIT_COMMIT_KEY); + in.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public String getBuildNumber() { + return version; + } + + public Date getBuildTimestamp() { + return buildTimestamp; + } + + public String getGitCommit() { + return gitCommit; + } + +} \ No newline at end of file