8000 Address feedback and make some other improvements. · dotnet/sdk@d8a3226 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8a3226

Browse files
committed
Address feedback and make some other improvements.
1 parent 2fe4c42 commit d8a3226

File tree

7 files changed

+137
-21
lines changed

7 files changed

+137
-21
lines changed

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/AssemblyLoaderFactory.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88

99
namespace Microsoft.DotNet.GenAPI;
1010

11+
/// <summary>
12+
/// Class that facilitates the creation of an assembly symbol loader and its corresponding assembly symbols.
13+
/// </summary>
1114
public class AssemblyLoaderFactory
1215
{
16+
/// <summary>
17+
/// Creates an assembly symbol loader and its corresponding assembly symbols from the given DLL files in the filesystem.
18+
/// </summary>
19+
/// <param name="assembliesPaths">A collection of paths where the assembly DLLs should be searched.</param>
20+
/// <param name="assemblyReferencesPaths">An optional collection of paths where the assembly references should be searched.</param>
21+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
22+
/// <returns>A tuple containing the assembly symbol loader and a dictionary of assembly symbols.</returns>
1323
public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) CreateFromFiles(string[] assembliesPaths, string[]? assemblyReferencesPaths, bool respectInternals = false)
1424
{
1525
AssemblySymbolLoader loader;
@@ -31,6 +41,11 @@ public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) Creat
3141
return (loader, assemblySymbols);
3242
}
3343

44+
/// <summary>
45+
/// Creates a default assembly symbol loader and a dictionary of assembly symbols.
46+
/// </summary>
47+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
48+
/// <returns>A tuple containing the assembly symbol loader and a dictionary of assembly symbols.</returns>
3449
public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) CreateWithNoAssemblies(bool respectInternals = false) =>
3550
(new AssemblySymbolLoader(resolveAssemblyReferences: true, includeInternalSymbols: respectInternals), new Dictionary<string, IAssemblySymbol>());
3651
}

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/CSharpAssemblyVisitor.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
namespace Microsoft.DotNet.GenAPI;
2121

22+
/// <summary>
23+
/// A class that visits a collection of specified assemblies and generates the corresponding C# document and syntax trees.
24+
/// </summary>
2225
public class CSharpAssemblyVisitor : IAssemblyVisitor
2326
{
2427
private readonly ILog _logger;
@@ -30,14 +33,30 @@ public class CSharpAssemblyVisitor : IAssemblyVisitor
3033
private readonly AdhocWorkspace _adhocWorkspace;
3134
private readonly SyntaxGenerator _syntaxGenerator;
3235
private readonly IEnumerable<MetadataReference>? _metadataReferences;
33-
36+
private readonly bool _addPartialModifier;
37+
private readonly bool _hideImplicitDefaultConstructors;
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="CSharpAssemblyVisitor"/> class.
41+
/// </summary>
42+
/// <param name="logger">The logger to use.</param>
43+
/// <param name="loader">The assembly symbol loader to use.</param>
44+
/// <param name="symbolFilter">The symbol filter to use.</param>
45+
/// <param name="attributeDataSymbolFilter">The attribute data symbol filter to use.</param>
46+
/// <param name="exceptionMessage">The optional exception message to use.</param>
47+
/// <param name="includeAssemblyAttributes">Whether to include assembly attributes or not.</param>
48+
/// <param name="metadataReferences">The metadata references to use. The default value is <see langword="null"/>.</param>
49+
/// <param name="addPartialModifier">Whether to add the partial modifier or not. The default value is <see langword="true"/>.</param>
50+
/// <param name="hideImplicitDefaultConstructors">Whether to hide implicit default constructors or not. The default value is <see langword="true"/>.</param>
3451
public CSharpAssemblyVisitor(ILog logger,
3552
IAssemblySymbolLoader loader,
3653
ISymbolFilter symbolFilter,
3754
ISymbolFilter attributeDataSymbolFilter,
3855
string? exceptionMessage,
3956
bool includeAssemblyAttributes,
40-
IEnumerable<MetadataReference>? metadataReferences = null)
57+
IEnumerable<MetadataReference>? metadataReferences = null,
58+
bool addPartialModifier = true,
59+
bool hideImplicitDefaultConstructors = true)
4160
{
4261
_logger = logger;
4362
_loader = loader;
@@ -48,6 +67,8 @@ public CSharpAssemblyVisitor(ILog logger,
4867
_adhocWorkspace = new AdhocWorkspace();
4968
_syntaxGenerator = SyntaxGenerator.GetGenerator(_adhocWorkspace, LanguageNames.CSharp);
5069
_metadataReferences = metadataReferences;
70+
_addPartialModifier = addPartialModifier;
71+
_hideImplicitDefaultConstructors = hideImplicitDefaultConstructors;
5172
}
5273

5374
/// <inheritdoc />
@@ -75,7 +96,7 @@ public Document GetDocumentForAssembly(IAssemblySymbol assemblySymbol)
7596

7697
SyntaxNode compilationUnit = _syntaxGenerator.CompilationUnit(namespaceSyntaxNodes)
7798
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
78-
.Rewrite(new TypeDeclarationCSharpSyntaxRewriter())
99+
.Rewrite(new TypeDeclarationCSharpSyntaxRewriter(_addPartialModifier))
79100
.Rewrite(new BodyBlockCSharpSyntaxRewriter(_exceptionMessage));
80101

81102
if (_includeAssemblyAttributes)
@@ -192,7 +213,7 @@ private SyntaxNode Visit(SyntaxNode namedTypeNode, INamedTypeSymbol namedType)
192213
}
193214

