Skip to content

Commit

Permalink
Add BracingStyle and IndentString to options (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Dec 3, 2023
1 parent 4f41526 commit e777c1c
Show file tree
Hide file tree
Showing 24 changed files with 162 additions and 143 deletions.
23 changes: 10 additions & 13 deletions src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio
{
options ??= new ApiGeneratorOptions();

var attributeFilter = new AttributeFilter(options.ExcludeAttributes);

using (var assemblyResolver = new DefaultAssemblyResolver())
{
var assemblyPath = assembly.Location;
Expand All @@ -43,14 +41,11 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio
{
return CreatePublicApiForAssembly(
asm,
options,
typeDefinition => !typeDefinition.IsNested &&
ShouldIncludeType(typeDefinition, options.DenyNamespacePrefixes, options.AllowNamespacePrefixes, options.UseDenyNamespacePrefixesForExtensionMethods) &&
(options.IncludeTypes == null || options.IncludeTypes.Any(type => type.FullName == typeDefinition.FullName && type.Assembly.FullName == typeDefinition.Module.Assembly.FullName)),
options.IncludeAssemblyAttributes,
options.DenyNamespacePrefixes,
options.AllowNamespacePrefixes,
options.UseDenyNamespacePrefixesForExtensionMethods,
attributeFilter);
(options.IncludeTypes == null || options.IncludeTypes.Any(type => type.FullName == typeDefinition.FullName && type.Assembly.FullName == typeDefinition.Module.Assembly.FullName))
);
}
}
}
Expand Down Expand Up @@ -86,12 +81,14 @@ public static string GeneratePublicApi(this Type type, ApiGeneratorOptions? opti

// TODO: Assembly references?
// TODO: Better handle namespaces - using statements? - requires non-qualified type names
private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Func<TypeDefinition, bool> shouldIncludeType, bool shouldIncludeAssemblyAttributes, string[] denyNamespacePrefixes, string[] allowNamespacePrefixes, bool useDenyNamespacePrefixesForExtensionMethods, AttributeFilter attributeFilter)
private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, ApiGeneratorOptions options, Func<TypeDefinition, bool> shouldIncludeType)
{
var attributeFilter = new AttributeFilter(options.ExcludeAttributes);

using var provider = new CSharpCodeProvider();

var compileUnit = new CodeCompileUnit();
if (shouldIncludeAssemblyAttributes && assembly.HasCustomAttributes)
if (options.IncludeAssemblyAttributes && assembly.HasCustomAttributes)
{
PopulateCustomAttributes(assembly, compileUnit.AssemblyCustomAttributes, attributeFilter);
}
Expand All @@ -110,7 +107,7 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu

using (NullableContext.Push(publicType))
{
var typeDeclaration = CreateTypeDeclaration(publicType, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods, attributeFilter);
var typeDeclaration = CreateTypeDeclaration(publicType, options.DenyNamespacePrefixes, options.AllowNamespacePrefixes, options.UseDenyNamespacePrefixesForExtensionMethods, attributeFilter);
@namespace.Types.Add(typeDeclaration);
}
}
Expand All @@ -119,10 +116,10 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu
{
var cgo = new CodeGeneratorOptions
{
BracingStyle = "C",
BracingStyle = options.BracingStyle,
BlankLinesBetweenMembers = false,
VerbatimOrder = false,
IndentString = " "
IndentString = options.IndentString
};

provider.GenerateCodeFromCompileUnit(compileUnit, writer, cgo);
Expand Down
10 changes: 10 additions & 0 deletions src/PublicApiGenerator/ApiGeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ public class ApiGeneratorOptions
private static readonly string[] _defaultAllowNamespacePrefixes = [];

private static readonly string[] _defaultDenyNamespacePrefixes = ["System", "Microsoft"];

/// <summary>
/// Indentation string. Defaults to 4 whitespace.
/// </summary>
public string IndentString { get; set; } = " ";

/// <summary>
/// Style for braces. Available values: C, Block. Defaults to C.
/// </summary>
public string BracingStyle { get; set; } = "C";
}
37 changes: 25 additions & 12 deletions src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,47 @@ public abstract class ApiGeneratorTestsBase
{
private static readonly Regex StripEmptyLines = new(@"^\s+$[\r\n]*", RegexOptions.Multiline | RegexOptions.Compiled);

protected void AssertPublicApi<T>(string expectedOutput, ApiGeneratorOptions? options = null)
protected void AssertPublicApi<T>(string expectedOutput) => AssertPublicApi<T>(expectedOutput, _ => { });

protected void AssertPublicApi<T>(string expectedOutput, Action<ApiGeneratorOptions> configure)
{
AssertPublicApi(typeof(T), expectedOutput, options);
AssertPublicApi(typeof(T), expectedOutput, configure);
}

protected void AssertPublicApi(Type type, string expectedOutput, ApiGeneratorOptions? options = null)
protected void AssertPublicApi(Type type, string expectedOutput)
{
AssertPublicApi([type], expectedOutput, options);
AssertPublicApi(type, expectedOutput, _ => { });
}

protected void AssertPublicApi(Type[] types, string expectedOutput, ApiGeneratorOptions? options = null)
protected void AssertPublicApi(Type type, string expectedOutput, Action<ApiGeneratorOptions> configure)
{
options ??= new DefaultApiGeneratorOptions();
options.IncludeTypes = types;
AssertPublicApi([type], expectedOutput, configure);
}

AssertPublicApi(types[0].Assembly, expectedOutput, options);
protected void AssertPublicApi(Type[] types, string expectedOutput)
{
AssertPublicApi(types, expectedOutput, _ => { });
}

private static void AssertPublicApi(Assembly assembly, string expectedOutput, ApiGeneratorOptions? options = null)
protected void AssertPublicApi(Type[] types, string expectedOutput, Action<ApiGeneratorOptions> configure)
{
options ??= new DefaultApiGeneratorOptions();
var options = new ApiGeneratorOptions
{
IncludeAssemblyAttributes = false,
IncludeTypes = types
};
configure(options);

AssertPublicApi(types[0].Assembly, expectedOutput, options);
}

private static void AssertPublicApi(Assembly assembly, string expectedOutput, ApiGeneratorOptions options)
{
string actualOutput = assembly.GeneratePublicApi(options);
actualOutput = StripEmptyLines.Replace(actualOutput, string.Empty);
try
{
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true);
}
catch
{
Expand Down
3 changes: 1 addition & 2 deletions src/PublicApiGeneratorTests/Assembly_attributes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using PublicApiGenerator;
using PublicApiGeneratorTests.Examples;

[assembly: Guid("3B8D506A-5247-47FF-B053-D29A51A97C33")]
Expand Down Expand Up @@ -54,7 +53,7 @@ public class NotImportant
}}
}}";

