2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2026-04-29 06:48:41 +00:00
This commit is contained in:
2026-04-04 23:54:59 +03:00
parent 5b0fba89a4
commit 44983d7784
17 changed files with 181 additions and 66 deletions
+66 -19
View File
@@ -10,6 +10,7 @@ using Esiur.Resource;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Org.BouncyCastle.Asn1.Cms;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -75,30 +76,71 @@ namespace Esiur.Proxy
{
try
{
var code = @$"using Esiur.Resource;
using Esiur.Core;
var code = new StringBuilder();
#nullable enable
var ns = ci.ClassSymbol.ContainingNamespace.ToDisplayString();
if (ns == null || ns == "<global namespace>")
{
namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
";
}
code.AppendLine("using Esiur.Resource;");
code.AppendLine("using Esiur.Core;");
code.AppendLine("#nullable enable");
if (!(ns == null || ns == "<global namespace>"))
{
code.AppendLine($"namespace {ns};");
}
code.AppendLine();
code.AppendLine($"// <auto-generated> DO NOT EDIT THIS FILE! </auto-generated>");
code.AppendLine($"// This file was generated by {nameof(ResourceGenerator)} based on the presence of [Resource] and [Export] attributes in {ci.ClassSymbol.Name}.cs");
if (IsInterfaceImplemented(ci, classes))
code += $"public partial class {ci.Name} {{\r\n";
{
code.AppendLine($"public partial class {ci.Name}");
code.AppendLine("{");
}
else
{
code +=
$@" public partial class {ci.Name} : IResource {{
public virtual Instance? Instance {{ get; set; }}
public virtual event DestroyedEvent? OnDestroy;
public virtual void Destroy() {{ OnDestroy?.Invoke(this); }}
";
code.AppendLine($"public partial class {ci.Name} : IResource");
code.AppendLine("{");
code.AppendLine("public virtual Instance? Instance { get; set; }");
code.AppendLine("public virtual event DestroyedEvent? OnDestroy;");
code.AppendLine("public virtual void Destroy() { OnDestroy?.Invoke(this); }");
if (!ci.HasTrigger)
code += "\tpublic virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n\r\n";
{
code.AppendLine("public virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);");
}
}
// var code = @$"using Esiur.Resource;
//using Esiur.Core;
//#nullable enable
//namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
//";
// if (IsInterfaceImplemented(ci, classes))
// code += $"public partial class {ci.Name} {{\r\n";
// else
// {
// code +=
//$@" public partial class {ci.Name} : IResource {{
// public virtual Instance? Instance {{ get; set; }}
// public virtual event DestroyedEvent? OnDestroy;
// public virtual void Destroy() {{ OnDestroy?.Invoke(this); }}
//";
// if (!ci.HasTrigger)
// code += "\tpublic virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n\r\n";
// }
foreach (var f in ci.Fields)
{
var givenName = f.GetAttributes().FirstOrDefault(x => x.AttributeClass?.Name == "ExportAttribute")?.ConstructorArguments.FirstOrDefault().Value as string;
@@ -106,21 +148,26 @@ $@" public partial class {ci.Name} : IResource {{
var fn = f.Name;
var pn = string.IsNullOrEmpty(givenName) ? SuggestExportName(fn) : givenName;
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => FormatAttribute(x)));
var attrs = f.GetAttributes().Select(x => FormatAttribute(x));
foreach(var attr in attrs)
code.AppendLine($"\t{attr}");
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
{
code += $"\t{attrs}\r\n\t public event {f.Type} {pn};\r\n";
code.AppendLine($"public event {f.Type} {pn};");
}
else
{
code += $"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n";
code.AppendLine($"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n");
}
}
code += "}}\r\n";
code.AppendLine("}\r\n");
spc.AddSource(ci.Name + ".g.cs", code);
spc.AddSource(ci.Name + ".g.cs", code.ToString());
}
catch (Exception ex)
{