Skip to content

Commit 902cbf4

Browse files
committed
Add version checker
1 parent 737e4c7 commit 902cbf4

File tree

3 files changed

+93
-26
lines changed

3 files changed

+93
-26
lines changed

src/main/java/de/neuwirthinformatik/Alexander/GitJarUpdate/Update.java

+42-9
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,75 @@ public class Update {
1717

1818
public static void main(String[] args)
1919
{
20-
loadUpdate("TeXCalc-all.jar","APN-Pucky","TeXCalc");
20+
//loadUpdate("TeXCalc-all.jar","APN-Pucky","TeXCalc");
21+
Version d = new Version("9999");
22+
Version t = new Version("5.5.5");
23+
System.out.println(d.compareTo(t));
24+
2125
}
2226

23-
public boolean someLibraryMethod() {
24-
return true;
25-
}
26-
2727

2828
public static void loadUpdate(String fname, String github_user,String github_project)
2929
{
3030
StreamUtil.copyFile("new-" + fname,fname);
31-
String up = checkUpdates(github_user,github_project);
31+
String up = getUpdateText(github_user,github_project);
3232
if(!up.equals(""))
3333
{
3434
int selection = JOptionPane.showConfirmDialog(null, up,"Update",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
3535
if(selection == JOptionPane.OK_OPTION)update(fname,github_user,github_project);
3636
}
3737
}
3838

39-
public static String checkUpdates(String github_user,String github_project)
39+
public static boolean checkUpdates(String github_user,String github_project)
4040
{
4141
String ret = "";
4242
String jsf = "git_jar_version.json";
4343
Wget.wGet(jsf, "https://api.github.com/repos/" + github_user + "/" + github_project + "/releases/latest");
4444
String tumjson=StreamUtil.readFile(jsf).replaceAll("\n", "");
4545
JSONObject tum = new JSONObject(tumjson);
4646
String tum_tag_name = tum.getString("tag_name");
47-
if(!tum_tag_name.equals(Info.VERSION) && !Info.VERSION.equals( "9999"))
47+
if(new Version(tum_tag_name).compareTo(new Version(Info.VERSION)) > 0)
48+
{
49+
return true;
50+
}
51+
return false;
52+
}
53+
public static String[] getUpdateInfos(String github_user,String github_project) {
54+
String ret = "";
55+
String jsf = "git_jar_version.json";
56+
Wget.wGet(jsf, "https://api.github.com/repos/" + github_user + "/" + github_project + "/releases/latest");
57+
String tumjson=StreamUtil.readFile(jsf).replaceAll("\n", "");
58+
JSONObject tum = new JSONObject(tumjson);
59+
String tum_tag_name = tum.getString("tag_name");
60+
if(new Version(tum_tag_name).compareTo(new Version(Info.VERSION)) > 0)
4861
{
4962
ret += "New version: " + tum_tag_name + " available:\n";
5063
ret += " - " + tum.getString("name") + "\n";
5164
ret += "Current version: " + Info.VERSION;
65+
66+
return new String[] {tum_tag_name,tum.getString("name")};
5267
}
68+
return new String[] {};
5369

70+
}
71+
72+
public static String getUpdateText(String github_user,String github_project) {
73+
String ret = "";
74+
String jsf = "git_jar_version.json";
75+
Wget.wGet(jsf, "https://api.github.com/repos/" + github_user + "/" + github_project + "/releases/latest");
76+
String tumjson=StreamUtil.readFile(jsf).replaceAll("\n", "");
77+
JSONObject tum = new JSONObject(tumjson);
78+
String tum_tag_name = tum.getString("tag_name");
79+
if(new Version(tum_tag_name).compareTo(new Version(Info.VERSION)) > 0)
80+
{
81+
ret += "New version: " + tum_tag_name + " available:\n";
82+
ret += " - " + tum.getString("name") + "\n";
83+
ret += "Current version: " + Info.VERSION;
84+
85+
}
5486
return ret;
55-
}
87+
88+
}
5689

5790
public static void update(String fname, String github_user, String github_project)
5891
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.neuwirthinformatik.Alexander.GitJarUpdate;
2+
3+
/**
4+
* https://stackoverflow.com/a/11024200
5+
*/
6+
public class Version implements Comparable<Version> {
7+
8+
private String version;
9+
10+
public final String get() {
11+
return this.version;
12+
}
13+
14+
public Version(String version) {
15+
if(version == null)
16+
throw new IllegalArgumentException("Version can not be null");
17+
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
18+
throw new IllegalArgumentException("Invalid version format");
19+
this.version = version;
20+
}
21+
22+
@Override public int compareTo(Version that) {
23+
if(that == null)
24+
return 1;
25+
String[] thisParts = this.get().split("\\.");
26+
String[] thatParts = that.get().split("\\.");
27+
int length = Math.max(thisParts.length, thatParts.length);
28+
for(int i = 0; i < length; i++) {
29+
int thisPart = i < thisParts.length ?
30+
Integer.parseInt(thisParts[i]) : 0;
31+
int thatPart = i < thatParts.length ?
32+
Integer.parseInt(thatParts[i]) : 0;
33+
if(thisPart < thatPart)
34+
return -1;
35+
if(thisPart > thatPart)
36+
return 1;
37+
}
38+
return 0;
39+
}
40+
41+
@Override public boolean equals(Object that) {
42+
if(this == that)
43+
return true;
44+
if(that == null)
45+
return false;
46+
if(this.getClass() != that.getClass())
47+
return false;
48+
return this.compareTo((Version) that) == 0;
49+
}
50+
51+
}

src/test/java/GitJarUpdate/LibraryTest.java

-17
This file was deleted.

0 commit comments

Comments
 (0)