Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add silentMode to DownloadHelper (#121) #130

Merged
merged 4 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
package com.redhat.devtools.intellij.common.utils;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.util.io.HttpRequests;
import com.redhat.devtools.intellij.common.CommonConstants;
import com.twelvemonkeys.lang.Platform;
Expand Down Expand Up @@ -42,6 +44,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -91,6 +97,7 @@ public static DownloadHelper getInstance() {
* "versionExtractRegExp": "", //the regular expression to extract the version string from the version command
* "versionMatchRegExpr": "", //the regular expression use to match the extracted version to decide if download if required
* "baseDir": "" //the basedir to install to, a sub folder named after version will be created, can use $HOME
* "silentMode": true, //if the download needs to be started automatically without user input
* "platforms": {
* "win": {
* "url": "https://tool.com/tool/v1.0.0/odo-windows-amd64.exe.tar.gz",
Expand Down Expand Up @@ -118,7 +125,8 @@ public static DownloadHelper getInstance() {
* @return the command path
* @throws IOException if the tool was not found in the config file
*/
public String downloadIfRequired(String toolName, URL url) throws IOException {
private CompletableFuture<String> downloadIfRequiredAsyncInner(String toolName, URL url) throws IOException {
CompletableFuture<String> result = new CompletableFuture<>();
ToolsConfig config = ConfigHelper.loadToolsConfig(url);
ToolsConfig.Tool tool = config.getTools().get(toolName);
if (tool == null) {
Expand All @@ -129,46 +137,80 @@ public String downloadIfRequired(String toolName, URL url) throws IOException {
String version = getVersionFromPath(tool, platform);
if (!areCompatible(version, tool.getVersionMatchRegExpr())) {
Path path = Paths.get(tool.getBaseDir().replace("$HOME", CommonConstants.HOME_FOLDER), "cache", tool.getVersion(), command);
final String cmd = path.toString();
if (!Files.exists(path)) {
final Path dlFilePath = path.resolveSibling(platform.getDlFileName());
final String cmd = path.toString();
if (isDownloadAllowed(toolName, version, tool.getVersion())) {
command = ProgressManager.getInstance().run(new Task.WithResult<String, IOException>(null, "Downloading " + toolName, true) {
@Override
public String compute(@NotNull ProgressIndicator progressIndicator) throws IOException {
return HttpRequests.request(platform.getUrl().toString()).useProxy(true).connect(request -> {
downloadFile(request.getInputStream(), dlFilePath, progressIndicator, request.getConnection().getContentLength());
if (progressIndicator.isCanceled()) {
throw new IOException("Cancelled");
} else {
uncompress(dlFilePath, path);
return cmd;
}
});
}
downloadInBackground(toolName, platform, path, cmd, tool, version, result);
} else {
result.complete(cmd);
}
} else {
result.complete(command);
}

return result;
}

private void downloadInBackground(String toolName, ToolsConfig.Platform platform, Path path, String cmd, ToolsConfig.Tool tool, String version, CompletableFuture<String> 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());
}
});
}
}

private void downloadInBackgroundManager(String toolName, ToolsConfig.Platform platform, Path path, String cmd, CompletableFuture<String> result) {
final Path dlFilePath = path.resolveSibling(platform.getDlFileName());
ProgressManager.getInstance().run(new Task.Backgroundable(null, "Downloading " + toolName, false) {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
try {
HttpRequests.request(platform.getUrl().toString()).useProxy(true).connect(request -> {
downloadFile(request.getInputStream(), dlFilePath, progressIndicator, request.getConnection().getContentLength());
uncompress(dlFilePath, path);
return cmd;
});
} catch (IOException ignored) {
result.completeExceptionally(new IOException("Error while setting tool " + toolName + "."));
}
} else {
command = path.toString();
}

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

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

public CompletableFuture<String> downloadIfRequiredAsync(String toolName, URL url) {
CompletableFuture<String> result = new CompletableFuture<>();
ApplicationManager.getApplication().invokeLater(() -> {
try {
result.complete(downloadIfRequired(toolName, url));
} catch (IOException e) {
result.completeExceptionally(e);
}
});
try {
return downloadIfRequiredAsyncInner(toolName, url);
} catch (IOException e) {
result.completeExceptionally(e);
}
return result;
}

private boolean isDownloadAllowed(String tool, String currentVersion, String requiredVersion) {
return Messages.showYesNoCancelDialog(StringUtils.isEmpty(currentVersion) ? tool + " not found , do you want to download " + tool + " " + requiredVersion + " ?" : tool + " " + currentVersion + " found, required version is " + requiredVersion + ", do you want to download " + tool + " ?", tool + " tool required", Messages.getQuestionIcon()) == Messages.YES;
return UIHelper.executeInUI(() ->
Messages.showYesNoCancelDialog(StringUtils.isEmpty(currentVersion) ? tool + " not found , do you want to download " + tool + " " + requiredVersion + " ?" : tool + " " + currentVersion + " found, required version is " + requiredVersion + ", do you want to download " + tool + " ?", tool + " tool required", Messages.getQuestionIcon()) == Messages.YES);
}

private boolean areCompatible(String version, String versionMatchRegExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class Tool {
private String versionExtractRegExp;
private String versionMatchRegExpr;
private String baseDir;
private boolean silentMode;

public Map<String, Platform> getPlatforms() {
return platforms;
Expand Down Expand Up @@ -74,6 +75,14 @@ public String getBaseDir() {
public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
}

public boolean isSilentMode() {
return silentMode;
}

public void setSilentMode(boolean silentMode) {
this.silentMode = silentMode;
}
}

public static class Platform {
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/tkn-test-gz.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"versionExtractRegExp": "Client version: (\\d+[\\.\\d+]*)\\s.*",
"versionMatchRegExpr": "0\\..*",
"baseDir": ".",
"silentMode": true,
"platforms": {
"win": {
"url": "file:src/test/resources/tkn.gz",
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/tkn-test-tar.gz.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"versionExtractRegExp": "Client version: (\\d+[\\.\\d+]*)\\s.*",
"versionMatchRegExpr": "0\\..*",
"baseDir": ".",
"silentMode": true,
"platforms": {
"win": {
"url": "file:src/test/resources/tkn.tar.gz",
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/tkn-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"versionExtractRegExp": "Client version: (\\d+[\\.\\d+]*)\\s.*",
"versionMatchRegExpr": "0\\..*",
"baseDir": "$HOME/.tekton",
"silentMode": true,
"platforms": {
"win": {
"url": "https://github.com/tektoncd/cli/releases/download/v0.5.0/tkn_0.5.0_Windows_x86_64.zip",
Expand Down