194215
// Filter out default constructors since these will be added automatically
195-
if (method.IsImplicitDefaultConstructor(_symbolFilter))
216+
if (_hideImplicitDefaultConstructors && method.IsImplicitDefaultConstructor(_symbolFilter))
196217
{
197218
continue;
198219
}

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/GenAPIApp.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,8 @@ public static void Run(ILog logger,
4646
writer.WriteAssembly(kvp.Value);
4747
}
4848

49-
if (loader.HasRoslynDiagnostics(out IReadOnlyList<Diagnostic> roslynDiagnostics))
50-
{
51-
foreach (Diagnostic warning in roslynDiagnostics)
52-
{
53-
logger.LogWarning(warning.Id, warning.ToString());
54-
}
55-
}
56-
57-
if (loader.HasLoadWarnings(out IReadOnlyList<AssemblyLoadWarning> loadWarnings))
58-
{
59-
foreach (AssemblyLoadWarning warning in loadWarnings)
60-
{
61-
logger.LogWarning(warning.DiagnosticId, warning.Message);
62-
}
63-
}
49+
loader.LogAllDiagnostics(logger);
50+
loader.LogAllWarnings(logger);
6451
}
6552

6653
// Creates a TextWriter capable of writing into Console or a cs file.

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/SymbolFilterFactory.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77

88
namespace Microsoft.DotNet.GenAPI;
99

