aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/ExtensibilityServices/SymbolDefinitionCreator.cs
blob: 2bff21d6a6752370d221a6fad2b3bc5ce69135cf (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
64
65
66
67
68
69
70
// 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.ExtensibilityServices
{
    using System;
    using System.Collections.Generic;
    using WixToolset.Data;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Services;

    internal class SymbolDefinitionCreator : ISymbolDefinitionCreator
    {
        public SymbolDefinitionCreator(IWixToolsetServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }

        private IWixToolsetServiceProvider ServiceProvider { get; }

        private IEnumerable<IExtensionData> ExtensionData { get; set; }

        private Dictionary<string, IntermediateSymbolDefinition> CustomDefinitionByName { get; } = new Dictionary<string, IntermediateSymbolDefinition>();

        public void AddCustomSymbolDefinition(IntermediateSymbolDefinition definition)
        {
            if (!this.CustomDefinitionByName.TryGetValue(definition.Name, out var existing) || definition.Revision > existing.Revision)
            {
                this.CustomDefinitionByName[definition.Name] = definition;
            }
        }

        public bool TryGetSymbolDefinitionByName(string name, out IntermediateSymbolDefinition symbolDefinition)
        {
            // First, look in the built-ins.
            symbolDefinition = SymbolDefinitions.ByName(name);

            if (symbolDefinition == null)
            {
                if (this.ExtensionData == null)
                {
                    this.LoadExtensionData();
                }

                // Second, look in the extensions.
                foreach (var data in this.ExtensionData)
                {
                    if (data.TryGetSymbolDefinitionByName(name, out symbolDefinition))
                    {
                        break;
                    }
                }

                // Finally, look in the custom symbol definitions provided during an intermediate load.
                if (symbolDefinition == null)
                {
                    this.CustomDefinitionByName.TryGetValue(name, out symbolDefinition);
                }
            }

            return symbolDefinition != null;
        }

        private void LoadExtensionData()
        {
            var extensionManager = this.ServiceProvider.GetService<IExtensionManager>();

            this.ExtensionData = extensionManager.GetServices<IExtensionData>();
        }
    }
}