diff options
| author | Rob Mensching <rob@firegiant.com> | 2022-11-08 14:58:05 -0800 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2022-11-08 16:20:25 -0800 |
| commit | c843b47d6233153fa961c6d0e61edf7cedf255bb (patch) | |
| tree | 9eae6badd42d3badb8665b7414b4d44ca48d6ae1 /src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs | |
| parent | 7e498d6348c26583972ea1cdf7d51dadc8f5b792 (diff) | |
| download | wix-c843b47d6233153fa961c6d0e61edf7cedf255bb.tar.gz wix-c843b47d6233153fa961c6d0e61edf7cedf255bb.tar.bz2 wix-c843b47d6233153fa961c6d0e61edf7cedf255bb.zip | |
Separate WixInternal content from official WixToolset namespace
Diffstat (limited to 'src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs')
| -rw-r--r-- | src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs b/src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs new file mode 100644 index 00000000..d9e3b5e8 --- /dev/null +++ b/src/internal/WixInternal.BaseBuildTasks.Sources/BaseToolsetTask.cs | |||
| @@ -0,0 +1,178 @@ | |||
| 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.BaseBuildTasks | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.IO; | ||
| 7 | using System.Runtime.InteropServices; | ||
| 8 | using Microsoft.Build.Utilities; | ||
| 9 | |||
| 10 | public abstract class BaseToolsetTask : ToolTask | ||
| 11 | { | ||
| 12 | /// <summary> | ||
| 13 | /// Gets or sets additional options that are appended the the tool command-line. | ||
| 14 | /// </summary> | ||
| 15 | /// <remarks> | ||
| 16 | /// This allows the task to support extended options in the tool which are not | ||
| 17 | /// explicitly implemented as properties on the task. | ||
| 18 | /// </remarks> | ||
| 19 | public string AdditionalOptions { get; set; } | ||
| 20 | |||
| 21 | /// <summary> | ||
| 22 | /// Gets or sets whether to display the logo. | ||
| 23 | /// </summary> | ||
| 24 | public bool NoLogo { get; set; } | ||
| 25 | |||
| 26 | /// <summary> | ||
| 27 | /// Gets or sets whether all warnings should be suppressed. | ||
| 28 | /// </summary> | ||
| 29 | public bool SuppressAllWarnings { get; set; } | ||
| 30 | |||
| 31 | /// <summary> | ||
| 32 | /// Gets or sets a list of specific warnings to be suppressed. | ||
| 33 | /// </summary> | ||
| 34 | public string[] SuppressSpecificWarnings { get; set; } | ||
| 35 | |||
| 36 | /// <summary> | ||
| 37 | /// Gets or sets whether all warnings should be treated as errors. | ||
| 38 | /// </summary> | ||
| 39 | public bool TreatWarningsAsErrors { get; set; } | ||
| 40 | |||
| 41 | /// <summary> | ||
| 42 | /// Gets or sets a list of specific warnings to treat as errors. | ||
| 43 | /// </summary> | ||
| 44 | public string[] TreatSpecificWarningsAsErrors { get; set; } | ||
| 45 | |||
| 46 | /// <summary> | ||
| 47 | /// Gets or sets whether to display verbose output. | ||
| 48 | /// </summary> | ||
| 49 | public bool VerboseOutput { get; set; } | ||
| 50 | |||
| 51 | /// <summary> | ||
| 52 | /// Get the path to the executable. | ||
| 53 | /// </summary> | ||
| 54 | /// <remarks> | ||
| 55 | /// ToolTask only calls GenerateFullPathToTool when the ToolPath property is not set. | ||
| 56 | /// WiX never sets the ToolPath property, but the user can through $(WixToolDir). | ||
| 57 | /// If we return only a file name, ToolTask will search the system paths for it. | ||
| 58 | /// </remarks> | ||
| 59 | protected sealed override string GenerateFullPathToTool() | ||
| 60 | { | ||
| 61 | var defaultToolFullPath = this.GetDefaultToolFullPath(); | ||
| 62 | |||
| 63 | #if NETCOREAPP | ||
| 64 | // If we're pointing at an executable use that. | ||
| 65 | if (IsSelfExecutable(defaultToolFullPath, out var finalToolFullPath)) | ||
| 66 | { | ||
| 67 | return finalToolFullPath; | ||
| 68 | } | ||
| 69 | |||
| 70 | // Otherwise, use "dotnet.exe" to run an assembly dll. | ||
| 71 | return Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet"; | ||
| 72 | #else | ||
| 73 | return defaultToolFullPath; | ||
| 74 | #endif | ||
| 75 | } | ||
| 76 | |||
| 77 | /// <summary> | ||
| 78 | /// Builds a command line from options in this and derivative tasks. | ||
| 79 | /// </summary> | ||
| 80 | /// <remarks> | ||
| 81 | /// Derivative classes should call BuildCommandLine() on the base class to ensure that common command line options are added to the command. | ||
| 82 | /// </remarks> | ||
| 83 | protected virtual void BuildCommandLine(WixCommandLineBuilder commandLineBuilder) | ||
| 84 | { | ||
| 85 | commandLineBuilder.AppendIfTrue("-nologo", this.NoLogo); | ||
| 86 | commandLineBuilder.AppendArrayIfNotNull("-sw", this.SuppressSpecificWarnings); | ||
| 87 | commandLineBuilder.AppendIfTrue("-sw", this.SuppressAllWarnings); | ||
| 88 | commandLineBuilder.AppendIfTrue("-v", this.VerboseOutput); | ||
| 89 | commandLineBuilder.AppendArrayIfNotNull("-wx", this.TreatSpecificWarningsAsErrors); | ||
| 90 | commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors); | ||
| 91 | commandLineBuilder.AppendTextIfNotNull(this.AdditionalOptions); | ||
| 92 | } | ||
| 93 | |||
| 94 | protected sealed override string GenerateResponseFileCommands() | ||
| 95 | { | ||
| 96 | var commandLineBuilder = new WixCommandLineBuilder(); | ||
| 97 | this.BuildCommandLine(commandLineBuilder); | ||
| 98 | return commandLineBuilder.ToString(); | ||
| 99 | } | ||
| 100 | |||
| 101 | #if NETCOREAPP | ||
| 102 | protected override string GenerateCommandLineCommands() | ||
| 103 | { | ||
| 104 | // If the target tool path is an executable, we don't need to add anything to the command-line. | ||
| 105 | var toolFullPath = this.GetToolFullPath(); | ||
| 106 | |||
| 107 | if (IsSelfExecutable(toolFullPath, out var finalToolFullPath)) | ||
| 108 | { | ||
| 109 | return null; | ||
| 110 | } | ||
| 111 | else // we're using "dotnet.exe" to run the assembly so add "exec" plus path to the command-line. | ||
| 112 | { | ||
| 113 | return $"exec \"{finalToolFullPath}\""; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | private static bool IsSelfExecutable(string proposedToolFullPath, out string finalToolFullPath) | ||
| 118 | { | ||
| 119 | var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath)); | ||
| 120 | var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty; | ||
| 121 | var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}"; | ||
| 122 | if (File.Exists(exeToolFullPath)) | ||
| 123 | { | ||
| 124 | finalToolFullPath = exeToolFullPath; | ||
| 125 | return true; | ||
| 126 | } | ||
| 127 | |||
| 128 | finalToolFullPath = $"{toolFullPathWithoutExtension}.dll"; | ||
| 129 | return false; | ||
| 130 | } | ||
| 131 | #else | ||
| 132 | private static string GetArchitectureFolder(string baseFolder) | ||
| 133 | { | ||
| 134 | // First try to find a folder that matches this task's architecture. | ||
| 135 | var folder = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); | ||
| 136 | |||
| 137 | if (Directory.Exists(Path.Combine(baseFolder, folder))) | ||
| 138 | { | ||
| 139 | return folder; | ||
| 140 | } | ||
| 141 | |||
| 142 | // Try to fallback to "x86" folder. | ||
| 143 | if (folder != "x86" && Directory.Exists(Path.Combine(baseFolder, "x86"))) | ||
| 144 | { | ||
| 145 | return "x86"; | ||
| 146 | } | ||
| 147 | |||
| 148 | // Return empty, even though this isn't likely to be useful. | ||
| 149 | return String.Empty; | ||
| 150 | } | ||
| 151 | #endif | ||
| 152 | |||
| 153 | private string GetDefaultToolFullPath() | ||
| 154 | { | ||
| 155 | #if NETCOREAPP | ||
| 156 | var thisTaskFolder = Path.GetDirectoryName(typeof(BaseToolsetTask).Assembly.Location); | ||
| 157 | |||
| 158 | return Path.Combine(thisTaskFolder, this.ToolExe); | ||
| 159 | #else | ||
| 160 | var thisTaskFolder = Path.GetDirectoryName(new Uri(typeof(BaseToolsetTask).Assembly.CodeBase).AbsolutePath); | ||
| 161 | |||
| 162 | var archFolder = GetArchitectureFolder(thisTaskFolder); | ||
| 163 | |||
| 164 | return Path.Combine(thisTaskFolder, archFolder, this.ToolExe); | ||
| 165 | #endif | ||
| 166 | } | ||
| 167 | |||
| 168 | private string GetToolFullPath() | ||
| 169 | { | ||
| 170 | if (String.IsNullOrEmpty(this.ToolPath)) | ||
| 171 | { | ||
| 172 | return this.GetDefaultToolFullPath(); | ||
| 173 | } | ||
| 174 | |||
| 175 | return Path.Combine(this.ToolPath, this.ToolExe); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
