-
Notifications
You must be signed in to change notification settings - Fork 351
/
GenAPITask.cs
449 lines (395 loc) · 18 KB
/
GenAPITask.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.DotNet.Build.Tasks;
using Microsoft.Cci;
using Microsoft.Cci.Extensions;
using Microsoft.Cci.Extensions.CSharp;
using Microsoft.Cci.Filters;
using Microsoft.Cci.Writers;
using Microsoft.Cci.Writers.CSharp;
using Microsoft.Cci.Writers.Syntax;
namespace Microsoft.DotNet.GenAPI
{
public class GenAPITask : BuildTask
{
private const string InternalsVisibleTypeName = "System.Runtime.CompilerServices.InternalsVisibleToAttribute";
private const string DefaultFileHeader =
"//------------------------------------------------------------------------------\r\n" +
"// <auto-generated>\r\n" +
"// This code was generated by a tool.\r\n" +
"// GenAPI Version: {0}\r\n" +
"//\r\n" +
"// Changes to this file may cause incorrect behavior and will be lost if\r\n" +
"// the code is regenerated.\r\n" +
"// </auto-generated>\r\n" +
"//------------------------------------------------------------------------------\r\n";
private WriterType _writerType;
private SyntaxWriterType _syntaxWriterType;
private DocIdKinds _docIdKinds = Cci.Writers.DocIdKinds.All;
/// <summary>
/// Path for an specific assembly or a directory to get all assemblies.
/// </summary>
[Required]
public string Assembly { get; set; }
/// <summary>
/// Delimited (',' or ';') set of paths to use for resolving assembly references.
/// </summary>
public string LibPath { get; set; }
/// <summary>
/// Specify a api list in the DocId format of which APIs to include.
/// </summary>
public string ApiList { get; set; }
/// <summary>
/// Output path. Default is the console. Can specify an existing directory as well and
/// then a file will be created for each assembly with the matching name of the assembly.
/// </summary>
public string OutputPath { get; set; }
/// <summary>
/// Specify a file with an alternate header content to prepend to output.
/// </summary>
public string HeaderFile { get; set; }
/// <summary>
/// Specify the writer type to use. Legal values: CSDecl, DocIds, TypeForwards, TypeList. Default is CSDecl.
/// </summary>
public string WriterType
{
get => _writerType.ToString();
set => _writerType = string.IsNullOrWhiteSpace(value) ? default : (WriterType) Enum.Parse(typeof(WriterType), value, true);
}
/// <summary>
/// Specific the syntax writer type. Only used if the writer is CSDecl. Legal values: Text, Html, Xml. Default is Text.
/// </summary>
public string SyntaxWriterType
{
get => _syntaxWriterType.ToString();
set => _syntaxWriterType = string.IsNullOrWhiteSpace(value) ? default : (SyntaxWriterType)Enum.Parse(typeof(SyntaxWriterType), value, true);
}
/// <summary>
/// Only include API of the specified kinds. Legal values: A, Assembly, Namespace, N, T, Type, Field, F, P, Property, Method, M, Event, E, All. Default is All.
/// </summary>
public string DocIdKinds
{
get => _docIdKinds.ToString();
set => _docIdKinds = string.IsNullOrWhiteSpace(value) ? Cci.Writers.DocIdKinds.All : (DocIdKinds)Enum.Parse(typeof(DocIdKinds), value, true);
}
/// <summary>
/// Method bodies should throw PlatformNotSupportedException.
/// </summary>
public string ExceptionMessage { get; set; }
/// <summary>
/// Include global prefix for compilation.
/// </summary>
public bool GlobalPrefix { get; set; }
/// <summary>
/// Specify a api list in the DocId format of which APIs to exclude.
/// </summary>
public string ExcludeApiList { get; set; }
/// <summary>
/// Specify a list in the DocId format of which attributes should be excluded from being applied on apis.
/// </summary>
public string ExcludeAttributesList { get; set; }
/// <summary>
/// [CSDecl] Resolve type forwards and include its members.
/// </summary>
public bool FollowTypeForwards { get; set; }
/// <summary>
/// [CSDecl] Include only API's not CS code that compiles.
/// </summary>
public bool ApiOnly { get; set; }
/// <summary>
/// Include all API's not just public APIs. Default is public only.
/// </summary>
public bool All { get; set; }
/// <summary>
/// Include both internal and public APIs if assembly contains an InternalsVisibleTo attribute. Otherwise, include only public APIs.
/// </summary>
public bool RespectInternals { get; set; }
/// <summary>
/// Exclude APIs marked with a CompilerGenerated attribute.
/// </summary>
public bool ExcludeCompilerGenerated { get; set; }
/// <summary>
/// [CSDecl] Include member headings for each type of member.
/// </summary>
public bool MemberHeadings { get; set; }
/// <summary>
/// [CSDecl] Highlight overridden base members.
/// </summary>
public bool HighlightBaseMembers { get; set; }
/// <summary>
/// [CSDecl] Highlight interface implementation members.
/// </summary>
public bool HighlightInterfaceMembers { get; set; }
/// <summary>
/// [CSDecl] Include base types, interfaces, and attributes, even when those types are filtered.
/// </summary>
public bool AlwaysIncludeBase { get; set; }
/// <summary>
/// [CSDecl] Specify one or more namespace+type list(s) in the DocId format of types that should be wrapped inside an #if with the symbol specified in the <c>Symbol</c> metadata item.
/// If the <c>Symbol</c> metadata item is empty the types won't be wrapped but will still be output after all other types, this can be used in combination with <see cref="DefaultCondition" /> to wrap all other types.
/// </summary>
public ITaskItem[] ConditionalTypeLists { get; set; }
/// <summary>
/// [CSDecl] #if condition to apply to all types not included in <see cref="ConditionalTypeLists" />.
/// </summary>
public string DefaultCondition { get; set; }
/// <summary>
/// Exclude members when return value or parameter types are excluded.
/// </summary>
public bool ExcludeMembers { get; set; }
/// <summary>
/// [CSDecl] Language Version to target.
/// </summary>
public string LangVersion { get; set; }
public override bool Execute()
{
HostEnvironment host = new();
host.UnableToResolve += (sender, e) =>
Log.LogWarning("Unable to resolve assembly '{0}' referenced by '{1}'.", e.Unresolved.ToString(), e.Referrer.ToString());
host.UnifyToLibPath = true;
if (!string.IsNullOrWhiteSpace(LibPath))
{
host.AddLibPaths(HostEnvironment.SplitPaths(LibPath));
}
IEnumerable<IAssembly> assemblies = host.LoadAssemblies(HostEnvironment.SplitPaths(Assembly));
if (!assemblies.Any())
{
Log.LogError("ERROR: Failed to load any assemblies from '{0}'", Assembly);
return false;
}
string headerText = GetHeaderText(HeaderFile, _writerType, _syntaxWriterType);
bool loopPerAssembly = Directory.Exists(OutputPath);
if (loopPerAssembly)
{
foreach (IAssembly assembly in assemblies)
{
using (TextWriter output = GetOutput(OutputPath, GetFilename(assembly, _writerType, _syntaxWriterType)))
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, _writerType, _syntaxWriterType))
{
ICciWriter writer = null;
try
{
if (headerText != null)
{
output.Write(headerText);
}
bool includeInternals = RespectInternals &&
assembly.Attributes.HasAttributeOfType(InternalsVisibleTypeName);
writer = GetWriter(output, syntaxWriter, includeInternals);
writer.WriteAssemblies(new IAssembly[] { assembly });
}
finally
{
if (writer is CSharpWriter csWriter)
{
csWriter.Dispose();
}
}
}
}
}
else
{
using (TextWriter output = GetOutput(OutputPath))
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, _writerType, _syntaxWriterType))
{
ICciWriter writer = null;
try
{
if (headerText != null)
{
output.Write(headerText);
}
bool includeInternals = RespectInternals &&
assemblies.Any(assembly => assembly.Attributes.HasAttributeOfType(InternalsVisibleTypeName));
writer = GetWriter(output, syntaxWriter, includeInternals);
writer.WriteAssemblies(assemblies);
}
finally
{
if (writer is CSharpWriter csWriter)
{
csWriter.Dispose();
}
}
}
}
return !Log.HasLoggedErrors;
}
private static string GetHeaderText(string headerFile, WriterType writerType, SyntaxWriterType syntaxWriterType)
{
if (!string.IsNullOrEmpty(headerFile))
{
return File.ReadAllText(headerFile);
}
string defaultHeader = string.Empty;
// This header is for CS source only
if ((writerType == GenAPI.WriterType.CSDecl || writerType == GenAPI.WriterType.TypeForwards) &&
syntaxWriterType == GenAPI.SyntaxWriterType.Text)
{
// Write default header (culture-invariant, so that the generated file will not be language-dependent)
defaultHeader = string.Format(CultureInfo.InvariantCulture,
DefaultFileHeader, typeof(GenAPITask).Assembly.GetName().Version.ToString());
}
return defaultHeader;
}
private static TextWriter GetOutput(string outFilePath, string filename = "")
{
// If this is a null, empty, whitespace, or a directory use console
if (string.IsNullOrWhiteSpace(outFilePath))
return Console.Out;
if (Directory.Exists(outFilePath) && !string.IsNullOrEmpty(filename))
{
return File.CreateText(Path.Combine(outFilePath, filename));
}
return File.CreateText(outFilePath);
}
private static string GetFilename(IAssembly assembly, WriterType writer, SyntaxWriterType syntax)
{
string name = assembly.Name.Value;
return writer switch
{
GenAPI.WriterType.DocIds or GenAPI.WriterType.TypeForwards => name + ".txt",
_ => syntax switch
{
GenAPI.SyntaxWriterType.Xml => name + ".xml",
GenAPI.SyntaxWriterType.Html => name + ".html",
_ => name + ".cs",
},
};
}
private static ICciFilter GetFilter(
string apiList,
bool all,
bool includeInternals,
bool apiOnly,
bool excludeCompilerGenerated,
string excludeApiList,
bool excludeMembers,
string excludeAttributesList,
bool includeForwardedTypes)
{
ICciFilter includeFilter;
if (!string.IsNullOrWhiteSpace(apiList))
{
includeFilter = new DocIdIncludeListFilter(apiList);
}
else if (all)
{
includeFilter = new IncludeAllFilter();
}
else if (includeInternals)
{
includeFilter = new InternalsAndPublicCciFilter(excludeAttributes: apiOnly, includeForwardedTypes);
}
else
{
includeFilter = new PublicOnlyCciFilter(excludeAttributes: apiOnly, includeForwardedTypes);
}
if (excludeCompilerGenerated)
{
includeFilter = new IntersectionFilter(includeFilter, new ExcludeCompilerGeneratedCciFilter());
}
if (!string.IsNullOrWhiteSpace(excludeApiList))
{
includeFilter = new IntersectionFilter(includeFilter, new DocIdExcludeListFilter(excludeApiList, excludeMembers) { IncludeForwardedTypes = includeForwardedTypes });
}
if (!string.IsNullOrWhiteSpace(excludeAttributesList))
{
includeFilter = new IntersectionFilter(includeFilter, new ExcludeAttributesFilter(excludeAttributesList));
}
return includeFilter;
}
private static IStyleSyntaxWriter GetSyntaxWriter(TextWriter output, WriterType writer, SyntaxWriterType syntax)
{
if (writer != GenAPI.WriterType.CSDecl && writer != GenAPI.WriterType.TypeList)
return null;
return syntax switch
{
GenAPI.SyntaxWriterType.Xml => new OpenXmlSyntaxWriter(output),
GenAPI.SyntaxWriterType.Html => new HtmlSyntaxWriter(output),
_ => new TextSyntaxWriter(output) { SpacesInIndent = 4 },
};
}
private ICciWriter GetWriter(TextWriter output, ISyntaxWriter syntaxWriter, bool includeInternals)
{
ICciFilter filter = GetFilter(
ApiList,
All,
includeInternals,
ApiOnly,
ExcludeCompilerGenerated,
ExcludeApiList,
ExcludeMembers,
ExcludeAttributesList,
FollowTypeForwards);
switch (_writerType)
{
case GenAPI.WriterType.DocIds:
return new DocumentIdWriter(output, filter, _docIdKinds);
case GenAPI.WriterType.TypeForwards:
return new TypeForwardWriter(output, filter)
{
IncludeForwardedTypes = true
};
case GenAPI.WriterType.TypeList:
return new TypeListWriter(syntaxWriter, filter);
case GenAPI.WriterType.CSDecl:
default:
{
CSharpWriter writer = new(syntaxWriter, filter, ApiOnly);
writer.IncludeSpaceBetweenMemberGroups = writer.IncludeMemberGroupHeadings = MemberHeadings;
writer.HighlightBaseMembers = HighlightBaseMembers;
writer.HighlightInterfaceMembers = HighlightInterfaceMembers;
writer.PutBraceOnNewLine = true;
writer.PlatformNotSupportedExceptionMessage = ExceptionMessage;
writer.IncludeGlobalPrefixForCompilation = GlobalPrefix;
writer.AlwaysIncludeBase = AlwaysIncludeBase;
writer.LangVersion = GetLangVersion(LangVersion);
writer.IncludeForwardedTypes = FollowTypeForwards;
writer.DefaultCondition = DefaultCondition;
if (ConditionalTypeLists != null)
{
writer.ConditionalTypeLists = ConditionalTypeLists.Select(c =>
new CSharpWriter.ConditionalTypeList
{
Symbol = c.GetMetadata("Symbol"),
Filter = new DocIdIncludeListFilter(c.ItemSpec) { IncludeForwardedTypes = FollowTypeForwards }
});
}
return writer;
}
}
}
private static Version GetLangVersion(string langVersion)
{
string langVersionValue = langVersion ?? string.Empty;
if (langVersionValue.Equals("default", StringComparison.OrdinalIgnoreCase))
{
return CSDeclarationWriter.LangVersionDefault;
}
else if (langVersionValue.Equals("latest", StringComparison.OrdinalIgnoreCase))
{
return CSDeclarationWriter.LangVersionLatest;
}
else if (langVersionValue.Equals("preview", StringComparison.OrdinalIgnoreCase))
{
return CSDeclarationWriter.LangVersionPreview;
}
else if (Version.TryParse(langVersionValue, out Version parsedVersion))
{
return parsedVersion;
}
else
{
return CSDeclarationWriter.LangVersionDefault;
}
}
}
}