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