2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +00:00
This commit is contained in:
Ahmed Zamil 2024-11-07 20:23:48 +03:00
parent 62343d7761
commit 0207b037f3
5 changed files with 167 additions and 96 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>

View File

@ -31,9 +31,11 @@ namespace Esiur.AspNetCore.Example
[Resource] [Resource]
public partial class MyResource public partial class MyResource
{ {
[Export] int number;
[Annotation("sss","bb")][Export] int number;
[Export] [Export]
public string[] GetInfo() => new string[] { Environment.MachineName, Environment.UserName, Environment.CurrentDirectory, public string[] GetInfo() => new string[] { Environment.MachineName, Environment.UserName, Environment.CurrentDirectory,
Environment.CommandLine, Environment.OSVersion.ToString(), Environment.ProcessPath }; Environment.CommandLine, Environment.OSVersion.ToString(), Environment.ProcessPath };

View File

@ -5,7 +5,7 @@
<Copyright>Ahmed Kh. Zamil</Copyright> <Copyright>Ahmed Kh. Zamil</Copyright>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.4.7</Version> <Version>2.4.8</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl> <RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors> <Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion></AssemblyVersion> <AssemblyVersion></AssemblyVersion>

View File

@ -0,0 +1,13 @@
{
"profiles": {
"Esiur": {
"commandName": "Project"
},
"Profile 1": {
"commandName": "Project"
},
"Profile 2": {
"commandName": "Executable"
}
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using System; using System;
@ -44,13 +45,11 @@ public class ResourceGenerator : ISourceGenerator
if (tmp.Type == TemplateType.Resource) if (tmp.Type == TemplateType.Resource)
{ {
var source = TemplateGenerator.GenerateClass(tmp, templates, false); var source = TemplateGenerator.GenerateClass(tmp, templates, false);
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
context.AddSource(tmp.ClassName + ".Generated.cs", source); context.AddSource(tmp.ClassName + ".Generated.cs", source);
} }
else if (tmp.Type == TemplateType.Record) else if (tmp.Type == TemplateType.Record)
{ {
var source = TemplateGenerator.GenerateRecord(tmp, templates); var source = TemplateGenerator.GenerateRecord(tmp, templates);
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
context.AddSource(tmp.ClassName + ".Generated.cs", source); context.AddSource(tmp.ClassName + ".Generated.cs", source);
} }
} }
@ -66,8 +65,6 @@ public class ResourceGenerator : ISourceGenerator
"\r\n } \r\n}"; "\r\n } \r\n}";
//File.WriteAllText($@"C:\gen\Esiur.Generated.cs", gen);
context.AddSource("Esiur.Generated.cs", typesFile); context.AddSource("Esiur.Generated.cs", typesFile);
} }
@ -82,9 +79,63 @@ public class ResourceGenerator : ISourceGenerator
return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1); return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
} }
public string FormatAttribute(AttributeData attribute)
{
if (attribute.AttributeClass is object)
{
string className = attribute.AttributeClass.ToDisplayString();
if (!attribute.ConstructorArguments.Any() & !attribute.ConstructorArguments.Any())
{
return className;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(className);
stringBuilder.Append('(');
bool first = true;
foreach (var constructorArgument in attribute.ConstructorArguments)
{
if (!first)
{
stringBuilder.Append(", ");
}
stringBuilder.Append(constructorArgument.ToCSharpString());
first = false;
}
foreach (var namedArgument in attribute.NamedArguments)
{
if (!first)
{
stringBuilder.Append(", ");
}
stringBuilder.Append(namedArgument.Key);
stringBuilder.Append(" = ");
stringBuilder.Append(namedArgument.Value.ToCSharpString());
first = false;
}
stringBuilder.Append(')');
return stringBuilder.ToString();
}
throw new Exception("Attribute not found");
}
public void Execute(GeneratorExecutionContext context) public void Execute(GeneratorExecutionContext context)
{ {
try
{
if (!(context.SyntaxContextReceiver is ResourceGeneratorReceiver receiver)) if (!(context.SyntaxContextReceiver is ResourceGeneratorReceiver receiver))
return; return;
@ -148,7 +199,7 @@ public class ResourceGenerator : ISourceGenerator
var code = @$"using Esiur.Resource; var code = @$"using Esiur.Resource;
using Esiur.Core; using Esiur.Core;
namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{ namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
"; ";
if (ci.IsInterfaceImplemented(receiver.Classes)) if (ci.IsInterfaceImplemented(receiver.Classes))
@ -156,7 +207,7 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
else else
{ {
code += code +=
@$" public partial class {ci.Name} : IResource {{ @$" public partial class {ci.Name} : IResource {{
public virtual Instance Instance {{ get; set; }} public virtual Instance Instance {{ get; set; }}
public virtual event DestroyedEvent OnDestroy; public virtual event DestroyedEvent OnDestroy;
@ -165,7 +216,7 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
if (!ci.HasTrigger) if (!ci.HasTrigger)
code += code +=
"\tpublic virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n\r\n"; "\tpublic virtual AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n\r\n";
} }
//Debugger.Launch(); //Debugger.Launch();
@ -177,10 +228,11 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
var fn = f.Name; var fn = f.Name;
var pn = string.IsNullOrEmpty(givenName) ? SuggestExportName(fn) : givenName; var pn = string.IsNullOrEmpty(givenName) ? SuggestExportName(fn) : givenName;
//System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n");
// copy attributes // copy attributes
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.ToString()}]")); //Debugger.Launch();
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.AttributeClass.Name}({x.ConstructorArguments.Select(x => x.Values.ToString())})]"));// x.ToString()}]"));
//Debugger.Launch(); //Debugger.Launch();
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler")) if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
{ {
@ -194,14 +246,18 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
code += "}}\r\n"; code += "}}\r\n";
//System.IO.File.WriteAllText("c:\\gen\\" + ci.Name + "_esiur.cs", code); context.AddSource(ci.Name + ".g.cs", code);
context.AddSource(ci.Name + ".Generated.cs", code);
} }
catch //(Exception ex) catch (Exception ex)
{ {
//System.IO.File.AppendAllText("c:\\gen\\error.log", ci.Name + " " + ex.ToString() + "\r\n"); context.AddSource(ci.Name + ".Error.g.cs", $"/*\r\n {ex}\r\n*/");
} }
} }
} }
catch (Exception ex)
{
context.AddSource("Error.g.cs", $"/*\r\n {ex}\r\n*/");
}
}
} }