Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Manifest File Build Date #3447

Merged
merged 4 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions megamek/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.time.LocalDateTime

plugins {
id 'application'
id 'maven-publish'
Expand Down Expand Up @@ -64,9 +66,10 @@ ext {
jar {
archiveFileName = 'MegaMek.jar'
manifest {
attributes "Main-Class": mainClassName
attributes 'Class-Path' : project.sourceSets.main.runtimeClasspath.files
attributes "Main-Class" : mainClassName
attributes "Class-Path" : project.sourceSets.main.runtimeClasspath.files
.findAll { it.name.endsWith(".jar") }.collect { "${lib}/${it.name}" }.join(' ')
attributes "Build-Date" : LocalDateTime.now()
}
}

Expand Down
31 changes: 26 additions & 5 deletions megamek/src/megamek/MegaMek.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@

import javax.swing.*;
import java.io.*;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Locale;
import java.util.Vector;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

/**
* This is the primary MegaMek class.
Expand Down Expand Up @@ -222,23 +227,39 @@ private static void startGUI() {
* @return the underlying information for this launch of MegaMek
*/
public static String getUnderlyingInformation(final String originProject) {
return MegaMek.getUnderlyingInformation(originProject, MMConstants.PROJECT_NAME);
return getUnderlyingInformation(originProject, MMConstants.PROJECT_NAME);
}

/**
* @param originProject the launching project
* @param currentProject the currently described project
* @return the underlying information for this launch
*/
public static String getUnderlyingInformation(final String originProject, final String currentProject) {
return String.format("Starting %s v%s\n\tRelease Date: %s\n\tToday: %s\n\tOrigin Project: %s\n\tJava Vendor: %s\n\tJava Version: %s\n\tPlatform: %s %s (%s)\n\tSystem Locale: %s\n\tTotal memory available to %s: %,.0f GB",
currentProject, MMConstants.VERSION, MMConstants.RELEASE_DATE, LocalDate.now(),
originProject, System.getProperty("java.vendor"), System.getProperty("java.version"),
public static String getUnderlyingInformation(final String originProject,
final String currentProject) {
final LocalDateTime buildDate = getBuildDate();
return String.format("Starting %s v%s\n\tBuild Date: %s\n\tRelease Date: %s\n\tToday: %s\n\tOrigin Project: %s\n\tJava Vendor: %s\n\tJava Version: %s\n\tPlatform: %s %s (%s)\n\tSystem Locale: %s\n\tTotal memory available to %s: %,.0f GB",
currentProject, MMConstants.VERSION, ((buildDate == null) ? "N/A" : buildDate),
MMConstants.RELEASE_DATE, LocalDate.now(), originProject,
System.getProperty("java.vendor"), System.getProperty("java.version"),
System.getProperty("os.name"), System.getProperty("os.version"),
System.getProperty("os.arch"), Locale.getDefault(), currentProject,
Runtime.getRuntime().maxMemory() / Math.pow(2, 30));
}

public static @Nullable LocalDateTime getBuildDate() {
try {
final URL url = Thread.currentThread().getContextClassLoader().getResource(JarFile.MANIFEST_NAME);
if (url == null) {
return null;
}
final Attributes attributes = new Manifest(url.openStream()).getMainAttributes();
return LocalDateTime.parse(attributes.getValue("Build-Date"));
} catch (Exception ignored) {
return null;
}
}

/**
* This class parses the options passed into to MegaMek from the command line.
*/
Expand Down