refactor some code
This commit is contained in:
@@ -14,125 +14,14 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
_context = context;
|
||||
}
|
||||
|
||||
//protected string GetReplacedType(AnyOf<IPropertySymbol, IParameterSymbol> x)
|
||||
//{
|
||||
// ITypeSymbol ts = x.IsFirst ? x.First.Type : x.Second.Type;
|
||||
// var propertyTypeAsString = ts.ToString();
|
||||
|
||||
// var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
// if (existing is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
// }
|
||||
|
||||
// return existing.InterfaceName;
|
||||
// }
|
||||
|
||||
// if (ts is INamedTypeSymbol namedTypedSymbol)
|
||||
// {
|
||||
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
// {
|
||||
// var typeArgumentAsString = typeArgument.ToString();
|
||||
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
// if (exist is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
|
||||
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return propertyTypeAsStringToBeModified;
|
||||
// }
|
||||
|
||||
// return propertyTypeAsString;
|
||||
//}
|
||||
|
||||
protected string GetPropertyType(IPropertySymbol property, out bool isReplaced)
|
||||
{
|
||||
return GetReplacedType(property.Type, out isReplaced);
|
||||
|
||||
//var propertyTypeAsString = property.Type.ToString();
|
||||
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
// }
|
||||
|
||||
// return existing.InterfaceName;
|
||||
//}
|
||||
|
||||
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
//{
|
||||
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
// {
|
||||
// var typeArgumentAsString = typeArgument.ToString();
|
||||
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
// if (exist is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
|
||||
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return propertyTypeAsStringToBeModified;
|
||||
//}
|
||||
|
||||
//return propertyTypeAsString;
|
||||
}
|
||||
|
||||
protected string GetParameterType(IParameterSymbol property, out bool isReplaced)
|
||||
{
|
||||
return GetReplacedType(property.Type, out isReplaced);
|
||||
//var propertyTypeAsString = property.Type.ToString();
|
||||
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
// }
|
||||
|
||||
// return existing.InterfaceName;
|
||||
//}
|
||||
|
||||
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
//{
|
||||
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
// {
|
||||
// var typeArgumentAsString = typeArgument.ToString();
|
||||
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
// if (exist is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
|
||||
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return propertyTypeAsStringToBeModified;
|
||||
//}
|
||||
|
||||
//return propertyTypeAsString;
|
||||
}
|
||||
|
||||
protected string GetReplacedType(ITypeSymbol property, out bool isReplaced)
|
||||
|
||||
@@ -1,63 +1,78 @@
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using ProxyInterfaceSourceGenerator.FileGenerators;
|
||||
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class ProxyInterfaceCodeGenerator : ISourceGenerator
|
||||
{
|
||||
private readonly ProxyAttributeGenerator _proxyAttributeGenerator = new ProxyAttributeGenerator();
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
//if (!System.Diagnostics.Debugger.IsAttached)
|
||||
//{
|
||||
// System.Diagnostics.Debugger.Launch();
|
||||
//}
|
||||
|
||||
context.RegisterForSyntaxNotifications(() => new ProxySyntaxReceiver());
|
||||
}
|
||||
|
||||
public void Execute(GeneratorExecutionContext ctx)
|
||||
{
|
||||
if (ctx.SyntaxReceiver is not ProxySyntaxReceiver receiver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var context1 = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
|
||||
var attributeData = _proxyAttributeGenerator.GenerateFile();
|
||||
context1.GeneratorExecutionContext.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
|
||||
|
||||
var context2 = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
var partialInterfacesGenerator = new PartialInterfacesGenerator(context2);
|
||||
foreach (var data in partialInterfacesGenerator.GenerateFiles())
|
||||
{
|
||||
context2.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
|
||||
}
|
||||
|
||||
var context3 = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
var proxyClassesGenerator = new ProxyClassesGenerator(context3);
|
||||
foreach (var data in proxyClassesGenerator.GenerateFiles())
|
||||
{
|
||||
context3.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using ProxyInterfaceSourceGenerator.FileGenerators;
|
||||
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class ProxyInterfaceCodeGenerator : ISourceGenerator
|
||||
{
|
||||
private readonly ProxyAttributeGenerator _proxyAttributeGenerator = new ProxyAttributeGenerator();
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
//if (!System.Diagnostics.Debugger.IsAttached)
|
||||
//{
|
||||
// System.Diagnostics.Debugger.Launch();
|
||||
//}
|
||||
|
||||
context.RegisterForSyntaxNotifications(() => new ProxySyntaxReceiver());
|
||||
}
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
if (context.SyntaxReceiver is not ProxySyntaxReceiver receiver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GenerateProxyAttribute(context, receiver);
|
||||
GeneratePartialInterfaces(context, receiver);
|
||||
GenerateProxyClasses(context, receiver);
|
||||
}
|
||||
|
||||
private void GenerateProxyAttribute(GeneratorExecutionContext ctx, ProxySyntaxReceiver receiver)
|
||||
{
|
||||
var context = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
|
||||
var attributeData = _proxyAttributeGenerator.GenerateFile();
|
||||
context.GeneratorExecutionContext.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
|
||||
}
|
||||
|
||||
private static void GeneratePartialInterfaces(GeneratorExecutionContext ctx, ProxySyntaxReceiver receiver)
|
||||
{
|
||||
var context = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
|
||||
var partialInterfacesGenerator = new PartialInterfacesGenerator(context);
|
||||
foreach (var data in partialInterfacesGenerator.GenerateFiles())
|
||||
{
|
||||
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
|
||||
private static void GenerateProxyClasses(GeneratorExecutionContext ctx, ProxySyntaxReceiver receiver)
|
||||
{
|
||||
var context = new Context
|
||||
{
|
||||
GeneratorExecutionContext = ctx,
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
|
||||
var proxyClassesGenerator = new ProxyClassesGenerator(context);
|
||||
foreach (var data in proxyClassesGenerator.GenerateFiles())
|
||||
{
|
||||
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user