Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public DocIdExcludeListFilter(string excludeListFilePath, bool excludeMembers)
_excludeMembers = excludeMembers;
}

public bool IncludeForwardedTypes { get; set; }

public bool Include(INamespaceDefinition ns)
{
// Only include non-empty namespaces
if (!ns.GetTypes().Any(Include))
if (!ns.GetTypes(IncludeForwardedTypes).Any(Include))
return false;

string namespaceId = ns.DocId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public DocIdIncludeListFilter(string includeListFilePath)

public bool AlwaysIncludeNonEmptyTypes { get; set; }

public bool IncludeForwardedTypes { get; set; }

public bool Include(INamespaceDefinition ns)
{
// Only include non-empty namespaces
if (!ns.GetTypes().Any(Include))
if (!ns.GetTypes(IncludeForwardedTypes).Any(Include))
return false;

string namespaceId = ns.DocId();
Expand Down
47 changes: 45 additions & 2 deletions src/Microsoft.Cci.Extensions/Writers/CSharp/CSharpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CSharpWriter : SimpleTypeMemberTraverser, ICciWriter, IDisposable
private readonly bool _apiOnly;
private readonly ICciFilter _cciFilter;
private bool _firstMemberGroup;
private bool _includeOnlyConditionalTypeListTypes;

public CSharpWriter(ISyntaxWriter writer, ICciFilter filter, bool apiOnly, bool writeAssemblyAttributes = false)
: base(filter)
Expand All @@ -48,6 +49,12 @@ public CSharpWriter(ISyntaxWriter writer, ICciFilter filter, bool apiOnly, bool

public bool PutBraceOnNewLine { get; set; }

public ICciFilter ConditionalTypeListFilter { get; set; }

public string ConditionalTypeListSymbol { get; set; }

public bool ConditionalTypeListWrapOtherTypes { get; set; }

public bool IncludeGlobalPrefixForCompilation
{
get { return _declarationWriter.ForCompilationIncludeGlobalPrefix; }
Expand Down Expand Up @@ -87,7 +94,35 @@ public override void Visit(IAssembly assembly)
_declarationWriter.WriteDeclaration(assembly);
}

base.Visit(assembly);
var namespaces = assembly.GetAllNamespaces();
if (ConditionalTypeListFilter == null)
{
Visit(namespaces);
}
else
{
if (ConditionalTypeListWrapOtherTypes)
{
_syntaxWriter.Write($"#if {ConditionalTypeListSymbol}");
_syntaxWriter.WriteLine();
}

// first pass
Visit(namespaces);

_syntaxWriter.Write(ConditionalTypeListWrapOtherTypes ? $"#endif // {ConditionalTypeListSymbol}" : $"#if {ConditionalTypeListSymbol}");
_syntaxWriter.WriteLine();

// second pass
_includeOnlyConditionalTypeListTypes = true;
Visit(namespaces.Where(ConditionalTypeListFilter.Include));

if (!ConditionalTypeListWrapOtherTypes)
{
_syntaxWriter.Write($"#endif // {ConditionalTypeListSymbol}");
_syntaxWriter.WriteLine();
}
}
}

public override void Visit(INamespaceDefinition ns)
Expand All @@ -102,7 +137,15 @@ public override void Visit(INamespaceDefinition ns)

using (_syntaxWriter.StartBraceBlock(PutBraceOnNewLine))
{
base.Visit(ns);
var types = ns.GetTypes(this.IncludeForwardedTypes);
if (ConditionalTypeListFilter != null)
{
// in the first pass we want all types *except* the ones in ConditionalTypeListFilter
// in the second pass we want *only* the types in ConditionalTypeListFilter
types = types.Where(t => ConditionalTypeListFilter.Include(t) == _includeOnlyConditionalTypeListTypes);
}

Visit(types);
}
}

Expand Down
27 changes: 25 additions & 2 deletions src/Microsoft.DotNet.GenAPI/GenAPITask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,30 @@ public string DocIdKinds
/// </summary>
public bool AlwaysIncludeBase { get; set; }

/// <summary>
/// [CSDecl] Specify a namespace+type list in the DocId format of types that should be wrapped inside an #if <see cref="ConditionalTypeListSymbol"/> (depending on <see cref="ConditionalTypeListWrapOtherTypes"/>).
/// </summary>
public string ConditionalTypeList { get; set; }

/// <summary>
/// [CSDecl] Symbol that will be used in the #if for <see cref="ConditionalTypeList"/>.
/// </summary>
public string ConditionalTypeListSymbol { get; set; }

/// <summary>
/// [CSDecl] If true, wraps the types _not_ mentioned in <see cref="ConditionalTypeListSymbol"/> with <see cref="ConditionalTypeListSymbol"/>.
/// If false, every type in the list will be wrapped.
/// Defaults to false.
/// </summary>
public bool ConditionalTypeListWrapOtherTypes { get; set; }

/// <summary>
/// Exclude members when return value or parameter types are excluded.
/// </summary>
public bool ExcludeMembers { get; set; }

/// <summary>
/// Language Version to target.
/// [CSDecl] Language Version to target.
/// </summary>
public string LangVersion { get; set; }

Expand Down Expand Up @@ -333,7 +350,7 @@ private static ICciFilter GetFilter(

if (!string.IsNullOrWhiteSpace(excludeApiList))
{
includeFilter = new IntersectionFilter(includeFilter, new DocIdExcludeListFilter(excludeApiList, excludeMembers));
includeFilter = new IntersectionFilter(includeFilter, new DocIdExcludeListFilter(excludeApiList, excludeMembers) { IncludeForwardedTypes = includeForwardedTypes });
}

if (!string.IsNullOrWhiteSpace(excludeAttributesList))
Expand Down Expand Up @@ -394,6 +411,12 @@ private ICciWriter GetWriter(TextWriter output, ISyntaxWriter syntaxWriter, bool
writer.AlwaysIncludeBase = AlwaysIncludeBase;
writer.LangVersion = GetLangVersion(LangVersion);
writer.IncludeForwardedTypes = FollowTypeForwards;
if (!string.IsNullOrWhiteSpace(ConditionalTypeList))
{
writer.ConditionalTypeListFilter = new DocIdIncludeListFilter(ConditionalTypeList) { IncludeForwardedTypes = FollowTypeForwards };
writer.ConditionalTypeListSymbol = ConditionalTypeListSymbol;
writer.ConditionalTypeListWrapOtherTypes = ConditionalTypeListWrapOtherTypes;
}
return writer;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
Assembly="$(GenAPIInputAssembly)"
LibPath="$(GenAPILibPath)"
ApiList="$(GenAPIApiList)"
ConditionalTypeList="$(GenAPIConditionalTypeList)"
ConditionalTypeListSymbol="$(GenAPIConditionalTypeListSymbol)"
ConditionalTypeListWrapOtherTypes="$(GenAPIConditionalTypeListWrapOtherTypes)"
OutputPath="$(GenAPITargetPath)"
HeaderFile="$(GenAPIHeaderFile)"
WriterType="$(GenAPIWriterType)"
Expand Down