Exemplo n.º 1
0
        public ImmutableArray <ItemsDiff> GetItemsDiff()
        {
            var oldItemGroups = from oldItem in _project.Items group oldItem by oldItem.ItemType;
            var newItemGroups = from newItem in _sdkBaselineProject.Items group newItem by newItem.ItemType;

            var addedRemovedGroups = from og in oldItemGroups
                                     from ng in newItemGroups
                                     where og.Key.Equals(ng.Key, StringComparison.OrdinalIgnoreCase)
                                     select new {
                ItemType          = og.Key,
                DefaultedItems    = ng.Intersect(og, ProjectItemComparer.MetadataComparer),
                IntroducedItems   = ng.Except(og, ProjectItemComparer.IncludeComparer),
                NotDefaultedItems = og.Except(ng, ProjectItemComparer.IncludeComparer),
                ChangedItems      = GetChangedItems(og, ng),
            };

            var builder = ImmutableArray.CreateBuilder <ItemsDiff>();

            foreach (var group in addedRemovedGroups)
            {
                var defaultedItems    = group.DefaultedItems.ToImmutableArray();
                var notDefaultedItems = group.NotDefaultedItems.ToImmutableArray();
                var introducedItems   = group.IntroducedItems.ToImmutableArray();
                var changedItems      = group.ChangedItems.ToImmutableArray();

                var diff = new ItemsDiff(group.ItemType, defaultedItems, notDefaultedItems, introducedItems, changedItems);
                builder.Add(diff);
            }

            return(builder.ToImmutable());
        }
Exemplo n.º 2
0
        private void RemoveOrUpdateItems()
        {
            foreach (var itemGroup in _projectRootElement.ItemGroups)
            {
                var configurationName = MSBuildUtilities.GetConfigurationName(itemGroup.Condition);
                var itemsDiff         = _differs[configurationName].GetItemsDiff();

                foreach (var item in itemGroup.Items)
                {
                    ItemsDiff itemTypeDiff = itemsDiff.FirstOrDefault(id => id.ItemType.Equals(item.ItemType, StringComparison.OrdinalIgnoreCase));
                    if (!itemTypeDiff.DefaultedItems.IsDefault)
                    {
                        var defaultedItems = itemTypeDiff.DefaultedItems.Select(i => i.EvaluatedInclude);
                        if (defaultedItems.Contains(item.Include, StringComparer.OrdinalIgnoreCase))
                        {
                            itemGroup.RemoveChild(item);
                        }
                    }

                    if (!itemTypeDiff.ChangedItems.IsDefault)
                    {
                        var changedItems = itemTypeDiff.ChangedItems.Select(i => i.EvaluatedInclude);
                        if (changedItems.Contains(item.Include, StringComparer.OrdinalIgnoreCase))
                        {
                            var path = item.Include;
                            item.Include = null;
                            item.Update  = path;
                        }
                    }
                }

                if (itemGroup.Items.Count == 0)
                {
                    _projectRootElement.RemoveChild(itemGroup);
                }
            }
        }