Skip to content

Commit

Permalink
Wraps up version information.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Dec 2, 2024
1 parent 66e2f25 commit 1a9018c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/main/java/com/structurizr/cli/VersionCommand.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,19 @@
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);

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());
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/structurizr/cli/util/DateUtils.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
}

}
48 changes: 48 additions & 0 deletions src/main/java/com/structurizr/cli/util/Version.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 1a9018c

Please sign in to comment.