aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/ToolsetTask_InProc.cs
blob: fcf4aea9b2375d1ba110a9a89889ce9a7293e616 (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
71
72
73
74
75
76
77
78
79
// 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.

#if !NETCOREAPP
namespace WixToolset.BuildTasks
{
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Build.Framework;
    using WixToolset.Core;
    using WixToolset.Data;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Services;

    public partial class ToolsetTask
    {
        protected sealed override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
        {
            if (this.RunAsSeparateProcess)
            {
                return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
            }

            return this.ExecuteInProc($"{commandLineCommands} {responseFileCommands}");
        }

        private int ExecuteInProc(string commandLineString)
        {
            this.Log.LogMessage(MessageImportance.Normal, $"({this.ToolName}){commandLineString}");

            var listener = new MsbuildMessageListener(this.Log, this.TaskShortName, this.BuildEngine.ProjectFileOfTaskNode);
            var exitCode = -1;

            try
            {
                var coreProvider = WixToolsetServiceProviderFactory.CreateServiceProvider();

                var messaging = coreProvider.GetService<IMessaging>();
                messaging.SetListener(listener);

                exitCode = this.ExecuteCoreAsync(coreProvider, commandLineString, CancellationToken.None).GetAwaiter().GetResult();
            }
            catch (WixException e)
            {
                listener.Write(e.Error);
            }
            catch (Exception e)
            {
                this.Log.LogErrorFromException(e, showStackTrace: true, showDetail: true, null);

                if (e is NullReferenceException || e is SEHException)
                {
                    throw;
                }
            }

            if (exitCode == 0 && this.Log.HasLoggedErrors)
            {
                exitCode = -1;
            }
            return exitCode;
        }

        protected sealed override void LogToolCommand(string message)
        {
            // Only log this if we're actually going to do it.
            if (this.RunAsSeparateProcess)
            {
                base.LogToolCommand(message);
            }
        }

        protected abstract Task<int> ExecuteCoreAsync(IWixToolsetCoreServiceProvider coreProvider, string commandLineString, CancellationToken cancellationToken);

        protected abstract string TaskShortName { get; }
    }
}
#endif