-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathServerProxy.cs
More file actions
68 lines (62 loc) · 2.36 KB
/
ServerProxy.cs
File metadata and controls
68 lines (62 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using static HandyIpc.Generator.TemplateExtensions;
namespace HandyIpc.Generator
{
public static class ServerProxy
{
public static string Generate(INamedTypeSymbol @interface, IReadOnlyCollection<IMethodSymbol> methods, IReadOnlyCollection<IEventSymbol> events)
{
var (@namespace, className, typeParameters) = @interface.GenerateNameFromInterface();
string interfaceType = @interface.ToFullDeclaration();
return $@"
namespace {@namespace}
{{
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Reflection.Obfuscation(Exclude = true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public class {nameof(ServerProxy)}{className}{typeParameters} : {interfaceType}
{@interface.TypeParameters.For(typeParameter => $@"
{typeParameter.ToGenericConstraint()}
")}
{{
private readonly {interfaceType} _instance;
{events.For(item => $@"
public event {item.Type.ToTypeDeclaration()} {item.Name};
")}
public {nameof(ServerProxy)}{className}({interfaceType} instance)
{{
_instance = instance;
{events.For(item => $@"
instance.{item.Name} += (sender, e) => {item.Name}?.Invoke(sender, e);
")}
}}
{methods.For(method =>
{
string methodName = method.ToFullDeclaration();
var parameterNames = method.Parameters.Select(parameter => $"{parameter.Name}_").ToList();
string parameters = method.Parameters
.Select(item => item.Type.ToTypeDeclaration())
.Zip(parameterNames, (type, parameter) => $"{type} {parameter}")
.Join(", ");
bool isAwaitable = method.ReturnType.IsAwaitable();
bool isVoid = method.ReturnsVoid || method.ReturnType.ReturnsVoidTask();
return $@"
/// <inheritdoc />
{Text(method.TypeParameters.Any() ? $@"
[global::HandyIpc.Core.IpcMethod(""{method.GenerateMethodId()}"")]
" : RemoveLineIfEmpty)}
{method.ReturnType.ToTypeDeclaration()} {interfaceType}.{methodName}({parameters})
{{
{(!isVoid || isAwaitable ? "return " : null)}_instance.{methodName}({parameterNames.Join(", ")});
}}
";
})}
}}
}}
".FormatCode();
}
}
}