Skip to content

Commit

Permalink
Remove obsolete APIs (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Mar 6, 2023
1 parent d877e4e commit bedff8b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 91 deletions.
1 change: 0 additions & 1 deletion src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<ItemGroup Condition="'$(IsPackable)' == 'true'">
<None Include="..\..\assets\icon.png" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="All" />
</ItemGroup>
Expand Down
31 changes: 6 additions & 25 deletions src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,9 @@ public static string GeneratePublicApi(this Type type, ApiGeneratorOptions? opti
return type.Assembly.GeneratePublicApi(options);
}

/// <summary>
/// Generates a public API from the specified assembly and provided options.
/// </summary>
[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
{
IncludeTypes = includeTypes,
IncludeAssemblyAttributes = shouldIncludeAssemblyAttributes,
ExcludeAttributes = excludeAttributes,
};
if (whitelistedNamespacePrefixes != null)
{
options.WhitelistedNamespacePrefixes = whitelistedNamespacePrefixes;
}
return GeneratePublicApi(assembly, options);
}

// 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[] blacklistedNamespacePrefixes, string[] whitelistedNamespacePrefixes, bool useBlacklistedNamespacePrefixesForExtensionMethods, AttributeFilter attributeFilter)
private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Func<TypeDefinition, bool> shouldIncludeType, bool shouldIncludeAssemblyAttributes, string[] denyNamespacePrefixes, string[] allowNamespacePrefixes, bool useDenyNamespacePrefixesForExtensionMethods, AttributeFilter attributeFilter)
{
using var provider = new CSharpCodeProvider();

Expand All @@ -130,7 +111,7 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu

using (NullableContext.Push(publicType))
{
var typeDeclaration = CreateTypeDeclaration(publicType, blacklistedNamespacePrefixes, whitelistedNamespacePrefixes, useBlacklistedNamespacePrefixesForExtensionMethods, attributeFilter);
var typeDeclaration = CreateTypeDeclaration(publicType, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods, attributeFilter);
@namespace.Types.Add(typeDeclaration);
}
}
Expand Down Expand Up @@ -234,7 +215,7 @@ private static bool ShouldIncludeMember(MemberAttributes memberAttributes)
}
}

private static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, string[] blacklistedNamespacePrefixes, string[] whitelistedNamespacePrefixes, bool useBlacklistedNamespacePrefixesForExtensionMethods, AttributeFilter attributeFilter)
private static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, string[] denyNamespacePrefixes, string[] allowNamespacePrefixes, bool useDenyNamespacePrefixesForExtensionMethods, AttributeFilter attributeFilter)
{
if (publicType.IsDelegate())
return CreateDelegateDeclaration(publicType, attributeFilter);
Expand Down Expand Up @@ -319,7 +300,7 @@ private static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicTy
.Select(t => t.Reference))
declaration.BaseTypes.Add(@interface.InterfaceType.CreateCodeTypeReference(@interface));

foreach (var memberInfo in publicType.GetMembers().Where(memberDefinition => ShouldIncludeMember(memberDefinition, blacklistedNamespacePrefixes, whitelistedNamespacePrefixes, useBlacklistedNamespacePrefixesForExtensionMethods)).OrderBy(m => m.Name, StringComparer.Ordinal))
foreach (var memberInfo in publicType.GetMembers().Where(memberDefinition => ShouldIncludeMember(memberDefinition, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods)).OrderBy(m => m.Name, StringComparer.Ordinal))
AddMemberToTypeDeclaration(declaration, publicType, memberInfo, attributeFilter);

// Fields should be in defined order for an enum
Expand All @@ -329,11 +310,11 @@ private static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicTy
foreach (var field in fields)
AddMemberToTypeDeclaration(declaration, publicType, field, attributeFilter);

foreach (var nestedType in publicType.NestedTypes.Where(t => ShouldIncludeType(t, blacklistedNamespacePrefixes, whitelistedNamespacePrefixes, useBlacklistedNamespacePrefixesForExtensionMethods)).OrderBy(t => t.FullName, StringComparer.Ordinal))
foreach (var nestedType in publicType.NestedTypes.Where(t => ShouldIncludeType(t, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods)).OrderBy(t => t.FullName, StringComparer.Ordinal))
{
using (NullableContext.Push(nestedType))
{
var nestedTypeDeclaration = CreateTypeDeclaration(nestedType, blacklistedNamespacePrefixes, whitelistedNamespacePrefixes, useBlacklistedNamespacePrefixesForExtensionMethods, attributeFilter);
var nestedTypeDeclaration = CreateTypeDeclaration(nestedType, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods, attributeFilter);
declaration.Members.Add(nestedTypeDeclaration);
}
}
Expand Down
53 changes: 0 additions & 53 deletions src/PublicApiGenerator/ApiGeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@ public class ApiGeneratorOptions
/// <remarks>Defaults to <see langword="true"/>.</remarks>
public bool IncludeAssemblyAttributes { get; set; } = true;

/// <summary>
/// Allows to print APIs in certain namespace prefixes.
/// For example by default types found in Microsoft or System namespaces are not treated as part of the public API.
/// This option has priority over <see cref="DenyNamespacePrefixes"/>.
/// </summary>
/// <example>
/// <code>
/// var options = new DefaultApiGeneratorOptions
/// {
/// WhitelistedNamespacePrefixes = new[] { "Microsoft" }
/// };
/// </code>
/// </example>
[Obsolete("Use AllowNamespacePrefixes instead. Will be removed in the next major.")]
public string[] WhitelistedNamespacePrefixes
{
get => AllowNamespacePrefixes;
set => AllowNamespacePrefixes = value;
}

