summaryrefslogtreecommitdiff
path: root/src/tools/WixToolset.HeatTasks/HeatTask.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/WixToolset.HeatTasks/HeatTask.cs')
-rw-r--r--src/tools/WixToolset.HeatTasks/HeatTask.cs176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/tools/WixToolset.HeatTasks/HeatTask.cs b/src/tools/WixToolset.HeatTasks/HeatTask.cs
new file mode 100644
index 00000000..8942a7e1
--- /dev/null
+++ b/src/tools/WixToolset.HeatTasks/HeatTask.cs
@@ -0,0 +1,176 @@
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
3namespace WixToolset.HeatTasks
4{
5 using System;
6 using System.IO;
7 using System.Runtime.InteropServices;
8 using Microsoft.Build.Framework;
9 using Microsoft.Build.Utilities;
10
11 /// <summary>
12 /// A base MSBuild task to run the WiX harvester.
13 /// Specific harvester tasks should extend this class.
14 /// </summary>
15 public abstract partial class HeatTask : ToolTask
16 {
17 private static readonly string ThisDllPath = new Uri(typeof(HeatTask).Assembly.CodeBase).AbsolutePath;
18
19 /// <summary>
20 /// Gets or sets additional options that are appended the the tool command-line.
21 /// </summary>
22 /// <remarks>
23 /// This allows the task to support extended options in the tool which are not
24 /// explicitly implemented as properties on the task.
25 /// </remarks>
26 public string AdditionalOptions { get; set; }
27
28 /// <summary>
29 /// Gets or sets whether to display the logo.
30 /// </summary>
31 public bool NoLogo { get; set; }
32
33 /// <summary>
34 /// Gets or sets whether all warnings should be suppressed.
35 /// </summary>
36 public bool SuppressAllWarnings { get; set; }
37
38 /// <summary>
39 /// Gets or sets a list of specific warnings to be suppressed.
40 /// </summary>
41 public string[] SuppressSpecificWarnings { get; set; }
42
43 /// <summary>
44 /// Gets or sets whether all warnings should be treated as errors.
45 /// </summary>
46 public bool TreatWarningsAsErrors { get; set; }
47
48 /// <summary>
49 /// Gets or sets a list of specific warnings to treat as errors.
50 /// </summary>
51 public string[] TreatSpecificWarningsAsErrors { get; set; }
52
53 /// <summary>
54 /// Gets or sets whether to display verbose output.
55 /// </summary>
56 public bool VerboseOutput { get; set; }
57
58 public bool AutogenerateGuids { get; set; }
59
60 public bool GenerateGuidsNow { get; set; }
61
62 [Required]
63 [Output]
64 public ITaskItem OutputFile { get; set; }
65
66 public bool SuppressFragments { get; set; }
67
68 public bool SuppressUniqueIds { get; set; }
69
70 public string[] Transforms { get; set; }
71
72 protected sealed override string ToolName => "heat.exe";
73
74 /// <summary>
75 /// Gets the name of the heat operation performed by the task.
76 /// </summary>
77 /// <remarks>This is the first parameter passed on the heat.exe command-line.</remarks>
78 /// <value>The name of the heat operation performed by the task.</value>
79 protected abstract string OperationName { get; }
80
81 private string ToolFullPath
82 {
83 get
84 {
85 if (String.IsNullOrEmpty(this.ToolPath))
86 {
87 return Path.Combine(Path.GetDirectoryName(ThisDllPath), this.ToolExe);
88 }
89
90 return Path.Combine(this.ToolPath, this.ToolExe);
91 }
92 }
93
94 /// <summary>
95 /// Get the path to the executable.
96 /// </summary>
97 /// <remarks>
98 /// ToolTask only calls GenerateFullPathToTool when the ToolPath property is not set.
99 /// WiX never sets the ToolPath property, but the user can through $(HeatToolDir).
100 /// If we return only a file name, ToolTask will search the system paths for it.
101 /// </remarks>
102 protected sealed override string GenerateFullPathToTool()
103 {
104#if NETCOREAPP
105 // If we're not using heat.exe, use dotnet.exe to exec heat.dll.
106 // See this.GenerateCommandLine() where "exec heat.dll" is added.
107 if (!IsSelfExecutable(this.ToolFullPath, out var toolFullPath))
108 {
109 return DotnetFullPath;
110 }
111
112 return toolFullPath;
113#else
114 return this.ToolFullPath;
115#endif
116 }
117
118 protected sealed override string GenerateCommandLineCommands()
119 {
120 var commandLineBuilder = new WixCommandLineBuilder();
121
122#if NETCOREAPP
123 // If we're using dotnet.exe as the target executable, see this.GenerateFullPathToTool(),
124 // then add "exec heat.dll" to the beginning of the command-line.
125 if (!IsSelfExecutable(this.ToolFullPath, out var toolFullPath))
126 {
127 //commandLineBuilder.AppendSwitchIfNotNull("exec ", toolFullPath);
128 commandLineBuilder.AppendSwitch($"exec \"{toolFullPath}\"");
129 }
130#endif
131
132 this.BuildCommandLine(commandLineBuilder);
133 return commandLineBuilder.ToString();
134 }
135
136 /// <summary>
137 /// Builds a command line from options in this task.
138 /// </summary>
139 protected virtual void BuildCommandLine(WixCommandLineBuilder commandLineBuilder)
140 {
141 commandLineBuilder.AppendIfTrue("-nologo", this.NoLogo);
142 commandLineBuilder.AppendArrayIfNotNull("-sw", this.SuppressSpecificWarnings);
143 commandLineBuilder.AppendIfTrue("-sw", this.SuppressAllWarnings);
144 commandLineBuilder.AppendIfTrue("-v", this.VerboseOutput);
145 commandLineBuilder.AppendArrayIfNotNull("-wx", this.TreatSpecificWarningsAsErrors);
146 commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors);
147
148 commandLineBuilder.AppendIfTrue("-ag", this.AutogenerateGuids);
149 commandLineBuilder.AppendIfTrue("-gg", this.GenerateGuidsNow);
150 commandLineBuilder.AppendIfTrue("-sfrag", this.SuppressFragments);
151 commandLineBuilder.AppendIfTrue("-suid", this.SuppressUniqueIds);
152 commandLineBuilder.AppendArrayIfNotNull("-t ", this.Transforms);
153 commandLineBuilder.AppendTextIfNotNull(this.AdditionalOptions);
154 commandLineBuilder.AppendSwitchIfNotNull("-out ", this.OutputFile);
155 }
156
157#if NETCOREAPP
158 private static readonly string DotnetFullPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet";
159
160 private static bool IsSelfExecutable(string proposedToolFullPath, out string toolFullPath)
161 {
162 var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath));
163 var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty;
164 var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}";
165 if (File.Exists(exeToolFullPath))
166 {
167 toolFullPath = exeToolFullPath;
168 return true;
169 }
170
171 toolFullPath = $"{toolFullPathWithoutExtension}.dll";
172 return false;
173 }
174#endif
175 }
176}