From 2dd99ce015ab9cbf10ef29287fc7153bf557960b Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Wed, 21 Sep 2016 14:41:16 -0700 Subject: [PATCH] improve finding encompassed items in a configuration --- .../MSBuildExtensions.cs | 17 +++++++++++++++++ .../transforms/TransformApplicator.cs | 10 +++++----- .../GivenThatIWantToMigrateConfigurations.cs | 5 +++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/MSBuildExtensions.cs b/src/Microsoft.DotNet.ProjectJsonMigration/MSBuildExtensions.cs index dc1a004e3..63330fc83 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/MSBuildExtensions.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/MSBuildExtensions.cs @@ -15,6 +15,18 @@ namespace Microsoft.DotNet.ProjectJsonMigration { public static class MSBuildExtensions { + public static IEnumerable GetEncompassedIncludes(this ProjectItemElement item, + ProjectItemElement otherItem) + { + if (otherItem.IsEquivalentToExceptIncludeAndExclude(item) && + new HashSet(otherItem.Excludes()).IsSubsetOf(new HashSet(item.Excludes()))) + { + return otherItem.IntersectIncludes(item); + } + + return Enumerable.Empty(); + } + public static bool IsEquivalentTo(this ProjectItemElement item, ProjectItemElement otherItem) { // Different includes @@ -31,6 +43,11 @@ namespace Microsoft.DotNet.ProjectJsonMigration return false; } + return item.IsEquivalentToExceptIncludeAndExclude(otherItem); + } + + public static bool IsEquivalentToExceptIncludeAndExclude(this ProjectItemElement item, ProjectItemElement otherItem) + { // Different remove if (item.Remove != otherItem.Remove) { diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/transforms/TransformApplicator.cs b/src/Microsoft.DotNet.ProjectJsonMigration/transforms/TransformApplicator.cs index 7af6ddbac..f60655133 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/transforms/TransformApplicator.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/transforms/TransformApplicator.cs @@ -102,12 +102,12 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms foreach (var existingItem in existingItemsWithDifferentCondition) { - // When the existing item encompasses this item and it's condition is empty, ignore the current item - if (item.IsEquivalentTo(existingItem)) + var encompassedIncludes = existingItem.GetEncompassedIncludes(item); + if (encompassedIncludes.Any()) { - MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: equivalent {existingItem.ConditionChain().Count()}"); - - if (existingItem.ConditionChain().Count() == 0) + MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: encompassed includes {string.Join(", ", encompassedIncludes)}"); + item.RemoveIncludes(encompassedIncludes); + if (!item.Includes().Any()) { MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Ignoring Item {{ ItemType: {existingItem.ItemType}, Condition: {existingItem.Condition}, Include: {existingItem.Include}, Exclude: {existingItem.Exclude} }}"); return null; diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateConfigurations.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateConfigurations.cs index dce468606..7e94fcd47 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateConfigurations.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateConfigurations.cs @@ -110,7 +110,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests ""buildOptions"": { ""copyToOutput"": { ""include"": [""root"", ""src"", ""rootfile.cs""], - ""exclude"": [""src"", ""rootfile.cs""], + ""exclude"": [""src"", ""root/rootfile.cs""], ""includeFiles"": [""src/file1.cs"", ""src/file2.cs""], ""excludeFiles"": [""src/file2.cs""] } @@ -118,6 +118,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } } }"); + Console.WriteLine(mockProj.RawXml); var contentItems = mockProj.Items.Where(item => item.ItemType == "Content"); @@ -137,7 +138,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests // Directories are not converted to globs in the result because we did not write the directory configRemoveContentItem.Remove.Should().Be(@"root;src;rootfile.cs"); configIncludeContentItem.Include.Should().Be(@"root;src;rootfile.cs"); - configIncludeContentItem.Exclude.Should().Be(@"src;rootfile.cs;src\file2.cs"); + configIncludeContentItem.Exclude.Should().Be(@"src;root\rootfile.cs;src\file2.cs"); } [Fact]