Skip to content

Commit

Permalink
dont refresh tree if no download occurred (#802)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Apr 26, 2024
1 parent d4d09d4 commit 794433c
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object s

protected Helm getHelm(AnActionEvent anActionEvent) {
try {
return ActionUtils.getApplicationRootNode(anActionEvent).getHelm(true).getNow(null);
return ActionUtils.getApplicationRootNode(anActionEvent).getHelm(true);
} catch (Exception e) {
LOGGER.warn("Could not get helm: " + e.getMessage(), e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object s

protected Odo getOdo(AnActionEvent anActionEvent) {
try {
return ActionUtils.getApplicationRootNode(anActionEvent).getOdo().getNow(null);
return ActionUtils.getApplicationRootNode(anActionEvent).getOdo();
} catch(Exception e) {
LOGGER.warn("Could not get odo: " + e.getMessage(), e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void execute(ParentableNode<?> node) {
if (node == null) {
return;
}
Odo odo = node.getRoot().getOdo().getNow(null);
Odo odo = node.getRoot().getOdo();
if (odo == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean isVisible(Object selected) {
boolean visible = super.isVisible(selected);
if (visible) {
ComponentNode componentNode = (ComponentNode) selected;
Odo odo = componentNode.getRoot().getOdo().getNow(null);
Odo odo = componentNode.getRoot().getOdo();
if (odo == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void actionPerformedOnSelectedObject(AnActionEvent anActionEvent, Object
return;
}
ApplicationsRootNode rootNode = parentableNode.getRoot();
Odo odo = rootNode.getOdo().getNow(null);
Odo odo = rootNode.getOdo();
if (odo == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void execute(ParentableNode<?> node) {
return;
}
ApplicationsRootNode rootNode = node.getRoot();
Odo odo = rootNode.getOdo().getNow(null);
Odo odo = rootNode.getOdo();
if (odo == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class CreateProjectAction extends LoggedInClusterAction {


public static void execute(ApplicationsRootNode rootNode) {
Odo odo = rootNode.getOdo().getNow(null);
Odo odo = rootNode.getOdo();
if (odo == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.tools.intellij.openshift.actions.NotificationUtils;
import org.jboss.tools.intellij.openshift.utils.ProjectUtils;
import org.jboss.tools.intellij.openshift.utils.ToolFactory;
import org.jboss.tools.intellij.openshift.utils.ToolFactory.Tool;
import org.jboss.tools.intellij.openshift.utils.helm.Helm;
import org.jboss.tools.intellij.openshift.utils.odo.ComponentDescriptor;
import org.jboss.tools.intellij.openshift.utils.odo.Odo;
Expand All @@ -50,8 +51,8 @@ public class ApplicationsRootNode
private final ApplicationsTreeStructure structure;
private final ProcessingNodeImpl processingNode = new ProcessingNodeImpl();
private final Map<String, ComponentDescriptor> components = new HashMap<>();
private CompletableFuture<Odo> odoFuture;
private CompletableFuture<Helm> helmFuture;
private CompletableFuture<Tool<Odo>> odoFuture;
private CompletableFuture<Tool<Helm>> helmFuture;
private boolean logged;
private Config config;
private final OdoProcessHelper processHelper;
Expand Down Expand Up @@ -80,46 +81,58 @@ public void setLogged(boolean logged) {
this.logged = logged;
}

private CompletableFuture<Odo> getOdo(BiConsumer<Odo, Throwable> whenComplete) {
private CompletableFuture<Tool<Odo>> getOdoTool(BiConsumer<Tool<Odo>, Throwable> whenComplete) {
if (odoFuture == null) {
this.odoFuture = ToolFactory.getInstance()
.createOdo(project)
.thenApply(odo -> (Odo) new ApplicationRootNodeOdo(odo, this, processHelper))
.whenComplete((odo, err) -> loadProjectModel(odo, project))
.whenComplete((tool, err) -> new ApplicationRootNodeOdo(tool.get(), this, processHelper))
.whenComplete((tool, err) -> loadProjectModel(tool.get(), project))
.whenComplete(whenComplete);
}
return odoFuture;
}

public CompletableFuture<Odo> getOdo() {
return getOdo((odo, err) -> structure.fireModified(this));
public CompletableFuture<Tool<Odo>> getOdoTool() {
return getOdoTool((Tool<Odo> tool, Throwable err) -> {
if (tool.isDownloaded()) {
structure.fireModified(this);
}
});
}

public Odo getOdo() {
Tool<Odo> tool = getOdoTool().getNow(null);
if (tool == null) {
return null;
}
return tool.get();
}

public void resetOdo() {
this.odoFuture = null;
}

public CompletableFuture<Helm> getHelm(boolean notify) {
public CompletableFuture<ToolFactory.Tool<Helm>> getHelmTool(boolean notify) {
if (helmFuture == null) {
try {
ToolFactory toolFactory = ToolFactory.getInstance();
boolean downloaded = toolFactory.helmRequiresDownload();
this.helmFuture = toolFactory
.createHelm(project)
.whenComplete((odo, err) -> {
if (notify
&& downloaded) {
this.helmFuture = ToolFactory.getInstance()
.createHelm(project)
.whenComplete((tool, err) -> {
if (notify && tool.isDownloaded()) {
structure.fireModified(this);
}
}
);
} catch (IOException e) {
LOGGER.warn("Could not get helm.", e);
}
});
}
return helmFuture;
}

public Helm getHelm(boolean notify) {
Tool<Helm> tool = getHelmTool(notify).getNow(null);
if (tool == null) {
return null;
}
return tool.get();
}

public Project getProject() {
return project;
}
Expand Down Expand Up @@ -147,7 +160,7 @@ protected void loadProjectModel(Odo odo, Project project) {

@Override
public void moduleAdded(@NotNull Project project, @NotNull Module module) {
addContext(getOdo().getNow(null), ProjectUtils.getModuleRoot(module));
addContext(getOdo(), ProjectUtils.getModuleRoot(module));
}

@Override
Expand All @@ -165,10 +178,11 @@ private void addContextToSettings(String path, ComponentDescriptor descriptor) {
}

private void migrateOdo(ComponentDescriptor descriptor) {
getOdo()
.thenAccept(odo -> {
if (odo != null) {
odo.migrateComponent(descriptor.getName());
getOdoTool()
.thenAccept(tool -> {
if (tool != null
&& tool.get() != null) {
tool.get().migrateComponent(descriptor.getName());
}
})
.thenRun(() ->
Expand Down Expand Up @@ -198,7 +212,7 @@ private void addContext(Odo odo, VirtualFile modulePathFile) {

public void addContext(String modulePath) {
addContext(
getOdo().getNow(null),
getOdo(),
LocalFileSystem.getInstance().refreshAndFindFileByPath(modulePath));
}

Expand Down Expand Up @@ -234,7 +248,7 @@ public void onUpdate(ConfigWatcher source, Config config) {

public synchronized void refresh() {
resetOdo();
getOdo((odo, err) -> structure.fireModified(ApplicationsRootNode.this));
getOdoTool((odo, err) -> structure.fireModified(ApplicationsRootNode.this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Object getApplicationsRoot() {

@NotNull
private Object[] createComponentChildren(ComponentNode componentNode) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new Object[]{new MessageNode<>(root, componentNode, "Could not get components")};
}
Expand All @@ -123,7 +123,7 @@ private Object[] createComponentChildren(ComponentNode componentNode) {

@NotNull
private Object[] createNamespaceChildren(@NotNull NamespaceNode namespaceNode) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new MessageNode[]{new MessageNode<>(root, namespaceNode, "Could not get project children")};
}
Expand All @@ -143,7 +143,7 @@ private Object[] createNamespaceChildren(@NotNull NamespaceNode namespaceNode) {
private Object getCurrentNamespace(ApplicationsRootNode element) {
Object node;
try {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new Object[]{new MessageNode<>(element, element, "Could not get current namespace")};
}
Expand All @@ -167,7 +167,7 @@ private Object getCurrentNamespace(ApplicationsRootNode element) {
}

private Object[] createHelmRepositoriesChildren(HelmRepositoriesNode parent) {
Helm helm = root.getHelm(true).getNow(null);
Helm helm = root.getHelm(true);
if (helm == null) {
return new Object[] { new MessageNode<>(root, parent, "Could not list repositories: Helm binary missing.") };
}
Expand Down Expand Up @@ -226,7 +226,7 @@ private List<BaseNode<?>> getServices(NamespaceNode namespaceNode, Odo odo) {
}

private List<BaseNode<?>> getHelmReleases(NamespaceNode namespaceNode) {
Helm helm = namespaceNode.getRoot().getHelm(true).getNow(null);
Helm helm = namespaceNode.getRoot().getHelm(true);
if (helm == null) {
return List.of(new MessageNode<>(root, namespaceNode, "Could not get chart releases"));
}
Expand All @@ -248,7 +248,7 @@ private List<BaseNode<?>> load(Callable<List<BaseNode<?>>> callable, NamespaceNo

private List<URLNode> getURLs(ComponentNode element) {
List<URLNode> results = new ArrayList<>();
Odo odo = element.getRoot().getOdo().getNow(null);
Odo odo = element.getRoot().getOdo();
if (odo == null) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -277,7 +277,7 @@ private List<BindingNode> getBindings(ComponentNode element, @NotNull Odo odo) {
}

private Object[] getRegistries(ApplicationsRootNode root) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new Object[]{new MessageNode<>(root, root, "Could not get registries")};
}
Expand All @@ -293,7 +293,7 @@ private Object[] getRegistries(ApplicationsRootNode root) {
}

private Object[] getRegistryComponentTypes(DevfileRegistryNode registryNode) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new Object[]{new MessageNode<>(root, registryNode, "Could not get registry component types")};
}
Expand All @@ -308,7 +308,7 @@ private Object[] getRegistryComponentTypes(DevfileRegistryNode registryNode) {
}

private Object[] getRegistryComponentTypeStarters(DevfileRegistryComponentTypeNode componentTypeNode) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return new Object[]{new MessageNode<>(root, componentTypeNode, "Could not get registry component starters")};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void execute() {
}

private static String getLabel(ApplicationsRootNode root) {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
String label = "Missing %s, <a>choose or create a different one</a>";
if (odo == null) {
return String.format(label, "project/namespace");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class DescriptorFactory {
root,
() -> {
try {
Odo odo = root.getOdo().getNow(null);
Odo odo = root.getOdo();
if (odo == null) {
return "Loading...";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,60 @@ public static ToolFactory getInstance() {
return INSTANCE;
}

private final Tool<Odo> odo = new Tool<>("odo", OdoCli::new);
private final Tool<Helm> helm = new Tool<>("helm", (project, command) -> new HelmCli(command));
private final Factory<Odo> odo = new Factory<>("odo", OdoCli::new);
private final Factory<Helm> helm = new Factory<>("helm", (project, command) -> new HelmCli(command));

private ToolFactory() {
}

public CompletableFuture<Odo> createOdo(Project project) {
public CompletableFuture<Tool<Odo>> createOdo(Project project) {
return odo.create(project);
}

public CompletableFuture<Helm> createHelm(Project project) {
public CompletableFuture<Tool<Helm>> createHelm(Project project) {
return helm.create(project);
}

public boolean helmRequiresDownload() throws IOException {
return helm.requiresDownload();
public static class Tool<T> {
private final T tool;
private final boolean isDownloaded;

private Tool(T tool, boolean isDownloaded) {
this.tool = tool;
this.isDownloaded = isDownloaded;
}

public T get() {
return tool;
}

public boolean isDownloaded() {
return isDownloaded;
}
}

private static class Tool<T> {
private static class Factory<T> {

private final String name;
private final URL url = ToolFactory.class.getResource(TOOLS_JSON);

private final BiFunction<Project, String, T> toolFactory;

private Tool(String name, BiFunction<Project, String, T> toolFactory) {
private Factory(String name, BiFunction<Project, String, T> toolFactory) {
this.name = name;
this.toolFactory = toolFactory;
}

private boolean requiresDownload() throws IOException {
return DownloadHelper.getInstance().isDownloadRequired(name, url);
}

private CompletableFuture<T> create(Project project) {
return create(name, url, toolFactory, project);
private CompletableFuture<Tool<T>> create(Project project) {
return create(name, toolFactory, project);
}

private CompletableFuture<T> create(String name, URL url, BiFunction<Project, String, T> toolFactory, Project project) {
private CompletableFuture<Tool<T>> create(String name, BiFunction<Project, String, T> toolFactory, Project project) {
return DownloadHelper.getInstance()
.downloadIfRequiredAsync(name, url)
.thenApply(command -> {
if (command != null) {
return toolFactory.apply(project, command);
return new Tool<>(toolFactory.apply(project, command.get()), command.isDownloaded());
} else {
return null;
}
Expand Down
Loading

0 comments on commit 794433c

Please sign in to comment.