-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzeFileUseCase.cs
More file actions
88 lines (75 loc) · 3.54 KB
/
AnalyzeFileUseCase.cs
File metadata and controls
88 lines (75 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProjGraph.Core.Models;
using ProjGraph.Lib.Core.Abstractions;
namespace ProjGraph.Lib.ClassDiagram.Application.UseCases;
/// <summary>
/// Use case for analyzing a C# source file to extract class definitions and their relationships.
/// </summary>
/// <param name="compilationFactory">The factory for creating compilations.</param>
/// <param name="typeProcessor">The type processor for analyzing type queues.</param>
/// <param name="fileSystem">The file system abstraction.</param>
public class AnalyzeFileUseCase(
ICompilationFactory compilationFactory,
ITypeProcessor typeProcessor,
IFileSystem fileSystem)
{
/// <summary>
/// Executes the analysis of a C# source file to extract class definitions and their relationships.
/// </summary>
/// <param name="filePath">The path to the C# source file to analyze.</param>
/// <param name="options">The analysis options.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the analyzed class model.</returns>
/// <exception cref="FileNotFoundException">Thrown when the specified source file is not found.</exception>
public async Task<ClassModel> ExecuteAsync(
string filePath,
AnalysisOptions? options = null)
{
options ??= new AnalysisOptions();
if (!fileSystem.FileExists(filePath))
{
throw new FileNotFoundException("Source file not found", filePath);
}
var startDir = fileSystem.GetDirectoryName(filePath) ?? Environment.CurrentDirectory;
#pragma warning disable CA1849, S6966 // Call async methods when in an async method
var code = fileSystem.ReadAllText(filePath);
#pragma warning restore CA1849, S6966
var syntaxTree = CSharpSyntaxTree.ParseText(code, path: filePath);
var compilation = (CSharpCompilation)compilationFactory.CreateCompilation([syntaxTree]);
var context = new AnalysisContext
{
AnalyzedTypeFullNames = [],
Types = [],
Relationships = [],
Compilation = compilation,
StartDirectory = startDir
};
var typesToAnalyze = new Queue<(INamedTypeSymbol Symbol, int Depth)>();
await EnqueueInitialTypesAsync(syntaxTree, compilation, typesToAnalyze);
await typeProcessor.ProcessTypeQueueAsync(typesToAnalyze, context, options);
return new ClassModel(Path.GetFileName(filePath), context.Types, context.Relationships);
}
/// <summary>
/// Enqueues the initial types from the syntax tree for analysis.
/// </summary>
/// <param name="syntaxTree">The syntax tree of the C# source file.</param>
/// <param name="compilation">The C# compilation object for semantic analysis.</param>
/// <param name="typesToAnalyze">The queue to store types to be analyzed, along with their depth.</param>
private static async Task EnqueueInitialTypesAsync(
SyntaxTree syntaxTree,
CSharpCompilation compilation,
Queue<(INamedTypeSymbol Symbol, int Depth)> typesToAnalyze)
{
var root = await syntaxTree.GetRootAsync();
var semanticModel = compilation.GetSemanticModel(syntaxTree);
foreach (var typeDecl in root.DescendantNodes().OfType<BaseTypeDeclarationSyntax>())
{
if (semanticModel.GetDeclaredSymbol(typeDecl) is not { } symbol)
{
continue;
}
typesToAnalyze.Enqueue((symbol, 0));
}
}
}