Repository navigation
Expand file tree
/
Copy pathCodeBoard.cs
More file actions
128 lines (114 loc) · 5.2 KB
/
Copy pathCodeBoard.cs
File metadata and controls
128 lines (114 loc) · 5.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using M31.FluentApi.Generator.CodeBuilding;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardActors.Commons;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardActors.MethodCreation;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardActors.MethodCreation.Forks;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardElements.FluentApiComments;
using M31.FluentApi.Generator.SourceAnalyzers;
using M31.FluentApi.Generator.SourceGenerators;
using M31.FluentApi.Generator.SourceGenerators.AttributeElements;
using M31.FluentApi.Generator.SourceGenerators.Generics;
using Microsoft.CodeAnalysis;
namespace M31.FluentApi.Generator.CodeGeneration.CodeBoardElements;
// black board approach
internal class CodeBoard
{
private readonly List<Diagnostic> diagnostics;
private CodeBoard(
IReadOnlyCollection<FluentApiInfo> fluentApiInfos,
IReadOnlyCollection<FluentApiInfoGroup> fluentApiInfoGroups,
BuilderAndTargetInfo builderAndTargetInfo,
CodeFile codeFile,
Class builderClass,
string newLineString,
CancellationToken cancellationToken)
{
FluentApiInfos = fluentApiInfos;
FluentApiInfoGroups = fluentApiInfoGroups;
Info = builderAndTargetInfo;
CodeFile = codeFile;
BuilderClass = builderClass;
InnerBodyCreationDelegates = new InnerBodyCreationDelegates();
TransformedComments = new TransformedComments();
GroupsToMethods = new Dictionary<FluentApiInfoGroup, BuilderMethods>();
BuilderMethodToAttributeData = new Dictionary<BuilderMethod, AttributeDataExtended>();
Forks = new List<Fork>();
ReservedVariableNames = new ReservedVariableNames();
diagnostics = new List<Diagnostic>();
NewLineString = newLineString;
CancellationToken = cancellationToken;
}
internal IReadOnlyCollection<FluentApiInfo> FluentApiInfos { get; }
internal IReadOnlyCollection<FluentApiInfoGroup> FluentApiInfoGroups { get; }
internal BuilderAndTargetInfo Info { get; }
internal CodeFile CodeFile { get; }
internal Class BuilderClass { get; }
internal InnerBodyCreationDelegates InnerBodyCreationDelegates { get; }
internal TransformedComments TransformedComments { get; }
internal Dictionary<FluentApiInfoGroup, BuilderMethods> GroupsToMethods { get; }
internal Dictionary<BuilderMethod, AttributeDataExtended> BuilderMethodToAttributeData { get; }
internal IReadOnlyList<Fork> Forks { get; set; }
internal ReservedVariableNames ReservedVariableNames { get; }
internal IReadOnlyCollection<Diagnostic> Diagnostics => diagnostics;
internal string NewLineString { get; }
internal CancellationToken CancellationToken { get; }
internal bool CancellationRequested => CancellationToken.IsCancellationRequested;
internal bool HasErrors => diagnostics.HaveErrors();
internal static CodeBoard Create(
BuilderAndTargetInfo builderAndTargetInfo,
IReadOnlyCollection<FluentApiInfo> fluentApiInfos,
IReadOnlyCollection<FluentApiInfoGroup> fluentApiInfoGroups,
IReadOnlyCollection<string> usingStatements,
string newLineString,
CancellationToken cancellationToken)
{
CodeFile codeFile = new CodeFile(builderAndTargetInfo.Namespace, newLineString);
Class builderClass = new Class(builderAndTargetInfo.BuilderClassName);
CodeBoard codeBoard = new CodeBoard(
fluentApiInfos,
fluentApiInfoGroups,
builderAndTargetInfo,
codeFile,
builderClass,
newLineString,
cancellationToken);
CreateHeader(codeFile);
codeFile.AddPreprocessorDirective(
"#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member");
codeFile.AddPreprocessorDirective("#nullable enable");
if (builderAndTargetInfo.GenericInfo != null)
{
foreach (GenericTypeParameter genericTypeParameter in builderAndTargetInfo.GenericInfo.Parameters)
{
builderClass.AddGenericParameter(
genericTypeParameter.ParameterName,
genericTypeParameter.Constraints.GetConstraintsForCodeGeneration());
}
}
builderClass.AddModifiers(builderAndTargetInfo.DefaultAccessModifier);
codeFile.AddDefinition(builderClass);
foreach (string usingStatement in usingStatements)
{
if (usingStatement == "using M31.FluentApi.Attributes;")
{
continue;
}
codeFile.AddUsingStatement(usingStatement);
}
return codeBoard;
}
private static void CreateHeader(CodeFile codeFile)
{
codeFile.AddHeaderLine("// <auto-generated/>");
codeFile.AddHeaderLine("// This code was generated by the library M31.FluentAPI.");
codeFile.AddHeaderLine("// Changes to this file may cause incorrect behavior and will be lost if the code is " +
"regenerated.");
}
internal CodeBuilder NewCodeBuilder()
{
return new CodeBuilder(NewLineString);
}
internal void ReportDiagnostic(Diagnostic diagnostic)
{
diagnostics.Add(diagnostic);
}
}