|
| 1 | +using AspireManifestGen.Options; |
| 2 | +using CliWrap; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace AspireManifestGen; |
| 10 | + |
| 11 | +[Command(PackageIds.InfraSynth)] |
| 12 | +internal sealed class InfraSynth : BaseDynamicCommand<InfraSynth, Project> |
| 13 | +{ |
| 14 | + const string STATUS_MESSAGE = "Synthesizing Azure infrastructure"; |
| 15 | + |
| 16 | + protected override void BeforeQueryStatus(OleMenuCommand menuItem, EventArgs e, Project item) |
| 17 | + { |
| 18 | + menuItem.Supported = item.IsCapabilityMatch("Aspire"); |
| 19 | + } |
| 20 | + |
| 21 | + protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project project) |
| 22 | + { |
| 23 | + var stdOutBuffer = new StringBuilder(); |
| 24 | + var stdErrBuffer = new StringBuilder(); |
| 25 | + OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General); |
| 26 | + |
| 27 | + var projectPath = FindAzureYaml(project.FullPath); |
| 28 | + |
| 29 | + await VS.StatusBar.StartAnimationAsync(StatusAnimation.Sync); |
| 30 | + await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2); |
| 31 | + |
| 32 | + var result = await Cli.Wrap("azd") |
| 33 | + .WithArguments($"infra synth --force --no-prompt") |
| 34 | + .WithWorkingDirectory(projectPath) |
| 35 | + .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer)) |
| 36 | + .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer)) |
| 37 | + .WithValidation(CommandResultValidation.None) |
| 38 | + .ExecuteAsync(); |
| 39 | + |
| 40 | + var stdErr = stdErrBuffer.ToString(); |
| 41 | + |
| 42 | + if (result.ExitCode != 0) |
| 43 | + { |
| 44 | + await pane.WriteLineAsync($"[AZD]: Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}"); |
| 45 | + goto Cleanup; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + await pane.WriteLineAsync($"[AZD]: infra synth completed"); |
| 50 | + } |
| 51 | + |
| 52 | + await VS.Documents.OpenAsync(Path.Combine(projectPath, "infra", "resources.bicep")); |
| 53 | + |
| 54 | + Cleanup: |
| 55 | + await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build); |
| 56 | + await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 2, 2); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + protected override IReadOnlyList<Project> GetItems() |
| 61 | + { |
| 62 | + return ThreadHelper.JoinableTaskFactory.Run(async () => |
| 63 | + { |
| 64 | + List<Project> items = [await VS.Solutions.GetActiveProjectAsync()]; |
| 65 | + return items; |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + public static string FindAzureYaml(string startDirectory) |
| 70 | + { |
| 71 | + var filePath = Path.Combine(startDirectory, "azure.yaml"); |
| 72 | + if (File.Exists(filePath)) |
| 73 | + { |
| 74 | + return Path.GetDirectoryName(filePath); |
| 75 | + } |
| 76 | + |
| 77 | + var parentDirectory = Directory.GetParent(startDirectory); |
| 78 | + if (parentDirectory == null) |
| 79 | + { |
| 80 | + return null; // File not found in any parent directories |
| 81 | + } |
| 82 | + |
| 83 | + return FindAzureYaml(parentDirectory.FullName); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments