Skip to content

Commit

Permalink
Print the version number from the manifest file
Browse files Browse the repository at this point in the history
  • Loading branch information
robcos committed Jan 16, 2024
1 parent c0a06a8 commit e144150
Showing 1 changed file with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.jar.Manifest;

/** Contains the CLI arguments that the script was invoked with. */
final class EvaluationSettings {
Expand Down Expand Up @@ -200,6 +204,13 @@ public static Optional<EvaluationSettings> parseFromArguments(String... argument
.desc("Turn on verbose mode. Optional.")
.hasArg(false)
.build();
Option versionOption =
Option.builder()
.longOpt("version")
.desc("Show the script's version and quit.")
.hasArg(false)
.build();

Option estimateOptimizationOption =
Option.builder()
.longOpt("estimate-optimization")
Expand All @@ -210,8 +221,9 @@ public static Optional<EvaluationSettings> parseFromArguments(String... argument
Option applyV1OffloadLimitationsOption =
Option.builder()
.longOpt("apply-v1-offload-limitations")
.desc("Whether or not V1 offloading limitations should be applied to " +
"the calculation. I.e. a maximum of 2 layers and two clocks.")
.desc(
"Whether or not V1 offloading limitations should be applied to the"
+ " calculation. I.e. a maximum of 2 layers and two clocks.")
.hasArg(false)
.build();

Expand Down Expand Up @@ -245,6 +257,7 @@ public static Optional<EvaluationSettings> parseFromArguments(String... argument
options.addOption(disableOldStyleClocksOption);
options.addOption(disableAmbientDeduplicationOption);
options.addOption(verboseOption);
options.addOption(versionOption);
options.addOption(applyV1OffloadLimitationsOption);
options.addOption(estimateOptimizationOption);
options.addOption(greedyEvaluationSwitchOption);
Expand All @@ -255,6 +268,11 @@ public static Optional<EvaluationSettings> parseFromArguments(String... argument
CommandLineParser parser = new DefaultParser();
try {
CommandLine line = parser.parse(options, arguments);

if (line.hasOption(versionOption)) {
printVersion();
System.exit(0);
}
validateSchemaVersion(line.getOptionValue(schemaVersionOption));
EvaluationSettings evaluationSettings =
new EvaluationSettings(
Expand Down Expand Up @@ -295,7 +313,7 @@ public static Optional<EvaluationSettings> parseFromArguments(String... argument
evaluationSettings.reportMode = true;
}
return Optional.of(evaluationSettings);
} catch (ParseException e) {
} catch (Exception e) {
System.out.println("Error: " + e.getLocalizedMessage());
new HelpFormatter().printHelp(cliInvokeCommand, options, true);
return Optional.empty();
Expand All @@ -310,4 +328,20 @@ private static void validateSchemaVersion(String schemaVersionOption) throws Par
String.join(", ", SUPPORTED_VERSIONS)));
}
}

private static void printVersion() throws IOException {
Enumeration<URL> resources =
EvaluationSettings.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
Manifest manifest = new Manifest(resources.nextElement().openStream());
String version = manifest.getMainAttributes().getValue("Version");

String hash = manifest.getMainAttributes().getValue("Git-Hash");
System.out.println(
"memory-footprint version: "
+ (version != null ? version : "n/a")
+ " hash: "
+ (hash != null ? hash : "n/a"));
}
}
}

0 comments on commit e144150

Please sign in to comment.