improve finding encompassed items in a configuration

This commit is contained in:
Bryan Thornbury 2016-09-21 14:41:16 -07:00
parent b03f4c282b
commit 2dd99ce015
3 changed files with 25 additions and 7 deletions

View file

@ -15,6 +15,18 @@ namespace Microsoft.DotNet.ProjectJsonMigration
{
public static class MSBuildExtensions
{
public static IEnumerable<string> GetEncompassedIncludes(this ProjectItemElement item,
ProjectItemElement otherItem)
{
if (otherItem.IsEquivalentToExceptIncludeAndExclude(item) &&
new HashSet<string>(otherItem.Excludes()).IsSubsetOf(new HashSet<string>(item.Excludes())))
{
return otherItem.IntersectIncludes(item);
}
return Enumerable.Empty<string>();
}
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)
{

View file

@ -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;

View file

@ -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]