AssertPublicApi<NotImportant>(api, new ApiGeneratorOptions { IncludeAssemblyAttributes = true });
AssertPublicApi<NotImportant>(api, opt => opt.IncludeAssemblyAttributes = true);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Class_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ public class ClassWithInternalAttribute
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM"]
};

AssertPublicApi<ClassWithMultipleAttributes>(
@"namespace PublicApiGeneratorTests.Examples
{
Expand All @@ -304,7 +299,7 @@ public class ClassWithMultipleAttributes
{
public ClassWithMultipleAttributes() { }
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM"]);
}

[Fact]
Expand Down
9 changes: 2 additions & 7 deletions src/PublicApiGeneratorTests/Class_event_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public class ClassWithEventWithAttribute
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
};

AssertPublicApi<ClassWithEventWithAttribute>(
@"namespace PublicApiGeneratorTests.Examples
{
Expand All @@ -35,15 +30,15 @@ public class ClassWithEventWithAttribute
public ClassWithEventWithAttribute() { }
public event System.EventHandler OnClicked;
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
}
}

namespace Examples
{
public class ClassWithEventWithAttribute
{
[SimpleAttribute]
[Simple]
public event EventHandler OnClicked;
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/PublicApiGeneratorTests/DefaultApiGeneratorOptions.cs

This file was deleted.

9 changes: 2 additions & 7 deletions src/PublicApiGeneratorTests/Delegate_attributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;

namespace PublicApiGeneratorTests
{
Expand Down Expand Up @@ -214,18 +214,13 @@ public void Should_output_attribute_for_parameter()
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_ZZ"]
};

AssertPublicApi<DelegateWithMultipleAttributes>(
@"namespace PublicApiGeneratorTests.Examples
{
[PublicApiGeneratorTests.Examples.Attribute_AA]
[PublicApiGeneratorTests.Examples.Attribute_MM]
public delegate void DelegateWithMultipleAttributes();
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_ZZ"]);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Field_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ public class FieldWithMultipleAttributes
[Fact]
public void Should_skip_excluded_attributes()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM", "PublicApiGeneratorTests.Examples.Attribute_ZZ"]
};

AssertPublicApi<FieldWithMultipleAttributes>(
@"namespace PublicApiGeneratorTests.Examples
{
Expand All @@ -174,7 +169,7 @@ public class FieldWithMultipleAttributes
public string Value;
public FieldWithMultipleAttributes() { }
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM", "PublicApiGeneratorTests.Examples.Attribute_ZZ"]);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Interface_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,11 @@ public interface IInterfaceWithAttributeWithStringArrayInitialiser { }
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
};

AssertPublicApi<IInterfaceWithSimpleAttribute>(
@"namespace PublicApiGeneratorTests.Examples
{
public interface IInterfaceWithSimpleAttribute { }
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Interface_event_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ public interface IInterfaceWithEventWithAttribute
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
};

AssertPublicApi<IInterfaceWithEventWithAttribute>(
@"namespace PublicApiGeneratorTests.Examples
{
public interface IInterfaceWithEventWithAttribute
{
event System.EventHandler OnClicked;
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Interface_method_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ public interface IMethodWithMultipleAttributes
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
};

AssertPublicApi<IMethodsWithAttributeWithNamedParameters>(
@"namespace PublicApiGeneratorTests.Examples
{
Expand All @@ -144,7 +139,7 @@ public interface IMethodsWithAttributeWithNamedParameters
void Method1();
void Method2();
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,14 @@ public interface IMethodWithAttributesOnMethodAndReturnValue
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
};

AssertPublicApi<IMethodReturnValueWithAttributeWithMultipleNamedParameters>(
@"namespace PublicApiGeneratorTests.Examples
{
public interface IMethodReturnValueWithAttributeWithMultipleNamedParameters
{
void Method();
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/PublicApiGeneratorTests/Method_attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ public class MethodWithCompilerAttributes
[Fact]
public void Should_skip_excluded_attribute()
{
var options = new DefaultApiGeneratorOptions
{
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
};

AssertPublicApi<MethodWithAttributeWithMultipleNamedParameters>(
@"namespace PublicApiGeneratorTests.Examples
{
Expand All @@ -171,7 +166,7 @@ public class MethodWithAttributeWithMultipleNamedParameters
public MethodWithAttributeWithMultipleNamedParameters() { }
public void Method() { }
}
}", options);
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/Method_extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class StringExtensionsInSystemNamespace
{
public static bool CheckLength(this string value, int length) { }
}
}", new PublicApiGenerator.ApiGeneratorOptions { UseDenyNamespacePrefixesForExtensionMethods = false, IncludeAssemblyAttributes = false });
}", opt => { opt.UseDenyNamespacePrefixesForExtensionMethods = false; opt.IncludeAssemblyAttributes = false; });
}

[Fact]
Expand Down

0 comments on commit e777c1c

Please sign in to comment.