Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DemoApp/Services/IntermediateCaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DemoApp.Services;

public class IntermediateCaGenerator
{
ISerialNumberProvider _serialNumberProvider;
readonly ISerialNumberProvider _serialNumberProvider;
public IntermediateCaGenerator(ISerialNumberProvider serialNumberProvider)
{
_serialNumberProvider = serialNumberProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.CommandLine.Minimal.SourceGenerator;
using System.Threading;

namespace System.CommandLine.Minimal.SourceGeneration;
Expand All @@ -22,17 +24,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
).Collect();

context.RegisterSourceOutput(bindersProvider, (spc, binders) => {

HashSet<string> commandNames = new();

// first generate and create all of the CommandOptions classes
foreach (GeneratingCommandBinder? binder in binders)
{
if(binder.CommandName is null || string.IsNullOrWhiteSpace(binder.CommandName))
{
spc.ReportCommandNameEmptyError(binder.CommandNameLocation);
continue;
}
if(commandNames.Contains(binder.CommandName))
{
spc.ReportCommandNameConflict(binder.CommandNameLocation, binder.CommandName);
continue;
}

// command name is validated so add to the hashset
commandNames.Add(binder.CommandName);

string? code = CommandOptionsWriter.GenerateOptions(binder);
if(code is not null)
{
spc.AddSource($"{binder.ClassName}_{binder.MethodName}_Command.g.cs", code);
}
}

// Emit the aggregated Register method
string? registryCode = MapAllCommandsExtensionWriter.GenerateMapAllCommandsExt(binders);
if(registryCode is not null)
Expand Down
45 changes: 45 additions & 0 deletions src/System.CommandLine.Minimal.SourceGenerator/DiagnosticErrors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.CodeAnalysis;

namespace System.CommandLine.Minimal.SourceGenerator
{
internal static class DiagnosticErrors
{
const string CommandNameCategory = "Command Name";
static readonly DiagnosticDescriptor CommandNameConflict =
new DiagnosticDescriptor(
"MIN0001",

Check warning on line 10 in src/System.CommandLine.Minimal.SourceGenerator/DiagnosticErrors.cs

View workflow job for this annotation

GitHub Actions / build

Enable analyzer release tracking for the analyzer project containing rule 'MIN0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 10 in src/System.CommandLine.Minimal.SourceGenerator/DiagnosticErrors.cs

View workflow job for this annotation

GitHub Actions / build

Enable analyzer release tracking for the analyzer project containing rule 'MIN0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)
"Command name conflict",
"Another Handler attribute already has this command name \"{0}\"",
CommandNameCategory,
DiagnosticSeverity.Error,
true
);
static readonly DiagnosticDescriptor CommandNameEmpty =
new DiagnosticDescriptor(
"MIN0002",

Check warning on line 19 in src/System.CommandLine.Minimal.SourceGenerator/DiagnosticErrors.cs

View workflow job for this annotation

GitHub Actions / build

Enable analyzer release tracking for the analyzer project containing rule 'MIN0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/System.CommandLine.Minimal.SourceGenerator/DiagnosticErrors.cs

View workflow job for this annotation

GitHub Actions / build

Enable analyzer release tracking for the analyzer project containing rule 'MIN0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)
"Command name empty",
"A command name is null or empty",
CommandNameCategory,
DiagnosticSeverity.Error,
true
);

/// <summary>
/// MIN0002 empty command name.
/// </summary>
internal static void ReportCommandNameEmptyError(
this SourceProductionContext ctx,
Location? location)
=> ctx.ReportDiagnostic(Diagnostic.Create(CommandNameEmpty, location));

/// <summary>
/// MIN0001 duplicate command name.
/// </summary>
internal static void ReportCommandNameConflict(
this SourceProductionContext ctx,
Location? location,
string commandName)
=> ctx.ReportDiagnostic(Diagnostic.Create(CommandNameConflict, location, commandName));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ public static GeneratingCommandBinder Transform(GeneratorAttributeSyntaxContext
&& a.AttributeClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.CommandLine.Minimal.HandlerAttribute");
string? commandName = handlerAttribute?.ConstructorArguments.FirstOrDefault().Value as string;

Location? argumentLocation = null;
if (ctx.TargetNode is MethodDeclarationSyntax methodDecl)
{
AttributeSyntax? handlerAttrSyntax = methodDecl.AttributeLists
.SelectMany(attrs => attrs.Attributes)
.FirstOrDefault(attr => ctx.SemanticModel
.GetTypeInfo(attr)
.Type?
.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.CommandLine.Minimal.HandlerAttribute"
);
argumentLocation = handlerAttrSyntax?.ArgumentList?.Arguments.FirstOrDefault()?.GetLocation();
}
//// Find the AttributeSyntax node for the HandlerAttribute
//AttributeSyntax? handlerAttributeSyntax = ctx.TargetNode switch
//{
// MethodDeclarationSyntax methodDecl => methodDecl.AttributeLists
// .SelectMany(list => list.Attributes)
// .FirstOrDefault(attr =>
// ctx.SemanticModel.GetTypeInfo(attr).Type?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
// == "global::System.CommandLine.Minimal.HandlerAttribute"),
// _ => null
//};
//Location? argumentLocation = handlerAttributeSyntax?.ArgumentList?.Arguments.FirstOrDefault()?.GetLocation();


string classNamespace = methodSymbol.ContainingNamespace.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
string className = methodSymbol.ContainingType.Name;
string methodName = methodSymbol.Name;
Expand Down Expand Up @@ -73,6 +98,7 @@ public static GeneratingCommandBinder Transform(GeneratorAttributeSyntaxContext
MethodName: methodName,
MethodReturnType: methodReturnType,
MethodIsStatic: methodIsStatic,
CommandNameLocation: argumentLocation,
Bindings: bindings.ToImmutableArray());
}

Expand Down Expand Up @@ -221,6 +247,7 @@ internal record GeneratingCommandBinder(
string MethodName,
string MethodReturnType,
bool MethodIsStatic,
Location? CommandNameLocation,
ImmutableArray<ParameterBinding>? Bindings
)
{
Expand Down