/// <summary>
/// Allows to print APIs in certain namespace prefixes.
/// For example by default types found in Microsoft or System namespaces are not treated as part of the public API.
Expand All @@ -52,25 +32,6 @@ public string[] WhitelistedNamespacePrefixes
/// </example>
public string[] AllowNamespacePrefixes { get; set; } = _defaultAllowNamespacePrefixes;

/// <summary>
/// Denies to print APIs in certain namespace prefixes.
/// By default types found in Microsoft or System namespaces are not treated as part of the public API.
/// </summary>
/// <example>
/// <code>
/// var options = new DefaultApiGeneratorOptions
/// {
/// BlacklistedNamespacePrefixes = new[] { "System", "Microsoft", "ThirdParty" }
/// };
/// </code>
/// </example>
[Obsolete("Use DenyNamespacePrefixes instead. Will be removed in the next major.")]
public string[] BlacklistedNamespacePrefixes
{
get => DenyNamespacePrefixes;
set => DenyNamespacePrefixes = value;
}

/// <summary>
/// Denies to print APIs in certain namespace prefixes.
/// By default types found in Microsoft or System namespaces are not treated as part of the public API.
Expand All @@ -85,20 +46,6 @@ public string[] BlacklistedNamespacePrefixes
/// </example>
public string[] DenyNamespacePrefixes { get; set; } = _defaultDenyNamespacePrefixes;

/// <summary>
/// Allows to control whether to include extension methods into generated API even when
/// containing class falls into <see cref="DenyNamespacePrefixes"/> option. This
/// option may be useful, for example, for those who writes extensions for IServiceCollection
/// keeping them into Microsoft.Extensions.DependencyInjection namespace for better discoverability.
/// </summary>
/// <remarks>Defaults to <see langword="true"/>, i.e. extension methods are excluded from output.</remarks>
[Obsolete("Use UseDenyNamespacePrefixesForExtensionMethods instead. Will be removed in the next major.")]
public bool UseBlacklistedNamespacePrefixesForExtensionMethods
{
get => UseDenyNamespacePrefixesForExtensionMethods;
set => UseDenyNamespacePrefixesForExtensionMethods = value;
}

/// <summary>
/// Allows to control whether to include extension methods into generated API even when
/// containing class falls into <see cref="DenyNamespacePrefixes"/> option. This
Expand Down
4 changes: 2 additions & 2 deletions src/PublicApiGeneratorTests/Namespaces_whitelisting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void Should_allow_microsoft_namespace_whitelisting()
{
var options = new DefaultApiGeneratorOptions
{
WhitelistedNamespacePrefixes = new[] { "Microsoft.Whitelisted" }
AllowNamespacePrefixes = new[] { "Microsoft.Whitelisted" }
};

AssertPublicApi(new[] { typeof(Simple1), typeof(Simple2) },
Expand Down Expand Up @@ -40,7 +40,7 @@ public void Should_allow_system_namespace_whitelisting()
{
var options = new DefaultApiGeneratorOptions
{
WhitelistedNamespacePrefixes = new[] { "System.Whitelisted" }
AllowNamespacePrefixes = new[] { "System.Whitelisted" }
};

AssertPublicApi(new[] { typeof(System1), typeof(System2) },
Expand Down
10 changes: 0 additions & 10 deletions src/PublicApiGeneratorTests/PublicApiGenerator.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@ namespace PublicApiGenerator
public static string GeneratePublicApi(this System.Reflection.Assembly assembly, PublicApiGenerator.ApiGeneratorOptions? options = null) { }
public static string GeneratePublicApi(this System.Type type, PublicApiGenerator.ApiGeneratorOptions? options = null) { }
public static string GeneratePublicApi(this System.Type[] types, PublicApiGenerator.ApiGeneratorOptions? options = null) { }
[System.Obsolete("Use `GeneratePublicApi(this Assembly assembly, ApiGeneratorOptions? options = nul" +
"l)` instead. Will be removed in the next major.")]
public static string GeneratePublicApi(System.Reflection.Assembly assembly, System.Type[]? includeTypes = null, bool shouldIncludeAssemblyAttributes = true, string[]? whitelistedNamespacePrefixes = null, string[]? excludeAttributes = null) { }
}
public class ApiGeneratorOptions
{
public ApiGeneratorOptions() { }
public string[] AllowNamespacePrefixes { get; set; }
[System.Obsolete("Use DenyNamespacePrefixes instead. Will be removed in the next major.")]
public string[] BlacklistedNamespacePrefixes { get; set; }
public string[] DenyNamespacePrefixes { get; set; }
public string[]? ExcludeAttributes { get; set; }
public bool IncludeAssemblyAttributes { get; set; }
public System.Type[]? IncludeTypes { get; set; }
[System.Obsolete("Use UseDenyNamespacePrefixesForExtensionMethods instead. Will be removed in the n" +
"ext major.")]
public bool UseBlacklistedNamespacePrefixesForExtensionMethods { get; set; }
public bool UseDenyNamespacePrefixesForExtensionMethods { get; set; }
[System.Obsolete("Use AllowNamespacePrefixes instead. Will be removed in the next major.")]
public string[] WhitelistedNamespacePrefixes { get; set; }
}
}

0 comments on commit bedff8b

Please sign in to comment.