Skip to content

Commit

Permalink
fix: post process correctly on windows. fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
jagodevreede committed Oct 14, 2024
1 parent 3903123 commit 9548170
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class PostProcessor implements CancelableTask {
private static final Logger logger = LoggerFactory.getLogger(PostProcessor.class);
public static final int MAX_AMOUNT_OF_DELTE_ATTEMPTS = 60;
private final ProgressInformation progressInformation;
private boolean canceled;

Expand All @@ -34,19 +35,41 @@ public void postProcess(File tempFile, String identifier) throws IOException {
}

private void processTarGz(File tempFile, String identifier) throws IOException {
File tempExtractFolder = new File(tempFile.getParent(), tempFile.getName().replaceAll("\\.", "") + "_extracted");
File tempExtractFolder = new File(tempFile.getParent(), tempFile.getName()
.replaceAll("\\.", "") + "_extracted");
try {
publishState("Repackaging download (extracting)");
TarGzExtractTask.extract(tempFile, tempExtractFolder);

if (isCancelled()) {
return;
}

publishState("Repackaging download");
File rootFolder = FileUtil.findRoot(tempExtractFolder, "bin");
File newRootFolder = new File(rootFolder.getParentFile(), identifier);
Files.move(rootFolder.toPath(), newRootFolder.toPath());
int deleteAttempts = 0;

publishState("Repackaging download (removing old temp file)");
// delete the old temp file, as we are about to create a new zip version, this can take a while sometimes the
// file is locked, by the java process
while (!tempFile.delete() && deleteAttempts < MAX_AMOUNT_OF_DELTE_ATTEMPTS) {
logger.debug("Attempt {}: Deleting old temp file: {} ", deleteAttempts, tempFile.getAbsolutePath());
deleteAttempts++;
Thread.sleep(1000);
if (isCancelled()) {
return;
}
progressInformation.publishProgress(deleteAttempts * 100 / MAX_AMOUNT_OF_DELTE_ATTEMPTS);
}
if (deleteAttempts >= MAX_AMOUNT_OF_DELTE_ATTEMPTS) {
throw new IllegalStateException("Failed to delete old temp file: " + tempFile.getAbsolutePath());
}

publishState("Repackaging download (compressing)");
ZipDirectory.zip(newRootFolder, tempFile);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
publishState("Cleanup");
FileUtil.deleteRecursively(tempExtractFolder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.jagodevreede.sdkman.api.files;

import io.github.jagodevreede.sdkman.api.SdkManUiPreferences;

import java.io.File;
import java.io.IOException;

import io.github.jagodevreede.sdkman.api.SdkManUiPreferences;

public class ZipDirectory {
private ZipDirectory() {
// no instantiation
Expand All @@ -13,8 +13,8 @@ private ZipDirectory() {
public static void zip(File folderToZip, File outputFile) throws IOException {
String zipExecutable = SdkManUiPreferences.getInstance().zipExecutable;
outputFile.delete();
// zip -qyr "$zip_output" "Java-22.1.0.1.r17-gln"
ProcessStarter.runIn(folderToZip.getParentFile(), zipExecutable, "-qyr", outputFile.getAbsolutePath(), folderToZip.getName());
// zip -qyr "$zip_output" "Java-22.1.0.1.r17-gln" (the -y is not supported on windows)
ProcessStarter.runIn(folderToZip.getParentFile(), zipExecutable, "-qr", outputFile.getAbsolutePath(), folderToZip.getName());
}

}

0 comments on commit 9548170

Please sign in to comment.