Skip to content

Commit c5f1f2e

Browse files
Added autoDelete option
1 parent 9fb8336 commit c5f1f2e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public void update() {
4040
}
4141
```
4242

43+
### Other uses
44+
45+
- Use `UpdaterAPI.setAutoDelete(true)` that the Updater is only show up in the directory, when the program needs to update
46+
4347
# Run parameter
4448

4549
1. URL. This parameter is the url from where the Updater downloads the latest file of your program. (for example: www.yoururl.com/api/yourprogram/v1.0/YourProgram.jar)

src/test/java/UpdaterAPI.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,37 @@ public class UpdaterAPI {
1616
private static final String API = "https://api.github.com/repos/ZeusSeinGrossopa/UpdaterAPI/releases/latest";
1717

1818
private static File updaterFile = null;
19+
private static boolean autoDelete = false;
1920

2021
private static File jarPath;
2122

2223
public static void downloadUpdater(File destination) {
24+
downloadUpdater(destination, null);
25+
}
26+
27+
public static void downloadUpdater(File destination, Consumer<File> consumer) {
2328
destination = new File((destination.isDirectory() ? destination : new File(FilenameUtils.getPath(destination.getAbsolutePath()))) + "/Updater.jar");
2429

2530
final File finalDestination = destination;
2631
updaterFile = finalDestination;
2732

33+
if (autoDelete) {
34+
if(destination.exists())
35+
destination.delete();
36+
37+
if(consumer != null)
38+
consumer.accept(destination);
39+
return;
40+
}
41+
2842
getLatestVersion(urlCallback -> {
2943
try {
3044
URL url = new URL(urlCallback);
3145

3246
FileUtils.copyURLToFile(url, finalDestination);
47+
48+
if(consumer != null)
49+
consumer.accept(finalDestination);
3350
} catch (IOException e) {
3451
e.printStackTrace();
3552
}
@@ -85,7 +102,20 @@ public static void update(File updaterFile, File oldFile, String url, File newFi
85102

86103
ProcessBuilder builder = new ProcessBuilder(javaBin, "-jar", updaterFile.getAbsolutePath(), url, oldFile.getAbsolutePath(), newFile.getAbsolutePath(), restart ? "true" : "");
87104

88-
builder.start();
105+
if (autoDelete) {
106+
autoDelete = false;
107+
downloadUpdater(oldFile.getParentFile(), file -> {
108+
try {
109+
builder.start();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
}
113+
});
114+
115+
autoDelete = true;
116+
} else {
117+
builder.start();
118+
}
89119
}
90120

91121
public static boolean needUpdate(String version1, String version2) {
@@ -119,4 +149,12 @@ public static File getJarPath() {
119149

120150
return jarPath;
121151
}
152+
153+
public static void setAutoDelete(boolean value) {
154+
autoDelete = value;
155+
}
156+
157+
public static File getCurrentUpdater() {
158+
return updaterFile;
159+
}
122160
}
723 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)