From 0cb65a7150d273d961aa7f7ae04973428f7bae8f Mon Sep 17 00:00:00 2001 From: Jan Horvath Date: Tue, 22 Oct 2024 15:53:28 +0200 Subject: [PATCH] VSCode integration commands --- .../cloud/oracle/assets/CloudAssets.java | 17 +++- .../oracle/assets/IntegrationCommands.java | 96 +++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/IntegrationCommands.java diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/CloudAssets.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/CloudAssets.java index b8b149a5ef93..0db32dbff1d2 100644 --- a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/CloudAssets.java +++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/CloudAssets.java @@ -244,6 +244,22 @@ public Collection getItems() { return list; } + /** + * Returns a Collection of all items for a given path + * + * @param path + * @return + */ + public List getItems(String path) { + List result = new ArrayList<> (); + for (OCIItem item : items) { + if (path != null && path.equals(item.getKey().getPath())) { + result.add(item); + } + } + return result; + } + /** * Returns a Collection of items assigned by user. This doesn't * include suggested items. @@ -292,7 +308,6 @@ public boolean setReferenceName(OCIItem item, String refName) { refNames.put(item, refName); storeAssets(); item.fireRefNameChanged(oldRefName, refName); -// item.fireRefNameChanged(null, refName); return true; } diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/IntegrationCommands.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/IntegrationCommands.java new file mode 100644 index 000000000000..d2037527e269 --- /dev/null +++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/IntegrationCommands.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.cloud.oracle.assets; + +import com.google.gson.Gson; +import com.google.gson.JsonPrimitive; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import org.netbeans.modules.cloud.oracle.developer.ContainerRepositoryItem; +import org.netbeans.modules.cloud.oracle.developer.ContainerTagItem; +import org.netbeans.modules.cloud.oracle.developer.ContainerTagNode; +import org.netbeans.modules.cloud.oracle.items.OCIItem; +import org.netbeans.spi.lsp.CommandProvider; +import org.openide.util.lookup.ServiceProvider; + +/** + * + * @author Jan Horvath + */ +@ServiceProvider(service = CommandProvider.class) +public class IntegrationCommands implements CommandProvider { + + private static final String COMMAND_ASSETS_GET = "nbls.cloud.assets.get"; //NOI18N + private static final String COMMAND_ASSETS_GET_IMAGE_VERSIONS = "nbls.cloud.assets.getImageVersions"; //NOI18N + + private static final Set COMMANDS = new HashSet<>(Arrays.asList( + COMMAND_ASSETS_GET, + COMMAND_ASSETS_GET_IMAGE_VERSIONS + )); + + private final Gson gson = new Gson(); + + @Override + public Set getCommands() { + return Collections.unmodifiableSet(COMMANDS); + } + + @Override + public CompletableFuture runCommand(String command, List arguments) { + CompletableFuture result = new CompletableFuture<>(); + if (COMMAND_ASSETS_GET.equals(command)) { + String path = parseStringArgument(arguments); //NOI18N + if (path != null) { + List items = CloudAssets.getDefault().getItems(path); + result.complete(items); + } else { + result.cancel(true); + } + } else if (COMMAND_ASSETS_GET_IMAGE_VERSIONS.equals(command)) { + List repos = CloudAssets.getDefault().getItems("ContainerRepository"); //NOI18N + if (repos != null && repos.size() == 1) { + ContainerRepositoryItem repo = (ContainerRepositoryItem) repos.get(0); + List tags = ContainerTagNode.getContainerTags().apply(repo); + Map tagUrls = new HashMap<> (); + for (ContainerTagItem tag : tags) { + tagUrls.put(tag.getName(), tag.getUrl()); + } + result.complete(tagUrls); + } else { + result.cancel(true); + } + } + return result; + } + + private String parseStringArgument(List arguments) { + if (!arguments.isEmpty()) { + JsonPrimitive item = gson.fromJson(gson.toJson(arguments.get(0)), JsonPrimitive.class); + return item.getAsString(); // NOI18N + } + return null; + } + +}