From 07b3d459ea0a45cbef29b98d283edafbab26462a Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 12 Oct 2022 22:01:55 -0700 Subject: Normalize ToolsetTask implementation to call wix.exe and heat.exe Share the ToolsetTask implementation that can find .NET Core and .NET Framework with multiple architectures. Fixes 6951 --- src/tools/WixToolset.HeatTasks/HeatDirectory.cs | 1 + src/tools/WixToolset.HeatTasks/HeatFile.cs | 1 + src/tools/WixToolset.HeatTasks/HeatProject.cs | 1 + src/tools/WixToolset.HeatTasks/HeatTask.cs | 139 +-------------------- .../WixToolset.HeatTasks/WixCommandLineBuilder.cs | 56 --------- .../WixToolset.HeatTasks.csproj | 1 + 6 files changed, 9 insertions(+), 190 deletions(-) delete mode 100644 src/tools/WixToolset.HeatTasks/WixCommandLineBuilder.cs (limited to 'src/tools') diff --git a/src/tools/WixToolset.HeatTasks/HeatDirectory.cs b/src/tools/WixToolset.HeatTasks/HeatDirectory.cs index 8a169055..14a8acfa 100644 --- a/src/tools/WixToolset.HeatTasks/HeatDirectory.cs +++ b/src/tools/WixToolset.HeatTasks/HeatDirectory.cs @@ -3,6 +3,7 @@ namespace WixToolset.HeatTasks { using Microsoft.Build.Framework; + using WixToolset.BaseBuildTasks; public sealed class HeatDirectory : HeatTask { diff --git a/src/tools/WixToolset.HeatTasks/HeatFile.cs b/src/tools/WixToolset.HeatTasks/HeatFile.cs index 83cbc4d1..b39de7da 100644 --- a/src/tools/WixToolset.HeatTasks/HeatFile.cs +++ b/src/tools/WixToolset.HeatTasks/HeatFile.cs @@ -3,6 +3,7 @@ namespace WixToolset.HeatTasks { using Microsoft.Build.Framework; + using WixToolset.BaseBuildTasks; public sealed class HeatFile : HeatTask { diff --git a/src/tools/WixToolset.HeatTasks/HeatProject.cs b/src/tools/WixToolset.HeatTasks/HeatProject.cs index d54f6ad1..998c14ab 100644 --- a/src/tools/WixToolset.HeatTasks/HeatProject.cs +++ b/src/tools/WixToolset.HeatTasks/HeatProject.cs @@ -3,6 +3,7 @@ namespace WixToolset.HeatTasks { using Microsoft.Build.Framework; + using WixToolset.BaseBuildTasks; public sealed class HeatProject : HeatTask { diff --git a/src/tools/WixToolset.HeatTasks/HeatTask.cs b/src/tools/WixToolset.HeatTasks/HeatTask.cs index 6b7b9399..cad6635b 100644 --- a/src/tools/WixToolset.HeatTasks/HeatTask.cs +++ b/src/tools/WixToolset.HeatTasks/HeatTask.cs @@ -2,64 +2,15 @@ namespace WixToolset.HeatTasks { - using System; - using System.IO; - using System.Runtime.InteropServices; using Microsoft.Build.Framework; - using Microsoft.Build.Utilities; + using WixToolset.BaseBuildTasks; /// /// A base MSBuild task to run the WiX harvester. /// Specific harvester tasks should extend this class. /// - public abstract partial class HeatTask : ToolTask + public abstract partial class HeatTask : BaseToolsetTask { -#if NETFRAMEWORK - private static readonly string ThisDllPath = new Uri(typeof(HeatTask).Assembly.CodeBase).AbsolutePath; -#else - private static readonly string ThisDllPath = typeof(HeatTask).Assembly.Location; -#endif - - - /// - /// Gets or sets additional options that are appended the the tool command-line. - /// - /// - /// This allows the task to support extended options in the tool which are not - /// explicitly implemented as properties on the task. - /// - public string AdditionalOptions { get; set; } - - /// - /// Gets or sets whether to display the logo. - /// - public bool NoLogo { get; set; } - - /// - /// Gets or sets whether all warnings should be suppressed. - /// - public bool SuppressAllWarnings { get; set; } - - /// - /// Gets or sets a list of specific warnings to be suppressed. - /// - public string[] SuppressSpecificWarnings { get; set; } - - /// - /// Gets or sets whether all warnings should be treated as errors. - /// - public bool TreatWarningsAsErrors { get; set; } - - /// - /// Gets or sets a list of specific warnings to treat as errors. - /// - public string[] TreatSpecificWarningsAsErrors { get; set; } - - /// - /// Gets or sets whether to display verbose output. - /// - public bool VerboseOutput { get; set; } - public bool AutogenerateGuids { get; set; } public bool GenerateGuidsNow { get; set; } @@ -74,8 +25,6 @@ namespace WixToolset.HeatTasks public string[] Transforms { get; set; } - protected sealed override string ToolName => "heat.exe"; - /// /// Gets the name of the heat operation performed by the task. /// @@ -83,99 +32,21 @@ namespace WixToolset.HeatTasks /// The name of the heat operation performed by the task. protected abstract string OperationName { get; } - private string ToolFullPath - { - get - { - if (String.IsNullOrEmpty(this.ToolPath)) - { - return Path.Combine(Path.GetDirectoryName(ThisDllPath), this.ToolExe); - } - - return Path.Combine(this.ToolPath, this.ToolExe); - } - } - - /// - /// Get the path to the executable. - /// - /// - /// ToolTask only calls GenerateFullPathToTool when the ToolPath property is not set. - /// WiX never sets the ToolPath property, but the user can through $(HeatToolDir). - /// If we return only a file name, ToolTask will search the system paths for it. - /// - protected sealed override string GenerateFullPathToTool() - { -#if NETCOREAPP - // If we're not using heat.exe, use dotnet.exe to exec heat.dll. - // See this.GenerateCommandLine() where "exec heat.dll" is added. - if (!IsSelfExecutable(this.ToolFullPath, out var toolFullPath)) - { - return DotnetFullPath; - } - - return toolFullPath; -#else - return this.ToolFullPath; -#endif - } - - protected sealed override string GenerateCommandLineCommands() - { - var commandLineBuilder = new WixCommandLineBuilder(); - -#if NETCOREAPP - // If we're using dotnet.exe as the target executable, see this.GenerateFullPathToTool(), - // then add "exec heat.dll" to the beginning of the command-line. - if (!IsSelfExecutable(this.ToolFullPath, out var toolFullPath)) - { - //commandLineBuilder.AppendSwitchIfNotNull("exec ", toolFullPath); - commandLineBuilder.AppendSwitch($"exec \"{toolFullPath}\""); - } -#endif - - this.BuildCommandLine(commandLineBuilder); - return commandLineBuilder.ToString(); - } + protected sealed override string ToolName => "heat.exe"; /// /// Builds a command line from options in this task. /// - protected virtual void BuildCommandLine(WixCommandLineBuilder commandLineBuilder) + protected override void BuildCommandLine(WixCommandLineBuilder commandLineBuilder) { - commandLineBuilder.AppendIfTrue("-nologo", this.NoLogo); - commandLineBuilder.AppendArrayIfNotNull("-sw", this.SuppressSpecificWarnings); - commandLineBuilder.AppendIfTrue("-sw", this.SuppressAllWarnings); - commandLineBuilder.AppendIfTrue("-v", this.VerboseOutput); - commandLineBuilder.AppendArrayIfNotNull("-wx", this.TreatSpecificWarningsAsErrors); - commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors); + base.BuildCommandLine(commandLineBuilder); commandLineBuilder.AppendIfTrue("-ag", this.AutogenerateGuids); commandLineBuilder.AppendIfTrue("-gg", this.GenerateGuidsNow); commandLineBuilder.AppendIfTrue("-sfrag", this.SuppressFragments); commandLineBuilder.AppendIfTrue("-suid", this.SuppressUniqueIds); commandLineBuilder.AppendArrayIfNotNull("-t ", this.Transforms); - commandLineBuilder.AppendTextIfNotNull(this.AdditionalOptions); commandLineBuilder.AppendSwitchIfNotNull("-out ", this.OutputFile); } - -#if NETCOREAPP - private static readonly string DotnetFullPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet"; - - private static bool IsSelfExecutable(string proposedToolFullPath, out string toolFullPath) - { - var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath)); - var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty; - var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}"; - if (File.Exists(exeToolFullPath)) - { - toolFullPath = exeToolFullPath; - return true; - } - - toolFullPath = $"{toolFullPathWithoutExtension}.dll"; - return false; - } -#endif } } diff --git a/src/tools/WixToolset.HeatTasks/WixCommandLineBuilder.cs b/src/tools/WixToolset.HeatTasks/WixCommandLineBuilder.cs deleted file mode 100644 index c3989902..00000000 --- a/src/tools/WixToolset.HeatTasks/WixCommandLineBuilder.cs +++ /dev/null @@ -1,56 +0,0 @@ -// 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.HeatTasks -{ - using System; - using System.Collections.Generic; - using Microsoft.Build.Utilities; - - /// - /// Helper class for appending the command line arguments. - /// - public class WixCommandLineBuilder : CommandLineBuilder - { - /// - /// Append a switch to the command line if the condition is true. - /// - /// Switch to append. - /// Condition specified by the user. - public void AppendIfTrue(string switchName, bool condition) - { - if (condition) - { - this.AppendSwitch(switchName); - } - } - - /// - /// Append a switch to the command line if any values in the array have been specified. - /// - /// Switch to append. - /// Values specified by the user. - public void AppendArrayIfNotNull(string switchName, IEnumerable values) - { - if (values != null) - { - foreach (var value in values) - { - this.AppendSwitchIfNotNull(switchName, value); - } - } - } - - /// - /// Append arbitrary text to the command-line if specified. - /// - /// Text to append. - public void AppendTextIfNotNull(string textToAppend) - { - if (!String.IsNullOrWhiteSpace(textToAppend)) - { - this.AppendSpaceIfNotEmpty(); - this.AppendTextUnquoted(textToAppend); - } - } - } -} diff --git a/src/tools/WixToolset.HeatTasks/WixToolset.HeatTasks.csproj b/src/tools/WixToolset.HeatTasks/WixToolset.HeatTasks.csproj index 21fb419c..a8538d9e 100644 --- a/src/tools/WixToolset.HeatTasks/WixToolset.HeatTasks.csproj +++ b/src/tools/WixToolset.HeatTasks/WixToolset.HeatTasks.csproj @@ -12,5 +12,6 @@ + -- cgit v1.2.3-55-g6feb