| 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 |
| 4 | { |
| 5 | using System; |
| 6 | using System.Diagnostics; |
| 7 | using System.Linq; |
| 8 | using System.Reflection; |
| 9 | using WixToolset.Data; |
| 10 | using WixToolset.Data.Symbols; |
| 11 | using WixToolset.Extensibility; |
| 12 | using WixToolset.Extensibility.Data; |
| 13 | using WixToolset.Extensibility.Services; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Binder of the WiX toolset. |
| 17 | /// </summary> |
| 18 | internal class Binder : IBinder |
| 19 | { |
| 20 | internal Binder(IServiceProvider serviceProvider) |
| 21 | { |
| 22 | this.ServiceProvider = serviceProvider; |
| 23 | } |
| 24 | |
| 25 | public IServiceProvider ServiceProvider { get; } |
| 26 | |
| 27 | public IBindResult Bind(IBindContext context) |
| 28 | { |
| 29 | // Prebind. |
| 30 | // |
| 31 | foreach (var extension in context.Extensions) |
| 32 | { |
| 33 | extension.PreBind(context); |
| 34 | } |
| 35 | |
| 36 | // Bind. |
| 37 | // |
| 38 | this.WriteBuildInfoSymbol(context.IntermediateRepresentation, context.OutputPath, context.PdbPath); |
| 39 | |
| 40 | var bindResult = this.BackendBind(context); |
| 41 | |
| 42 | if (bindResult != null) |
| 43 | { |
| 44 | // Postbind. |
| 45 | // |
| 46 | foreach (var extension in context.Extensions) |
| 47 | { |
| 48 | extension.PostBind(bindResult); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return bindResult; |
| 53 | } |
| 54 | |
| 55 | private IBindResult BackendBind(IBindContext context) |
| 56 | { |
| 57 | var extensionManager = context.ServiceProvider.GetService<IExtensionManager>(); |
| 58 | |
| 59 | var backendFactories = extensionManager.GetServices<IBackendFactory>(); |
| 60 | |
| 61 | foreach (var factory in backendFactories) |
| 62 | { |
| 63 | if (factory.TryCreateBackend(context.OutputType, context.OutputPath, out var backend)) |
| 64 | { |
| 65 | var result = backend.Bind(context); |
| 66 | return result; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | var messaging = context.ServiceProvider.GetService<IMessaging>(); |
| 71 | |
| 72 | messaging.Write(CoreErrors.BackendNotFound(context.OutputType, context.OutputPath)); |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | private void WriteBuildInfoSymbol(Intermediate output, string outputFile, string outputPdbPath) |
| 78 | { |
| 79 | var entrySection = output.Sections.First(s => s.Type != SectionType.Fragment); |
| 80 | |
| 81 | var executingAssembly = Assembly.GetExecutingAssembly(); |
| 82 | var fileVersion = FileVersionInfo.GetVersionInfo(executingAssembly.Location); |
| 83 | |
| 84 | var buildInfoSymbol = entrySection.AddSymbol(new WixBuildInfoSymbol() |
| 85 | { |
| 86 | WixVersion = fileVersion.FileVersion, |
| 87 | WixOutputFile = outputFile, |
| 88 | }); |
| 89 | |
| 90 | if (!String.IsNullOrEmpty(outputPdbPath)) |
| 91 | { |
| 92 | buildInfoSymbol.WixPdbFile = outputPdbPath; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |