Skip to content

Commit eef3405

Browse files
committed
add AddDynamicDependency
1 parent bc7f421 commit eef3405

File tree

7 files changed

+93
-6
lines changed

7 files changed

+93
-6
lines changed

App/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public void ConfigureServices(IServiceCollection services)
4949
services
5050
.AddWebApiClient()
5151
.UseJsonFirstApiActionDescriptor()
52-
.UseSourceGeneratorHttpApiActivator();
52+
.UseSourceGeneratorHttpApiActivator()
53+
.AddDynamicDependencyApp();
5354

5455
// 注册userApi
5556
services.AddHttpApi(typeof(IUserApi), o =>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Text;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace WebApiClientCore.Analyzers.SourceGenerator
8+
{
9+
sealed class DynamicDependencyBuilder
10+
{
11+
private readonly Compilation compilation;
12+
private readonly IEnumerable<HttpApiCodeBuilder> codeBuilders;
13+
14+
public string FileName => "WebApiClientBuilderExtensions.g.cs";
15+
public string ClassName => "WebApiClientBuilderExtensions_G";
16+
17+
public DynamicDependencyBuilder(Compilation compilation, IEnumerable<HttpApiCodeBuilder> codeBuilders)
18+
{
19+
this.compilation = compilation;
20+
this.codeBuilders = codeBuilders;
21+
}
22+
23+
/// <summary>
24+
/// 转换为SourceText
25+
/// </summary>
26+
/// <returns></returns>
27+
public SourceText ToSourceText()
28+
{
29+
var code = this.ToString();
30+
return SourceText.From(code, Encoding.UTF8);
31+
}
32+
33+
public override string ToString()
34+
{
35+
var builder = new StringBuilder();
36+
builder.AppendLine("#if NET5_0_OR_GREATER");
37+
builder.AppendLine("using System.Diagnostics.CodeAnalysis;");
38+
builder.AppendLine("namespace Microsoft.Extensions.DependencyInjection");
39+
builder.AppendLine("{");
40+
builder.AppendLine(" /// <summary>IWebApiClientBuilder扩展</summary>");
41+
builder.AppendLine($" public static partial class {this.ClassName}");
42+
builder.AppendLine(" {");
43+
44+
builder.AppendLine($"""
45+
/// <summary>
46+
/// 注册程序集{compilation.AssemblyName}的所有动态依赖
47+
/// 避免程序集在裁剪时裁剪掉由SourceGenerator生成的代理类
48+
/// </summary>
49+
/// <param name="builder"></param>
50+
/// <returns></returns>
51+
""");
52+
53+
var assemblyName = GetAssemblyName(compilation);
54+
foreach (var codeBuilder in this.codeBuilders)
55+
{
56+
builder.AppendLine($" [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({codeBuilder.Namespace}.{codeBuilder.ClassName}))]");
57+
}
58+
59+
builder.AppendLine($" public static IWebApiClientBuilder AddDynamicDependency{assemblyName}(this IWebApiClientBuilder builder)");
60+
builder.AppendLine(" {");
61+
builder.AppendLine(" return builder;");
62+
builder.AppendLine(" }");
63+
64+
builder.AppendLine(" }");
65+
builder.AppendLine("}");
66+
builder.AppendLine("#endif");
67+
return builder.ToString();
68+
}
69+
70+
private static string GetAssemblyName(Compilation compilation)
71+
{
72+
var assemblyName = compilation.AssemblyName ?? string.Empty;
73+
return new string(assemblyName.Where(IsAllowChar).ToArray());
74+
75+
static bool IsAllowChar(char c)
76+
{
77+
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
78+
}
79+
}
80+
}
81+
}

WebApiClientCore.Analyzers.SourceGenerator/HttpApiSourceGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public void Execute(GeneratorExecutionContext context)
3535
{
3636
context.AddSource(builder.HttpApiTypeName, builder.ToSourceText());
3737
}
38+
39+
var dependencyBuilder = new DynamicDependencyBuilder(context.Compilation, builders);
40+
context.AddSource(dependencyBuilder.FileName, dependencyBuilder.ToSourceText());
3841
}
3942
}
4043
}

WebApiClientCore.Analyzers.SourceGenerator/WebApiClientCore.Analyzers.SourceGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Nullable>enable</Nullable>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>latest</LangVersion>
66
<TargetFramework>netstandard2.0</TargetFramework>
77
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
88

WebApiClientCore.Extensions.SourceGenerator/WebApiClientCore.Extensions.SourceGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<Version>2.0.5.1</Version>
45
<Nullable>enable</Nullable>
56
<TargetFramework>netstandard2.1</TargetFramework>
67
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>

WebApiClientCore.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29519.181
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.34909.67
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiClientCore", "WebApiClientCore\WebApiClientCore.csproj", "{6D2952E5-CFD8-40AF-91B1-3C30F043E79B}"
77
EndProject
@@ -23,7 +23,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiClientCore.Extensions
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiClientCore.Abstractions", "WebApiClientCore.Abstractions\WebApiClientCore.Abstractions.csproj", "{BB0EFCA6-6AD2-45A2-B62D-62426F5015D2}"
2525
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiClientCore.Extensions.SourceGenerator", "WebApiClientCore.Extensions.SourceGenerator\WebApiClientCore.Extensions.SourceGenerator.csproj", "{344CF9EB-5B11-406A-A8BC-93E5BAC6A5A0}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiClientCore.Extensions.SourceGenerator", "WebApiClientCore.Extensions.SourceGenerator\WebApiClientCore.Extensions.SourceGenerator.csproj", "{344CF9EB-5B11-406A-A8BC-93E5BAC6A5A0}"
2727
EndProject
2828
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiClientCore.Analyzers.SourceGenerator", "WebApiClientCore.Analyzers.SourceGenerator\WebApiClientCore.Analyzers.SourceGenerator.csproj", "{DFE35D63-0888-4EE2-A9F1-AC45756F5909}"
2929
EndProject

docs/guide/source-generator.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ SourceGenerator是一种新的c#编译时代码补充生成技术,可以非常
88
// 应用编译时生成接口的代理类型代码
99
services
1010
.AddWebApiClient()
11-
.UseSourceGeneratorHttpApiActivator();
11+
.UseSourceGeneratorHttpApiActivator()
12+
.AddDynamicDependency{AssemblyName};
1213
```

0 commit comments

Comments
 (0)