Skip to content

Commit e777c1c

Browse files
authored
Add BracingStyle and IndentString to options (#293)
1 parent 4f41526 commit e777c1c

24 files changed

+162
-143
lines changed

src/PublicApiGenerator/ApiGenerator.cs

+10-13
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio
2626
{
2727
options ??= new ApiGeneratorOptions();
2828

29-
var attributeFilter = new AttributeFilter(options.ExcludeAttributes);
30-
3129
using (var assemblyResolver = new DefaultAssemblyResolver())
3230
{
3331
var assemblyPath = assembly.Location;
@@ -43,14 +41,11 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio
4341
{
4442
return CreatePublicApiForAssembly(
4543
asm,
44+
options,
4645
typeDefinition => !typeDefinition.IsNested &&
4746
ShouldIncludeType(typeDefinition, options.DenyNamespacePrefixes, options.AllowNamespacePrefixes, options.UseDenyNamespacePrefixesForExtensionMethods) &&
48-
(options.IncludeTypes == null || options.IncludeTypes.Any(type => type.FullName == typeDefinition.FullName && type.Assembly.FullName == typeDefinition.Module.Assembly.FullName)),
49-
options.IncludeAssemblyAttributes,
50-
options.DenyNamespacePrefixes,
51-
options.AllowNamespacePrefixes,
52-
options.UseDenyNamespacePrefixesForExtensionMethods,
53-
attributeFilter);
47+
(options.IncludeTypes == null || options.IncludeTypes.Any(type => type.FullName == typeDefinition.FullName && type.Assembly.FullName == typeDefinition.Module.Assembly.FullName))
48+
);
5449
}
5550
}
5651
}
@@ -86,12 +81,14 @@ public static string GeneratePublicApi(this Type type, ApiGeneratorOptions? opti
8681

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

9390
var compileUnit = new CodeCompileUnit();
94-
if (shouldIncludeAssemblyAttributes && assembly.HasCustomAttributes)
91+
if (options.IncludeAssemblyAttributes && assembly.HasCustomAttributes)
9592
{
9693
PopulateCustomAttributes(assembly, compileUnit.AssemblyCustomAttributes, attributeFilter);
9794
}
@@ -110,7 +107,7 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu
110107

111108
using (NullableContext.Push(publicType))
112109
{
113-
var typeDeclaration = CreateTypeDeclaration(publicType, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods, attributeFilter);
110+
var typeDeclaration = CreateTypeDeclaration(publicType, options.DenyNamespacePrefixes, options.AllowNamespacePrefixes, options.UseDenyNamespacePrefixesForExtensionMethods, attributeFilter);
114111
@namespace.Types.Add(typeDeclaration);
115112
}
116113
}
@@ -119,10 +116,10 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu
119116
{
120117
var cgo = new CodeGeneratorOptions
121118
{
122-
BracingStyle = "C",
119+
BracingStyle = options.BracingStyle,
123120
BlankLinesBetweenMembers = false,
124121
VerbatimOrder = false,
125-
IndentString = " "
122+
IndentString = options.IndentString
126123
};
127124

128125
provider.GenerateCodeFromCompileUnit(compileUnit, writer, cgo);

src/PublicApiGenerator/ApiGeneratorOptions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ public class ApiGeneratorOptions
7171
private static readonly string[] _defaultAllowNamespacePrefixes = [];
7272

7373
private static readonly string[] _defaultDenyNamespacePrefixes = ["System", "Microsoft"];
74+
75+
/// <summary>
76+
/// Indentation string. Defaults to 4 whitespace.
77+
/// </summary>
78+
public string IndentString { get; set; } = " ";
79+
80+
/// <summary>
81+
/// Style for braces. Available values: C, Block. Defaults to C.
82+
/// </summary>
83+
public string BracingStyle { get; set; } = "C";
7484
}

src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs

