Refactor the tasks so that the in-proc code is in partial classes.
Sean Hall committed
Jun 2, 2020 at 19:45 UTC
a04ddca42b2070124c63a61c661e2b96a5bddac2
6 files changed
+158
-132
src/WixToolset.BuildTasks/HeatTask.cs
+1
-19
@@ -3,16 +3,12 @@
3
namespace WixToolset.BuildTasks
4
{
5
using Microsoft.Build.Framework;
6
- using WixToolset.Extensibility;
7
- using WixToolset.Extensibility.Data;
8
- using WixToolset.Extensibility.Services;
9
- using WixToolset.Harvesters;
6
7
/// <summary>
8
/// A base MSBuild task to run the WiX harvester.
9
/// Specific harvester tasks should extend this class.
10
/// </summary>
15
- public abstract class HeatTask : ToolsetTask
11
+ public abstract partial class HeatTask : ToolsetTask
12
{
13
private bool autogenerageGuids;
14
private bool generateGuidsNow;
@@ -59,7 +55,6 @@ namespace WixToolset.BuildTasks
55
set { this.transforms = value; }
56
}
57
62
- protected sealed override string TaskShortName => "HEAT";
58
protected sealed override string ToolName => "heat.exe";
59
60
/// <summary>
@@ -72,19 +67,6 @@ namespace WixToolset.BuildTasks
67
get;
68
}
69
75
- protected sealed override int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener listener, string commandLineString)
76
- {
77
- var messaging = serviceProvider.GetService<IMessaging>();
78
- messaging.SetListener(listener);
79
-
80
- var arguments = serviceProvider.GetService<ICommandLineArguments>();
81
- arguments.Populate(commandLineString);
82
-
83
- var commandLine = HeatCommandLineFactory.CreateCommandLine(serviceProvider, true);
84
- var command = commandLine.ParseStandardCommandLine(arguments);
85
- return command?.Execute() ?? -1;
86
- }
87
-
70
/// <summary>
71
/// Builds a command line from options in this task.
72
/// </summary>
src/WixToolset.BuildTasks/HeatTask_InProc.cs
new
+27
@@ -0,0 +1,27 @@
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.BuildTasks
4
+{
5
+ using WixToolset.Extensibility;
6
+ using WixToolset.Extensibility.Data;
7
+ using WixToolset.Extensibility.Services;
8
+ using WixToolset.Harvesters;
9
+
10
+ public partial class HeatTask
11
+ {
12
+ protected sealed override string TaskShortName => "HEAT";
13
+
14
+ protected sealed override int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener listener, string commandLineString)
15
+ {
16
+ var messaging = serviceProvider.GetService<IMessaging>();
17
+ messaging.SetListener(listener);
18
+
19
+ var arguments = serviceProvider.GetService<ICommandLineArguments>();
20
+ arguments.Populate(commandLineString);
21
+
22
+ var commandLine = HeatCommandLineFactory.CreateCommandLine(serviceProvider, true);
23
+ var command = commandLine.ParseStandardCommandLine(arguments);
24
+ return command?.Execute() ?? -1;
25
+ }
26
+ }
27
+}
src/WixToolset.BuildTasks/ToolsetTask.cs
+5
-68
@@ -4,15 +4,9 @@ namespace WixToolset.BuildTasks
4
{
5
using System;
6
using System.IO;
7
- using System.Runtime.InteropServices;
8
- using Microsoft.Build.Framework;
7
using Microsoft.Build.Utilities;
10
- using WixToolset.Core;
11
- using WixToolset.Data;
12
- using WixToolset.Extensibility;
13
- using WixToolset.Extensibility.Services;
8
15
- public abstract class ToolsetTask : ToolTask
9
+ public abstract partial class ToolsetTask : ToolTask
10
{
11
/// <summary>
12
/// Gets or sets additional options that are appended the the tool command-line.
@@ -59,49 +53,6 @@ namespace WixToolset.BuildTasks
53
/// </summary>
54
public bool VerboseOutput { get; set; }
55
62
- protected sealed override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
63
- {
64
- if (this.RunAsSeparateProcess)
65
- {
66
- return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
67
- }
68
-
69
- return this.ExecuteInProc($"{commandLineCommands} {responseFileCommands}");
70
- }
71
-
72
- private int ExecuteInProc(string commandLineString)
73
- {
74
- this.Log.LogMessage(MessageImportance.Normal, $"({this.ToolName}){commandLineString}");
75
-
76
- var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider();
77
- var listener = new MsbuildMessageListener(this.Log, this.TaskShortName, this.BuildEngine.ProjectFileOfTaskNode);
78
- int exitCode = -1;
79
-
80
- try
81
- {
82
- exitCode = this.ExecuteCore(serviceProvider, listener, commandLineString);
83
- }
84
- catch (WixException e)
85
- {
86
- listener.Write(e.Error);
87
- }
88
- catch (Exception e)
89
- {
90
- this.Log.LogErrorFromException(e, showStackTrace: true, showDetail: true, null);
91
-
92
- if (e is NullReferenceException || e is SEHException)
93
- {
94
- throw;
95
- }
96
- }
97
-
98
- if (exitCode == 0 && this.Log.HasLoggedErrors)
99
- {
100
- exitCode = -1;
101
- }
102
- return exitCode;
103
- }
104
-
56
/// <summary>
57
/// Get the path to the executable.
58
/// </summary>
@@ -113,13 +64,12 @@ namespace WixToolset.BuildTasks
64
protected sealed override string GenerateFullPathToTool()
65
{
66
var thisDllPath = new Uri(typeof(ToolsetTask).Assembly.CodeBase).AbsolutePath;
116
- if (this.RunAsSeparateProcess)
67
+ if (!this.RunAsSeparateProcess)
68
{
118
- return Path.Combine(Path.GetDirectoryName(thisDllPath), this.ToolExe);
69
+ // We need to return a path that exists, so if we're not actually going to run the tool then just return this dll path.
70
+ return thisDllPath;
71
}
120
-
121
- // We need to return a path that exists, so if we're not actually going to run the tool then just return this dll path.
122
- return thisDllPath;
72
+ return Path.Combine(Path.GetDirectoryName(thisDllPath), this.ToolExe);
73
}
74
75
protected sealed override string GenerateResponseFileCommands()
@@ -129,15 +79,6 @@ namespace WixToolset.BuildTasks
79
return commandLineBuilder.ToString();
80
}
81
132
- protected sealed override void LogToolCommand(string message)
133
- {
134
- // Only log this if we're actually going to do it.
135
- if (this.RunAsSeparateProcess)
136
- {
137
- base.LogToolCommand(message);
138
- }
139
- }
140
-
82
/// <summary>
83
/// Builds a command line from options in this and derivative tasks.
84
/// </summary>
@@ -153,9 +94,5 @@ namespace WixToolset.BuildTasks
94
commandLineBuilder.AppendArrayIfNotNull("-wx ", this.TreatSpecificWarningsAsErrors);
95
commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors);
96
}
156
-
157
- protected abstract int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener messageListener, string commandLineString);
158
-
159
- protected abstract string TaskShortName { get; }
97
}
98
}
src/WixToolset.BuildTasks/ToolsetTask_InProc.cs
new
+71
@@ -0,0 +1,71 @@
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.BuildTasks
4
+{
5
+ using System;
6
+ using System.Runtime.InteropServices;
7
+ using Microsoft.Build.Framework;
8
+ using WixToolset.Core;
9
+ using WixToolset.Data;
10
+ using WixToolset.Extensibility;
11
+ using WixToolset.Extensibility.Services;
12
+
13
+ public partial class ToolsetTask
14
+ {
15
+ protected sealed override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
16
+ {
17
+ if (this.RunAsSeparateProcess)
18
+ {
19
+ return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
20
+ }
21
+
22
+ return this.ExecuteInProc($"{commandLineCommands} {responseFileCommands}");
23
+ }
24
+
25
+ private int ExecuteInProc(string commandLineString)
26
+ {
27
+ this.Log.LogMessage(MessageImportance.Normal, $"({this.ToolName}){commandLineString}");
28
+
29
+ var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider();
30
+ var listener = new MsbuildMessageListener(this.Log, this.TaskShortName, this.BuildEngine.ProjectFileOfTaskNode);
31
+ int exitCode = -1;
32
+
33
+ try
34
+ {
35
+ exitCode = this.ExecuteCore(serviceProvider, listener, commandLineString);
36
+ }
37
+ catch (WixException e)
38
+ {
39
+ listener.Write(e.Error);
40
+ }
41
+ catch (Exception e)
42
+ {
43
+ this.Log.LogErrorFromException(e, showStackTrace: true, showDetail: true, null);
44
+
45
+ if (e is NullReferenceException || e is SEHException)
46
+ {
47
+ throw;
48
+ }
49
+ }
50
+
51
+ if (exitCode == 0 && this.Log.HasLoggedErrors)
52
+ {
53
+ exitCode = -1;
54
+ }
55
+ return exitCode;
56
+ }
57
+
58
+ protected sealed override void LogToolCommand(string message)
59
+ {
60
+ // Only log this if we're actually going to do it.
61
+ if (this.RunAsSeparateProcess)
62
+ {
63
+ base.LogToolCommand(message);
64
+ }
65
+ }
66
+
67
+ protected abstract int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener messageListener, string commandLineString);
68
+
69
+ protected abstract string TaskShortName { get; }
70
+ }
71
+}
src/WixToolset.BuildTasks/WixBuild.cs
+1
-45
@@ -5,15 +5,11 @@ namespace WixToolset.BuildTasks
5
using System;
6
using System.Collections.Generic;
7
using Microsoft.Build.Framework;
8
- using WixToolset.Data;
9
- using WixToolset.Extensibility;
10
- using WixToolset.Extensibility.Data;
11
- using WixToolset.Extensibility.Services;
8
9
/// <summary>
10
/// An MSBuild task to run the WiX compiler.
11
/// </summary>
16
- public sealed class WixBuild : ToolsetTask
12
+ public sealed partial class WixBuild : ToolsetTask
13
{
14
public string[] Cultures { get; set; }
15
@@ -76,24 +72,8 @@ namespace WixToolset.BuildTasks
72
public string[] SuppressIces { get; set; }
73
public string AdditionalCub { get; set; }
74
79
- protected override string TaskShortName => "WIX";
75
protected override string ToolName => "wix.exe";
76
82
- protected override int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener listener, string commandLineString)
83
- {
84
- var messaging = serviceProvider.GetService<IMessaging>();
85
- messaging.SetListener(listener);
86
-
87
- var arguments = serviceProvider.GetService<ICommandLineArguments>();
88
- arguments.Populate(commandLineString);
89
-
90
- var commandLine = serviceProvider.GetService<ICommandLine>();
91
- commandLine.ExtensionManager = this.CreateExtensionManagerWithStandardBackends(serviceProvider, messaging, arguments.Extensions);
92
- commandLine.Arguments = arguments;
93
- var command = commandLine.ParseStandardCommandLine();
94
- return command?.Execute() ?? -1;
95
- }
96
-
77
protected override void BuildCommandLine(WixCommandLineBuilder commandLineBuilder)
78
{
79
commandLineBuilder.AppendTextUnquoted("build");
@@ -126,30 +106,6 @@ namespace WixToolset.BuildTasks
106
commandLineBuilder.AppendFileNamesIfNotNull(this.SourceFiles, " ");
107
}
108
129
- private IExtensionManager CreateExtensionManagerWithStandardBackends(IWixToolsetServiceProvider serviceProvider, IMessaging messaging, string[] extensions)
130
- {
131
- var extensionManager = serviceProvider.GetService<IExtensionManager>();
132
-
133
- foreach (var type in new[] { typeof(WixToolset.Core.Burn.WixToolsetStandardBackend), typeof(WixToolset.Core.WindowsInstaller.WixToolsetStandardBackend) })
134
- {
135
- extensionManager.Add(type.Assembly);
136
- }
137
-
138
- foreach (var extension in extensions)
139
- {
140
- try
141
- {
142
- extensionManager.Load(extension);
143
- }
144
- catch (WixException e)
145
- {
146
- messaging.Write(e.Error);
147
- }
148
- }
149
-
150
- return extensionManager;
151
- }
152
-
109
private IEnumerable<string> CalculateBindPathStrings()
110
{
111
if (null != this.BindInputPaths)
src/WixToolset.BuildTasks/WixBuild_InProc.cs
new
+53
@@ -0,0 +1,53 @@
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.BuildTasks
4
+{
5
+ using WixToolset.Data;
6
+ using WixToolset.Extensibility;
7
+ using WixToolset.Extensibility.Data;
8
+ using WixToolset.Extensibility.Services;
9
+
10
+ public partial class WixBuild
11
+ {
12
+ protected override string TaskShortName => "WIX";
13
+
14
+ protected override int ExecuteCore(IWixToolsetServiceProvider serviceProvider, IMessageListener listener, string commandLineString)
15
+ {
16
+ var messaging = serviceProvider.GetService<IMessaging>();
17
+ messaging.SetListener(listener);
18
+
19
+ var arguments = serviceProvider.GetService<ICommandLineArguments>();
20
+ arguments.Populate(commandLineString);
21
+
22
+ var commandLine = serviceProvider.GetService<ICommandLine>();
23
+ commandLine.ExtensionManager = this.CreateExtensionManagerWithStandardBackends(serviceProvider, messaging, arguments.Extensions);
24
+ commandLine.Arguments = arguments;
25
+ var command = commandLine.ParseStandardCommandLine();
26
+ return command?.Execute() ?? -1;
27
+ }
28
+
29
+ private IExtensionManager CreateExtensionManagerWithStandardBackends(IWixToolsetServiceProvider serviceProvider, IMessaging messaging, string[] extensions)
30
+ {
31
+ var extensionManager = serviceProvider.GetService<IExtensionManager>();
32
+
33
+ foreach (var type in new[] { typeof(WixToolset.Core.Burn.WixToolsetStandardBackend), typeof(WixToolset.Core.WindowsInstaller.WixToolsetStandardBackend) })
34
+ {
35
+ extensionManager.Add(type.Assembly);
36
+ }
37
+
38
+ foreach (var extension in extensions)
39
+ {
40
+ try
41
+ {
42
+ extensionManager.Load(extension);
43
+ }
44
+ catch (WixException e)
45
+ {
46
+ messaging.Write(e.Error);
47
+ }
48
+ }
49
+
50
+ return extensionManager;
51
+ }
52
+ }
53
+}