Skip to content

Commit

Permalink
feat: DownloadHelper#downloadIfRequired allows to know if was downloaded
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed May 7, 2024
1 parent 5dbda3b commit 1ecbc93
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public static DownloadHelper getInstance() {
* @return the command path
* @throws IOException if the tool was not found in the config file
*/
private CompletableFuture<String> downloadIfRequiredAsyncInner(String toolName, URL url) throws IOException {
CompletableFuture<String> result = new CompletableFuture<>();
private CompletableFuture<ToolInstance> downloadIfRequiredAsyncInner(String toolName, URL url) throws IOException {
CompletableFuture<ToolInstance> result = new CompletableFuture<>();
ToolsConfig config = ConfigHelper.loadToolsConfig(url);
ToolsConfig.Tool tool = config.getTools().get(toolName);
if (tool == null) {
Expand All @@ -140,12 +140,11 @@ private CompletableFuture<String> downloadIfRequiredAsyncInner(String toolName,
if (!Files.exists(path)) {
downloadInBackground(toolName, platform, path, cmd, tool, version, result);
} else {
result.complete(cmd);
result.complete(new ToolInstance(cmd, false));
}
} else {
result.complete(command);
result.complete(new ToolInstance(command, false));
}

return result;
}

Expand All @@ -159,21 +158,21 @@ private ToolsConfig.Platform getPlatformBasedOnOs(ToolsConfig.Tool tool) {

}

private void downloadInBackground(String toolName, ToolsConfig.Platform platform, Path path, String cmd, ToolsConfig.Tool tool, String version, CompletableFuture<String> result) {
private void downloadInBackground(String toolName, ToolsConfig.Platform platform, Path path, String cmd, ToolsConfig.Tool tool, String version, CompletableFuture<ToolInstance> result) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
downloadInBackgroundManager(toolName, platform, path, cmd, result);
} else {
ApplicationManager.getApplication().invokeLater(() -> {
if (tool.isSilentMode() || isDownloadAllowed(toolName, version, tool.getVersion())) {
downloadInBackgroundManager(toolName, platform, path, cmd, result);
} else {
result.complete(platform.getCmdFileName());
result.complete(new ToolInstance(platform.getCmdFileName(), false));
}
});
}
}

private void downloadInBackgroundManager(String toolName, ToolsConfig.Platform platform, Path path, String cmd, CompletableFuture<String> result) {
private void downloadInBackgroundManager(String toolName, ToolsConfig.Platform platform, Path path, String cmd, CompletableFuture<ToolInstance> result) {
final Path dlFilePath = path.resolveSibling(platform.getDlFileName());
ProgressManager.getInstance().run(new Task.Backgroundable(null, "Downloading " + toolName, false) {
@Override
Expand All @@ -184,37 +183,37 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
uncompress(dlFilePath, path);
return cmd;
});
} catch (IOException ignored) {
result.completeExceptionally(new IOException("Error while setting tool " + toolName + "."));
} catch (IOException e) {
result.completeExceptionally(new IOException("Error while setting tool " + toolName + ".", e));
}
}

@Override
public void onFinished() {
if (!result.isCompletedExceptionally()) {
result.complete(cmd);
result.complete(new ToolInstance(cmd, true));
}
}
});
}

public String downloadIfRequired(String toolName, URL url) throws IOException {
CompletableFuture<String> future = downloadIfRequiredAsyncInner(toolName, url);
public ToolInstance downloadIfRequired(String toolName, URL url) throws IOException {
CompletableFuture<ToolInstance> future = downloadIfRequiredAsyncInner(toolName, url);
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
throw new IOException(e);
}
}

public CompletableFuture<String> downloadIfRequiredAsync(String toolName, URL url) {
CompletableFuture<String> result = new CompletableFuture<>();
public CompletableFuture<ToolInstance> downloadIfRequiredAsync(String toolName, URL url) {
try {
return downloadIfRequiredAsyncInner(toolName, url);
} catch (IOException e) {
result.completeExceptionally(e);
CompletableFuture<ToolInstance> future = new CompletableFuture<>();
future.completeExceptionally(e);
return future;
}
return result;
}

private boolean isDownloadAllowed(String tool, String currentVersion, String requiredVersion) {
Expand Down Expand Up @@ -317,4 +316,22 @@ private void save(InputStream source, Path destination, long length) throws IOEx
}
destination.toFile().setExecutable(true);
}

public static class ToolInstance {
private final String command;
private final boolean isDownloaded;
public ToolInstance(String command, boolean isDownloaded) {
this.command = command;
this.isDownloaded = isDownloaded;
}

public String getCommand() {
return command;
}

public boolean isDownloaded() {
return isDownloaded;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ protected void tearDown() throws Exception {
}

public void testThatGZIsDownloaded() throws IOException {
String cmd = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-gz.json"));
assertNotNull(cmd);
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", cmd);
assertEquals(17, new File(cmd).length());
DownloadHelper.ToolInstance toolInstance = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-gz.json"));
assertNotNull(toolInstance);
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
}

public void testThatTarGZIsDownloaded() throws IOException {
String cmd = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-tar.gz.json"));
assertNotNull(cmd);
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", cmd);
assertEquals(17, new File(cmd).length());
DownloadHelper.ToolInstance toolInstance = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-tar.gz.json"));
assertNotNull(toolInstance);
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
}

public void testThatPlainFileDownloaded() throws IOException{
String cmd = DownloadHelper.getInstance().downloadIfRequired("kn", DownloadHelperTest.class.getResource("/knative-test.json"));
assertNotNull(cmd);
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", cmd);
assertEquals(17, new File(cmd).length());
DownloadHelper.ToolInstance toolInstance = DownloadHelper.getInstance().downloadIfRequired("kn", DownloadHelperTest.class.getResource("/knative-test.json"));
assertNotNull(toolInstance);
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
}
}

0 comments on commit 1ecbc93

Please sign in to comment.