Skip to content

Instantly share code, notes, and snippets.

@ViktorHofer
Created August 12, 2022 20:09
Show Gist options
  • Save ViktorHofer/ce835af086e8c0fc54a8ea2d6acaec6b to your computer and use it in GitHub Desktop.
Save ViktorHofer/ce835af086e8c0fc54a8ea2d6acaec6b to your computer and use it in GitHub Desktop.
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidateAssemblies.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidateAssemblies.cs
@@ -40,6 +40,7 @@ internal static class ValidateAssemblies
bool generateSuppressionFile,
string? suppressionFile,
string? noWarn,
+ string[]? excludeAttributesFiles,
string[] leftAssemblies,
string[] rightAssemblies,
bool enableStrictMode,
@@ -55,7 +56,7 @@ internal static class ValidateAssemblies
// Initialize the service provider
ValidateAssembliesServiceProvider serviceProvider = new(logFactory,
() => new SuppressionEngine(suppressionFileForEngine, noWarn, generateSuppressionFile),
- new RuleFactory());
+ new RuleFactory(excludeAttributesFiles));
IApiCompatRunner apiCompatRunner = serviceProvider.GetService<IApiCompatRunner>();
ApiCompatRunnerOptions apiCompatOptions = new(enableStrictMode);
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidatePackage.cs b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidatePackage.cs
index 273e5d60e7..0d3688c286 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidatePackage.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Shared/ValidatePackage.cs
@@ -44,6 +44,7 @@ internal static class ValidatePackage
bool generateSuppressionFile,
string? suppressionFile,
string? noWarn,
+ string[]? excludeAttributesFiles,
string packagePath,
bool runApiCompat,
bool enableStrictModeForCompatibleTfms,
@@ -60,7 +61,7 @@ internal static class ValidatePackage
// Initialize the service provider
ValidatePackageServiceProvider serviceProvider = new(logFactory,
() => new SuppressionEngine(suppressionFileForEngine, noWarn, generateSuppressionFile),
- new RuleFactory());
+ new RuleFactory(excludeAttributesFiles));
// If a runtime graph is provided, parse and use it for asset selection during the in-memory package construction.
if (runtimeGraph != null)
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs
index 48af257795..0fdf210d5a 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs
@@ -48,6 +48,11 @@ public class ValidateAssembliesTask : TaskBase
/// </summary>
public string? NoWarn { get; set; }
+ /// <summary>
+ /// Set of files with types in DocId format of which attributes to exclude.
+ /// </summary>
+ public string[]? ExcludeAttributesFiles { get; set; }
+
/// <summary>
/// Performs api comparison checks in strict mode.
/// </summary>
@@ -108,6 +113,7 @@ protected override void ExecuteCore()
GenerateCompatibilitySuppressionFile,
CompatibilitySuppressionFilePath,
NoWarn,
+ ExcludeAttributesFiles,
LeftAssemblies!,
RightAssemblies!,
EnableStrictMode,
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidatePackageTask.cs b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidatePackageTask.cs
index 663c63387f..30460ce289 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidatePackageTask.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidatePackageTask.cs
@@ -38,6 +38,11 @@ public class ValidatePackageTask : TaskBase
/// </summary>
public string? NoWarn { get; set; }
+ /// <summary>
+ /// Set of files with types in DocId format of which attributes to exclude.
+ /// </summary>
+ public string[]? ExcludeAttributesFiles { get; set; }
+
/// <summary>
/// If true, performs api compatibility checks on the package assets.
/// </summary>
@@ -124,6 +129,7 @@ protected override void ExecuteCore()
GenerateCompatibilitySuppressionFile,
CompatibilitySuppressionFilePath,
NoWarn,
+ ExcludeAttributesFiles,
PackageTargetPath!,
RunApiCompat,
EnableStrictModeForCompatibleTfms,
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/build/Microsoft.DotNet.ApiCompat.ValidateAssemblies.Common.targets b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/build/Microsoft.DotNet.ApiCompat.ValidateAssemblies.Common.targets
index 299a6d7da8..99a262fc4c 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/build/Microsoft.DotNet.ApiCompat.ValidateAssemblies.Common.targets
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Task/build/Microsoft.DotNet.ApiCompat.ValidateAssemblies.Common.targets
@@ -19,6 +19,7 @@
GenerateCompatibilitySuppressionFile="$(GenerateCompatibilitySuppressionFile)"
CompatibilitySuppressionFilePath="$(CompatibilitySuppressionFilePath)"
NoWarn="$(NoWarn)"
+ ExcludeAttributesFiles="@(ApiCompatExcludeAttributesFile)"
EnableStrictMode="$(ApiCompatStrictMode)"
LeftAssembliesReferences="@(ApiCompatLeftAssembliesReferences)"
RightAssembliesReferences="@(ApiCompatRightAssembliesReferences)"
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Tool/Program.cs b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Tool/Program.cs
index 1f63d47ea7..13ab069d10 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompat.Tool/Program.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompat.Tool/Program.cs
@@ -35,6 +35,9 @@ static int Main(string[] args)
Option<MessageImportance> verbosityOption = new(new string[] { "--verbosity", "-v" },
"Controls the log level verbosity. Allowed values are high, normal, and low.");
verbosityOption.SetDefaultValue(MessageImportance.Normal);
+ Option<string[]> excludeAttributesFilesOption = new("--exclude-attributes-file",
+ "The path to one or more attribute exclusion files with types in DocId format.");
+
// Root command
Option<string[]> leftAssembliesOption = new(new string[] { "--left-assembly", "--left", "-l" },
@@ -95,6 +98,7 @@ static int Main(string[] args)
rootCommand.AddGlobalOption(noWarnOption);
rootCommand.AddGlobalOption(roslynAssembliesPathOption);
rootCommand.AddGlobalOption(verbosityOption);
+ rootCommand.AddGlobalOption(excludeAttributesFilesOption);
rootCommand.AddOption(leftAssembliesOption);
rootCommand.AddOption(rightAssembliesOption);
@@ -117,6 +121,8 @@ static int Main(string[] args)
bool generateSuppressionFile = context.ParseResult.GetValueForOption(generateSuppressionFileOption);
string? suppressionFile = context.ParseResult.GetValueForOption(suppressionFileOption);
string? noWarn = context.ParseResult.GetValueForOption(noWarnOption);
+ string[]? excludeAttributesFiles = context.ParseResult.GetValueForOption(excludeAttributesFilesOption);
+
string[] leftAssemblies = context.ParseResult.GetValueForOption(leftAssembliesOption)!;
string[] rightAssemblies = context.ParseResult.GetValueForOption(rightAssembliesOption)!;
bool strictMode = context.ParseResult.GetValueForOption(strictModeOption);
@@ -131,6 +137,7 @@ static int Main(string[] args)
generateSuppressionFile,
suppressionFile,
noWarn,
+ excludeAttributesFiles,
leftAssemblies,
rightAssemblies,
strictMode,
@@ -205,6 +212,8 @@ static int Main(string[] args)
bool generateSuppressionFile = context.ParseResult.GetValueForOption(generateSuppressionFileOption);
string? suppressionFile = context.ParseResult.GetValueForOption(suppressionFileOption);
string? noWarn = context.ParseResult.GetValueForOption(noWarnOption);
+ string[]? excludeAttributesFiles = context.ParseResult.GetValueForOption(excludeAttributesFilesOption);
+
string package = context.ParseResult.GetValueForArgument(packageArgument);
bool runApiCompat = context.ParseResult.GetValueForOption(runApiCompatOption);
bool enableStrictModeForCompatibleTfms = context.ParseResult.GetValueForOption(enableStrictModeForCompatibleTfmsOption);
@@ -220,6 +229,7 @@ static int Main(string[] args)
generateSuppressionFile,
suppressionFile,
noWarn,
+ excludeAttributesFiles,
package,
runApiCompat,
enableStrictModeForCompatibleTfms,
diff --git a/src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/RuleFactory.cs b/src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/RuleFactory.cs
index a4dd2d3d0c..5e810cc997 100644
--- a/src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/RuleFactory.cs
+++ b/src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/RuleFactory.cs
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+using System.Collections.Generic;
+
namespace Microsoft.DotNet.ApiCompatibility.Rules
{
/// <summary>
@@ -8,6 +10,13 @@ namespace Microsoft.DotNet.ApiCompatibility.Rules
/// </summary>
public class RuleFactory : IRuleFactory
{
+ private readonly IEnumerable<string>? _excludeAttributesFiles;
+
+ public RuleFactory(IEnumerable<string>? excludeAttributesFiles = null)
+ {
+ _excludeAttributesFiles = excludeAttributesFiles;
+ }
+
/// <inheritdoc />
public IRule[] CreateRules(RuleSettings settings, IRuleRegistrationContext context)
{
@@ -21,6 +30,7 @@ public IRule[] CreateRules(RuleSettings settings, IRuleRegistrationContext conte
new CannotSealType(settings, context),
new EnumsMustMatch(settings, context),
new MembersMustExist(settings, context)
+ // TODO: Add AttributesRule and pass in _excludeAttributesFiles
};
}
}
diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ApiCompat.ValidatePackage.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ApiCompat.ValidatePackage.targets
index f86d5d3c32..b21681c980 100644
--- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ApiCompat.ValidatePackage.targets
+++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ApiCompat.ValidatePackage.targets
@@ -33,6 +33,7 @@ Copyright (c) .NET Foundation. All rights reserved.
PackageTargetPath="$([MSBuild]::ValueOrDefault('$(PackageTargetPath)', '$([MSBuild]::NormalizePath('$(PackageOutputPath)', '$(PackageId).$(PackageVersion).nupkg'))'))"
RuntimeGraph="$(RuntimeIdentifierGraphPath)"
NoWarn="$(NoWarn)"
+ ExcludeAttributesFiles="@(ApiCompatExcludeAttributesFile)"
RunApiCompat="$(RunApiCompat)"
EnableStrictModeForCompatibleTfms="$([MSBuild]::ValueOrDefault('$(EnableStrictModeForCompatibleTfms)', 'true'))"
EnableStrictModeForCompatibleFrameworksInPackage="$(EnableStrictModeForCompatibleFrameworksInPackage)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment