Skip to content

Commit

Permalink
Merge pull request #134 from ApiApprover/new-method
Browse files Browse the repository at this point in the history
Extension methods as public API
  • Loading branch information
danielmarbach committed Nov 8, 2019
2 parents 3c67c62 + eac1954 commit bde14bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
41 changes: 37 additions & 4 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -18,10 +18,14 @@ namespace PublicApiGenerator
{
public static class ApiGenerator
{
public static string GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? options = null)
/// <summary>
/// Generates a public API from the specified assembly.
/// </summary>
/// <param name="assembly">The assembly to generate an API from.</param>
/// <param name="options">The options to control the API output.</param>
/// <returns>The API output.</returns>
public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptions? options = null)
{
if (assembly is null) throw new ArgumentNullException(nameof(assembly));

options ??= new ApiGeneratorOptions();

var attributeFilter = new AttributeFilter(options.ExcludeAttributes);
Expand Down Expand Up @@ -49,7 +53,36 @@ public static string GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? o
}
}

[Obsolete("Use `GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? options = null)` instead. Will be removed in the next major.")]
/// <summary>
/// Generates a public API from the specified types.
/// </summary>
/// <param name="types">The types to generate an API from.</param>
/// <param name="options">The options to control the API output.</param>
/// <remarks>This method assumes all the types belong to the same assembly. The assembly of the first type <code>types[0].Assembly</code> is used.</remarks>
/// <returns>The API output.</returns>
public static string GeneratePublicApi(this Type[] types, ApiGeneratorOptions? options = null)
{
if (types.Length == 0)
{
return string.Empty;
}
(options ??= new ApiGeneratorOptions()).IncludeTypes = types;
return types[0].Assembly.GeneratePublicApi(options);
}

/// <summary>
/// Generates a public API from the specified type.
/// </summary>
/// <param name="type">The type to generate an API from.</param>
/// <param name="options">The options to control the API output.</param>
/// <returns>The API output.</returns>
public static string GeneratePublicApi(this Type type, ApiGeneratorOptions? options = null)
{
(options ??= new ApiGeneratorOptions()).IncludeTypes = new Type[] { type };
return type.Assembly.GeneratePublicApi(options);
}

[Obsolete("Use `GeneratePublicApi(this Assembly assembly, ApiGeneratorOptions? options = null)` instead. Will be removed in the next major.")]
public static string GeneratePublicApi(Assembly assembly, Type[]? includeTypes = null, bool shouldIncludeAssemblyAttributes = true, string[]? whitelistedNamespacePrefixes = null, string[]? excludeAttributes = null)
{
var options = new ApiGeneratorOptions
Expand Down
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs
Expand Up @@ -32,7 +32,7 @@ private static void AssertPublicApi(Assembly assembly, string expectedOutput, A
{
options ??= new DefaultApiGeneratorOptions();

var actualOutput = ApiGenerator.GeneratePublicApi(assembly, options);
var actualOutput = assembly.GeneratePublicApi(options);
actualOutput = StripEmptyLines.Replace(actualOutput, string.Empty);
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
Expand Down

0 comments on commit bde14bb

Please sign in to comment.