Skip to content

Commit

Permalink
Merge pull request #117 from dotnet-campus/t/walterlv/member-id
Browse files Browse the repository at this point in the history
为 IPC 代理和对接增加 Id
  • Loading branch information
lindexi authored Feb 13, 2023
2 parents 064e9e3 + 1ed2ee8 commit 72beeda
Show file tree
Hide file tree
Showing 27 changed files with 470 additions and 313 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
5.0.x
6.0.100
- name: Build
run: dotnet build --configuration Release

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nuget-tag-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.0.100
6.0.x
- name: Install dotnet tool
run: dotnet tool install -g dotnetCampus.TagToVersion
Expand Down
19 changes: 8 additions & 11 deletions dotnetCampus.Ipc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Ipc.PipeMvcCli
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "dotnetCampus.Ipc.PipeMvcShare", "src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.shproj", "{05ACE3BB-61E5-4592-AC73-747850DB1081}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{11767A0B-CBFB-4323-BCEE-DE1CDD6C4B4B}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{11767A0B-CBFB-4323-BCEE-DE1CDD6C4B4B}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
.github\workflows\dotnet-core.yml = .github\workflows\dotnet-core.yml
.github\workflows\dotnet-format.yml = .github\workflows\dotnet-format.yml
.github\workflows\nuget-tag-publish.yml = .github\workflows\nuget-tag-publish.yml
build\Version.props = build\Version.props
EndProjectSection
EndProject
Expand All @@ -49,16 +46,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PipeMvcServerDemo", "demo\P
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IpcRemotingObjectDemo", "IpcRemotingObjectDemo", "{927CE552-9207-47AD-9595-650C5A0624A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpcRemotingObjectServerDemo", "demo\IpcRemotingObjectDemo\IpcRemotingObjectServerDemo\IpcRemotingObjectServerDemo.csproj", "{CB343EAE-9DA2-49F3-B39B-E9CDDCEB2292}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IpcRemotingObjectServerDemo", "demo\IpcRemotingObjectDemo\IpcRemotingObjectServerDemo\IpcRemotingObjectServerDemo.csproj", "{CB343EAE-9DA2-49F3-B39B-E9CDDCEB2292}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpcRemotingObjectClientDemo", "demo\IpcRemotingObjectDemo\IpcRemotingObjectClientDemo\IpcRemotingObjectClientDemo.csproj", "{6639929E-3AC1-47D9-BE74-97FDF3C29B6B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IpcRemotingObjectClientDemo", "demo\IpcRemotingObjectDemo\IpcRemotingObjectClientDemo\IpcRemotingObjectClientDemo.csproj", "{6639929E-3AC1-47D9-BE74-97FDF3C29B6B}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{05ace3bb-61e5-4592-ac73-747850db1081}*SharedItemsImports = 13
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{0f6b9c0c-7a64-4b21-bc28-e52565245a1a}*SharedItemsImports = 5
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{1ace3261-cc3d-4442-8c83-516721b3da46}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -234,4 +226,9 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8828641-2F8C-4B6A-BF1D-F8F3C8C8454D}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{05ace3bb-61e5-4592-ac73-747850db1081}*SharedItemsImports = 13
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{0f6b9c0c-7a64-4b21-bc28-e52565245a1a}*SharedItemsImports = 5
src\PipeMvc\dotnetCampus.Ipc.PipeMvcShare\dotnetCampus.Ipc.PipeMvcShare.projitems*{1ace3261-cc3d-4442-8c83-516721b3da46}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace dotnetCampus.Ipc.SourceGenerators.Compiling;

/// <summary>
/// 提供 IPC 对象(契约接口)的语法和语义分析。
/// </summary>
[DebuggerDisplay("IpcPublic : {IpcType.Name,nq}")]
internal class IpcPublicCompilation
internal class IpcPublicCompilation : IEquatable<IpcPublicCompilation?>
{
/// <summary>
/// IPC 对象文件的编译信息。
Expand Down Expand Up @@ -42,7 +41,7 @@ public IpcPublicCompilation(SyntaxTree syntaxTree, SemanticModel semanticModel,
public string GetUsing()
{
var usingsSyntax = _compilationUnitSyntax.Usings;
var usings = string.Join(Environment.NewLine, usingsSyntax.Select(x => x.ToString()));
var usings = string.Join("\r\n", usingsSyntax.Select(x => x.ToString()));
return usings;
}

Expand Down Expand Up @@ -91,6 +90,31 @@ public string GetNamespace()
}
}

/// <summary>
/// 试图解析一个接口定义语法节点并创建 IPC 对象(契约接口)。
/// </summary>
/// <param name="syntaxNode">接口定义语法节点。</param>
/// <param name="semanticModel">此接口定义语法节点的语义模型。</param>
/// <param name="ipcPublicCompilation">如果找到了 IPC 对象,则此参数为此语法树中的所有 IPC 对象;如果没有找到,则为空集合。</param>
/// <returns>如果找到了 IPC 类型,则返回 true;如果没有找到,则返回 false。</returns>
public static bool TryCreateIpcPublicCompilation(InterfaceDeclarationSyntax syntaxNode, SemanticModel semanticModel,
[NotNullWhen(true)] out IpcPublicCompilation? ipcPublicCompilation)
{
var syntaxTree = syntaxNode.SyntaxTree;
if (semanticModel.GetDeclaredSymbol(syntaxNode) is { } typeSymbol
&& typeSymbol.GetAttributes().FirstOrDefault(x => string.Equals(
x.AttributeClass?.ToString(),
typeof(IpcPublicAttribute).FullName,
StringComparison.Ordinal)) is { } ipcPublicAttribute)
{
ipcPublicCompilation = new IpcPublicCompilation(syntaxTree, semanticModel, typeSymbol);
return true;
}

ipcPublicCompilation = null;
return false;
}

/// <summary>
/// 在一个语法树(单个文件)中查找所有的 IPC 对象(契约接口)。
/// </summary>
Expand All @@ -109,17 +133,29 @@ where node.IsKind(SyntaxKind.InterfaceDeclaration)
var semanticModel = compilation.GetSemanticModel(syntaxTree);
foreach (var typeDeclarationSyntax in typeDeclarationSyntaxes)
{
if (semanticModel.GetDeclaredSymbol(typeDeclarationSyntax) is { } typeSymbol
&& typeSymbol.GetAttributes().FirstOrDefault(x => string.Equals(
x.AttributeClass?.ToString(),
typeof(IpcPublicAttribute).FullName,
StringComparison.Ordinal)) is { } ipcPublicAttribute)
if (TryCreateIpcPublicCompilation(typeDeclarationSyntax, semanticModel, out var publicIpcObjectCompilation))
{
result.Add(new IpcPublicCompilation(syntaxTree, semanticModel, typeSymbol));
result.Add(publicIpcObjectCompilation);
}
}

publicIpcObjectCompilations = result;
return publicIpcObjectCompilations.Count > 0;
}

