| 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.Tools.Heat |
| 4 | { |
| 5 | using System; |
| 6 | using System.Runtime.InteropServices; |
| 7 | using System.Threading; |
| 8 | using System.Threading.Tasks; |
| 9 | using WixToolset.Core; |
| 10 | using WixToolset.Data; |
| 11 | using WixToolset.Extensibility; |
| 12 | using WixToolset.Extensibility.Data; |
| 13 | using WixToolset.Extensibility.Services; |
| 14 | using WixToolset.Harvesters; |
| 15 | using WixToolset.Harvesters.Extensibility; |
| 16 | using WixToolset.Tools.Core; |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Wix Toolset Harvester. |
| 20 | /// </summary> |
| 21 | public sealed class Program |
| 22 | { |
| 23 | /// <summary> |
| 24 | /// The main entry point for the application. |
| 25 | /// </summary> |
| 26 | /// <param name="args">Commandline arguments for the application.</param> |
| 27 | /// <returns>Returns the application error code.</returns> |
| 28 | [MTAThread] |
| 29 | public static async Task<int> Main(string[] args) |
| 30 | { |
| 31 | var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider(); |
| 32 | var listener = new ConsoleMessageListener("HEAT", "heat.exe"); |
| 33 | |
| 34 | try |
| 35 | { |
| 36 | var program = new Program(); |
| 37 | return await program.Run(serviceProvider, listener, args); |
| 38 | } |
| 39 | catch (WixException e) |
| 40 | { |
| 41 | listener.Write(e.Error); |
| 42 | |
| 43 | return e.Error.Id; |
| 44 | } |
| 45 | catch (Exception e) |
| 46 | { |
| 47 | listener.Write(ErrorMessages.UnexpectedException(e)); |
| 48 | |
| 49 | if (e is NullReferenceException || e is SEHException) |
| 50 | { |
| 51 | throw; |
| 52 | } |
| 53 | |
| 54 | return e.HResult; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// <summary> |
| 59 | /// Run the application with the given arguments. |
| 60 | /// </summary> |
| 61 | /// <param name="serviceProvider">Service provider to use throughout this execution.</param> |
| 62 | /// <param name="args">The commandline arguments.</param> |
| 63 | /// <returns>Returns the application error code.</returns> |
| 64 | public Task<int> Run(IServiceProvider serviceProvider, IMessageListener listener, string[] args) |
| 65 | { |
| 66 | var messaging = serviceProvider.GetService<IMessaging>(); |
| 67 | messaging.SetListener(listener); |
| 68 | |
| 69 | var arguments = serviceProvider.GetService<ICommandLineArguments>(); |
| 70 | arguments.Populate(args); |
| 71 | |
| 72 | var extensionManager = serviceProvider.GetService<IExtensionManager>(); |
| 73 | foreach (var extension in arguments.Extensions) |
| 74 | { |
| 75 | extensionManager.Load(extension); |
| 76 | } |
| 77 | var heatExtensions = extensionManager.GetServices<IHeatExtension>(); |
| 78 | |
| 79 | var commandLine = HeatCommandLineFactory.CreateCommandLine(serviceProvider, heatExtensions); |
| 80 | var command = commandLine.ParseStandardCommandLine(arguments); |
| 81 | return command?.ExecuteAsync(CancellationToken.None) ?? Task.FromResult(1); |
| 82 | } |
| 83 | } |
| 84 | } |