10+
/// <summary>
11+
/// Factory class that creates composite filters based on the given exclusion lists.
12+
/// </summary>
1013
public static class SymbolFilterFactory
1114
{
15+
/// <summary>
16+
/// Creates a symbol filter based on the given exclusion file paths.
17+
/// </summary>
18+
/// <param name="apiExclusionFilePaths">A collection of paths where the exclusion files should be searched.</param>
19+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
20+
/// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param>
21+
/// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param>
22+
/// <returns>An instance of the symbol filter.</returns>
1223
public static ISymbolFilter GetSymbolFilterFromFiles(string[]? apiExclusionFilePaths,
1324
bool respectInternals = false,
1425
bool includeEffectivelyPrivateSymbols = true,
@@ -21,6 +32,14 @@ public static ISymbolFilter GetSymbolFilterFromFiles(string[]? apiExclusionFileP
2132
return GetCompositeSymbolFilter(docIdSymbolFilter, respectInternals, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols, withImplicitSymbolFilter: true);
2233
}
2334

35+
/// <summary>
36+
/// Creates a symbol filter based on the given exclusion list.
37+
/// </summary>
38+
/// <param name="apiExclusionList">A collection of exclusion list.</param>
39+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
40+
/// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param>
41+
/// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param>
42+
/// <returns>An instance of the symbol filter.</returns>
2443
public static ISymbolFilter GetSymbolFilterFromList(string[]? apiExclusionList,
2544
bool respectInternals = false,
2645
bool includeEffectivelyPrivateSymbols = true,
@@ -33,6 +52,14 @@ public static ISymbolFilter GetSymbolFilterFromList(string[]? apiExclusionList,
3352
return GetCompositeSymbolFilter(docIdSymbolFilter, respectInternals, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols, withImplicitSymbolFilter: true);
3453
}
3554

55+
/// <summary>
56+
/// Creates an attribute filter based on the given exclusion file paths.
57+
/// </summary>
58+
/// <param name="attributeExclusionFilePaths">A collection of paths where the exclusion files should be searched.</param>
59+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
60+
/// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param>
61+
/// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param>
62+
/// <returns>An instance of the attribute filter.</returns>
3663
public static ISymbolFilter GetAttributeFilterFromPaths(string[]? attributeExclusionFilePaths,
3764
bool respectInternals = false,
3865
bool includeEffectivelyPrivateSymbols = true,
@@ -45,6 +72,14 @@ public static ISymbolFilter GetAttributeFilterFromPaths(string[]? attributeExclu
4572
return GetCompositeSymbolFilter(docIdSymbolFilter, respectInternals, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols, withImplicitSymbolFilter: false);
4673
}
4774

75+
/// <summary>
76+
/// Creates an attribute filter based on the given exclusion list.
77+
/// </summary>
78+
/// <param name="attributeExclusionList">A collection of exclusion list.</param>
79+
/// <param name="respectInternals">Whether to include internal symbols or not.</param>
80+
/// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param>
81+
/// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param>
82+
/// <returns>An instance of the attribute filter.</returns>
4883
public static ISymbolFilter GetAttributeFilterFromList(string[]? attributeExclusionList,
4984
bool respectInternals = false,
5085
bool includeEffectivelyPrivateSymbols = true,

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/SyntaxRewriter/TypeDeclarationCSharpSyntaxRewriter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ namespace Microsoft.DotNet.GenAPI.SyntaxRewriter
1717
/// </summary>
1818
public class TypeDeclarationCSharpSyntaxRewriter : CSharpSyntaxRewriter
1919
{
20+
private readonly bool _addPartialModifier;
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="TypeDeclarationCSharpSyntaxRewriter"/> class, and optionally allows deciding whether to insert the partial modifier for types or not.
24+
/// </summary>
25+
/// <param name="addPartialModifier">Determines whether to insert the partial modifier for types or not.</param>
26+
public TypeDeclarationCSharpSyntaxRewriter(bool addPartialModifier = true) => _addPartialModifier = addPartialModifier;
27+
2028
/// <inheritdoc />
2129
public override SyntaxNode? VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
2230
{
@@ -83,15 +91,15 @@ public class TypeDeclarationCSharpSyntaxRewriter : CSharpSyntaxRewriter
8391
}
8492
}
8593

86-
private static T? VisitCommonTypeDeclaration<T>(T? node) where T : TypeDeclarationSyntax
94+
private T? VisitCommonTypeDeclaration<T>(T? node) where T : TypeDeclarationSyntax
8795
{
8896
if (node == null)
8997
{
9098
return null;
9199
}
92100

93101
node = RemoveBaseType(node, "global::System.Object");
94-
return AddPartialModifier(node);
102+
return _addPartialModifier ? AddPartialModifier(node) : node;
95103
}
96104

97105
private static T? AddPartialModifier<T>(T? node) where T : TypeDeclarationSyntax =>

src/Compatibility/Microsoft.DotNet.ApiSymbolExtensions/AssemblySymbolLoader.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Reflection.PortableExecutable;
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
1011

1112
namespace Microsoft.DotNet.ApiSymbolExtensions
1213
{
@@ -279,6 +280,40 @@ public IEnumerable<IAssemblySymbol> LoadMatchingAssemblies(IEnumerable<IAssembly
279280
return matchingAssemblies;
280281
}
281282

283+
/// <inheritdoc />
284+
public void LogAllDiagnostics(ILog logger, string? customMessage = null)
285+
{
286+
if (HasRoslynDiagnostics(out IReadOnlyList<Diagnostic> roslynDiagnostics))
287+
{
288+
if (!string.IsNullOrEmpty(customMessage))
289+
{
290+
logger.LogWarning(customMessage!);
291+
}
292+
293+
foreach (Diagnostic warning in roslynDiagnostics)
294+
{
295+
logger.LogWarning(warning.Id, warning.ToString());
296+
}
297+
}
298+
}
299+
300+
/// <inheritdoc />
301+
public void LogAllWarnings(ILog logger, string? customMessage = null)
302+
{
303+
if (HasLoadWarnings(out IReadOnlyList<AssemblyLoadWarning> loadWarnings))
304+
{
305+
if (!string.IsNullOrEmpty(customMessage))
306+
{
307+
logger.LogWarning(customMessage!);
308+
}
309+
310+
foreach (AssemblyLoadWarning warning in loadWarnings)
311+
{
312+
logger.LogWarning(warning.DiagnosticId, warning.Message);
313+
}
314+
}
315+
}
316+
282317
/// <inheritdoc />
283318
public IEnumerable<MetadataReference> MetadataReferences => _cSharpCompilation.References;
284319

src/Compatibility/Microsoft.DotNet.ApiSymbolExtensions/IAssemblySymbolLoader.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.CodeAnalysis;
5+
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
56

67
namespace Microsoft.DotNet.ApiSymbolExtensions
78
{
@@ -90,6 +91,20 @@ public interface IAssemblySymbolLoader
9091
/// <returns>The list of matching assemblies represented as <see cref="IAssemblySymbol"/>.</returns>
9192
IEnumerable<IAssemblySymbol> LoadMatchingAssemblies(IEnumerable<IAssemblySymbol> fromAssemblies, IEnumerable<string> searchPaths, bool validateMatchingIdentity = true, bool warnOnMissingAssemblies = true);
9293

94+
/// <summary>
95+
///
96+
/// </summary>
97+
/// <param name="logger"></param>
98+
/// <param name="customMessage"></param>
99+
void LogAllDiagnostics(ILog logger, string? customMessage = null);
100+
101+
/// <summary>
102+
///
103+
/// </summary>
104+
/// <param name="logger"></param>
105+
/// <param name="customMessage"></param>
106+
void LogAllWarnings(ILog logger, string? customMessage = null);
107+
93108
/// <summary>
94109
/// The list of metadata references represented as <see cref="MetadataReference" />.
95110
/// </summary>

0 commit comments

Comments
 (0)
0