-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from dotnet-campus/t/lindexi/TelescopeExport
源代码生成类型的方法导出功能
- Loading branch information
Showing
41 changed files
with
1,883 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/TelescopeSourceGenerator/Analyzers/Core/AccessibilityHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System; | ||
|
||
namespace dotnetCampus.Telescope.SourceGeneratorAnalyzers.Core; | ||
|
||
static class AccessibilityHelper | ||
{ | ||
public static string ToCSharpCode(this Accessibility accessibility) | ||
=> accessibility switch | ||
{ | ||
Accessibility.Public => "public", | ||
Accessibility.Private => "private", | ||
Accessibility.Internal => "internal", | ||
Accessibility.Protected => "protected", | ||
Accessibility.ProtectedAndInternal => "private protected", | ||
_ => string.Empty | ||
}; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/TelescopeSourceGenerator/Analyzers/Core/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
|
||
namespace dotnetCampus.Telescope.SourceGeneratorAnalyzers.Core; | ||
|
||
internal static class AssemblyInfo | ||
{ | ||
public static string ToolName => | ||
Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyProductAttribute>().Product; | ||
|
||
public static string ToolVersion => | ||
Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>() | ||
.InformationalVersion; | ||
|
||
/// <summary> | ||
/// 如果需要为类加上 <see cref="System.CodeDom.Compiler.GeneratedCodeAttribute"/>,则使用此字符串。 | ||
/// </summary> | ||
public static string GeneratedCodeAttribute => | ||
$@"[global::System.CodeDom.Compiler.GeneratedCode(""{ToolName}"", ""{ToolVersion}"")]"; | ||
|
||
/// <summary> | ||
/// 获取可以为每一个生成的文件都增加的文件头。 | ||
/// </summary> | ||
public const string GeneratedCodeComment = | ||
$@"//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// 此代码由工具生成。 | ||
// | ||
// 对此文件的更改可能会导致不正确的行为,并且如果 | ||
// 重新生成代码,这些更改将会丢失。 | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
#define GENERATED_CODE | ||
"; | ||
} |
92 changes: 92 additions & 0 deletions
92
src/TelescopeSourceGenerator/Analyzers/Core/AssemblySymbolHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace dotnetCampus.Telescope.SourceGeneratorAnalyzers.Core; | ||
|
||
static class AssemblySymbolHelper | ||
{ | ||
/// <summary> | ||
/// 获取当前程序集里面的所有类型 | ||
/// </summary> | ||
/// <param name="assemblySymbol"></param> | ||
/// <returns></returns> | ||
public static IEnumerable<INamedTypeSymbol> GetAllTypeSymbols(IAssemblySymbol assemblySymbol) => GetAllTypeSymbols(assemblySymbol.GlobalNamespace); | ||
|
||
public static IEnumerable<INamedTypeSymbol> GetAllTypeSymbols(INamespaceSymbol namespaceSymbol) | ||
{ | ||
var typeMemberList = namespaceSymbol.GetTypeMembers(); | ||
|
||
foreach (var typeSymbol in typeMemberList) | ||
{ | ||
yield return typeSymbol; | ||
} | ||
|
||
foreach (var namespaceMember in namespaceSymbol.GetNamespaceMembers()) | ||
{ | ||
foreach (var typeSymbol in GetAllTypeSymbols(namespaceMember)) | ||
{ | ||
yield return typeSymbol; | ||
} | ||
} | ||
} | ||
|
||
public static bool IsReference(IAssemblySymbol currentAssemblySymbol, IAssemblySymbol requiredAssemblySymbol) | ||
{ | ||
var visited = new Dictionary<IAssemblySymbol, bool /*是否引用*/>(SymbolEqualityComparer.Default); | ||
return IsReference(currentAssemblySymbol, requiredAssemblySymbol, visited); | ||
} | ||
|
||
public static bool IsReference(IAssemblySymbol currentAssemblySymbol, IAssemblySymbol requiredAssemblySymbol, | ||
Dictionary<IAssemblySymbol, bool /*是否引用*/> visited) | ||
{ | ||
if (SymbolEqualityComparer.Default.Equals(currentAssemblySymbol, requiredAssemblySymbol)) | ||
{ | ||
// 这个就看业务了,如果两个程序集是相同的,是否判断为引用关系 | ||
return true; | ||
} | ||
|
||
foreach (var moduleSymbol in currentAssemblySymbol.Modules) | ||
{ | ||
foreach (var referencedAssemblySymbol in moduleSymbol.ReferencedAssemblySymbols) | ||
{ | ||
if (SymbolEqualityComparer.Default.Equals(referencedAssemblySymbol, requiredAssemblySymbol)) | ||
{ | ||
// 记录当前程序集存在引用关系 | ||
visited[currentAssemblySymbol] = true; | ||
return true; | ||
} | ||
else if (SymbolEqualityComparer.Default.Equals(referencedAssemblySymbol, currentAssemblySymbol)) | ||
{ | ||
// 循环引用,跳过 | ||
continue; | ||
} | ||
else | ||
{ | ||
if (visited.TryGetValue(referencedAssemblySymbol, out var isReference)) | ||
{ | ||
// 这个是访问过的,那就从字典获取缓存,不需要再访问一次 | ||
// 同时也能解决程序集循环引用问题 | ||
} | ||
else | ||
{ | ||
// 进入递归之前,先将自身设置到字典,先设置为没有引用。防止循环引用炸掉 | ||
visited[referencedAssemblySymbol] = false; | ||
|
||
// 没有访问过的,获取引用的程序集是否存在引用关系 | ||
isReference = IsReference(referencedAssemblySymbol, requiredAssemblySymbol, visited); | ||
visited[referencedAssemblySymbol] = isReference; | ||
} | ||
|
||
if (isReference) | ||
{ | ||
// 如果这个程序集有引用,那也算上 | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/TelescopeSourceGenerator/Analyzers/Core/IncrementalValuesProviderHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace dotnetCampus.Telescope.SourceGeneratorAnalyzers.Core; | ||
|
||
static class IncrementalValuesProviderHelper | ||
{ | ||
/// <summary> | ||
/// 过滤掉空的值 | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="provider"></param> | ||
/// <returns></returns> | ||
public static IncrementalValuesProvider<T> ExcludeNulls<T>(this IncrementalValuesProvider<T?> provider) | ||
{ | ||
return provider.Where(static t => t != null).Select(static (t, _) => t!); | ||
} | ||
} |
Oops, something went wrong.