-
Notifications
You must be signed in to change notification settings - Fork 15
/
DocsCommentsContainer.cs
314 lines (273 loc) · 13.2 KB
/
DocsCommentsContainer.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Libraries.Docs
{
internal class DocsCommentsContainer
{
private Configuration Config { get; set; }
private XDocument? xDoc = null;
public readonly Dictionary<string, DocsType> Types = new();
public readonly Dictionary<string, DocsMember> Members = new();
public DocsCommentsContainer(Configuration config)
{
Config = config;
}
public void CollectFiles()
{
Log.Info("Looking for Docs xml files...");
foreach (FileInfo fileInfo in EnumerateFiles())
{
LoadFile(fileInfo);
}
Log.Success("Finished looking for Docs xml files.");
Log.Line();
}
public void Save()
{
if (!Config.Save)
{
Log.Line();
Log.Error("[No files were saved]");
Log.Warning($"Did you forget to specify '-{nameof(Config.Save)} true'?");
Log.Line();
return;
}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding encoding = Encoding.GetEncoding(1252); // Preserves original xml encoding from Docs repo
List<string> savedFiles = new List<string>();
foreach (var type in Types.Values.Where(x => x.Changed))
{
Log.Warning(false, $"Saving changes for {type.FilePath}:");
try
{
StreamReader sr = new StreamReader(type.FilePath);
int x = sr.Read(); // Force the first read to be done so the encoding is detected
sr.Close();
// These settings prevent the addition of the <xml> element on the first line and will preserve indentation+endlines
XmlWriterSettings xws = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
Encoding = encoding,
CheckCharacters = false
};
using (XmlWriter xw = XmlWriter.Create(type.FilePath, xws))
{
type.XDoc.Save(xw);
}
// Workaround to delete the annoying endline added by XmlWriter.Save
string fileData = File.ReadAllText(type.FilePath);
if (!fileData.EndsWith(Environment.NewLine))
{
File.WriteAllText(type.FilePath, fileData + Environment.NewLine, encoding);
}
Log.Success(" [Saved]");
}
catch (Exception e)
{
Log.Error("Failed to write to {0}. {1}", type.FilePath, e.Message);
Log.Line();
Log.Error(e.StackTrace ?? string.Empty);
if (e.InnerException != null)
{
Log.Line();
Log.Error(e.InnerException.Message);
Log.Line();
Log.Error(e.InnerException.StackTrace ?? string.Empty);
}
System.Threading.Thread.Sleep(1000);
}
Log.Line();
}
}
private bool HasAllowedDirName(DirectoryInfo dirInfo)
{
return !Configuration.ForbiddenBinSubdirectories.Contains(dirInfo.Name) && !dirInfo.Name.EndsWith(".Tests");
}
private bool HasAllowedFileName(FileInfo fileInfo)
{
return !fileInfo.Name.StartsWith("ns-") &&
fileInfo.Name != "index.xml" &&
fileInfo.Name != "_filter.xml";
}
private IEnumerable<FileInfo> EnumerateFiles()
{
// Union avoids duplication
var includedAssembliesAndNamespaces = Config.IncludedAssemblies.Union(Config.IncludedNamespaces);
var excludedAssembliesAndNamespaces = Config.ExcludedAssemblies.Union(Config.ExcludedNamespaces);
foreach (DirectoryInfo rootDir in Config.DirsDocsXml)
{
// Try to find folders with the names of assemblies AND namespaces (if the user specified any)
foreach (string included in includedAssembliesAndNamespaces)
{
// If the user specified a sub-assembly or sub-namespace to exclude, we need to skip it
if (excludedAssembliesAndNamespaces.Any(excluded => included.StartsWith(excluded)))
{
continue;
}
foreach (DirectoryInfo subDir in rootDir.EnumerateDirectories($"{included}*", SearchOption.TopDirectoryOnly))
{
if (HasAllowedDirName(subDir))
{
foreach (FileInfo fileInfo in subDir.EnumerateFiles("*.xml", SearchOption.AllDirectories))
{
if (HasAllowedFileName(fileInfo))
{
// LoadFile will determine if the Type is allowed or not
yield return fileInfo;
}
}
}
}
if (!Config.SkipInterfaceImplementations)
{
// Find interfaces only inside System.* folders.
// Including Microsoft.* folders reaches the max limit of files to include in a list, plus there are no essential interfaces there.
foreach (DirectoryInfo subDir in rootDir.EnumerateDirectories("System*", SearchOption.AllDirectories))
{
if (!Configuration.ForbiddenBinSubdirectories.Contains(subDir.Name) &&
// Exclude any folder that starts with the excluded assemblies OR excluded namespaces
!excludedAssembliesAndNamespaces.Any(excluded => subDir.Name.StartsWith(excluded)) && !subDir.Name.EndsWith(".Tests"))
{
// Ensure including interface files that start with I and then an uppercase letter, and prevent including files like 'Int'
foreach (FileInfo fileInfo in subDir.EnumerateFiles("I*.xml", SearchOption.AllDirectories))
{
if (fileInfo.Name[1] >= 'A' || fileInfo.Name[1] <= 'Z')
{
yield return fileInfo;
}
}
}
}
}
}
}
}
private void LoadFile(FileInfo fileInfo)
{
try
{
// docs repo uses code page 1252
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
StreamReader sr = new(fileInfo.FullName, Encoding.GetEncoding(1252));
xDoc = XDocument.Load(sr);
}
catch(Exception ex)
{
Log.Error($"Failed to load '{fileInfo.FullName}'. {ex}");
return;
}
if (IsXmlMalformed(xDoc, fileInfo.FullName))
{
return;
}
DocsType docsType = new DocsType(fileInfo.FullName, xDoc, xDoc.Root!);
bool add = false;
bool addedAsInterface = false;
bool containsForbiddenAssembly = docsType.AssemblyInfos.Any(assemblyInfo =>
Config.ExcludedAssemblies.Any(excluded => assemblyInfo.AssemblyName.StartsWith(excluded)) ||
Config.ExcludedNamespaces.Any(excluded => assemblyInfo.AssemblyName.StartsWith(excluded)));
if (!containsForbiddenAssembly)
{
// If it's an interface, always add it if the user wants to detect EIIs,
// even if it's in an assembly that was not included but was not explicitly excluded
addedAsInterface = false;
if (!Config.SkipInterfaceImplementations)
{
// Interface files start with I, and have an 2nd alphabetic character
addedAsInterface = docsType.Name.Length >= 2 && docsType.Name[0] == 'I' && docsType.Name[1] >= 'A' && docsType.Name[1] <= 'Z';
add |= addedAsInterface;
}
bool containsAllowedAssembly = docsType.AssemblyInfos.Any(assemblyInfo =>
Config.IncludedAssemblies.Any(included => assemblyInfo.AssemblyName.StartsWith(included)) ||
Config.IncludedNamespaces.Any(included => assemblyInfo.AssemblyName.StartsWith(included)));
if (containsAllowedAssembly)
{
// If it was already added above as an interface, skip this part
// Otherwise, find out if the type belongs to the included assemblies, and if specified, to the included (and not excluded) types
// This includes interfaces even if user wants to skip EIIs - They will be added if they belong to this namespace or to the list of
// included (and not exluded) types, but will not be used for EII, but rather as normal types whose comments should be ported
if (!addedAsInterface)
{
// Either the user didn't specify namespace filtering (allow all namespaces) or specified particular ones to include/exclude
if (!Config.IncludedNamespaces.Any() ||
(Config.IncludedNamespaces.Any(included => docsType.Namespace.StartsWith(included)) &&
!Config.ExcludedNamespaces.Any(excluded => docsType.Namespace.StartsWith(excluded))))
{
// Can add if the user didn't specify type filtering (allow all types), or specified particular ones to include/exclude
add = !Config.IncludedTypes.Any() ||
(Config.IncludedTypes.Contains(docsType.Name) &&
!Config.ExcludedTypes.Contains(docsType.Name));
}
}
}
}
if (add)
{
int totalMembersAdded = 0;
Types.TryAdd(docsType.DocId, docsType); // is it OK this encounters duplicates?
if (XmlHelper.TryGetChildElement(xDoc.Root!, "Members", out XElement? xeMembers) && xeMembers != null)
{
foreach (XElement xeMember in xeMembers.Elements("Member"))
{
DocsMember member = new DocsMember(fileInfo.FullName, docsType, xeMember);
totalMembersAdded++;
Members.TryAdd(member.DocId, member); // is it OK this encounters duplicates?
}
}
string message = $"Type '{docsType.DocId}' added with {totalMembersAdded} member(s) included: {fileInfo.FullName}";
if (addedAsInterface)
{
Log.Magenta("[Interface] - " + message);
}
else if (totalMembersAdded == 0)
{
Log.Warning(message);
}
else
{
Log.Success(message);
}
}
}
private bool IsXmlMalformed(XDocument? xDoc, string fileName)
{
if(xDoc == null)
{
Log.Error($"XDocument is null: {fileName}");
return true;
}
if (xDoc.Root == null)
{
Log.Error($"Docs xml file does not have a root element: {fileName}");
return true;
}
if (xDoc.Root.Name == "Namespace")
{
Log.Error($"Skipping namespace file (should have been filtered already): {fileName}");
return true;
}
if (xDoc.Root.Name != "Type")
{
Log.Error($"Docs xml file does not have a 'Type' root element: {fileName}");
return true;
}
if (!xDoc.Root.HasElements)
{
Log.Error($"Docs xml file Type element does not have any children: {fileName}");
return true;
}
if (xDoc.Root.Elements("Docs").Count() != 1)
{
Log.Error($"Docs xml file Type element does not have a Docs child: {fileName}");
return true;
}
return false;
}
}
}