Skip to content

Commit d3fca6a

Browse files
authored
Update GenAPI to make Enum task parameters usable in project files. (#9008)
* Update GenAPI to make Enum task parameters usable in project files. Follow-up to PR #8507. Task parameters of type Enum cannot be passed in a project file. Change task parameter type to string for WriterType, SyntaxWriterType, and DocIdKinds. Do the necessary conversion inside the property getters/setters with private backing fields. * Add proper default handling for Enum string task parameters. * Fix OutputPath parameter handling when it is an existing directory.
1 parent 339a58e commit d3fca6a

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/Microsoft.DotNet.GenAPI/GenAPITask.cs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class GenAPITask : BuildTask
3131
"// the code is regenerated.\r\n" +
3232
"// </auto-generated>\r\n" +
3333
"//------------------------------------------------------------------------------\r\n";
34+
35+
private WriterType _writerType;
36+
private SyntaxWriterType _syntaxWriterType;
37+
private DocIdKinds _docIdKinds = Cci.Writers.DocIdKinds.All;
3438

3539
/// <summary>
3640
/// Path for an specific assembly or a directory to get all assemblies.
@@ -62,17 +66,29 @@ public class GenAPITask : BuildTask
6266
/// <summary>
6367
/// Specify the writer type to use. Legal values: CSDecl, DocIds, TypeForwards, TypeList. Default is CSDecl.
6468
/// </summary>
65-
public WriterType WriterType { get; set; }
69+
public string WriterType
70+
{
71+
get => _writerType.ToString();
72+
set => _writerType = string.IsNullOrWhiteSpace(value) ? default : (WriterType) Enum.Parse(typeof(WriterType), value, true);
73+
}
6674

6775
/// <summary>
6876
/// Specific the syntax writer type. Only used if the writer is CSDecl. Legal values: Text, Html, Xml. Default is Text.
6977
/// </summary>
70-
public SyntaxWriterType SyntaxWriterType { get; set; }
78+
public string SyntaxWriterType
79+
{
80+
get => _syntaxWriterType.ToString();
81+
set => _syntaxWriterType = string.IsNullOrWhiteSpace(value) ? default : (SyntaxWriterType)Enum.Parse(typeof(SyntaxWriterType), value, true);
82+
}
7183

7284
/// <summary>
7385
/// 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.
7486
/// </summary>
75-
public DocIdKinds DocIdKinds { get; set; }
87+
public string DocIdKinds
88+
{
89+
get => _docIdKinds.ToString();
90+
set => _docIdKinds = string.IsNullOrWhiteSpace(value) ? Cci.Writers.DocIdKinds.All : (DocIdKinds)Enum.Parse(typeof(DocIdKinds), value, true);
91+
}
7692

7793
/// <summary>
7894
/// Method bodies should throw PlatformNotSupportedException.
@@ -168,15 +184,15 @@ public override bool Execute()
168184
return false;
169185
}
170186

171-
string headerText = GetHeaderText(HeaderFile, WriterType, SyntaxWriterType);
187+
string headerText = GetHeaderText(HeaderFile, _writerType, _syntaxWriterType);
172188
bool loopPerAssembly = Directory.Exists(OutputPath);
173189

174190
if (loopPerAssembly)
175191
{
176192
foreach (IAssembly assembly in assemblies)
177193
{
178-
using (TextWriter output = GetOutput(GetFilename(assembly, WriterType, SyntaxWriterType)))
179-
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, WriterType, SyntaxWriterType))
194+
using (TextWriter output = GetOutput(OutputPath, GetFilename(assembly, _writerType, _syntaxWriterType)))
195+
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, _writerType, _syntaxWriterType))
180196
{
181197
ICciWriter writer = null;
182198
try
@@ -204,7 +220,7 @@ public override bool Execute()
204220
else
205221
{
206222
using (TextWriter output = GetOutput(OutputPath))
207-
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, WriterType, SyntaxWriterType))
223+
using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, _writerType, _syntaxWriterType))
208224
{
209225
ICciWriter writer = null;
210226
try
@@ -241,8 +257,8 @@ private static string GetHeaderText(string headerFile, WriterType writerType, Sy
241257

242258
string defaultHeader = string.Empty;
243259
// This header is for CS source only
244-
if ((writerType == WriterType.CSDecl || writerType == WriterType.TypeForwards) &&
245-
syntaxWriterType == SyntaxWriterType.Text)
260+
if ((writerType == GenAPI.WriterType.CSDecl || writerType == GenAPI.WriterType.TypeForwards) &&
261+
syntaxWriterType == GenAPI.SyntaxWriterType.Text)
246262
{
247263
// Write default header (culture-invariant, so that the generated file will not be language-dependent)
248264
defaultHeader = string.Format(CultureInfo.InvariantCulture,
@@ -271,11 +287,11 @@ private static string GetFilename(IAssembly assembly, WriterType writer, SyntaxW
271287
string name = assembly.Name.Value;
272288
return writer switch
273289
{
274-
WriterType.DocIds or WriterType.TypeForwards => name + ".txt",
290+
GenAPI.WriterType.DocIds or GenAPI.WriterType.TypeForwards => name + ".txt",
275291
_ => syntax switch
276292
{
277-
SyntaxWriterType.Xml => name + ".xml",
278-
SyntaxWriterType.Html => name + ".html",
293+
GenAPI.SyntaxWriterType.Xml => name + ".xml",
294+
GenAPI.SyntaxWriterType.Html => name + ".html",
279295
_ => name + ".cs",
280296
},
281297
};
@@ -330,13 +346,13 @@ private static ICciFilter GetFilter(
330346

331347
private static IStyleSyntaxWriter GetSyntaxWriter(TextWriter output, WriterType writer, SyntaxWriterType syntax)
332348
{
333-
if (writer != WriterType.CSDecl && writer != WriterType.TypeList)
349+
if (writer != GenAPI.WriterType.CSDecl && writer != GenAPI.WriterType.TypeList)
334350
return null;
335351

336352
return syntax switch
337353
{
338-
SyntaxWriterType.Xml => new OpenXmlSyntaxWriter(output),
339-
SyntaxWriterType.Html => new HtmlSyntaxWriter(output),
354+
GenAPI.SyntaxWriterType.Xml => new OpenXmlSyntaxWriter(output),
355+
GenAPI.SyntaxWriterType.Html => new HtmlSyntaxWriter(output),
340356
_ => new TextSyntaxWriter(output) { SpacesInIndent = 4 },
341357
};
342358
}
@@ -354,18 +370,18 @@ private ICciWriter GetWriter(TextWriter output, ISyntaxWriter syntaxWriter, bool
354370
ExcludeAttributesList,
355371
FollowTypeForwards);
356372

357-
switch (WriterType)
373+
switch (_writerType)
358374
{
359-
case WriterType.DocIds:
360-
return new DocumentIdWriter(output, filter, DocIdKinds);
361-
case WriterType.TypeForwards:
375+
case GenAPI.WriterType.DocIds:
376+
return new DocumentIdWriter(output, filter, _docIdKinds);
377+
case GenAPI.WriterType.TypeForwards:
362378
return new TypeForwardWriter(output, filter)
363379
{
364380
IncludeForwardedTypes = true
365381
};
366-
case WriterType.TypeList:
382+
case GenAPI.WriterType.TypeList:
367383
return new TypeListWriter(syntaxWriter, filter);
368-
case WriterType.CSDecl:
384+
case GenAPI.WriterType.CSDecl:
369385
default:
370386
{
371387
CSharpWriter writer = new(syntaxWriter, filter, ApiOnly);

0 commit comments

Comments
 (0)