Skip to content

Commit

Permalink
Open cached file as ZipFile for pom (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Proximyst committed Aug 30, 2020
1 parent 467c9f8 commit d504b27
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions java8/src/main/java/io/papermc/paperclip/Paperclip.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Optional;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.compress.compressors.CompressorException;
import org.jbsdiff.InvalidHeaderException;
import org.jbsdiff.Patch;
Expand Down Expand Up @@ -232,6 +233,28 @@ private static Method getMainMethod(final Path paperJar, final String mainClass)
}
}

private static Path extractPom(Path paperJar) throws IOException {
try (final ZipFile zipFile = new ZipFile(paperJar.toFile())) {
Path pomPath = Paths.get("paper.xml");

ZipEntry pomEntry = zipFile.getEntry("META-INF/maven/io.papermc.paper/paper/pom.xml");

if (pomEntry == null) {
pomEntry = zipFile.getEntry("META-INF/maven/com.destroystokyo.paper/paper/pom.xml");
}
if (pomEntry == null) {
System.err.println("No Paper pom file could be found.");
return null;
}
try (InputStream pom = zipFile.getInputStream(pomEntry)) {
Files.copy(pom, pomPath, StandardCopyOption.REPLACE_EXISTING);
pomPath.toFile().deleteOnExit();
}

return pomPath;
}
}

private static void mavenInstall(Path paperJar) {
try {
if (new ProcessBuilder("mvn", "-version").start().waitFor() != 0) {
Expand All @@ -246,34 +269,20 @@ private static void mavenInstall(Path paperJar) {
}

try {
Path pomPath = Paths.get("paper.xml");

InputStream pom = Paperclip.class.getResourceAsStream("/META-INF/maven/io.papermc.paper/paper/pom.xml");
if (pom == null) {
pom = Paperclip.class.getResourceAsStream("/META-INF/maven/com.destroystokyo.paper/paper/pom.xml");
}
if (pom == null) {
Path pomPath = extractPom(paperJar);
if (pomPath == null) {
System.err.println("No Paper pom file could be found.");
System.exit(1);
}
try {
Files.copy(pom, pomPath, StandardCopyOption.REPLACE_EXISTING);
pomPath.toFile().deleteOnExit();
} finally {
// We didn't use try-with-resources, so we have to finally-block this.
pom.close();
}

if (new ProcessBuilder("mvn", "install:install-file", "-Dfile=" + paperJar, "-DpomFile=" + pomPath).start().waitFor() != 0) {
// Error! Could not install the file.
System.err.println("Could not install the Paper file.");
System.exit(1);
return;
}
} catch (IOException | InterruptedException ex) {
System.err.println("Could not install the Paper file.");
ex.printStackTrace();
System.exit(1);
return;
}

Expand Down

0 comments on commit d504b27

Please sign in to comment.