+25-12
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,47 @@ public abstract class ApiGeneratorTestsBase
88
{
99
private static readonly Regex StripEmptyLines = new(@"^\s+$[\r\n]*", RegexOptions.Multiline | RegexOptions.Compiled);
1010

11-
protected void AssertPublicApi<T>(string expectedOutput, ApiGeneratorOptions? options = null)
11+
protected void AssertPublicApi<T>(string expectedOutput) => AssertPublicApi<T>(expectedOutput, _ => { });
12+
13+
protected void AssertPublicApi<T>(string expectedOutput, Action<ApiGeneratorOptions> configure)
1214
{
13-
AssertPublicApi(typeof(T), expectedOutput, options);
15+
AssertPublicApi(typeof(T), expectedOutput, configure);
1416
}
1517

16-
protected void AssertPublicApi(Type type, string expectedOutput, ApiGeneratorOptions? options = null)
18+
protected void AssertPublicApi(Type type, string expectedOutput)
1719
{
18-
AssertPublicApi([type], expectedOutput, options);
20+
AssertPublicApi(type, expectedOutput, _ => { });
1921
}
2022

21-
protected void AssertPublicApi(Type[] types, string expectedOutput, ApiGeneratorOptions? options = null)
23+
protected void AssertPublicApi(Type type, string expectedOutput, Action<ApiGeneratorOptions> configure)
2224
{
23-
options ??= new DefaultApiGeneratorOptions();
24-
options.IncludeTypes = types;
25+
AssertPublicApi([type], expectedOutput, configure);
26+
}
2527

26-
AssertPublicApi(types[0].Assembly, expectedOutput, options);
28+
protected void AssertPublicApi(Type[] types, string expectedOutput)
29+
{
30+
AssertPublicApi(types, expectedOutput, _ => { });
2731
}
2832

29-
private static void AssertPublicApi(Assembly assembly, string expectedOutput, ApiGeneratorOptions? options = null)
33+
protected void AssertPublicApi(Type[] types, string expectedOutput, Action<ApiGeneratorOptions> configure)
3034
{
31-
options ??= new DefaultApiGeneratorOptions();
35+
var options = new ApiGeneratorOptions
36+
{
37+
IncludeAssemblyAttributes = false,
38+
IncludeTypes = types
39+
};
40+
configure(options);
3241

42+
AssertPublicApi(types[0].Assembly, expectedOutput, options);
43+
}
44+
45+
private static void AssertPublicApi(Assembly assembly, string expectedOutput, ApiGeneratorOptions options)
46+
{
3347
string actualOutput = assembly.GeneratePublicApi(options);
3448
actualOutput = StripEmptyLines.Replace(actualOutput, string.Empty);
3549
try
3650
{
37-
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true,
38-
ignoreWhiteSpaceDifferences: true);
51+
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true);
3952
}
4053
catch
4154
{

src/PublicApiGeneratorTests/Assembly_attributes.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Runtime.InteropServices;
2-
using PublicApiGenerator;
32
using PublicApiGeneratorTests.Examples;
43

54
[assembly: Guid("3B8D506A-5247-47FF-B053-D29A51A97C33")]
@@ -54,7 +53,7 @@ public NotImportant() {{ }}
5453
}}
5554
}}";
5655

57-
AssertPublicApi<NotImportant>(api, new ApiGeneratorOptions { IncludeAssemblyAttributes = true });
56+
AssertPublicApi<NotImportant>(api, opt => opt.IncludeAssemblyAttributes = true);
5857
}
5958
}
6059

src/PublicApiGeneratorTests/Class_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,6 @@ public ClassWithInternalAttribute() { }
290290
[Fact]
291291
public void Should_skip_excluded_attribute()
292292
{
293-
var options = new DefaultApiGeneratorOptions
294-
{
295-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM"]
296-
};
297-
298293
AssertPublicApi<ClassWithMultipleAttributes>(
299294
@"namespace PublicApiGeneratorTests.Examples
300295
{
@@ -304,7 +299,7 @@ public class ClassWithMultipleAttributes
304299
{
305300
public ClassWithMultipleAttributes() { }
306301
}
307-
}", options);
302+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM"]);
308303
}
309304

310305
[Fact]

src/PublicApiGeneratorTests/Class_event_attributes.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public ClassWithEventWithAttribute() { }
2222
[Fact]
2323
public void Should_skip_excluded_attribute()
2424
{
25-
var options = new DefaultApiGeneratorOptions
26-
{
27-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
28-
};
29-
3025
AssertPublicApi<ClassWithEventWithAttribute>(
3126
@"namespace PublicApiGeneratorTests.Examples
3227
{
@@ -35,15 +30,15 @@ public class ClassWithEventWithAttribute
3530
public ClassWithEventWithAttribute() { }
3631
public event System.EventHandler OnClicked;
3732
}
38-
}", options);
33+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
3934
}
4035
}
4136

4237
namespace Examples
4338
{
4439
public class ClassWithEventWithAttribute
4540
{
46-
[SimpleAttribute]
41+
[Simple]
4742
public event EventHandler OnClicked;
4843
}
4944
}

src/PublicApiGeneratorTests/DefaultApiGeneratorOptions.cs

-11
This file was deleted.

src/PublicApiGeneratorTests/Delegate_attributes.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using PublicApiGeneratorTests.Examples;
1+
using PublicApiGeneratorTests.Examples;
22

33
namespace PublicApiGeneratorTests
44
{
@@ -214,18 +214,13 @@ public void Should_output_attribute_for_parameter()
214214
[Fact]
215215
public void Should_skip_excluded_attribute()
216216
{
217-
var options = new DefaultApiGeneratorOptions
218-
{
219-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_ZZ"]
220-
};
221-
222217
AssertPublicApi<DelegateWithMultipleAttributes>(
223218
@"namespace PublicApiGeneratorTests.Examples
224219
{
225220
[PublicApiGeneratorTests.Examples.Attribute_AA]
226221
[PublicApiGeneratorTests.Examples.Attribute_MM]
227222
public delegate void DelegateWithMultipleAttributes();
228-
}", options);
223+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_ZZ"]);
229224
}
230225
}
231226

src/PublicApiGeneratorTests/Field_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ public FieldWithMultipleAttributes() { }
160160
[Fact]
161161
public void Should_skip_excluded_attributes()
162162
{
163-
var options = new DefaultApiGeneratorOptions
164-
{
165-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM", "PublicApiGeneratorTests.Examples.Attribute_ZZ"]
166-
};
167-
168163
AssertPublicApi<FieldWithMultipleAttributes>(
169164
@"namespace PublicApiGeneratorTests.Examples
170165
{
@@ -174,7 +169,7 @@ public class FieldWithMultipleAttributes
174169
public string Value;
175170
public FieldWithMultipleAttributes() { }
176171
}
177-
}", options);
172+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.Attribute_MM", "PublicApiGeneratorTests.Examples.Attribute_ZZ"]);
178173
}
179174
}
180175

src/PublicApiGeneratorTests/Interface_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,11 @@ public interface IInterfaceWithAttributeWithStringArrayInitialiser { }
153153
[Fact]
154154
public void Should_skip_excluded_attribute()
155155
{
156-
var options = new DefaultApiGeneratorOptions
157-
{
158-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
159-
};
160-
161156
AssertPublicApi<IInterfaceWithSimpleAttribute>(
162157
@"namespace PublicApiGeneratorTests.Examples
163158
{
164159
public interface IInterfaceWithSimpleAttribute { }
165-
}", options);
160+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
166161
}
167162
}
168163

src/PublicApiGeneratorTests/Interface_event_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ public interface IInterfaceWithEventWithAttribute
2121
[Fact]
2222
public void Should_skip_excluded_attribute()
2323
{
24-
var options = new DefaultApiGeneratorOptions
25-
{
26-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]
27-
};
28-
2924
AssertPublicApi<IInterfaceWithEventWithAttribute>(
3025
@"namespace PublicApiGeneratorTests.Examples
3126
{
3227
public interface IInterfaceWithEventWithAttribute
3328
{
3429
event System.EventHandler OnClicked;
3530
}
36-
}", options);
31+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.SimpleAttribute"]);
3732
}
3833
}
3934

src/PublicApiGeneratorTests/Interface_method_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ public interface IMethodWithMultipleAttributes
131131
[Fact]
132132
public void Should_skip_excluded_attribute()
133133
{
134-
var options = new DefaultApiGeneratorOptions
135-
{
136-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
137-
};
138-
139134
AssertPublicApi<IMethodsWithAttributeWithNamedParameters>(
140135
@"namespace PublicApiGeneratorTests.Examples
141136
{
@@ -144,7 +139,7 @@ public interface IMethodsWithAttributeWithNamedParameters
144139
void Method1();
145140
void Method2();
146141
}
147-
}", options);
142+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
148143
}
149144
}
150145

src/PublicApiGeneratorTests/Interface_method_return_value_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,14 @@ public interface IMethodWithAttributesOnMethodAndReturnValue
128128
[Fact]
129129
public void Should_skip_excluded_attribute()
130130
{
131-
var options = new DefaultApiGeneratorOptions
132-
{
133-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
134-
};
135-
136131
AssertPublicApi<IMethodReturnValueWithAttributeWithMultipleNamedParameters>(
137132
@"namespace PublicApiGeneratorTests.Examples
138133
{
139134
public interface IMethodReturnValueWithAttributeWithMultipleNamedParameters
140135
{
141136
void Method();
142137
}
143-
}", options);
138+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
144139
}
145140
}
146141

src/PublicApiGeneratorTests/Method_attributes.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ public void Method() { }
158158
[Fact]
159159
public void Should_skip_excluded_attribute()
160160
{
161-
var options = new DefaultApiGeneratorOptions
162-
{
163-
ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]
164-
};
165-
166161
AssertPublicApi<MethodWithAttributeWithMultipleNamedParameters>(
167162
@"namespace PublicApiGeneratorTests.Examples
168163
{
@@ -171,7 +166,7 @@ public class MethodWithAttributeWithMultipleNamedParameters
171166
public MethodWithAttributeWithMultipleNamedParameters() { }
172167
public void Method() { }
173168
}
174-
}", options);
169+
}", opt => opt.ExcludeAttributes = ["PublicApiGeneratorTests.Examples.AttributeWithNamedParameterAttribute"]);
175170
}
176171
}
177172

src/PublicApiGeneratorTests/Method_extensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class StringExtensionsInSystemNamespace
2828
{
2929
public static bool CheckLength(this string value, int length) { }
3030
}
31-
}", new PublicApiGenerator.ApiGeneratorOptions { UseDenyNamespacePrefixesForExtensionMethods = false, IncludeAssemblyAttributes = false });
31+
}", opt => { opt.UseDenyNamespacePrefixesForExtensionMethods = false; opt.IncludeAssemblyAttributes = false; });
3232
}
3333

3434
[Fact]

0 commit comments

Comments
 (0)