aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Link/ReportConflictingSymbolsCommand.cs
blob: 39c3a5c289937dcccfa3ea69ada13d119c339810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Link
{
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;

    public class ReportConflictingSymbolsCommand : ICommand
    {
        private IEnumerable<Symbol> possibleConflicts;
        private IEnumerable<Section> resolvedSections;

        public ReportConflictingSymbolsCommand(IEnumerable<Symbol> possibleConflicts, IEnumerable<Section> resolvedSections)
        {
            this.possibleConflicts = possibleConflicts;
            this.resolvedSections = resolvedSections;
        }

        public void Execute()
        {
            // Do a quick check if there are any possibly conflicting symbols that don't come from tables that allow
            // overriding. Hopefully the symbols with possible conflicts list is usually very short list (empty should
            // be the most common). If we find any matches, we'll do a more costly check to see if the possible conflicting
            // symbols are in sections we actually referenced. From the resulting set, show an error for each duplicate
            // (aka: conflicting) symbol. This should catch any rows with colliding primary keys (since symbols are based
            // on the primary keys of rows).
            List<Symbol> illegalDuplicates = possibleConflicts.Where(s => "WixAction" != s.Row.Table.Name && "WixVariable" != s.Row.Table.Name).ToList();
            if (0 < illegalDuplicates.Count)
            {
                HashSet<Section> referencedSections = new HashSet<Section>(resolvedSections);
                foreach (Symbol referencedDuplicateSymbol in illegalDuplicates.Where(s => referencedSections.Contains(s.Section)))
                {
                    List<Symbol> actuallyReferencedDuplicateSymbols = referencedDuplicateSymbol.PossiblyConflictingSymbols.Where(s => referencedSections.Contains(s.Section)).ToList();

                    if (actuallyReferencedDuplicateSymbols.Any())
                    {
                        Messaging.Instance.OnMessage(WixErrors.DuplicateSymbol(referencedDuplicateSymbol.Row.SourceLineNumbers, referencedDuplicateSymbol.Name));

                        foreach (Symbol duplicate in actuallyReferencedDuplicateSymbols)
                        {
                            Messaging.Instance.OnMessage(WixErrors.DuplicateSymbol2(duplicate.Row.SourceLineNumbers));
                        }
                    }
                }
            }
        }
    }
}