Skip to content

Commit

Permalink
Improve Spigot Updater (#3)
Browse files Browse the repository at this point in the history
* process on SpigotUpdater

* debug line

* exception

* not need to use debug line

* debug line

* log process

* external property for SpigotUpdater

* destroy the process
  • Loading branch information
HSGamer committed Dec 28, 2022
1 parent a6b9015 commit 3e6afac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ class Main {
}
```

3. Do whatever you want with the `UpdateStatus`
3. Do whatever you want with the `UpdateStatus`

## Additional System Properties

| Name | Description | Default |
|----------------------------------|----------------------------------------------------------------------|---------|
| `MCServerUpdater.javaExecutable` | The Java executable to run external processes (Spigot Updater, etc.) | `java` |
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
Expand All @@ -27,6 +24,7 @@ private File downloadBuildTools() {
File file = new File(updateBuilder.workingDirectory(), "BuildTools.jar");
try {
String buildToolsURL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar";
updateBuilder.debug("Downloading BuildTools from " + buildToolsURL);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(buildToolsURL));
InputStream inputStream = connection.getInputStream();
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
Expand All @@ -44,25 +42,44 @@ public boolean update(File file, String version, String build) throws Exception
return false;
}
File outputDir = new File(updateBuilder.workingDirectory(), "output");
try (URLClassLoader classLoader = new URLClassLoader(new URL[]{buildTools.toURI().toURL()}, getClass().getClassLoader())) {
Class<?> clazz = Class.forName("org.spigotmc.builder.Bootstrap", true, classLoader);
Method method = clazz.getMethod("main", String[].class);
method.invoke(null, (Object) new String[]{
"--rev", version,
"--output-dir", outputDir.getAbsolutePath(),
"--compile-if-changed"
});

for (File outputFile : Objects.requireNonNull(outputDir.listFiles())) {
String name = outputFile.getName();
if (name.startsWith("spigot-") && name.endsWith(".jar")) {
Files.copy(outputFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.delete(outputFile.toPath());
break;
}
updateBuilder.debug("Running BuildTools...");
if (!runBuildTools(buildTools, outputDir, version)) {
return false;
}
for (File outputFile : Objects.requireNonNull(outputDir.listFiles())) {
String name = outputFile.getName();
if (name.startsWith("spigot-") && name.endsWith(".jar")) {
updateBuilder.debug("Copying " + name + " to " + file.getName());
Files.copy(outputFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.delete(outputFile.toPath());
break;
}
return true;
}
return true;
}

private boolean runBuildTools(File buildTools, File outputDir, String version) throws IOException, InterruptedException {
String javaExecutable = System.getProperty("MCServerUpdater.javaExecutable", "java");
ProcessBuilder processBuilder = new ProcessBuilder(
javaExecutable,
"-jar",
buildTools.getAbsolutePath(),
"--rev", version,
"--output-dir", outputDir.getAbsolutePath(),
"--compile-if-changed",
"--disable-java-check"
);
processBuilder.directory(updateBuilder.workingDirectory());
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
Runtime.getRuntime().addShutdownHook(new Thread(process::destroy));
InputStream inputStream = process.getInputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
updateBuilder.debug(new String(buffer, 0, length).trim());
}
return process.waitFor() == 0;
}

@Override
Expand Down

0 comments on commit 3e6afac

Please sign in to comment.