| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Core.ExtensibilityServices |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using WixToolset.Data; |
| 8 | using WixToolset.Extensibility; |
| 9 | using WixToolset.Extensibility.Services; |
| 10 | |
| 11 | internal class SymbolDefinitionCreator : ISymbolDefinitionCreator |
| 12 | { |
| 13 | public SymbolDefinitionCreator(IServiceProvider serviceProvider) |
| 14 | { |
| 15 | this.ServiceProvider = serviceProvider; |
| 16 | } |
| 17 | |
| 18 | private IServiceProvider ServiceProvider { get; } |
| 19 | |
| 20 | private IEnumerable<IExtensionData> ExtensionData { get; set; } |
| 21 | |
| 22 | private Dictionary<string, IntermediateSymbolDefinition> CustomDefinitionByName { get; } = new Dictionary<string, IntermediateSymbolDefinition>(); |
| 23 | |
| 24 | public void AddCustomSymbolDefinition(IntermediateSymbolDefinition definition) |
| 25 | { |
| 26 | if (!this.CustomDefinitionByName.TryGetValue(definition.Name, out var existing) || definition.Revision > existing.Revision) |
| 27 | { |
| 28 | this.CustomDefinitionByName[definition.Name] = definition; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public bool TryGetSymbolDefinitionByName(string name, out IntermediateSymbolDefinition symbolDefinition) |
| 33 | { |
| 34 | // First, look in the built-ins. |
| 35 | symbolDefinition = SymbolDefinitions.ByName(name); |
| 36 | if (symbolDefinition != null) |
| 37 | { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | if (this.ExtensionData == null) |
| 42 | { |
| 43 | this.LoadExtensionData(); |
| 44 | } |
| 45 | |
| 46 | // Second, look in the extensions. |
| 47 | foreach (var data in this.ExtensionData) |
| 48 | { |
| 49 | if (data.TryGetSymbolDefinitionByName(name, out symbolDefinition)) |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Finally, look in the custom symbol definitions provided during an intermediate load. |
| 56 | return this.CustomDefinitionByName.TryGetValue(name, out symbolDefinition); |
| 57 | } |
| 58 | |
| 59 | private void LoadExtensionData() |
| 60 | { |
| 61 | var extensionManager = this.ServiceProvider.GetService<IExtensionManager>(); |
| 62 | |
| 63 | this.ExtensionData = extensionManager.GetServices<IExtensionData>(); |
| 64 | } |
| 65 | } |
| 66 | } |