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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,124 +5,120 @@
namespace EasyNetQ.Management.Client.ExtensionsGenerator;

[Generator]
public class ExtensionsGenerator : ISourceGenerator
public class ExtensionsGenerator : IIncrementalGenerator
{
public void Execute(GeneratorExecutionContext context)
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var compilation = context.Compilation;
// if (!System.Diagnostics.Debugger.IsAttached)
// {
// System.Diagnostics.Debugger.Launch();
// }

string typeName = "EasyNetQ.Management.Client.IManagementClient";
string typeExtensionsName = $"{typeName}Extensions";
context.RegisterSourceOutput(context.CompilationProvider, (context, compilation) =>
{
string typeName = "EasyNetQ.Management.Client.IManagementClient";
string typeExtensionsName = $"{typeName}Extensions";

var typeNames = new List<string> { typeName, typeExtensionsName };
var types = typeNames.Select(tn => compilation.GetTypeByMetadataName(tn) ?? throw new KeyNotFoundException(tn));
var typeNames = new List<string> { typeName, typeExtensionsName };
var types = typeNames.Select(tn => compilation.GetTypeByMetadataName(tn) ?? throw new KeyNotFoundException(tn));

Dictionary<string, CompilationUnitSyntax> compilationUnits = new();
Dictionary<string, CompilationUnitSyntax> compilationUnits = new();

var thisParameter = "this IManagementClient client".GetParameterSyntax();
QualifiedNameSyntax extensionsClassName = (SyntaxFactory.ParseTypeName(typeExtensionsName) as QualifiedNameSyntax)!;
var thisParameter = "this IManagementClient client".GetParameterSyntax();
QualifiedNameSyntax extensionsClassName = (SyntaxFactory.ParseTypeName(typeExtensionsName) as QualifiedNameSyntax)!;

{
var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left)
.WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true)));

foreach (var t in types)
{
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", "Vhost vhost", "vhost.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "exchange.Vhost"),
("string exchangeName", "ExchangeName exchange", "exchange.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "queue.Vhost"),
("string queueName", "QueueName queue", "queue.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "exchange.Vhost"),
("string exchangeName", "ExchangeName exchange", "exchange.Name"),
("string queueName", "QueueName queue", "queue.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "sourceExchange.Vhost"),
("string sourceExchangeName", "ExchangeName sourceExchange", "sourceExchange.Name"),
("string destinationExchangeName", "ExchangeName destinationExchange", "destinationExchange.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string connectionName", "Connection connection", "connection.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string userName", "User user", "user.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", "Vhost vhost", "vhost.Name"),
("string userName", "User user", "user.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string componentName", null, "parameter.Component"),
("string vhostName", null, "parameter.Vhost"),
("string parameterName", "Parameter parameter", "parameter.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string componentName", null, "parameter.Component"),
("string vhostName", null, "parameter.Vhost"),
("string parameterName", null, "parameter.Name"),
("object parameterValue", "Parameter parameter", "parameter.Value")
);
var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left)
.WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true)));

foreach (var t in types)
{
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", "Vhost vhost", "vhost.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "exchange.Vhost"),
("string exchangeName", "ExchangeName exchange", "exchange.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "queue.Vhost"),
("string queueName", "QueueName queue", "queue.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "exchange.Vhost"),
("string exchangeName", "ExchangeName exchange", "exchange.Name"),
("string queueName", "QueueName queue", "queue.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", null, "sourceExchange.Vhost"),
("string sourceExchangeName", "ExchangeName sourceExchange", "sourceExchange.Name"),
("string destinationExchangeName", "ExchangeName destinationExchange", "destinationExchange.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string connectionName", "Connection connection", "connection.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string userName", "User user", "user.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string vhostName", "Vhost vhost", "vhost.Name"),
("string userName", "User user", "user.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string componentName", null, "parameter.Component"),
("string vhostName", null, "parameter.Vhost"),
("string parameterName", "Parameter parameter", "parameter.Name")
);
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddReplacementExtensionsClass(extensionsClassName, t, thisParameter,
("string componentName", null, "parameter.Component"),
("string vhostName", null, "parameter.Vhost"),
("string parameterName", null, "parameter.Name"),
("object parameterValue", "Parameter parameter", "parameter.Value")
);
}

var compilationUnit = SyntaxFactory.CompilationUnit()
.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model")))
.AddMembers(fileScopedNamespaceDeclaration)
.NormalizeWhitespace(eol: "\n");

compilationUnits[$"{extensionsClassName.Right}Replacement"] = compilationUnit;

compilation = compilation.AddSyntaxTrees(compilationUnit.SyntaxTree);
}

var compilationUnit = SyntaxFactory.CompilationUnit()
.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model")))
.AddMembers(fileScopedNamespaceDeclaration)
.NormalizeWhitespace(eol: "\n");
{
var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left)
.WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true)));

compilationUnits[$"{extensionsClassName.Right}Replacement"] = compilationUnit;
foreach (var t in types)
{
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddSyncExtensionsClass(extensionsClassName, t, thisParameter);
}

compilation = compilation.AddSyntaxTrees(compilationUnit.SyntaxTree);
}
var compilationUnit = SyntaxFactory.CompilationUnit()
.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model")))
.AddMembers(fileScopedNamespaceDeclaration)
.NormalizeWhitespace(eol: "\n");

{
var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left)
.WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true)));
compilationUnits[$"{extensionsClassName.Right}Sync"] = compilationUnit;
}

foreach (var t in types)
foreach (var kvpair in compilationUnits)
{
fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration
.AddSyncExtensionsClass(extensionsClassName, t, thisParameter);
context.AddSource($"{kvpair.Key}.g.cs", kvpair.Value.ToString());
}

var compilationUnit = SyntaxFactory.CompilationUnit()
.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model")))
.AddMembers(fileScopedNamespaceDeclaration)
.NormalizeWhitespace(eol: "\n");

compilationUnits[$"{extensionsClassName.Right}Sync"] = compilationUnit;
}

foreach (var kvpair in compilationUnits)
{
context.AddSource($"{kvpair.Key}.g.cs", kvpair.Value.ToString());
}
}

public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this one

// if (!System.Diagnostics.Debugger.IsAttached)
// {
// System.Diagnostics.Debugger.Launch();
// }
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<MinVerTagPrefix>v</MinVerTagPrefix>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

<EmitCompilerGeneratedFiles Condition="'$(TargetFramework)' == 'netstandard2.0' AND '$(Configuration)' == 'Debug'">true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading