From 550c154f63fd390234b4fd1d0efa2daae4b7d9e0 Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Fri, 26 Apr 2024 15:46:28 +0200 Subject: [PATCH] feat: DownloadHelper#downloadIfRequired allows to know if was downloaded Signed-off-by: Andre Dietisheim --- .../intellij/common/utils/DownloadHelper.java | 51 ++++++++++++------- .../common/utils/DownloadHelperTest.java | 27 +++++----- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java b/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java index 95a08a0..1ce360f 100644 --- a/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java @@ -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 downloadIfRequiredAsyncInner(String toolName, URL url) throws IOException { - CompletableFuture result = new CompletableFuture<>(); + private CompletableFuture downloadIfRequiredAsyncInner(String toolName, URL url) throws IOException { + CompletableFuture result = new CompletableFuture<>(); ToolsConfig config = ConfigHelper.loadToolsConfig(url); ToolsConfig.Tool tool = config.getTools().get(toolName); if (tool == null) { @@ -140,12 +140,11 @@ private CompletableFuture 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; } @@ -159,7 +158,7 @@ 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 result) { + private void downloadInBackground(String toolName, ToolsConfig.Platform platform, Path path, String cmd, ToolsConfig.Tool tool, String version, CompletableFuture result) { if (ApplicationManager.getApplication().isUnitTestMode()) { downloadInBackgroundManager(toolName, platform, path, cmd, result); } else { @@ -167,13 +166,13 @@ private void downloadInBackground(String toolName, ToolsConfig.Platform platform 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 result) { + private void downloadInBackgroundManager(String toolName, ToolsConfig.Platform platform, Path path, String cmd, CompletableFuture result) { final Path dlFilePath = path.resolveSibling(platform.getDlFileName()); ProgressManager.getInstance().run(new Task.Backgroundable(null, "Downloading " + toolName, false) { @Override @@ -184,22 +183,22 @@ 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 future = downloadIfRequiredAsyncInner(toolName, url); + public ToolInstance downloadIfRequired(String toolName, URL url) throws IOException { + CompletableFuture future = downloadIfRequiredAsyncInner(toolName, url); try { return future.get(); } catch (InterruptedException | ExecutionException e) { @@ -207,14 +206,14 @@ public String downloadIfRequired(String toolName, URL url) throws IOException { } } - public CompletableFuture downloadIfRequiredAsync(String toolName, URL url) { - CompletableFuture result = new CompletableFuture<>(); + public CompletableFuture downloadIfRequiredAsync(String toolName, URL url) { try { return downloadIfRequiredAsyncInner(toolName, url); } catch (IOException e) { - result.completeExceptionally(e); + CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(e); + return future; } - return result; } private boolean isDownloadAllowed(String tool, String currentVersion, String requiredVersion) { @@ -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; + } + } + } diff --git a/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java b/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java index f784498..fb06e1f 100644 --- a/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java +++ b/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java @@ -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()); } }