Skip to content

Commit

Permalink
added infra synth experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Dec 11, 2023
1 parent 3fb1da0 commit a176653
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/AspireManifestGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Commands\InfraSynth.cs" />
<Compile Include="Options\General.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Commands\ManifestGen.cs" />
Expand Down
86 changes: 86 additions & 0 deletions src/Commands/InfraSynth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using AspireManifestGen.Options;
using CliWrap;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace AspireManifestGen;

[Command(PackageIds.InfraSynth)]
internal sealed class InfraSynth : BaseDynamicCommand<InfraSynth, Project>
{
const string STATUS_MESSAGE = "Synthesizing Azure infrastructure";

protected override void BeforeQueryStatus(OleMenuCommand menuItem, EventArgs e, Project item)
{
menuItem.Supported = item.IsCapabilityMatch("Aspire");
}

protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project project)
{
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();
OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General);

var projectPath = FindAzureYaml(project.FullPath);

await VS.StatusBar.StartAnimationAsync(StatusAnimation.Sync);
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2);

var result = await Cli.Wrap("azd")
.WithArguments($"infra synth --force --no-prompt")
.WithWorkingDirectory(projectPath)
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
.WithValidation(CommandResultValidation.None)
.ExecuteAsync();

var stdErr = stdErrBuffer.ToString();

if (result.ExitCode != 0)
{
await pane.WriteLineAsync($"[AZD]: Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}");
goto Cleanup;
}
else
{
await pane.WriteLineAsync($"[AZD]: infra synth completed");
}

await VS.Documents.OpenAsync(Path.Combine(projectPath, "infra", "resources.bicep"));

Cleanup:
await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build);
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 2, 2);
return;
}

protected override IReadOnlyList<Project> GetItems()
{
return ThreadHelper.JoinableTaskFactory.Run(async () =>
{
List<Project> items = [await VS.Solutions.GetActiveProjectAsync()];
return items;
});
}

public static string FindAzureYaml(string startDirectory)
{
var filePath = Path.Combine(startDirectory, "azure.yaml");
if (File.Exists(filePath))
{
return Path.GetDirectoryName(filePath);
}

var parentDirectory = Directory.GetParent(startDirectory);
if (parentDirectory == null)
{
return null; // File not found in any parent directories
}

return FindAzureYaml(parentDirectory.FullName);
}

}
6 changes: 4 additions & 2 deletions src/Commands/ManifestGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace AspireManifestGen;
[Command(PackageIds.MyCommand)]
internal sealed class ManifestGen : BaseDynamicCommand<ManifestGen, Project>
{
const string STATUS_MESSAGE = "Generating Aspire Manifest";

protected override void BeforeQueryStatus(OleMenuCommand menuItem, EventArgs e, Project item)
{
menuItem.Supported = item.IsCapabilityMatch("Aspire");
Expand Down Expand Up @@ -38,7 +40,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
}

await VS.StatusBar.StartAnimationAsync(StatusAnimation.Build);
await VS.StatusBar.ShowProgressAsync("Generating Aspire Manifest", 1, 2);
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2);

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

Cleanup:
await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build);
await VS.StatusBar.ShowProgressAsync("Generating Aspire Manifest", 2, 2);
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 2, 2);
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/VSCommandTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal sealed partial class PackageIds
{
public const int ContextMenuGroup = 0x0001;
public const int MyCommand = 0x0100;
public const int InfraSynth = 0x0200;
public const int CommandIcon1 = 0x0001;
}
}
13 changes: 13 additions & 0 deletions src/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
<LocCanonicalName>.AspireManifestGen.MyCommand</LocCanonicalName>
</Strings>
</Button>
<Button guid="AspireManifestGen" id="InfraSynth" priority="0x0101" type="Button">
<Parent guid="AspireManifestGen" id="ContextMenuGroup" />
<Icon guid="ImageCatalogGuid" id="Sync"/>
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<CommandFlag>IconIsMoniker</CommandFlag>
<Strings>
<ButtonText>Infra Synth</ButtonText>
<LocCanonicalName>.AspireManifestGen.InfraSynth</LocCanonicalName>
</Strings>
</Button>
</Buttons>
<Bitmaps>
<Bitmap guid="CommandIcon" href="Resources\commandIcon.png" usedList="CommandIcon1"/>
Expand All @@ -34,12 +45,14 @@

<VisibilityConstraints>
<VisibilityItem guid="AspireManifestGen" id="MyCommand" context="UIContextGuid"></VisibilityItem>
<VisibilityItem guid="AspireManifestGen" id="InfraSynth" context="UIContextGuid"></VisibilityItem>
</VisibilityConstraints>

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

0 comments on commit a176653

Please sign in to comment.