// 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.TestPackage { using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using WixToolset.Core.Burn; using WixToolset.Core.WindowsInstaller; using WixToolset.Data; using WixToolset.Extensibility.Services; /// /// Utility class to emulate wix.exe with standard backends. /// public static class WixRunner { /// /// Emulates calling wix.exe with standard backends. /// /// /// /// /// public static int Execute(string[] args, out List messages, bool warningsAsErrors = true) { var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider(); var task = Execute(args, serviceProvider, out messages, warningsAsErrors: warningsAsErrors); return task.Result; } /// /// Emulates calling wix.exe with standard backends. /// This overload always treats warnings as errors. /// /// /// public static WixRunnerResult Execute(params string[] args) { return Execute(true, args); } /// /// Emulates calling wix.exe with standard backends. /// /// /// /// public static WixRunnerResult Execute(bool warningsAsErrors, params string[] args) { var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider(); var exitCode = Execute(args, serviceProvider, out var messages, warningsAsErrors: warningsAsErrors); return new WixRunnerResult { ExitCode = exitCode.Result, Messages = messages.ToArray() }; } /// /// Emulates calling wix.exe with standard backends. /// /// /// /// /// /// public static Task Execute(string[] args, IWixToolsetCoreServiceProvider coreProvider, out List messages, bool warningsAsErrors = true) { coreProvider.AddWindowsInstallerBackend() .AddBundleBackend(); var listener = new TestMessageListener(); messages = listener.Messages; var messaging = coreProvider.GetService(); messaging.SetListener(listener); var arguments = new List(args); if (warningsAsErrors) { arguments.Add("-wx"); } var commandLine = coreProvider.GetService(); var command = commandLine.CreateCommand(arguments.ToArray()); return command?.ExecuteAsync(CancellationToken.None) ?? Task.FromResult(1); } } }