2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-26 21:13:13 +00:00

Source Generation

This commit is contained in:
2021-05-17 05:34:09 +03:00
parent 5bf258673d
commit c8683e17e9
16 changed files with 148 additions and 57 deletions

View File

@ -24,47 +24,57 @@ namespace Esiur.Proxy
if (!(context.SyntaxContextReceiver is ResourceGeneratorReceiver receiver))
return;
//#if DEBUG
// if (!Debugger.IsAttached)
// {
// Debugger.Launch();
// }
//#endif
//var toImplement = receiver.Classes.Where(x => x.Fields.Length > 0);
foreach (var ci in receiver.Classes)
try
{
var code = @$"using Esiur.Resource;
//#if DEBUG
// if (!Debugger.IsAttached)
// {
// Debugger.Launch();
// }
//#endif
//var toImplement = receiver.Classes.Where(x => x.Fields.Length > 0);
foreach (var ci in receiver.Classes)
{
var code = @$"using Esiur.Resource;
using Esiur.Core;
namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
";
if (ci.ImplementInterface)
code += $"public partial class {ci.Name} {{";
else
{
code += @$"public partial class {ci.Name} : IResource {{
if (ci.ImplementInterface)
code += $"public partial class {ci.Name} {{";
else
{
code += @$"public partial class {ci.Name} : IResource {{
public Instance Instance {{ get; set; }}
public event DestroyedEvent OnDestroy;
public virtual void Destroy() {{ OnDestroy?.Invoke(this); }}
";
if (!ci.ImplementTrigger)
code += "public AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);";
if (!ci.ImplementTrigger)
code += "public AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n";
}
foreach (var f in ci.Fields)
{
var fn = f.Name;
var pn = fn.Substring(0, 1).ToUpper() + fn.Substring(1);
// copy attributes
var attrs = string.Join(" ", f.GetAttributes().Select(x => $"[{x.ToString()}]"));
code += $"{attrs} public {f.Type} {pn} {{ get => {fn}; set {{ {fn} = value; Instance?.Modified(); }} }}\r\n";
}
code += "}}\r\n";
//System.IO.File.WriteAllText("c:\\gen\\" + ci.Name + "_esiur.cs", code);
context.AddSource(ci.Name + "_esiur.cs", code);
}
foreach (var f in ci.Fields)
{
var fn = f.Name;
var pn = fn.Substring(0, 1).ToUpper() + fn.Substring(1);
code += $@"[Public] public {f.Type} {pn} {{ get => {fn}; set {{ {fn} = value; Instance.Modified(); }} }}";
}
code += "}}";
//System.IO.File.WriteAllText("C:\\www\\class.cs", code);
context.AddSource(ci.Name + "_esiur.cs", code);
}
catch //(Exception ex)
{
//System.IO.File.AppendAllText("c:\\gen\\error.log", ex.ToString() + "\r\n");
}
}
}

View File

@ -6,7 +6,7 @@ using System.Text;
namespace Esiur.Proxy
{
public struct GenerationInfo
public struct ResourceGeneratorClassInfo
{
public string Name { get; set; }
public bool ImplementInterface { get; set; }
@ -16,5 +16,6 @@ namespace Esiur.Proxy
public ITypeSymbol ClassSymbol { get; set; }
public ClassDeclarationSyntax ClassDeclaration { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Proxy
{
public struct ResourceGeneratorFieldInfo
{
public IFieldSymbol FieldSymbol { get; set; }
public string[] Attributes { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace Esiur.Proxy
public class ResourceGeneratorReceiver : ISyntaxContextReceiver
{
public List<GenerationInfo> Classes { get; } = new();
public List<ResourceGeneratorClassInfo> Classes { get; } = new();
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
@ -23,11 +23,8 @@ namespace Esiur.Proxy
if (cls.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ResourceAttribute"))
{
//if (!Debugger.IsAttached)
//{
// Debugger.Launch();
//}
var hasTrigger = cds.Members
.Where(x => x is MethodDeclarationSyntax)
.Select(x => context.SemanticModel.GetDeclaredSymbol(x) as IMethodSymbol)
@ -40,9 +37,16 @@ namespace Esiur.Proxy
.Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.PublicAttribute"))
.ToArray();
//if (!Debugger.IsAttached)
//{
// if (cls.Name == "User")
// Debugger.Launch();
//}
// get fields
Classes.Add(new GenerationInfo()
Classes.Add(new ResourceGeneratorClassInfo()
{
Name = cls.Name,
ClassDeclaration = cds,