Skip to content

Commit fcb5d5a

Browse files
committed
Make self-updating work
1 parent eee4a80 commit fcb5d5a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ tasks.create("copyAgentJar", Copy) {
155155
tasks.create("selfUpdateJar", Jar).configure {
156156
from(sourceSets.launcher.output) {
157157
include "com/mcmoddev/relauncher/selfupdate/**/*.class"
158+
include "relauncher-restart"
158159
}
159160
classifier("selfupdate")
160161
group("build")
@@ -188,6 +189,7 @@ tasks.named('shadowJar', ShadowJar).configure {
188189
from sourceSets.main.output // The logback file
189190
from(sourceSets.launcher.output) {
190191
exclude "com/mcmoddev/relauncher/selfupdate/**/*.class"
192+
exclude "relauncher-restart"
191193
}
192194
configurations = [
193195
project.configurations.shade,
@@ -203,6 +205,7 @@ tasks.named(JavaPlugin.JAR_TASK_NAME, Jar).configure {
203205
from sourceSets.api.output
204206
from(sourceSets.launcher.output) {
205207
exclude "com/mcmoddev/relauncher/selfupdate/**.class"
208+
exclude "relauncher-restart"
206209
}
207210

208211
exclude "logback.xml"

src/launcher/java/com/mcmoddev/relauncher/selfupdate/SelfUpdate.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
package com.mcmoddev.relauncher.selfupdate;
2222

2323
import java.io.BufferedInputStream;
24-
import java.io.BufferedReader;
25-
import java.io.File;
24+
import java.io.ByteArrayInputStream;
2625
import java.io.IOException;
27-
import java.io.InputStreamReader;
26+
import java.io.InputStream;
2827
import java.net.URL;
28+
import java.nio.charset.StandardCharsets;
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
31+
import java.nio.file.StandardCopyOption;
3132
import java.util.List;
3233
import java.util.Locale;
34+
import java.util.Objects;
3335

3436
public class SelfUpdate {
3537

@@ -44,23 +46,14 @@ public static void main(String[] args) throws IOException, InterruptedException
4446
Files.createDirectories(parent);
4547
}
4648
Files.deleteIfExists(jarPath);
49+
4750
try (final var is = new BufferedInputStream(new URL(url).openStream())) {
4851
Files.copy(is, jarPath);
4952
System.out.println(colour("Update to '" + url + "' successful! Re-starting the launcher..."));
50-
final var process = Runtime.getRuntime()
51-
.exec(System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win") ? "cmd /c " : "" + relaunchCmd,
52-
null,
53-
parent == null ? Path.of("").toFile() : parent.toFile()
54-
);
5553

56-
try (final var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
57-
// Redirect console
58-
// TODO find a better way to inherit IO, or use ProcessBuilder and find a way to split the command
59-
String line;
60-
while (process.isAlive() && (line = reader.readLine()) != null) {
61-
System.out.println(line);
62-
}
63-
}
54+
new ProcessBuilder(resolveScript(relaunchCmd))
55+
.inheritIO()
56+
.start();
6457

6558
System.exit(0);
6659
}
@@ -71,5 +64,26 @@ public static String colour(String text) {
7164
+ " \033[94;1m====\033[0m";
7265
}
7366

67+
public static String readAllLines(InputStream is) throws IOException {
68+
return new String(is.readAllBytes());
69+
}
70+
71+
public static List<String> resolveScript(String script) throws IOException {
72+
final var directory = Path.of(".relauncher");
73+
var scriptFull = readAllLines(Objects.requireNonNull(SelfUpdate.class.getResourceAsStream("/relauncher-restart")));
74+
final Path path;
75+
final List<String> cmd;
76+
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
77+
path = directory.resolve("relaunch.bat").toAbsolutePath();
78+
scriptFull = scriptFull.formatted("cmd /c " + script);
79+
cmd = List.of(path.toString());
80+
} else {
81+
path = directory.resolve("relaunch.sh").toAbsolutePath();
82+
scriptFull = scriptFull.formatted(script);
83+
cmd = List.of("sh", path.toString());
84+
}
85+
Files.copy(new ByteArrayInputStream(scriptFull.getBytes(StandardCharsets.UTF_8)), path, StandardCopyOption.REPLACE_EXISTING);
86+
return cmd;
87+
}
7488

7589
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%s

0 commit comments

Comments
 (0)