Skip to content

Commit a176653

Browse files
committed
added infra synth experimental
1 parent 3fb1da0 commit a176653

5 files changed

+105
-2
lines changed

src/AspireManifestGen.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<WarningLevel>4</WarningLevel>
4646
</PropertyGroup>
4747
<ItemGroup>
48+
<Compile Include="Commands\InfraSynth.cs" />
4849
<Compile Include="Options\General.cs" />
4950
<Compile Include="Properties\AssemblyInfo.cs" />
5051
<Compile Include="Commands\ManifestGen.cs" />

src/Commands/InfraSynth.cs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/Commands/ManifestGen.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace AspireManifestGen;
1111
[Command(PackageIds.MyCommand)]
1212
internal sealed class ManifestGen : BaseDynamicCommand<ManifestGen, Project>
1313
{
14+
const string STATUS_MESSAGE = "Generating Aspire Manifest";
15+
1416
protected override void BeforeQueryStatus(OleMenuCommand menuItem, EventArgs e, Project item)
1517
{
1618
menuItem.Supported = item.IsCapabilityMatch("Aspire");
@@ -38,7 +40,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
3840
}
3941

4042
await VS.StatusBar.StartAnimationAsync(StatusAnimation.Build);
41-
await VS.StatusBar.ShowProgressAsync("Generating Aspire Manifest", 1, 2);
43+
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2);
4244

4345
var result = await Cli.Wrap("dotnet")
4446
.WithArguments($"msbuild /t:GenerateAspireManifest /p:AspireManifestPublishOutputPath={manifestPath}")
@@ -65,7 +67,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
6567

6668
Cleanup:
6769
await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build);
68-
await VS.StatusBar.ShowProgressAsync("Generating Aspire Manifest", 2, 2);
70+
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 2, 2);
6971
return;
7072
}
7173

src/VSCommandTable.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ internal sealed partial class PackageIds
2828
{
2929
public const int ContextMenuGroup = 0x0001;
3030
public const int MyCommand = 0x0100;
31+
public const int InfraSynth = 0x0200;
3132
public const int CommandIcon1 = 0x0001;
3233
}
3334
}

src/VSCommandTable.vsct

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
<LocCanonicalName>.AspireManifestGen.MyCommand</LocCanonicalName>
2727
</Strings>
2828
</Button>
29+
<Button guid="AspireManifestGen" id="InfraSynth" priority="0x0101" type="Button">
30+
<Parent guid="AspireManifestGen" id="ContextMenuGroup" />
31+
<Icon guid="ImageCatalogGuid" id="Sync"/>
32+
<CommandFlag>DynamicVisibility</CommandFlag>
33+
<CommandFlag>DefaultInvisible</CommandFlag>
34+
<CommandFlag>IconIsMoniker</CommandFlag>
35+
<Strings>
36+
<ButtonText>Infra Synth</ButtonText>
37+
<LocCanonicalName>.AspireManifestGen.InfraSynth</LocCanonicalName>
38+
</Strings>
39+
</Button>
2940
</Buttons>
3041
<Bitmaps>
3142
<Bitmap guid="CommandIcon" href="Resources\commandIcon.png" usedList="CommandIcon1"/>
@@ -34,12 +45,14 @@
3445

3546
<VisibilityConstraints>
3647
<VisibilityItem guid="AspireManifestGen" id="MyCommand" context="UIContextGuid"></VisibilityItem>
48+
<VisibilityItem guid="AspireManifestGen" id="InfraSynth" context="UIContextGuid"></VisibilityItem>
3749
</VisibilityConstraints>
3850

3951
<Symbols>
4052
<GuidSymbol name="AspireManifestGen" value="{caf1ee1b-7891-494a-8b1d-39bbff587cab}">
4153
<IDSymbol name="ContextMenuGroup" value="0x0001" />
4254
<IDSymbol name="MyCommand" value="0x0100" />
55+
<IDSymbol name="InfraSynth" value="0x0200"/>
4356
</GuidSymbol>
4457
<GuidSymbol name="UIContextGuid" value="{F686D1D0-9DDF-47DB-A0DC-59032E168F69}" />
4558
<GuidSymbol name="CommandIcon" value="{e70c51b3-9a0c-495d-9638-0578a72342c2}">

0 commit comments

Comments
 (0)