From 8a11946054b8a44718a4c56d6ed797a9186a59ae Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Fri, 8 Nov 2019 22:52:13 +0100 Subject: [PATCH 1/3] extension methods --- src/PublicApiGenerator/ApiGenerator.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/PublicApiGenerator/ApiGenerator.cs b/src/PublicApiGenerator/ApiGenerator.cs index 8522beb..51e765d 100644 --- a/src/PublicApiGenerator/ApiGenerator.cs +++ b/src/PublicApiGenerator/ApiGenerator.cs @@ -18,10 +18,8 @@ namespace PublicApiGenerator { public static class ApiGenerator { - public static string GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? options = null) + 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); @@ -49,7 +47,13 @@ public static string GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? o } } - [Obsolete("Use `GeneratePublicApi(Assembly assembly, ApiGeneratorOptions? options = null)` instead. Will be removed in the next major.")] + 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 From 954491af204e53a4318aeb72f9b06b8337f493ce Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Fri, 8 Nov 2019 23:10:24 +0100 Subject: [PATCH 2/3] Some doco --- src/PublicApiGenerator/ApiGenerator.cs | 25 +++++++++++++++++++ .../ApiGeneratorTestsBase.cs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/PublicApiGenerator/ApiGenerator.cs b/src/PublicApiGenerator/ApiGenerator.cs index 51e765d..d20c62a 100644 --- a/src/PublicApiGenerator/ApiGenerator.cs +++ b/src/PublicApiGenerator/ApiGenerator.cs @@ -18,6 +18,12 @@ namespace PublicApiGenerator { public static class ApiGenerator { + /// + /// Generates a public API from the specified assembly. + /// + /// The assembly to generate an API from. + /// The options to control the API output. + /// The API output. public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptions? options = null) { options ??= new ApiGeneratorOptions(); @@ -47,6 +53,25 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio } } + /// + /// Generates a public API from the specified types. + /// + /// The types to generate an API from. + /// The options to control the API output. + /// This method assumes all the types belong to the same assembly. The assembly of the first type types[0].Assembly is used. + /// The API output. + public static string GeneratePublicApi(this Type[] types, ApiGeneratorOptions? options = null) + { + (options ??= new ApiGeneratorOptions()).IncludeTypes = types; + return types[0].Assembly.GeneratePublicApi(options); + } + + /// + /// Generates a public API from the specified type. + /// + /// The type to generate an API from. + /// The options to control the API output. + /// The API output. public static string GeneratePublicApi(this Type type, ApiGeneratorOptions? options = null) { (options ??= new ApiGeneratorOptions()).IncludeTypes = new Type[] { type }; diff --git a/src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs b/src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs index e1b3122..5f5ee90 100644 --- a/src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs +++ b/src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs @@ -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); From eac1954bf6399079543860c6262b79f75e044e59 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Fri, 8 Nov 2019 23:27:33 +0100 Subject: [PATCH 3/3] Return empty string when array empty --- src/PublicApiGenerator/ApiGenerator.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PublicApiGenerator/ApiGenerator.cs b/src/PublicApiGenerator/ApiGenerator.cs index d20c62a..f184fe4 100644 --- a/src/PublicApiGenerator/ApiGenerator.cs +++ b/src/PublicApiGenerator/ApiGenerator.cs @@ -62,6 +62,10 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio /// The API output. 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); }