Skip to content

Commit a4c1df3

Browse files
authored
Merge pull request #48 from Sandro642/feature/versionAlert
Update version to 0.3.7-STABLE and add RetrieveLastVersion class for …
2 parents f6f13e9 + 5e61373 commit a4c1df3

4 files changed

Lines changed: 96 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik
1717
---
1818

1919
```java
20-
Stable Version: 0.3.6-STABLE
20+
Stable Version: 0.3.7-STABLE
2121
```
2222

2323
---
@@ -115,7 +115,7 @@ repositories {
115115

116116
dependencies {
117117

118-
implementation("fr.sandro642.github:ConnectLib:0.3.6-STABLE")
118+
implementation("fr.sandro642.github:ConnectLib:0.3.7-STABLE")
119119

120120
}
121121

build.gradle

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@ plugins {
55
}
66

77
group = 'fr.sandro642.github'
8-
version = '0.3.6-STABLE'
8+
version = '0.3.7-STABLE'
9+
10+
// Générer une classe de version automatiquement
11+
task generateVersionClass {
12+
doLast {
13+
def versionFile = file("$buildDir/generated/source/version/fr/sandro642/github/update/Version.java")
14+
versionFile.parentFile.mkdirs()
15+
versionFile.text = """
16+
package fr.sandro642.github.update;
17+
18+
public class Version {
19+
public static final String VERSION = "${project.version}";
20+
}
21+
"""
22+
}
23+
}
24+
25+
compileJava.dependsOn generateVersionClass
26+
sourceSets.main.java.srcDir "$buildDir/generated/source/version"
927

1028
tasks.register('printVersion') {
1129
doLast {
@@ -34,6 +52,8 @@ dependencies {
3452

3553
// Version moderne sans problèmes de dépendances
3654
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
55+
56+
implementation 'com.google.code.gson:gson:2.10.1'
3757
}
3858

3959
application {

src/main/java/fr/sandro642/github/ConnectLib.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import fr.sandro642.github.misc.*;
99
import fr.sandro642.github.jobs.JobGetInfos;
1010
import fr.sandro642.github.enums.ResourceType;
11+
import fr.sandro642.github.update.RetrieveLastVersion;
1112

1213
import java.util.HashMap;
1314
import java.util.Map;
@@ -35,6 +36,7 @@ public class ConnectLib {
3536
private static final Map<String,String> routes = new HashMap<>();
3637
private static Logs logs = new Logs();
3738
private static LangManager langManager;
39+
private static RetrieveLastVersion rlv;
3840

3941
/**
4042
* Init the ConnectLib with the specified resource type and routes.
@@ -47,6 +49,9 @@ public void Init(ResourceType resourceType, LangType langType, Class<? extends E
4749
storeAndRetrieve = new StoreAndRetrieve();
4850
yamlUtils = new YamlUtils();
4951
logs = new Logs();
52+
rlv = new RetrieveLastVersion();
53+
54+
rlv.isLatestVersion();
5055

5156
HookManager().initHook(resourceType);
5257
LangSupport().setLangTypeVariable(langType);
@@ -94,16 +99,6 @@ public String getRoute(String routeName) {
9499
}
95100
}
96101

97-
/**
98-
* Return the route associated with the given Enum route.
99-
* The Enum name is converted to lowercase to match the route names.
100-
* @param routeEnum the Enum representing the route
101-
* @return the route as a String
102-
*/
103-
public String getRoute(Enum<?> routeEnum) {
104-
return getRoute(routeEnum.name().toLowerCase());
105-
}
106-
107102
/**
108103
* Return an instance of JobGetInfos.
109104
* @return JobGetInfos instance
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package fr.sandro642.github.update;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
7+
import java.net.URI;
8+
import java.net.http.HttpClient;
9+
import java.net.http.HttpRequest;
10+
import java.net.http.HttpResponse;
11+
12+
/**
13+
* RetrieveLastVersion is a class that retrieves the latest version of the software.
14+
* It contains a field to store the latest version and can be extended to include methods for fetching
15+
*/
16+
17+
public class RetrieveLastVersion {
18+
19+
/**
20+
* The latest version of the software.
21+
* Default is set to the current version defined in the Version class.
22+
*/
23+
private String latestVersion = Version.VERSION;
24+
25+
/**
26+
* Fetches the latest version from the GitHub API.
27+
* It sends a GET request to the tags endpoint of the ConnectLib repository
28+
* and retrieves the name of the first tag, which represents the latest version.
29+
* @return
30+
*/
31+
public String fetchVersion() {
32+
try {
33+
HttpClient client = HttpClient.newHttpClient();
34+
HttpRequest request = HttpRequest.newBuilder()
35+
.uri(URI.create("https://api.github.com/repos/Sandro642/ConnectLib/tags"))
36+
.build();
37+
38+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
39+
40+
Gson gson = new Gson();
41+
JsonArray data = gson.fromJson(response.body(), JsonArray.class);
42+
43+
if (data != null && data.size() > 0) {
44+
JsonObject firstTag = data.get(0).getAsJsonObject();
45+
return firstTag.get("name").getAsString();
46+
} else {
47+
return "No tags found";
48+
}
49+
50+
} catch (Exception e) {
51+
return "Error";
52+
}
53+
}
54+
55+
/**
56+
* Compares the fetched version with the current version.
57+
* If they are different, it prints a message indicating that a new version is available.
58+
*/
59+
public void isLatestVersion() {
60+
String fetchedVersion = fetchVersion();
61+
if (fetchedVersion.equals("Error") || fetchedVersion.equals("No tags found")) {
62+
System.out.println("Could not fetch the latest version.");
63+
} else if (!fetchedVersion.equals(Version.VERSION)) {
64+
System.out.println("A new version is available: " + fetchedVersion + ". You can download it from https://connectlib.xyz/#downloads");
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)