Skip to content

Commit 31deacb

Browse files
authored
[azsdk-cli] Refactor CLI command classes and remove output helper usage (#12125)
* [azsdk-cli] Support returning a command list for tool classes * Update tool classes to use multi command and SetHandlers
1 parent fc91cb6 commit 31deacb

91 files changed

Lines changed: 1456 additions & 2003 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Analyzer/EnforceToolsList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public override void Initialize(AnalysisContext context)
7474
return;
7575
}
7676

77-
var baseTool = symCtx.Compilation.GetTypeByMetadataName("Azure.Sdk.Tools.Cli.Contract.MCPTool");
77+
var baseTool = symCtx.Compilation.GetTypeByMetadataName("Azure.Sdk.Tools.Cli.Contract.MCPToolBase");
7878
if (baseTool != null && InheritsFrom(named, baseTool))
7979
{
8080
implementations.Add(named);

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Analyzer/EnforceToolsReturnTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private static bool IsValidReturnType(ITypeSymbol returnType, Compilation compil
159159
}
160160

161161
// Check if it implements Response (look in Azure.Sdk.Tools.Cli.Models namespace)
162-
var responseType = compilation.GetTypeByMetadataName("Azure.Sdk.Tools.Cli.Models.Response");
162+
var responseType = compilation.GetTypeByMetadataName("Azure.Sdk.Tools.Cli.Models.CommandResponse");
163163
if (responseType != null && InheritsFromOrImplements(returnType, responseType))
164164
{
165165
return true;

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Analyzer/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public async Task<Response> ProcessData(string myArg)
5353

5454
## Overview
5555

56-
The `EnforceToolsListAnalyzer` ensures that every class inheriting from `MCPTool` is properly registered in the `SharedOptions.ToolsList` static list, otherwise they will not be loaded at startup.
56+
The `EnforceToolsListAnalyzer` ensures that every class inheriting from `MCPToolBase` is properly registered in the `SharedOptions.ToolsList` static list, otherwise they will not be loaded at startup.
5757

5858
All non-abstract classes that inherit from `Azure.Sdk.Tools.Cli.Contract.MCPTool` must be included as `typeof(ClassName)` entries in the `SharedOptions.ToolsList` static field (`Azure.Sdk.Tools.Cli/Commands/SharedOptions.cs`).
5959

@@ -72,7 +72,7 @@ When you encounter MCP002 violations:
7272
// 1. Tool implementation
7373
public class MyCustomTool : MCPTool
7474
{
75-
public override Command GetCommand() { /* implementation */ }
75+
protected override Command GetCommand() { /* implementation */ }
7676
public override Task HandleCommand(InvocationContext ctx, CancellationToken ct) { /* implementation */ }
7777
}
7878

@@ -92,7 +92,7 @@ public static readonly List<Type> ToolsList = [
9292
The `EnforceToolsReturnTypesAnalyzer` ensures that all public non-static methods in classes within
9393
the `Azure.Sdk.Tools.Cli.Tools` namespace return only approved types at compile time.
9494

95-
This excludes inherited methods `GetCommand`, `HandleCommand`, etc.
95+
This excludes inherited methods `GetCommand`, `GetCommands`, `HandleCommand`, etc.
9696

9797
## Allowed Return Types
9898

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Contract/Azure.Sdk.Tools.Cli.Contract.csproj

Lines changed: 0 additions & 28 deletions
This file was deleted.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Contract/CommandGroup.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Contract/MCPTool.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Contract/README.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Tests/Azure.Sdk.Tools.Cli.Tests.csproj

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,31 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<ProjectReference Include="..\Azure.Sdk.Tools.Cli.Contract\Azure.Sdk.Tools.Cli.Contract.csproj" />
26-
<ProjectReference Include="..\Azure.Sdk.Tools.Cli\Azure.Sdk.Tools.Cli.csproj" />
25+
<ProjectReference Include="../Azure.Sdk.Tools.Cli/Azure.Sdk.Tools.Cli.csproj" />
2726
</ItemGroup>
2827

2928
<ItemGroup>
3029
<Using Include="NUnit.Framework" />
3130
</ItemGroup>
3231

3332
<ItemGroup>
34-
<None Include="TestAssets\**\*" CopyToOutputDirectory="PreserveNewest" />
33+
<None Include="TestAssets/**/*" CopyToOutputDirectory="PreserveNewest" />
3534
</ItemGroup>
3635

37-
<ItemGroup>
38-
<None Include="..\Azure.Sdk.Tools.Cli\Templates\ReadMeGenerator\README-template.go.md" Link="TestAssets\README-template.go.md">
39-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40-
</None>
36+
<ItemGroup>
37+
<None Include="../Azure.Sdk.Tools.Cli/Templates/ReadMeGenerator/README-template.go.md" Link="TestAssets/README-template.go.md">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</None>
4140
</ItemGroup>
4241

4342
<ItemGroup>
44-
<None Update="TypeSpecTestData\specification\testcontoso\Contoso.Management\employee.tsp">
43+
<None Update="TypeSpecTestData/specification/testcontoso/Contoso.Management/employee.tsp">
4544
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4645
</None>
47-
<None Update="TypeSpecTestData\specification\testcontoso\Contoso.Management\main.tsp">
46+
<None Update="TypeSpecTestData/specification/testcontoso/Contoso.Management/main.tsp">
4847
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4948
</None>
50-
<None Update="TypeSpecTestData\specification\testcontoso\Contoso.Management\tspconfig.yaml">
49+
<None Update="TypeSpecTestData/specification/testcontoso/Contoso.Management/tspconfig.yaml">
5150
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5251
</None>
5352
</ItemGroup>

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Tests/CliIntegrationTests.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Tests/Helpers/OutputHelperTests.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace Azure.Sdk.Tools.Cli.Tests.Helpers;
55

66
internal class OutputHelperTests
77
{
8-
private readonly string summary = "a test summary";
9-
private readonly List<LogEntry> errors =
10-
[
11-
new LogEntry
8+
private readonly string summary = "a test summary";
9+
private readonly List<LogEntry> errors =
10+
[
11+
new LogEntry
1212
{
1313
File = "file1",
1414
Line = 1,
@@ -20,13 +20,13 @@ internal class OutputHelperTests
2020
Line = 2,
2121
Message = "message2"
2222
}
23-
];
24-
private readonly string suggestedFix = "a test suggested fix";
23+
];
24+
private readonly string suggestedFix = "a test suggested fix";
2525

26-
[Test]
27-
public void TestTypedJsonOutput()
28-
{
29-
var json = @"{
26+
[Test]
27+
public void TestTypedJsonOutput()
28+
{
29+
var json = @"{
3030
""summary"": ""a test summary"",
3131
""errors"": [
3232
{
@@ -43,23 +43,23 @@ public void TestTypedJsonOutput()
4343
""suggested_fix"": ""a test suggested fix""
4444
}";
4545

46-
var output = new OutputHelper(OutputModes.Json);
47-
var formatted = output.ValidateAndFormat<LogAnalysisResponse>(json);
46+
var output = new OutputHelper(OutputHelper.OutputModes.Json);
47+
var formatted = output.ValidateAndFormat<LogAnalysisResponse>(json);
4848

49-
Assert.That(formatted, Is.EqualTo(json));
50-
}
49+
Assert.That(formatted, Is.EqualTo(json));
50+
}
5151

52-
[Test]
53-
public void TestPlainTextOutput()
54-
{
55-
var response = new LogAnalysisResponse
52+
[Test]
53+
public void TestPlainTextOutput()
5654
{
57-
Summary = summary,
58-
Errors = errors,
59-
SuggestedFix = suggestedFix
60-
};
55+
var response = new LogAnalysisResponse
56+
{
57+
Summary = summary,
58+
Errors = errors,
59+
SuggestedFix = suggestedFix
60+
};
6161

62-
var expectedStr = @"
62+
var expectedStr = @"
6363
### Summary:
6464
a test summary
6565
@@ -74,9 +74,9 @@ a test suggested fix
7474
message2
7575
".TrimStart();
7676

77-
var output = new OutputHelper(OutputModes.Plain);
78-
var formatted = output.Format(response);
77+
var output = new OutputHelper(OutputHelper.OutputModes.Plain);
78+
var formatted = output.Format(response);
7979

80-
Assert.That(formatted, Is.EqualTo(expectedStr));
81-
}
80+
Assert.That(formatted, Is.EqualTo(expectedStr));
81+
}
8282
}

0 commit comments

Comments
 (0)