Skip to content

Commit 27ebfd8

Browse files
authored
Merge pull request #2 from dotnet-campus/t/walterlv/source-generator
使用源生成器优化库,并消灭所有库里的垃圾
2 parents 1a779dc + 2f0c97a commit 27ebfd8

31 files changed

Lines changed: 314 additions & 700 deletions

.github/workflows/dotnet-build.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v1
1212

13+
- name: Setup .NET
14+
uses: actions/setup-dotnet@v1
15+
with:
16+
dotnet-version: |
17+
3.1.x
18+
5.0.x
19+
6.0.x
20+
7.0.x
21+
1322
- name: Build with dotnet
1423
run: dotnet build --configuration Release
1524

.github/workflows/nuget-tag-publish.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ jobs:
1212

1313
steps:
1414
- uses: actions/checkout@v1
15-
15+
16+
- name: Setup .NET
17+
uses: actions/setup-dotnet@v1
18+
with:
19+
dotnet-version: |
20+
3.1.x
21+
5.0.x
22+
6.0.x
23+
7.0.x
24+
1625
- name: Install dotnet tool
1726
run: dotnet tool install -g dotnetCampus.TagToVersion
1827

Directory.Build.props

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<LangVersion>latest</LangVersion>
88
<GenerateDocumentationFile>True</GenerateDocumentationFile>
99

10-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
10+
<!--
11+
NETSDK1138: 目标框架 netxx 不受支持,将来不会收到安全更新。
12+
NU5100: 程序集不在 lib 文件夹内
13+
-->
14+
<NoWarn>$(NoWarn);NETSDK1138;NU5100</NoWarn>
15+
1116
<PackageOutputPath>$(MSBuildThisFileDirectory)bin\$(Configuration)</PackageOutputPath>
1217
<Company>dotnet campus(.NET 职业技术学院)</Company>
1318
<Authors>dotnet-campus</Authors>

build/Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.0.1-alpha01</Version>
3+
<Version>11.0.0</Version>
44
</PropertyGroup>
55
</Project>

dotnetCampus.LatestCSharpFeatures.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InternalUsageLibrary", "sam
3333
EndProject
3434
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures.Source", "src\dotnetCampus.LatestCSharpFeatures.Source\dotnetCampus.LatestCSharpFeatures.Source.csproj", "{16D75C29-3176-4CFD-A1B5-B5F24876B038}"
3535
EndProject
36+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IsExternalInit", "IsExternalInit", "{4E06689B-E73A-4A04-AAA0-7959A6EC06E6}"
37+
EndProject
38+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Nullable", "Nullable", "{CCAAE7A8-1879-4F67-98AA-D837C80227A2}"
39+
EndProject
40+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Required", "Required", "{F0CBB519-BB38-4104-B840-4076BB206E36}"
41+
EndProject
42+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CAB44C21-D58C-4732-B9D8-60B1DAE82BEF}"
43+
EndProject
44+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{878C1D77-0A77-44BD-847E-164188B10677}"
45+
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnetCampus.LatestCSharpFeatures.Tests", "tests\dotnetCampus.LatestCSharpFeatures.Tests\dotnetCampus.LatestCSharpFeatures.Tests.csproj", "{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}"
47+
EndProject
48+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures.Analyzer", "src\dotnetCampus.LatestCSharpFeatures.Analyzer\dotnetCampus.LatestCSharpFeatures.Analyzer.csproj", "{642FA0CA-E827-4D96-9358-D596CD87DAF4}"
49+
EndProject
3650
Global
3751
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3852
Debug|Any CPU = Debug|Any CPU
@@ -75,12 +89,30 @@ Global
7589
{16D75C29-3176-4CFD-A1B5-B5F24876B038}.Debug|Any CPU.Build.0 = Debug|Any CPU
7690
{16D75C29-3176-4CFD-A1B5-B5F24876B038}.Release|Any CPU.ActiveCfg = Release|Any CPU
7791
{16D75C29-3176-4CFD-A1B5-B5F24876B038}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Release|Any CPU.Build.0 = Release|Any CPU
96+
{642FA0CA-E827-4D96-9358-D596CD87DAF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
97+
{642FA0CA-E827-4D96-9358-D596CD87DAF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
98+
{642FA0CA-E827-4D96-9358-D596CD87DAF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
99+
{642FA0CA-E827-4D96-9358-D596CD87DAF4}.Release|Any CPU.Build.0 = Release|Any CPU
78100
EndGlobalSection
79101
GlobalSection(SolutionProperties) = preSolution
80102
HideSolutionNode = FALSE
81103
EndGlobalSection
82104
GlobalSection(NestedProjects) = preSolution
105+
{2ECF1CEB-070E-4933-93C4-F7FD42D8693C} = {CCAAE7A8-1879-4F67-98AA-D837C80227A2}
106+
{0DDC38EF-851E-49E7-B72A-DF46E44E3586} = {4E06689B-E73A-4A04-AAA0-7959A6EC06E6}
107+
{94E8B753-81BF-44DC-A8D6-AC1D1FE54244} = {4E06689B-E73A-4A04-AAA0-7959A6EC06E6}
108+
{6ED76C7A-A16A-45A5-BC1F-91A880839C31} = {F0CBB519-BB38-4104-B840-4076BB206E36}
109+
{4EBF62CB-613E-4496-B890-B1F433ED7581} = {F0CBB519-BB38-4104-B840-4076BB206E36}
110+
{72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB} = {CCAAE7A8-1879-4F67-98AA-D837C80227A2}
83111
{DF937968-A710-4079-86D2-B2483FF3D81F} = {19AD69CA-F593-4789-A51F-9E547033147D}
112+
{4E06689B-E73A-4A04-AAA0-7959A6EC06E6} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF}
113+
{CCAAE7A8-1879-4F67-98AA-D837C80227A2} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF}
114+
{F0CBB519-BB38-4104-B840-4076BB206E36} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF}
115+
{D10BF09A-FDF3-43F0-BD54-49A73554CDAB} = {878C1D77-0A77-44BD-847E-164188B10677}
84116
EndGlobalSection
85117
GlobalSection(ExtensibilityGlobals) = postSolution
86118
SolutionGuid = {C90536BB-EF2C-4F5A-ABA9-27A8F412D3C3}

