Skip to content

Commit

Permalink
Rewrite version command to show information about Ava and Watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
Senither committed Feb 7, 2019
1 parent 1a9fb46 commit 8bef016
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
apply plugin: 'java'
apply plugin: 'idea'

version '0.1.7'
version '0.2.0'
group 'com.avairebot'
description = 'AvaIre Watchdog'
mainClassName = 'com.avairebot.watchdog.Main'
Expand Down Expand Up @@ -58,3 +58,15 @@ dependencies {
compile group: 'commons-cli', name: 'commons-cli', version: '1.3.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

import org.apache.tools.ant.filters.ReplaceTokens

processResources {
filesMatching("**/app.properties") {
filter ReplaceTokens, tokens: [
"project.version" : project.version,
"project.groupId" : project.group,
"project.artifactId": project.ext.moduleName
]
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/avairebot/watchdog/AppInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.avairebot.watchdog;

import java.io.IOException;
import java.util.Properties;

public class AppInfo {

private static AppInfo instance;

private final String version;
private final String groupId;
private final String artifactId;

private AppInfo() {
Properties properties = new Properties();
try {
properties.load(
getClass().getClassLoader().getResourceAsStream("app.properties")
);
} catch (IOException e) {
throw new RuntimeException("Failed to load app.properties", e);
}

this.version = properties.getProperty("version");
this.groupId = properties.getProperty("groupId");
this.artifactId = properties.getProperty("artifactId");
}

public static AppInfo getAppInfo() {
if (instance == null) {
instance = new AppInfo();
}
return instance;
}

public String getVersion() {
return version;
}

public String getGroupId() {
return groupId;
}

public String getArtifactId() {
return artifactId;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/avairebot/watchdog/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static void main(String[] args) throws IOException {
System.exit(ExitCodes.EXIT_CODE_NORMAL);
}

if (cmd.hasOption("version")) {
VersionFormatter.formatAndSend();
System.exit(ExitCodes.EXIT_CODE_NORMAL);
}

new Application(cmd);
} catch (ParseException e) {
System.out.println(e.getMessage());
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/avairebot/watchdog/VersionFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.avairebot.watchdog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

class VersionFormatter {

static void formatAndSend() {
ProcessBuilder pb = new ProcessBuilder();

pb.command(Arrays.asList("java", "-jar", "AvaIre.jar", "--version", "--no-colors"));

try {
Process process = pb.start();

StringBuilder content = new StringBuilder();
try (InputStream inputStream = process.getInputStream()) {
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(streamReader);

String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
process.waitFor();
process.destroy();

if (content.toString().trim().isEmpty()) {
System.out.println("No AvaIre.jar file were found, unable to show the version of Ava being used.");
System.out.println("Watchdog is version: " + AppInfo.getAppInfo().getVersion());
return;
}

for (String line : content.toString().split("\n")) {
System.out.println(line);
if (line.contains("Version:")) {
StringBuilder watchdogLine = new StringBuilder("\tWatchdog:");
int index = line.indexOf(line.trim().split("\\s+")[1]);
while (watchdogLine.length() < index) {
watchdogLine.append(" ");
}
System.out.println(watchdogLine.append(AppInfo.getAppInfo().getVersion()).toString());
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/app.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version[email protected]@
groupId[email protected]@
artifactId[email protected]@

0 comments on commit 8bef016

Please sign in to comment.