public override bool Equals(object? obj)
{
return Equals(obj as IpcPublicCompilation);
}

public bool Equals(IpcPublicCompilation? other)
{
return other is not null &&
SymbolEqualityComparer.Default.Equals(IpcType, other.IpcType); ;
}

public override int GetHashCode()
{
return 1998130605 + EqualityComparer<INamedTypeSymbol>.Default.GetHashCode(IpcType);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace dotnetCampus.Ipc.SourceGenerators.Compiling;

/// <summary>
/// 提供 IPC 对象(代理壳类)的语法和语义分析。
/// </summary>
[DebuggerDisplay("{ShapeType} : {ContractType.Name,nq}")]
internal class IpcShapeCompilation : IpcPublicCompilation
internal class IpcShapeCompilation : IpcPublicCompilation, IEquatable<IpcShapeCompilation?>
{
/// <summary>
/// 创建 IPC 对象的语法和语义分析。
Expand Down Expand Up @@ -74,14 +73,61 @@ public IpcShapeCompilation(SyntaxTree syntaxTree, SemanticModel semanticModel,
}
}

/// <summary>
/// 试图解析一个类型定义语法节点并创建 IPC 对象(契约接口)。
/// </summary>
/// <param name="syntaxNode">类型定义语法节点。</param>
/// <param name="semanticModel">此类型定义语法节点的语义模型。</param>
/// <param name="ipcShapeCompilation">如果找到了 IPC 对象,则此参数为此语法树中的所有 IPC 对象;如果没有找到,则为空集合。</param>
/// <returns>如果找到了 IPC 类型,则返回 true;如果没有找到,则返回 false。</returns>
public static bool TryCreateIpcShapeCompilation(ClassDeclarationSyntax syntaxNode, SemanticModel semanticModel,
[NotNullWhen(true)] out IpcShapeCompilation? ipcShapeCompilation)
{
var syntaxTree = syntaxNode.SyntaxTree;
if (semanticModel.GetDeclaredSymbol(syntaxNode) is { } typeSymbol
&& typeSymbol.GetAttributes().FirstOrDefault(x => string.Equals(
x.AttributeClass?.ToString(),
typeof(IpcShapeAttribute).FullName,
StringComparison.Ordinal)) is { } ipcPublicAttribute)
{
if (ipcPublicAttribute.ConstructorArguments.Length == 1)
{
if (ipcPublicAttribute.ConstructorArguments[0] is TypedConstant typedConstant
&& typedConstant.Value is INamedTypeSymbol contractType)
{
if (contractType.TypeKind == TypeKind.Interface)
{
ipcShapeCompilation = new IpcShapeCompilation(syntaxTree, semanticModel, typeSymbol, contractType);
return true;
}
else
{
throw new DiagnosticException(IPC002_KnownDiagnosticError);
}
}
else
{
throw new DiagnosticException(IPC001_KnownCompilerError);
}
}
else
{
throw new DiagnosticException(IPC001_KnownCompilerError);
}
}

ipcShapeCompilation = null;
return false;
}

/// <summary>
/// 在一个语法树(单个文件)中查找所有的 IPC 对象(契约接口)。
/// </summary>
/// <param name="compilation">整个项目的编译信息。</param>
/// <param name="syntaxTree">单个文件的语法树。</param>
/// <param name="publicIpcObjectCompilations">如果找到了 IPC 对象,则此参数为此语法树中的所有 IPC 对象;如果没有找到,则为空集合。</param>
/// <returns>如果找到了 IPC 类型,则返回 true;如果没有找到,则返回 false。</returns>
public static bool TryFindIpcShapeCpmpilations(Compilation compilation, SyntaxTree syntaxTree,
public static bool TryFindIpcShapeCompilations(Compilation compilation, SyntaxTree syntaxTree,
out IReadOnlyList<IpcShapeCompilation> publicIpcObjectCompilations)
{
var result = new List<IpcShapeCompilation>();
Expand All @@ -92,39 +138,34 @@ where node.IsKind(SyntaxKind.ClassDeclaration)
var semanticModel = compilation.GetSemanticModel(syntaxTree);
foreach (var typeDeclarationSyntax in typeDeclarationSyntaxes)
{
if (semanticModel.GetDeclaredSymbol(typeDeclarationSyntax) is { } typeSymbol
&& typeSymbol.GetAttributes().FirstOrDefault(x => string.Equals(
x.AttributeClass?.ToString(),
typeof(IpcShapeAttribute).FullName,
StringComparison.Ordinal)) is { } ipcPublicAttribute)
if (TryCreateIpcShapeCompilation(typeDeclarationSyntax, semanticModel, out var publicIpcObjectCompilation))
{
if (ipcPublicAttribute.ConstructorArguments.Length == 1)
{
if (ipcPublicAttribute.ConstructorArguments[0] is TypedConstant typedConstant
&& typedConstant.Value is INamedTypeSymbol contractType)
{
if (contractType.TypeKind == TypeKind.Interface)
{
result.Add(new IpcShapeCompilation(syntaxTree, semanticModel, typeSymbol, contractType));
}
else
{
throw new DiagnosticException(IPC002_KnownDiagnosticError);
}
}
else
{
throw new DiagnosticException(IPC001_KnownCompilerError);
}
}
else
{
throw new DiagnosticException(IPC001_KnownCompilerError);
}
result.Add(publicIpcObjectCompilation);
}
}

publicIpcObjectCompilations = result;
return publicIpcObjectCompilations.Count > 0;
}

public override bool Equals(object? obj)
{
return Equals(obj as IpcShapeCompilation);
}

public bool Equals(IpcShapeCompilation? other)
{
return other is not null &&
SymbolEqualityComparer.Default.Equals(IpcType, other.IpcType) &&
SymbolEqualityComparer.Default.Equals(ContractType, other.ContractType);
}

public override int GetHashCode()
{
var hashCode = -1723556882;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<INamedTypeSymbol>.Default.GetHashCode(IpcType);
hashCode = hashCode * -1521134295 + EqualityComparer<INamedTypeSymbol>.Default.GetHashCode(ContractType);
return hashCode;
}
}
Loading

0 comments on commit 72beeda

Please sign in to comment.