From 09b4534f7bf7e95539421a0f41806a4b513e5932 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Mon, 19 Jul 2021 18:03:36 -0300 Subject: [PATCH] Properly evaluate PackInference.PackExclude We were previously assuming Exclude was a valuated while doing the Include for entire item groups, while this is not the case. We need instead to evaluate using the same MSBuild syntax (we use minimatch for this) so we can properly exclude items. Fixes #128 and #122. --- src/NuGetizer.Tasks/EvaluateWildcards.cs | 62 +++++++++++++++++++ src/NuGetizer.Tasks/Extensions.cs | 4 +- .../NuGetizer.Inference.targets | 18 +++++- src/NuGetizer.Tasks/NuGetizer.Tasks.csproj | 1 + src/NuGetizer.Tests/given_packinference.cs | 30 +++++++++ 5 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/NuGetizer.Tasks/EvaluateWildcards.cs diff --git a/src/NuGetizer.Tasks/EvaluateWildcards.cs b/src/NuGetizer.Tasks/EvaluateWildcards.cs new file mode 100644 index 00000000..7af1dfa0 --- /dev/null +++ b/src/NuGetizer.Tasks/EvaluateWildcards.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Minimatch; + +namespace NuGetizer.Tasks +{ + /// + /// Evaluates one or more minimatch expressions against a set of + /// items and returns two lists: those that matched and those that + /// didn't. + /// + public class EvaluateWildcards : Task + { + [Required] + public ITaskItem[] Items { get; set; } + + [Required] + public string Wildcards { get; set; } + + [Output] + public ITaskItem[] MatchingItems { get; set; } + + [Output] + public ITaskItem[] NonMatchingItems { get; set; } + + public override bool Execute() + { + var matching = new List(); + var nonMatching = new List(); + + var options = new Options + { + AllowWindowsPaths = true, + Dot = true, + IgnoreCase = true + }; + + var matchers = Wildcards + .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) + .Select(wildcard => new Minimatcher(wildcard.Trim(), options)) + .ToList(); + + foreach (var item in Items) + { + if (matchers.Any(matcher => matcher.IsMatch(item.ItemSpec)) || + matchers.Any(matcher => matcher.IsMatch(item.GetMetadata("Fullpath")))) + matching.Add(item); + else + nonMatching.Add(item); + } + + MatchingItems = matching.ToArray(); + NonMatchingItems = nonMatching.ToArray(); + + return true; + } + } +} diff --git a/src/NuGetizer.Tasks/Extensions.cs b/src/NuGetizer.Tasks/Extensions.cs index b8682e4a..1ec4d29a 100644 --- a/src/NuGetizer.Tasks/Extensions.cs +++ b/src/NuGetizer.Tasks/Extensions.cs @@ -139,7 +139,7 @@ public static string GetShortFrameworkName(this FrameworkName frameworkName) public static void LogErrorCode(this TaskLoggingHelper log, string code, string message, params object[] messageArgs) => log.LogError(string.Empty, code, string.Empty, string.Empty, 0, 0, 0, 0, message, messageArgs); - public static void LogWarningCode(this TaskLoggingHelper log, string code, string file, string message, params object[] messageArgs) => - log.LogWarning(string.Empty, code, string.Empty, file, 0, 0, 0, 0, message, messageArgs); + public static void LogWarningCode(this TaskLoggingHelper log, string code, string message, params object[] messageArgs) => + log.LogWarning(string.Empty, code, string.Empty, string.Empty, 0, 0, 0, 0, message, messageArgs); } } diff --git a/src/NuGetizer.Tasks/NuGetizer.Inference.targets b/src/NuGetizer.Tasks/NuGetizer.Inference.targets index 6a2a0774..41cd08fb 100644 --- a/src/NuGetizer.Tasks/NuGetizer.Inference.targets +++ b/src/NuGetizer.Tasks/NuGetizer.Inference.targets @@ -11,6 +11,7 @@ Copyright (c) .NET Foundation. All rights reserved. --> + @@ -177,9 +178,22 @@ Copyright (c) .NET Foundation. All rights reserved. - + + + %(PackInference.PackExclude) + + + + + + + + + + + + - true diff --git a/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj b/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj index 181f4d27..1a1f7c08 100644 --- a/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj +++ b/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj @@ -14,6 +14,7 @@ + diff --git a/src/NuGetizer.Tests/given_packinference.cs b/src/NuGetizer.Tests/given_packinference.cs index 15fadba2..e2df44d7 100644 --- a/src/NuGetizer.Tests/given_packinference.cs +++ b/src/NuGetizer.Tests/given_packinference.cs @@ -541,6 +541,36 @@ public void when_adding_new_inference_then_can_change_defaults() })); } + [Fact] + public void when_updating_inference_then_can_exclude_by_wildcard() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + true + + + + + + + + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Filename = "grpc_csharp_ext.x86", + })); + } + [Fact] public void when_direct_and_indirect_packagereference_then_packs_once() {