Skip to content

Commit 7e253ee

Browse files
committed
update
1 parent f81c637 commit 7e253ee

File tree

7 files changed

+359
-271
lines changed

7 files changed

+359
-271
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-service-explorer/src/main/java/com/microsoft/azure/toolkit/intellij/explorer/AzureExplorer.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import com.microsoft.azure.toolkit.ide.common.favorite.Favorites;
2929
import com.microsoft.azure.toolkit.ide.common.genericresource.GenericResourceActionsContributor;
3030
import com.microsoft.azure.toolkit.ide.common.genericresource.GenericResourceNode;
31-
import com.microsoft.azure.toolkit.ide.common.icon.AzureIcon;
3231
import com.microsoft.azure.toolkit.ide.common.icon.AzureIcons;
33-
import com.microsoft.azure.toolkit.intellij.common.TerminalUtils;
3432
import com.microsoft.azure.toolkit.intellij.common.component.Tree;
3533
import com.microsoft.azure.toolkit.intellij.common.component.TreeUtils;
36-
import com.microsoft.azure.toolkit.intellij.explorer.azd.AzdInitializeFromTemplates;
34+
import com.microsoft.azure.toolkit.intellij.explorer.azd.AzdNode;
3735
import com.microsoft.azure.toolkit.lib.Azure;
3836
import com.microsoft.azure.toolkit.lib.auth.AzureAccount;
3937
import com.microsoft.azure.toolkit.lib.auth.IAccountActions;
@@ -70,17 +68,19 @@ public class AzureExplorer extends Tree {
7068
@Getter
7169
public static final AzureExplorerNodeProviderManager manager = new AzureExplorerNodeProviderManager();
7270
public static final String AZURE_ICON = AzureIcons.Common.AZURE.getIconPath();
71+
private final AzdNode azdNode;
7372

7473
private AzureExplorer(Project project) {
7574
super();
7675
this.putClientProperty(PLACE, ResourceCommonActionsContributor.AZURE_EXPLORER);
76+
this.azdNode = new AzdNode(project);
7777
this.root = new Node<>("Azure")
7878
.withChildrenLoadLazily(false)
7979
.addChild(buildFavoriteRoot())
8080
.addChild(buildAppGroupedResourcesRoot())
8181
.addChild(buildTypeGroupedResourcesRoot())
8282
.addChildren(buildNonAzServiceNodes())
83-
.addChildren(buildAzdGroup(project));
83+
.addChild(azdNode);
8484

8585
this.init(this.root);
8686
this.setRootVisible(false);
@@ -107,52 +107,6 @@ private AzureExplorer(Project project) {
107107
}));
108108
}
109109

110-
private List<Node<?>> buildAzdGroup(Project project) {
111-
final Node<String> azd = new Node<>("Azure Developer (AZD)");
112-
azd.addChild(getInitializeFromTemplatesNode(project));
113-
azd.addChild(getInitializeFromSourceNode(project));
114-
azd.addChild(getProvisionResourcesNode(project));
115-
azd.addChild(getDeployToAzureNode(project));
116-
azd.addChild(getProvisionAndDeployToAzureNode(project));
117-
return List.of(azd);
118-
}
119-
120-
@Nonnull
121-
private static Node<String> getProvisionAndDeployToAzureNode(Project project) {
122-
123-
return new Node<>("Provision and Deploy to Azure")
124-
.withIcon(AzureIcons.Action.START)
125-
.onClicked(e -> TerminalUtils.executeInTerminal(project, "azd up", "azd"));
126-
}
127-
128-
@Nonnull
129-
private static Node<String> getDeployToAzureNode(Project project) {
130-
return new Node<>("Deploy to Azure")
131-
.withIcon(AzureIcons.Action.DEPLOY)
132-
.onClicked(e -> TerminalUtils.executeInTerminal(project, "azd deploy", "azd"));
133-
}
134-
135-
@Nonnull
136-
private static Node<String> getProvisionResourcesNode(Project project) {
137-
return new Node<>("Provision resources")
138-
.withIcon(AzureIcons.Action.EXPORT)
139-
.onClicked(e -> TerminalUtils.executeInTerminal(project, "azd provision", "azd"));
140-
}
141-
142-
@Nonnull
143-
private static Node<String> getInitializeFromSourceNode(Project project) {
144-
return new Node<>("Initialize from source")
145-
.withIcon(AzureIcons.Action.EDIT)
146-
.onClicked(e -> TerminalUtils.executeInTerminal(project, "azd init", "azd"));
147-
}
148-
149-
@Nonnull
150-
private static Node<String> getInitializeFromTemplatesNode(Project project) {
151-
return new Node<>("Initialize from templates")
152-
.withIcon(AzureIcons.Common.CREATE)
153-
.onClicked(e -> AzdInitializeFromTemplates.showToolPopup(project));
154-
}
155-
156110
@Override
157111
public String getToolTipText(MouseEvent event) {
158112
// add tooltip for hover actions

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-service-explorer/src/main/java/com/microsoft/azure/toolkit/intellij/explorer/azd/AzdInitializeFromTemplates.java

Lines changed: 0 additions & 142 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.microsoft.azure.toolkit.intellij.explorer.azd;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.intellij.openapi.project.Project;
9+
import com.microsoft.azure.toolkit.ide.common.component.Node;
10+
import com.microsoft.azure.toolkit.ide.common.icon.AzureIcons;
11+
import com.microsoft.azure.toolkit.intellij.common.TerminalUtils;
12+
13+
import javax.annotation.Nonnull;
14+
import java.io.BufferedReader;
15+
import java.io.InputStreamReader;
16+
import java.util.Map;
17+
import java.util.stream.Collectors;
18+
19+
public class AzdNode extends Node<String> {
20+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
21+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
22+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
23+
24+
private final Project project;
25+
26+
public AzdNode(Project project) {
27+
super("Azure Developer (Preview)");
28+
this.project = project;
29+
withIcon(AzureIcons.Common.SERVICES);
30+
addChildren();
31+
}
32+
33+
public void addChildren() {
34+
if (isAzdInstalled()) {
35+
if (isAzdSignedIn()) {
36+
withDescription("Signed In");
37+
addChild(getCreateFromTemplatesNode(project));
38+
addChild(getInitializeFromSourceNode(project));
39+
addChild(getProvisionResourcesNode(project));
40+
addChild(getDeployToAzureNode(project));
41+
addChild(getProvisionAndDeployToAzureNode(project));
42+
} else {
43+
withDescription("Not Signed In");
44+
onClicked(e -> {
45+
final ConfirmAndRunDialog confirmAndRunDialog = new ConfirmAndRunDialog(project, "Sign in", "Do you want to sign in to Azure Developer CLI (azd)?", "azd auth login");
46+
confirmAndRunDialog.setOkButtonText("Sign In");
47+
confirmAndRunDialog.show();
48+
});
49+
}
50+
} else {
51+
withDescription("Install azd");
52+
onClicked(e -> {
53+
final String command;
54+
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
55+
command = "winget install microsoft.azd";
56+
TerminalUtils.executeInTerminal(project, "winget install microsoft.azd", "azd");
57+
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
58+
command = "curl -fsSL https://aka.ms/install-azd.sh | bash";
59+
TerminalUtils.executeInTerminal(project, "curl -fsSL https://aka.ms/install-azd.sh | bash", "azd");
60+
} else {
61+
command = "brew tap azure/azd && brew install azd";
62+
TerminalUtils.executeInTerminal(project, "brew tap azure/azd && brew install azd", "azd");
63+
}
64+
final ConfirmAndRunDialog installDialog = new ConfirmAndRunDialog(project, "Install azd", "Do you want to install Azure Developer CLI (azd)?", command);
65+
installDialog.setOkButtonText("Install");
66+
installDialog.show();
67+
});
68+
}
69+
}
70+
71+
private static Node<String> getProvisionAndDeployToAzureNode(Project project) {
72+
return new Node<>("Provision and Deploy")
73+
.withIcon(AzureIcons.Action.START)
74+
.onClicked(e -> new ConfirmAndRunDialog(project, "Provision and deploy", "Do you want to provision and deploy to Azure?", "azd up").show());
75+
}
76+
77+
private static Node<String> getDeployToAzureNode(Project project) {
78+
return new Node<>("Deploy to Azure")
79+
.withIcon(AzureIcons.Action.DEPLOY)
80+
.onClicked(e -> new ConfirmAndRunDialog(project, "Deploy to Azure", "Do you want to start deployment to Azure?", "azd deploy").show());
81+
}
82+
83+
private static Node<String> getProvisionResourcesNode(Project project) {
84+
return new Node<>("Provision resources")
85+
.withIcon(AzureIcons.Action.EXPORT)
86+
.onClicked(e -> new ConfirmAndRunDialog(project, "Provision Resources", "Do you want to provision Azure resources?", "azd provision").show());
87+
}
88+
89+
private static Node<String> getInitializeFromSourceNode(Project project) {
90+
return new Node<>("Initialize from source")
91+
.withIcon(AzureIcons.Action.EDIT)
92+
.onClicked(e -> new ConfirmAndRunDialog(project, "Initialize from source", "Do you want to initialize using existing code?", "azd init").show());
93+
}
94+
95+
private static Node<String> getCreateFromTemplatesNode(Project project) {
96+
return new Node<>("Create from templates")
97+
.withIcon(AzureIcons.Common.CREATE)
98+
.onClicked(e -> new AzdTemplatesDialog(project).show());
99+
}
100+
101+
private static boolean isAzdInstalled() {
102+
final String azdVersionJson = runCommand("azd version -o json");
103+
if (azdVersionJson != null && !azdVersionJson.isEmpty()) {
104+
try {
105+
final Map<String, String> response = OBJECT_MAPPER.readValue(azdVersionJson, Map.class);
106+
if (response.containsKey("azd")) {
107+
return true;
108+
}
109+
} catch (JsonProcessingException e) {
110+
}
111+
}
112+
return false;
113+
}
114+
115+
private static boolean isAzdSignedIn() {
116+
final String loginStatusJson = runCommand("azd auth login --check-status -o json");
117+
if (loginStatusJson != null && !loginStatusJson.isEmpty()) {
118+
try {
119+
final Map<String, String> response = OBJECT_MAPPER.readValue(loginStatusJson, Map.class);
120+
if (response.containsKey("status") && "success".equals(response.get("status"))) {
121+
return true;
122+
}
123+
} catch (JsonProcessingException e) {
124+
}
125+
}
126+
return false;
127+
}
128+
129+
public static String runCommand(String command) {
130+
try {
131+
final ProcessBuilder processBuilder = new ProcessBuilder();
132+
// Detect OS and set the appropriate command
133+
String os = System.getProperty("os.name").toLowerCase();
134+
if (os.contains("win")) {
135+
processBuilder.command("cmd", "/c", command); // Windows
136+
} else {
137+
processBuilder.command("bash", "-c", command); // Linux/Unix
138+
}
139+
140+
Process process = processBuilder.start();
141+
142+
// Read the command output
143+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
144+
String output = reader.lines().collect(Collectors.joining("\n"));
145+
int exitCode = process.waitFor();
146+
if (exitCode != 0) {
147+
return null;
148+
}
149+
return output;
150+
}
151+
} catch (Exception e) {
152+
return null; // Handle error appropriately
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)