Skip to content

Commit 6e77eb5

Browse files
authored
GH-8 More docs and rename (#8)
* Add more docs * Update HttpClient.java
1 parent 259a6f0 commit 6e77eb5

File tree

9 files changed

+250
-99
lines changed

9 files changed

+250
-99
lines changed

README.md

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,79 @@
1-
# EternalUpdater
2-
✔ Updater for EternalCode plugins
1+
# Updater
32

3+
#### Updater is a library for checking for updates for your plugin on GitHub. It uses the GitHub API to get information about the latest release of your plugin and compare it to the current version of your plugin.
4+
5+
### Usage
6+
First, you need to create an instance of the `Updater` class by passing the plugin name, current version, and Github repository name as arguments. Then, you can call the `checkUpdates()` method to check for updates and get the `RemoteInformation` object.
7+
8+
The `RemoteInformation` object contains information about the update, such as the availability of a new version, the current version, and the download URI.
9+
10+
```java
11+
if (remoteInformation.isAvailableNewVersion()) {
12+
System.out.println("A new version is available: " + remoteInformation.getCurrentVersion());
13+
System.out.println("Download URI: " + remoteInformation.getDownloadUri());
14+
} else {
15+
System.out.println("You are already running the latest version.");
16+
}
17+
```
18+
19+
### Example
20+
Here's an example of how to can use the Updater in `Spigot` plugin
421

5-
# Example usage
622
```java
7-
import com.eternalcode.eternalupdater.EternalUpdater;
823

9-
EternalUpdater eternalUpdater = new EternalUpdater("name", "currentPluginVer", "githubrepo");
10-
RemoteInformation pluginUpdate = eternalUpdater.checkUpdates();
11-
```
24+
import com.eternalcode.updater.Updater;
25+
import com.eternalcode.updater.http.RemoteInformation;
26+
27+
public class MyPlugin {
28+
29+
private Updater updater;
30+
31+
public void onEnable() {
32+
updater = new Updater("MyPlugin", "1.0", "MyGithubUsername/MyPlugin");
33+
checkForUpdates();
34+
}
35+
36+
private void checkForUpdates() {
37+
RemoteInformation remoteInformation = updater.checkUpdates();
38+
if (remoteInformation.isAvailableNewVersion()) {
39+
System.out.println("A new version is available: " + remoteInformation.getCurrentVersion());
40+
System.out.println("Download URI: " + remoteInformation.getDownloadUri());
41+
} else {
42+
System.out.println("You are already running the latest version.");
43+
}
44+
}
45+
}
46+
```
47+
48+
### Maven/Gradle
49+
Get the latest version from [EternalCode Repository](https://repo.eternalcode.pl/#/releases/com/eternalcode/eternalupdater)
50+
51+
#### gradle groovy
52+
```groovy
53+
maven { url "https://repo.eternalcode.pl/releases" }
54+
55+
implementation "com.eternalcode:updater:{VERSION}"
56+
```
57+
58+
```kotlin
59+
maven { url = uri("https://repo.eternalcode.pl/releases") }
60+
61+
implementation("com.eternalcode:updater:{VERSION}")
62+
```
63+
64+
```xml
65+
<repository>
66+
<id>eternalcode-reposilite-releases</id>
67+
<name>EternalCode Repository</name>
68+
<url>https://repo.eternalcode.pl/releases</url>
69+
</repository>
70+
71+
<dependency>
72+
<groupId>com.eternalcode</groupId>
73+
<artifactId>updater</artifactId>
74+
<version>{VERSION}</version>
75+
</dependency>
76+
```
77+
78+
79+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/com/eternalcode/eternalupdater/EternalUpdater.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/main/java/com/eternalcode/eternalupdater/data/PluginData.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/com/eternalcode/eternalupdater/data/RemoteInformation.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.eternalcode.updater;
2+
3+
import org.jetbrains.annotations.Contract;
4+
5+
/**
6+
* The PluginData class is used to store information about a plugin.
7+
*/
8+
public class PluginData {
9+
10+
private final String githubRepository;
11+
private final String pluginVersion;
12+
private final String pluginName;
13+
14+
/**
15+
* Creates a new instance of PluginData with the GitHub repository name, plugin version, and plugin name.
16+
*
17+
* @param githubRepository The GitHub repository name
18+
* @param pluginVersion The version of the plugin
19+
* @param pluginName The name of the plugin
20+
*/
21+
public PluginData(String githubRepository, String pluginVersion, String pluginName) {
22+
this.githubRepository = githubRepository;
23+
this.pluginVersion = pluginVersion;
24+
this.pluginName = pluginName;
25+
}
26+
27+
/**
28+
* Returns the GitHub repository name
29+
*
30+
* @return The GitHub repository name
31+
*/
32+
@Contract(pure = true)
33+
public String getGithubRepository() {
34+
return this.githubRepository;
35+
}
36+
37+
/**
38+
* Returns the version of the plugin
39+
*
40+
* @return The version of the plugin
41+
*/
42+
@Contract(pure = true)
43+
public String getPluginVersion() {
44+
return this.pluginVersion;
45+
}
46+
47+
/**
48+
* Returns the name of the plugin
49+
*
50+
* @return The name of the plugin
51+
*/
52+
@Contract(pure = true)
53+
public String getPluginName() {
54+
return this.pluginName;
55+
}
56+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.eternalcode.updater;
2+
3+
import com.eternalcode.updater.http.RemoteInformation;
4+
import com.eternalcode.updater.http.HttpClient;
5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.json.simple.JSONObject;
8+
9+
/**
10+
* The Updater class is used to check for updates for a plugin.
11+
*/
12+
public class Updater {
13+
14+
private final PluginData pluginData;
15+
16+
/**
17+
* Creates a new instance of Updater with the plugin name, plugin version, and GitHub remote repository name.
18+
*
19+
* @param pluginName The name of the plugin
20+
* @param pluginVersion The current version of the plugin
21+
* @param githubRepository The name of the GitHub remote repository (e.g. eternalcodeteam/eternalcore)
22+
*/
23+
public Updater(@NotNull String pluginName, @NotNull String pluginVersion, @NotNull String githubRepository) {
24+
this.pluginData = new PluginData(githubRepository, pluginVersion, pluginName);
25+
}
26+
27+
/**
28+
* Checks for updates for the plugin.
29+
*
30+
* @return Remote plugin information
31+
* @throws RuntimeException If there is a connection problem or the remote repository is not found
32+
*/
33+
@Contract(pure = true)
34+
public RemoteInformation checkUpdates() {
35+
JSONObject response = HttpClient.doRequest("repos/" + this.pluginData.getGithubRepository() + "/releases/latest");
36+
boolean newVersionAvailable = !this.pluginData.getPluginVersion().equals(response.get("tag_name"));
37+
String latestTag = (String) response.get("tag_name");
38+
String newDownloadUri = (String) response.get("zipball_url");
39+
40+
return new RemoteInformation(
41+
newVersionAvailable,
42+
latestTag,
43+
newDownloadUri
44+
);
45+
}
46+
47+
}

src/main/java/com/eternalcode/eternalupdater/util/HttpClient.java renamed to src/main/java/com/eternalcode/updater/http/HttpClient.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
package com.eternalcode.eternalupdater.util;
1+
package com.eternalcode.updater.http;
22

33
import okhttp3.OkHttpClient;
44
import okhttp3.Request;
55
import okhttp3.Response;
6+
import org.jetbrains.annotations.Contract;
67
import org.json.simple.JSONObject;
78
import org.json.simple.parser.JSONParser;
89

10+
/**
11+
* The HttpClient class is used to send HTTP requests to the GitHub server.
12+
*/
913
public class HttpClient {
1014

1115
private final static String baseUri = "https://api.github.com/";
1216
private final static OkHttpClient client = new OkHttpClient();
1317

18+
/**
19+
* Sends an HTTP request to the GitHub server and returns the response as a JSON object.
20+
*
21+
* @param url The URL of the request
22+
* @return The server's response in JSON format
23+
* @throws RuntimeException If there is a connection problem or the provided repository is not found
24+
*/
25+
@Contract(pure = true)
1426
public static JSONObject doRequest(String url) {
1527
Request request = new Request
1628
.Builder()
@@ -26,9 +38,9 @@ public static JSONObject doRequest(String url) {
2638
else {
2739
return jsonResponse;
2840
}
29-
} catch (Exception e) {
30-
throw new RuntimeException(e);
41+
}
42+
catch (Exception exception) {
43+
throw new RuntimeException(exception);
3144
}
3245
}
33-
3446
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.eternalcode.updater.http;
2+
3+
import org.jetbrains.annotations.Contract;
4+
5+
/**
6+
* The RemoteInformation class is used to store information about a plugin update.
7+
*/
8+
public class RemoteInformation {
9+
private final boolean isAvailableNewVersion;
10+
private final String currentVersion;
11+
private final String downloadUri;
12+
13+
/**
14+
* Creates a new instance of RemoteInformation with the update availability, current version, and download URI.
15+
*
16+
* @param isAvailableNewVersion True if a new version is available, false otherwise
17+
* @param currentVersion The current version of the plugin
18+
* @param downloadUri The URI to download the update
19+
*/
20+
public RemoteInformation(boolean isAvailableNewVersion, String currentVersion, String downloadUri) {
21+
this.isAvailableNewVersion = isAvailableNewVersion;
22+
this.currentVersion = currentVersion;
23+
this.downloadUri = downloadUri;
24+
}
25+
26+
/**
27+
* Returns the URI to download the update
28+
*
29+
* @return The URI to download the update
30+
*/
31+
@Contract(pure = true)
32+
public String getDownloadUri() {
33+
return this.downloadUri;
34+
}
35+
36+
/**
37+
* Indicates if a new version is available
38+
*
39+
* @return True if a new version is available, false otherwise
40+
*/
41+
@Contract(pure = true)
42+
public boolean isAvailableNewVersion() {
43+
return this.isAvailableNewVersion;
44+
}
45+
46+
/**
47+
* Returns the current version of the plugin
48+
*
49+
* @return The current version of the plugin
50+
*/
51+
@Contract(pure = true)
52+
public String getCurrentVersion() {
53+
return this.currentVersion;
54+
}
55+
}

0 commit comments

Comments
 (0)