aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Bind/ValidateComponentGuidsCommand.cs
blob: 5cad924744236c09f59cce8f8475d22b643846b3 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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.Core.WindowsInstaller.Bind
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;
    using WixToolset.Data.Symbols;
    using WixToolset.Extensibility.Services;

    /// <summary>
    /// Validate that there are no duplicate GUIDs in the output.
    /// </summary>
    /// <remarks>
    /// Duplicate GUIDs without conditions are an error condition; with conditions, it's a
    /// warning, as the conditions might be mutually exclusive.
    /// </remarks>
    internal class ValidateComponentGuidsCommand
    {
        internal ValidateComponentGuidsCommand(IMessaging messaging, IntermediateSection section)
        {
            this.Messaging = messaging;
            this.Section = section;
        }

        private IMessaging Messaging { get; }

        private IntermediateSection Section { get; }

        public void Execute()
        {
            var componentGuidConditions = new Dictionary<string, bool>();

            foreach (var componentSymbol in this.Section.Symbols.OfType<ComponentSymbol>())
            {
                // We don't care about unmanaged components and if there's a * GUID remaining,
                // there's already an error that prevented it from being replaced with a real GUID.
                if (!String.IsNullOrEmpty(componentSymbol.ComponentId) && "*" != componentSymbol.ComponentId)
                {
                    var thisComponentHasCondition = !String.IsNullOrEmpty(componentSymbol.Condition);
                    var allComponentsHaveConditions = thisComponentHasCondition;

                    if (componentGuidConditions.TryGetValue(componentSymbol.ComponentId, out var alreadyCheckedCondition))
                    {
                        allComponentsHaveConditions = thisComponentHasCondition && alreadyCheckedCondition;

                        if (allComponentsHaveConditions)
                        {
                            this.Messaging.Write(WarningMessages.DuplicateComponentGuidsMustHaveMutuallyExclusiveConditions(componentSymbol.SourceLineNumbers, componentSymbol.Id.Id, componentSymbol.ComponentId));
                        }
                        else
                        {
                            this.Messaging.Write(ErrorMessages.DuplicateComponentGuids(componentSymbol.SourceLineNumbers, componentSymbol.Id.Id, componentSymbol.ComponentId));
                        }
                    }

                    componentGuidConditions[componentSymbol.ComponentId] = allComponentsHaveConditions;
                }
            }
        }
    }
}