Skip to content

Commit 8bef016

Browse files
committed
Rewrite version command to show information about Ava and Watchdog
1 parent 1a9fb46 commit 8bef016

5 files changed

Lines changed: 123 additions & 1 deletion

File tree

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
apply plugin: 'java'
77
apply plugin: 'idea'
88

9-
version '0.1.7'
9+
version '0.2.0'
1010
group 'com.avairebot'
1111
description = 'AvaIre Watchdog'
1212
mainClassName = 'com.avairebot.watchdog.Main'
@@ -58,3 +58,15 @@ dependencies {
5858
compile group: 'commons-cli', name: 'commons-cli', version: '1.3.1'
5959
testCompile group: 'junit', name: 'junit', version: '4.12'
6060
}
61+
62+
import org.apache.tools.ant.filters.ReplaceTokens
63+
64+
processResources {
65+
filesMatching("**/app.properties") {
66+
filter ReplaceTokens, tokens: [
67+
"project.version" : project.version,
68+
"project.groupId" : project.group,
69+
"project.artifactId": project.ext.moduleName
70+
]
71+
}
72+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.avairebot.watchdog;
2+
3+
import java.io.IOException;
4+
import java.util.Properties;
5+
6+
public class AppInfo {
7+
8+
private static AppInfo instance;
9+
10+
private final String version;
11+
private final String groupId;
12+
private final String artifactId;
13+
14+
private AppInfo() {
15+
Properties properties = new Properties();
16+
try {
17+
properties.load(
18+
getClass().getClassLoader().getResourceAsStream("app.properties")
19+
);
20+
} catch (IOException e) {
21+
throw new RuntimeException("Failed to load app.properties", e);
22+
}
23+
24+
this.version = properties.getProperty("version");
25+
this.groupId = properties.getProperty("groupId");
26+
this.artifactId = properties.getProperty("artifactId");
27+
}
28+
29+
public static AppInfo getAppInfo() {
30+
if (instance == null) {
31+
instance = new AppInfo();
32+
}
33+
return instance;
34+
}
35+
36+
public String getVersion() {
37+
return version;
38+
}
39+
40+
public String getGroupId() {
41+
return groupId;
42+
}
43+
44+
public String getArtifactId() {
45+
return artifactId;
46+
}
47+
}

src/main/java/com/avairebot/watchdog/Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public static void main(String[] args) throws IOException {
3838
System.exit(ExitCodes.EXIT_CODE_NORMAL);
3939
}
4040

41+
if (cmd.hasOption("version")) {
42+
VersionFormatter.formatAndSend();
43+
System.exit(ExitCodes.EXIT_CODE_NORMAL);
44+
}
45+
4146
new Application(cmd);
4247
} catch (ParseException e) {
4348
System.out.println(e.getMessage());
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.avairebot.watchdog;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.util.Arrays;
8+
9+
class VersionFormatter {
10+
11+
static void formatAndSend() {
12+
ProcessBuilder pb = new ProcessBuilder();
13+
14+
pb.command(Arrays.asList("java", "-jar", "AvaIre.jar", "--version", "--no-colors"));
15+
16+
try {
17+
Process process = pb.start();
18+
19+
StringBuilder content = new StringBuilder();
20+
try (InputStream inputStream = process.getInputStream()) {
21+
InputStreamReader streamReader = new InputStreamReader(inputStream);
22+
BufferedReader reader = new BufferedReader(streamReader);
23+
24+
String line;
25+
while ((line = reader.readLine()) != null) {
26+
content.append(line).append("\n");
27+
}
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
process.waitFor();
32+
process.destroy();
33+
34+
if (content.toString().trim().isEmpty()) {
35+
System.out.println("No AvaIre.jar file were found, unable to show the version of Ava being used.");
36+
System.out.println("Watchdog is version: " + AppInfo.getAppInfo().getVersion());
37+
return;
38+
}
39+
40+
for (String line : content.toString().split("\n")) {
41+
System.out.println(line);
42+
if (line.contains("Version:")) {
43+
StringBuilder watchdogLine = new StringBuilder("\tWatchdog:");
44+
int index = line.indexOf(line.trim().split("\\s+")[1]);
45+
while (watchdogLine.length() < index) {
46+
watchdogLine.append(" ");
47+
}
48+
System.out.println(watchdogLine.append(AppInfo.getAppInfo().getVersion()).toString());
49+
}
50+
}
51+
} catch (IOException | InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
}

src/main/resources/app.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version=@project.version@
2+
groupId=@project.groupId@
3+
artifactId=@project.artifactId@

0 commit comments

Comments
 (0)