aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/ExtensibilityServices/TupleDefinitionCreator.cs
blob: b442da2b0b4b8eb3df764b543ea2dc76a382bfb3 (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
// 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 TupleDefinitionCreator : ITupleDefinitionCreator
    {
        public TupleDefinitionCreator(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }

        private IServiceProvider ServiceProvider { get; }

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

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

        public void AddCustomTupleDefinition(IntermediateTupleDefinition definition)
        {
            this.CustomDefinitionByName.Add(definition.Name, definition);
        }

        public bool TryGetTupleDefinitionByName(string name, out IntermediateTupleDefinition tupleDefinition)
        {
            // First, look in the built-ins.
            tupleDefinition = TupleDefinitions.ByName(name);

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

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

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

            return tupleDefinition != null;
        }

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

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