aboutsummaryrefslogtreecommitdiff
path: root/src/wix/WixToolset.BuildTasks/ToolsetTask.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wix/WixToolset.BuildTasks/ToolsetTask.cs')
-rw-r--r--src/wix/WixToolset.BuildTasks/ToolsetTask.cs153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/wix/WixToolset.BuildTasks/ToolsetTask.cs b/src/wix/WixToolset.BuildTasks/ToolsetTask.cs
new file mode 100644
index 00000000..d48f2a20
--- /dev/null
+++ b/src/wix/WixToolset.BuildTasks/ToolsetTask.cs
@@ -0,0 +1,153 @@
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.BuildTasks
4{
5 using System;
6 using System.IO;
7 using System.Runtime.InteropServices;
8 using Microsoft.Build.Utilities;
9
10 public abstract partial class ToolsetTask : ToolTask
11 {
12 private static readonly string ThisDllPath = new Uri(typeof(ToolsetTask).Assembly.CodeBase).AbsolutePath;
13
14 /// <summary>
15 /// Gets or sets additional options that are appended the the tool command-line.
16 /// </summary>
17 /// <remarks>
18 /// This allows the task to support extended options in the tool which are not
19 /// explicitly implemented as properties on the task.
20 /// </remarks>
21 public string AdditionalOptions { get; set; }
22
23 /// <summary>
24 /// Gets or sets whether to display the logo.
25 /// </summary>
26 public bool NoLogo { get; set; }
27
28 /// <summary>
29 /// Gets or sets a flag indicating whether the task
30 /// should be run as separate process or in-proc.
31 /// </summary>
32 public virtual bool RunAsSeparateProcess { get; set; }
33
34 /// <summary>
35 /// Gets or sets whether all warnings should be suppressed.
36 /// </summary>
37 public bool SuppressAllWarnings { get; set; }
38
39 /// <summary>
40 /// Gets or sets a list of specific warnings to be suppressed.
41 /// </summary>
42 public string[] SuppressSpecificWarnings { get; set; }
43
44 /// <summary>
45 /// Gets or sets whether all warnings should be treated as errors.
46 /// </summary>
47 public bool TreatWarningsAsErrors { get; set; }
48
49 /// <summary>
50 /// Gets or sets a list of specific warnings to treat as errors.
51 /// </summary>
52 public string[] TreatSpecificWarningsAsErrors { get; set; }
53
54 /// <summary>
55 /// Gets or sets whether to display verbose output.
56 /// </summary>
57 public bool VerboseOutput { get; set; }
58
59 private string DefaultToolFullPath => Path.Combine(Path.GetDirectoryName(ThisDllPath), this.ToolExe);
60
61 private string ToolFullPath
62 {
63 get
64 {
65 if (String.IsNullOrEmpty(this.ToolPath))
66 {
67 return this.DefaultToolFullPath;
68 }
69 return Path.Combine(this.ToolPath, this.ToolExe);
70 }
71 }
72
73 /// <summary>
74 /// Get the path to the executable.
75 /// </summary>
76 /// <remarks>
77 /// ToolTask only calls GenerateFullPathToTool when the ToolPath property is not set.
78 /// WiX never sets the ToolPath property, but the user can through $(WixToolDir).
79 /// If we return only a file name, ToolTask will search the system paths for it.
80 /// </remarks>
81 protected sealed override string GenerateFullPathToTool()
82 {
83#if !NETCOREAPP
84 if (!this.RunAsSeparateProcess)
85 {
86 // 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.
87 return ThisDllPath;
88 }
89 return this.DefaultToolFullPath;
90#else
91 if (IsSelfExecutable(this.DefaultToolFullPath, out var toolFullPath))
92 {
93 return toolFullPath;
94 }
95 return DotnetFullPath;
96#endif
97 }
98
99 protected sealed override string GenerateResponseFileCommands()
100 {
101 var commandLineBuilder = new WixCommandLineBuilder();
102 this.BuildCommandLine(commandLineBuilder);
103 return commandLineBuilder.ToString();
104 }
105
106 /// <summary>
107 /// Builds a command line from options in this and derivative tasks.
108 /// </summary>
109 /// <remarks>
110 /// Derivative classes should call BuildCommandLine() on the base class to ensure that common command line options are added to the command.
111 /// </remarks>
112 protected virtual void BuildCommandLine(WixCommandLineBuilder commandLineBuilder)
113 {
114 commandLineBuilder.AppendIfTrue("-nologo", this.NoLogo);
115 commandLineBuilder.AppendArrayIfNotNull("-sw", this.SuppressSpecificWarnings);
116 commandLineBuilder.AppendIfTrue("-sw", this.SuppressAllWarnings);
117 commandLineBuilder.AppendIfTrue("-v", this.VerboseOutput);
118 commandLineBuilder.AppendArrayIfNotNull("-wx", this.TreatSpecificWarningsAsErrors);
119 commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors);
120 }
121
122#if NETCOREAPP
123 private static readonly string DotnetFullPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet";
124
125 protected override string GenerateCommandLineCommands()
126 {
127 if (IsSelfExecutable(this.ToolFullPath, out var toolFullPath))
128 {
129 return null;
130 }
131 else
132 {
133 return $"exec \"{toolFullPath}\"";
134 }
135 }
136
137 private static bool IsSelfExecutable(string proposedToolFullPath, out string toolFullPath)
138 {
139 var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath));
140 var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty;
141 var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}";
142 if (File.Exists(exeToolFullPath))
143 {
144 toolFullPath = exeToolFullPath;
145 return true;
146 }
147
148 toolFullPath = $"{toolFullPathWithoutExtension}.dll";
149 return false;
150 }
151#endif
152 }
153}