From dd92d60bf78f591b3c1636b3922aff9849abcc73 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 | 50 ++++++++++++------- .../common/utils/DownloadHelperTest.java | 27 +++++----- 2 files changed, 48 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..8521649 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 Command(cmd, false)); } } else { - result.complete(command); + result.complete(new Command(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 Command(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 Command(cmd, true)); } } }); } - public String downloadIfRequired(String toolName, URL url) throws IOException { - CompletableFuture future = downloadIfRequiredAsyncInner(toolName, url); + public Command 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) { @@ -253,6 +252,23 @@ private String getVersionFromPath(ToolsConfig.Tool tool, ToolsConfig.Platform pl } + public static class Command { + private final String command; + private final boolean isDownloaded; + public Command(String command, boolean isDownloaded) { + this.command = command; + this.isDownloaded = isDownloaded; + } + + public String get() { + return command; + } + + public boolean isDownloaded() { + return isDownloaded; + } + } + private static void downloadFile(InputStream input, Path dlFileName, ProgressIndicator progressIndicator, long size) throws IOException { byte[] buffer = new byte[4096]; Files.createDirectories(dlFileName.getParent()); 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..bc4b263 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.Command command = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-gz.json")); + assertNotNull(command); + assertNotNull(command.get()); + assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", command.get()); + assertEquals(17, new File(command.get()).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.Command command = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test-tar.gz.json")); + assertNotNull(command); + assertNotNull(command.get()); + assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", command.get()); + assertEquals(17, new File(command.get()).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.Command command = DownloadHelper.getInstance().downloadIfRequired("kn", DownloadHelperTest.class.getResource("/knative-test.json")); + assertNotNull(command); + assertNotNull(command.get()); + assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", command.get()); + assertEquals(17, new File(command.get()).length()); } }