src/dotnetCampus.IsExternalInit.Source/Properties/AssmeblyInfo.cs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/dotnetCampus.IsExternalInit.Source/dotnetCampus.IsExternalInit.Source.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<TargetFrameworks>net5.0;netcoreapp3.0;netstandard1.0;net40</TargetFrameworks>
55
<RootNamespace>System.Diagnostics.CodeAnalysis</RootNamespace>
6-
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
76
<PackageId>dotnetCampus.IsExternalInit</PackageId>
87
<Description>If you use C# 9.0 or later and want to use record types, this '.Source' library adds internal IsExternalInit attribute support to your project. This confines the new language feature to the project itself without affecting other projects that reference it.</Description>
98
</PropertyGroup>

src/dotnetCampus.IsExternalInit/IsExternalInit.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/dotnetCampus.IsExternalInit/dotnetCampus.IsExternalInit.csproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net5.0;netcoreapp3.0;netstandard1.0;net40</TargetFrameworks>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
56
<RootNamespace>System.Diagnostics.CodeAnalysis</RootNamespace>
67
<Description>If you use C# 9.0 or later and want to use record types, this library adds public IsExternalInit attribute support for your project, allowing the new language feature to be propagated to other projects that reference this one.</Description>
7-
<BuildOutputTargetFolder>Miscellaneous</BuildOutputTargetFolder>
8+
<DefineConstants>$(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES</DefineConstants>
89
</PropertyGroup>
910

11+
<PropertyGroup Condition="'$(TargetFramework)'=='net5.0'">
12+
<IncludeBuildOutput>false</IncludeBuildOutput>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<Compile Include="..\dotnetCampus.IsExternalInit.Source\*.cs" Link="%(RecursiveDir)\%(FileName)%(Extension)" />
17+
</ItemGroup>
18+
1019
<Target Name="IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
1120
<ItemGroup>
1221
<None Include="_._" Pack="True" PackagePath="lib\net5.0" />
13-
<None Include="$(OutputPath)\netcoreapp3.0\$(PackageId).dll" Pack="True" PackagePath="lib\netcoreapp3.0" />
14-
<None Include="$(OutputPath)\netcoreapp3.0\$(PackageId).pdb" Pack="True" PackagePath="lib\netcoreapp3.0" />
15-
<None Include="$(OutputPath)\netcoreapp3.0\$(PackageId).xml" Pack="True" PackagePath="lib\netcoreapp3.0" />
16-
<None Include="$(OutputPath)\netstandard1.0\$(PackageId).dll" Pack="True" PackagePath="lib\netstandard1.0" />
17-
<None Include="$(OutputPath)\netstandard1.0\$(PackageId).pdb" Pack="True" PackagePath="lib\netstandard1.0" />
18-
<None Include="$(OutputPath)\netstandard1.0\$(PackageId).xml" Pack="True" PackagePath="lib\netstandard1.0" />
19-
<None Include="$(OutputPath)\net40\$(PackageId).dll" Pack="True" PackagePath="lib\net40" />
20-
<None Include="$(OutputPath)\net40\$(PackageId).pdb" Pack="True" PackagePath="lib\net40" />
21-
<None Include="$(OutputPath)\net40\$(PackageId).xml" Pack="True" PackagePath="lib\net40" />
2222
</ItemGroup>
2323
</Target>
2424

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using dotnetCampus.LatestCSharpFeatures.Analyzer;
2+
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
using Microsoft.CodeAnalysis.Text;
6+
7+
using System.Text;
8+
9+
/// <summary>
10+
/// 生成 C# 新特性所需的类。
11+
/// </summary>
12+
[Generator(LanguageNames.CSharp)]
13+
public class FeatureGenerator : IIncrementalGenerator
14+
{
15+
public void Initialize(IncrementalGeneratorInitializationContext context)
16+
{
17+
context.RegisterSourceOutput(context.AnalyzerConfigOptionsProvider, OnExecute);
18+
}
19+
20+
private void OnExecute(SourceProductionContext context, AnalyzerConfigOptionsProvider provider)
21+
{
22+
GenerateFeatureSource(context, provider, "IsExternalInit");
23+
GenerateFeatureSource(context, provider, "Nullable");
24+
GenerateFeatureSource(context, provider, "Required");
25+
}
26+
27+
private void GenerateFeatureSource(SourceProductionContext context, AnalyzerConfigOptionsProvider provider, string featureName)
28+
{
29+
//if (provider.GlobalOptions.TryGetValue($"build_property._dotnetCampusUse{featureName}", out var useFeature)
30+
// && useFeature.Equals(featureName, StringComparison.OrdinalIgnoreCase))
31+
{
32+
foreach (var source in EmbededSourceFiles.Enumerate($"Features.{featureName}"))
33+
{
34+
var name = source.GuessFileNameWithoutExtension().ToString();
35+
context.AddSource($"{name}.g.cs", SourceText.From(source.Content, Encoding.UTF8));
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)