генерация кода практически завершена

This commit is contained in:
2025-02-04 23:19:24 +03:00
parent cfd68aeac9
commit 2fac34a1ce
12 changed files with 186 additions and 121 deletions
@@ -104,11 +104,72 @@ namespace {Namespace}
// Go through all class members with a particular type (property) to generate method lines.
var methodBody = classSymbol.GetMembers()
.OfType<IMethodSymbol>();
methods.AddRange(methodBody.Select(i => (namespaceName + "." + className, i)));
var originalName = className;
// Build up the source code
className = className.TrimStart('I') + "RemoteEndpoint";
var methodsText = new List<string>();
foreach (var method in methodBody)
{
var index = methods.Count;
methods.Add((namespaceName + "." + className, method));
var sb = new StringBuilder();
//Creating signature
// if (method.Parameters.Length == 0)
// sb.AppendLine($"public {method.ReturnType.ToDisplayString()} {method.Name}(){{");
// else
bool isAsync = method.ReturnType.ToDisplayString() == "Task";
sb.AppendLine("public" + (isAsync
? " async "
: " ") +
$"{method.ReturnType.ToDisplayString()} {method.Name}({string.Join(", ", method.Parameters.Select(p => p.ToDisplayString()))}){{");
//Creating request
if (method.Parameters.Length == 0)
sb.AppendLine(
$"\t\tvar request = new DefaultCallRequest {{ CommandId = {index}, ObjectId = id }};");
else if (method.Parameters.Length == 1 && method.ReturnType.Name != "Task" ||
method.Parameters.Length == 2 && method.ReturnType.Name == "Task")
{
sb.AppendLine(
$"\t\tvar request = new DefaultCallRequest {{ CommandId = {index}, ObjectId = id, Parameter = {method.Parameters.First().Name} }};");
}
//Post created request
sb.AppendLine("\t\tserialisationModule.PostCallRequest(request);");
if (method.ReturnType.ToString() == "System.Threading.Tasks.Task")
{
sb.AppendLine(
"\t\tawait serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId);");
}
else if (method.ReturnType.OriginalDefinition.ToString() == "System.Threading.Tasks.Task<TResult>")
{
var type = method.ReturnType.ToString().Split(['<', '>'], StringSplitOptions.RemoveEmptyEntries)
.Last();
sb.AppendLine(
$"\t\tvar response = await serialisationModule.GetFinalCommandExecution<{type}>(request.CallRequestId);");
sb.AppendLine($"\t\treturn ({type})response.Result;");
}
else if (!isAsync && method.ReturnType.ToDisplayString() != "void")
{
var type = method.ReturnType.ToDisplayString();
sb.AppendLine(
$"\t\tvar response = serialisationModule.GetFinalCommandExecution<{type}>(request.CallRequestId).GetAwaiter().GetResult();");
sb.AppendLine($"\t\treturn ({type})response.Result;");
}
sb.AppendLine("\t}");
methodsText.Add(sb.ToString());
}
var code = $@"// <auto-generated/>
using System;
@@ -120,12 +181,14 @@ namespace {namespaceName};
partial class {className} (int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule) : {originalName}, IRemoteObject
{{
public int Id => id;
{string.Join("\r\n\t", methodsText)}
}}
";
// Add the source code to the compilation.
// context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
}
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Codegen")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0f8460632a9a837735d6bbfcc5c59708a0c44ad")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cfd68aeac9fca2a50d5524c0e1d6cfce6b35653c")]
[assembly: System.Reflection.AssemblyProductAttribute("mROA.Codegen")]
[assembly: System.Reflection.AssemblyTitleAttribute("mROA.Codegen")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]