diff options
Diffstat (limited to 'src/wix/WixToolset.BuildTasks/ToolsetTask.cs')
-rw-r--r-- | src/wix/WixToolset.BuildTasks/ToolsetTask.cs | 121 |
1 files changed, 71 insertions, 50 deletions
diff --git a/src/wix/WixToolset.BuildTasks/ToolsetTask.cs b/src/wix/WixToolset.BuildTasks/ToolsetTask.cs index 7d4a7687..3eee7625 100644 --- a/src/wix/WixToolset.BuildTasks/ToolsetTask.cs +++ b/src/wix/WixToolset.BuildTasks/ToolsetTask.cs | |||
@@ -7,15 +7,8 @@ namespace WixToolset.BuildTasks | |||
7 | using System.Runtime.InteropServices; | 7 | using System.Runtime.InteropServices; |
8 | using Microsoft.Build.Utilities; | 8 | using Microsoft.Build.Utilities; |
9 | 9 | ||
10 | public abstract partial class ToolsetTask : ToolTask | 10 | public abstract class ToolsetTask : ToolTask |
11 | { | 11 | { |
12 | #if NETCOREAPP | ||
13 | private static readonly string DotnetFullPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet"; | ||
14 | private static readonly string ThisDllPath = typeof(ToolsetTask).Assembly.Location; | ||
15 | #else | ||
16 | private static readonly string ThisDllPath = new Uri(typeof(ToolsetTask).Assembly.CodeBase).AbsolutePath; | ||
17 | #endif | ||
18 | |||
19 | /// <summary> | 12 | /// <summary> |
20 | /// Gets or sets additional options that are appended the the tool command-line. | 13 | /// Gets or sets additional options that are appended the the tool command-line. |
21 | /// </summary> | 14 | /// </summary> |
@@ -31,12 +24,6 @@ namespace WixToolset.BuildTasks | |||
31 | public bool NoLogo { get; set; } | 24 | public bool NoLogo { get; set; } |
32 | 25 | ||
33 | /// <summary> | 26 | /// <summary> |
34 | /// Gets or sets a flag indicating whether the task | ||
35 | /// should be run as separate process or in-proc. | ||
36 | /// </summary> | ||
37 | public virtual bool RunAsSeparateProcess { get; set; } | ||
38 | |||
39 | /// <summary> | ||
40 | /// Gets or sets whether all warnings should be suppressed. | 27 | /// Gets or sets whether all warnings should be suppressed. |
41 | /// </summary> | 28 | /// </summary> |
42 | public bool SuppressAllWarnings { get; set; } | 29 | public bool SuppressAllWarnings { get; set; } |
@@ -61,20 +48,6 @@ namespace WixToolset.BuildTasks | |||
61 | /// </summary> | 48 | /// </summary> |
62 | public bool VerboseOutput { get; set; } | 49 | public bool VerboseOutput { get; set; } |
63 | 50 | ||
64 | private string DefaultToolFullPath => Path.Combine(Path.GetDirectoryName(ThisDllPath), this.ToolExe); | ||
65 | |||
66 | private string ToolFullPath | ||
67 | { | ||
68 | get | ||
69 | { | ||
70 | if (String.IsNullOrEmpty(this.ToolPath)) | ||
71 | { | ||
72 | return this.DefaultToolFullPath; | ||
73 | } | ||
74 | return Path.Combine(this.ToolPath, this.ToolExe); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | /// <summary> | 51 | /// <summary> |
79 | /// Get the path to the executable. | 52 | /// Get the path to the executable. |
80 | /// </summary> | 53 | /// </summary> |
@@ -85,29 +58,22 @@ namespace WixToolset.BuildTasks | |||
85 | /// </remarks> | 58 | /// </remarks> |
86 | protected sealed override string GenerateFullPathToTool() | 59 | protected sealed override string GenerateFullPathToTool() |
87 | { | 60 | { |
61 | var defaultToolFullPath = this.GetDefaultToolFullPath(); | ||
62 | |||
88 | #if NETCOREAPP | 63 | #if NETCOREAPP |
89 | if (IsSelfExecutable(this.DefaultToolFullPath, out var toolFullPath)) | 64 | // If we're pointing at an executable use that. |
65 | if (IsSelfExecutable(defaultToolFullPath, out var finalToolFullPath)) | ||
90 | { | 66 | { |
91 | return toolFullPath; | 67 | return finalToolFullPath; |
92 | } | 68 | } |
93 | return DotnetFullPath; | 69 | |
70 | // Otherwise, use "dotnet.exe" to run an assembly dll. | ||
71 | return Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet"; | ||
94 | #else | 72 | #else |
95 | if (!this.RunAsSeparateProcess) | 73 | return defaultToolFullPath; |
96 | { | ||
97 | // 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. | ||
98 | return ThisDllPath; | ||
99 | } | ||
100 | return this.DefaultToolFullPath; | ||
101 | #endif | 74 | #endif |
102 | } | 75 | } |
103 | 76 | ||
104 | protected sealed override string GenerateResponseFileCommands() | ||
105 | { | ||
106 | var commandLineBuilder = new WixCommandLineBuilder(); | ||
107 | this.BuildCommandLine(commandLineBuilder); | ||
108 | return commandLineBuilder.ToString(); | ||
109 | } | ||
110 | |||
111 | /// <summary> | 77 | /// <summary> |
112 | /// Builds a command line from options in this and derivative tasks. | 78 | /// Builds a command line from options in this and derivative tasks. |
113 | /// </summary> | 79 | /// </summary> |
@@ -124,33 +90,88 @@ namespace WixToolset.BuildTasks | |||
124 | commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors); | 90 | commandLineBuilder.AppendIfTrue("-wx", this.TreatWarningsAsErrors); |
125 | } | 91 | } |
126 | 92 | ||
93 | protected sealed override string GenerateResponseFileCommands() | ||
94 | { | ||
95 | var commandLineBuilder = new WixCommandLineBuilder(); | ||
96 | this.BuildCommandLine(commandLineBuilder); | ||
97 | return commandLineBuilder.ToString(); | ||
98 | } | ||
99 | |||
127 | #if NETCOREAPP | 100 | #if NETCOREAPP |
128 | protected override string GenerateCommandLineCommands() | 101 | protected override string GenerateCommandLineCommands() |
129 | { | 102 | { |
130 | if (IsSelfExecutable(this.ToolFullPath, out var toolFullPath)) | 103 | // If the target tool path is an executable, we don't need to add anything to the command-line. |
104 | var toolFullPath = this.GetToolFullPath(); | ||
105 | |||
106 | if (IsSelfExecutable(toolFullPath, out var finalToolFullPath)) | ||
131 | { | 107 | { |
132 | return null; | 108 | return null; |
133 | } | 109 | } |
134 | else | 110 | else // we're using "dotnet.exe" to run the assembly so add "exec" plus path to the command-line. |
135 | { | 111 | { |
136 | return $"exec \"{toolFullPath}\""; | 112 | return $"exec \"{finalToolFullPath}\""; |
137 | } | 113 | } |
138 | } | 114 | } |
139 | 115 | ||
140 | private static bool IsSelfExecutable(string proposedToolFullPath, out string toolFullPath) | 116 | private static bool IsSelfExecutable(string proposedToolFullPath, out string finalToolFullPath) |
141 | { | 117 | { |
142 | var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath)); | 118 | var toolFullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(proposedToolFullPath), Path.GetFileNameWithoutExtension(proposedToolFullPath)); |
143 | var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty; | 119 | var exeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : String.Empty; |
144 | var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}"; | 120 | var exeToolFullPath = $"{toolFullPathWithoutExtension}{exeExtension}"; |
145 | if (File.Exists(exeToolFullPath)) | 121 | if (File.Exists(exeToolFullPath)) |
146 | { | 122 | { |
147 | toolFullPath = exeToolFullPath; | 123 | finalToolFullPath = exeToolFullPath; |
148 | return true; | 124 | return true; |
149 | } | 125 | } |
150 | 126 | ||
151 | toolFullPath = $"{toolFullPathWithoutExtension}.dll"; | 127 | finalToolFullPath = $"{toolFullPathWithoutExtension}.dll"; |
152 | return false; | 128 | return false; |
153 | } | 129 | } |
130 | #else | ||
131 | private static string GetArchitectureFolder(string baseFolder) | ||
132 | { | ||
133 | // First try to find a folder that matches this task's architecture. | ||
134 | var folder = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); | ||
135 | |||
136 | if (Directory.Exists(Path.Combine(baseFolder, folder))) | ||
137 | { | ||
138 | return folder; | ||
139 | } | ||
140 | |||
141 | // Try to fallback to "x86" folder. | ||
142 | if (folder != "x86" && Directory.Exists(Path.Combine(baseFolder, "x86"))) | ||
143 | { | ||
144 | return "x86"; | ||
145 | } | ||
146 | |||
147 | // Return empty, even though this isn't likely to be useful. | ||
148 | return String.Empty; | ||
149 | } | ||
150 | #endif | ||
151 | |||
152 | private string GetDefaultToolFullPath() | ||
153 | { | ||
154 | #if NETCOREAPP | ||
155 | var thisTaskFolder = Path.GetDirectoryName(typeof(ToolsetTask).Assembly.Location); | ||
156 | |||
157 | return Path.Combine(thisTaskFolder, this.ToolExe); | ||
158 | #else | ||
159 | var thisTaskFolder = Path.GetDirectoryName(new Uri(typeof(ToolsetTask).Assembly.CodeBase).AbsolutePath); | ||
160 | |||
161 | var archFolder = GetArchitectureFolder(thisTaskFolder); | ||
162 | |||
163 | return Path.Combine(thisTaskFolder, archFolder, this.ToolExe); | ||
154 | #endif | 164 | #endif |
165 | } | ||
166 | |||
167 | private string GetToolFullPath() | ||
168 | { | ||
169 | if (String.IsNullOrEmpty(this.ToolPath)) | ||
170 | { | ||
171 | return this.GetDefaultToolFullPath(); | ||
172 | } | ||
173 | |||
174 | return Path.Combine(this.ToolPath, this.ToolExe); | ||
175 | } | ||
155 | } | 176 | } |
156 | } | 177 | } |