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

        private IWixToolsetServiceProvider ServiceProvider { get; }

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

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

        public void AddCustomTupleDefinition(IntermediateTupleDefinition definition)
        {
            if (!this.CustomDefinitionByName.TryGetValue(definition.Name, out var existing) || definition.Revision > existing.Revision)
            {
                this.CustomDefinitionByName[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 = this.ServiceProvider.GetService<IExtensionManager>();

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