diff options
Diffstat (limited to 'src/tools/heat/HeatCommand.cs')
-rw-r--r-- | src/tools/heat/HeatCommand.cs | 277 |
1 files changed, 0 insertions, 277 deletions
diff --git a/src/tools/heat/HeatCommand.cs b/src/tools/heat/HeatCommand.cs deleted file mode 100644 index c8703ec1..00000000 --- a/src/tools/heat/HeatCommand.cs +++ /dev/null | |||
@@ -1,277 +0,0 @@ | |||
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.Harvesters | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Globalization; | ||
8 | using System.IO; | ||
9 | using System.Runtime.InteropServices; | ||
10 | using System.Threading; | ||
11 | using System.Threading.Tasks; | ||
12 | using System.Xml; | ||
13 | using WixToolset.Data; | ||
14 | using WixToolset.Extensibility; | ||
15 | using WixToolset.Extensibility.Data; | ||
16 | using WixToolset.Extensibility.Services; | ||
17 | using WixToolset.Harvesters.Extensibility; | ||
18 | |||
19 | internal class HeatCommand : BaseCommandLineCommand | ||
20 | { | ||
21 | private bool showLogo; | ||
22 | |||
23 | public HeatCommand(string harvestType, IList<IHeatExtension> extensions, IServiceProvider serviceProvider) | ||
24 | { | ||
25 | this.Extensions = extensions; | ||
26 | this.Messaging = serviceProvider.GetService<IMessaging>(); | ||
27 | this.ServiceProvider = serviceProvider; | ||
28 | |||
29 | this.ExtensionType = harvestType; | ||
30 | this.ExtensionOptions.Add(harvestType); | ||
31 | } | ||
32 | |||
33 | public override bool ShowLogo => this.showLogo; | ||
34 | |||
35 | private string ExtensionArgument { get; set; } | ||
36 | |||
37 | private List<string> ExtensionOptions { get; } = new List<string>(); | ||
38 | |||
39 | private string ExtensionType { get; } | ||
40 | |||
41 | private IList<IHeatExtension> Extensions { get; } | ||
42 | |||
43 | private int Indent { get; set; } = 4; | ||
44 | |||
45 | private IMessaging Messaging { get; } | ||
46 | |||
47 | private string OutputFile { get; set; } | ||
48 | |||
49 | private IServiceProvider ServiceProvider { get; } | ||
50 | |||
51 | public override CommandLineHelp GetCommandLineHelp() | ||
52 | { | ||
53 | return null; | ||
54 | } | ||
55 | |||
56 | public override Task<int> ExecuteAsync(CancellationToken cancellationToken) | ||
57 | { | ||
58 | var exitCode = this.Harvest(); | ||
59 | return Task.FromResult(exitCode); | ||
60 | } | ||
61 | |||
62 | public override bool TryParseArgument(ICommandLineParser parser, string arg) | ||
63 | { | ||
64 | if (this.ExtensionArgument == null) | ||
65 | { | ||
66 | this.ExtensionArgument = arg; | ||
67 | } | ||
68 | else if ('-' == arg[0] || '/' == arg[0]) | ||
69 | { | ||
70 | var parameter = arg.Substring(1); | ||
71 | if ("nologo" == parameter) | ||
72 | { | ||
73 | this.showLogo = false; | ||
74 | } | ||
75 | else if ("o" == parameter || "out" == parameter) | ||
76 | { | ||
77 | this.OutputFile = parser.GetNextArgumentAsFilePathOrError(arg, "output source file"); | ||
78 | |||
79 | if (String.IsNullOrEmpty(this.OutputFile)) | ||
80 | { | ||
81 | return false; | ||
82 | } | ||
83 | } | ||
84 | else if ("swall" == parameter) | ||
85 | { | ||
86 | this.Messaging.Write(WarningMessages.DeprecatedCommandLineSwitch("swall", "sw")); | ||
87 | this.Messaging.SuppressAllWarnings = true; | ||
88 | } | ||
89 | else if (parameter.StartsWith("sw")) | ||
90 | { | ||
91 | var paramArg = parameter.Substring(2); | ||
92 | try | ||
93 | { | ||
94 | if (0 == paramArg.Length) | ||
95 | { | ||
96 | this.Messaging.SuppressAllWarnings = true; | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | var suppressWarning = Convert.ToInt32(paramArg, CultureInfo.InvariantCulture.NumberFormat); | ||
101 | if (0 >= suppressWarning) | ||
102 | { | ||
103 | this.Messaging.Write(ErrorMessages.IllegalSuppressWarningId(paramArg)); | ||
104 | } | ||
105 | |||
106 | this.Messaging.SuppressWarningMessage(suppressWarning); | ||
107 | } | ||
108 | } | ||
109 | catch (FormatException) | ||
110 | { | ||
111 | this.Messaging.Write(ErrorMessages.IllegalSuppressWarningId(paramArg)); | ||
112 | } | ||
113 | catch (OverflowException) | ||
114 | { | ||
115 | this.Messaging.Write(ErrorMessages.IllegalSuppressWarningId(paramArg)); | ||
116 | } | ||
117 | } | ||
118 | else if ("wxall" == parameter) | ||
119 | { | ||
120 | this.Messaging.Write(WarningMessages.DeprecatedCommandLineSwitch("wxall", "wx")); | ||
121 | this.Messaging.WarningsAsError = true; | ||
122 | } | ||
123 | else if (parameter.StartsWith("wx")) | ||
124 | { | ||
125 | var paramArg = parameter.Substring(2); | ||
126 | try | ||
127 | { | ||
128 | if (0 == paramArg.Length) | ||
129 | { | ||
130 | this.Messaging.WarningsAsError = true; | ||
131 | } | ||
132 | else | ||
133 | { | ||
134 | var elevateWarning = Convert.ToInt32(paramArg, CultureInfo.InvariantCulture.NumberFormat); | ||
135 | if (0 >= elevateWarning) | ||
136 | { | ||
137 | this.Messaging.Write(ErrorMessages.IllegalWarningIdAsError(paramArg)); | ||
138 | } | ||
139 | |||
140 | this.Messaging.ElevateWarningMessage(elevateWarning); | ||
141 | } | ||
142 | } | ||
143 | catch (FormatException) | ||
144 | { | ||
145 | this.Messaging.Write(ErrorMessages.IllegalWarningIdAsError(paramArg)); | ||
146 | } | ||
147 | catch (OverflowException) | ||
148 | { | ||
149 | this.Messaging.Write(ErrorMessages.IllegalWarningIdAsError(paramArg)); | ||
150 | } | ||
151 | } | ||
152 | else if ("v" == parameter) | ||
153 | { | ||
154 | this.Messaging.ShowVerboseMessages = true; | ||
155 | } | ||
156 | else if ("indent" == parameter) | ||
157 | { | ||
158 | try | ||
159 | { | ||
160 | this.Indent = Int32.Parse(parser.GetNextArgumentOrError(arg), CultureInfo.InvariantCulture); | ||
161 | } | ||
162 | catch | ||
163 | { | ||
164 | throw new ArgumentException("Invalid numeric argument.", parameter); | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | |||
169 | this.ExtensionOptions.Add(arg); | ||
170 | return true; | ||
171 | } | ||
172 | |||
173 | private int Harvest() | ||
174 | { | ||
175 | try | ||
176 | { | ||
177 | if (String.IsNullOrEmpty(this.ExtensionArgument)) | ||
178 | { | ||
179 | this.Messaging.Write(ErrorMessages.HarvestSourceNotSpecified()); | ||
180 | } | ||
181 | else if (String.IsNullOrEmpty(this.OutputFile)) | ||
182 | { | ||
183 | this.Messaging.Write(ErrorMessages.OutputTargetNotSpecified()); | ||
184 | } | ||
185 | |||
186 | // exit if there was an error parsing the core command line | ||
187 | if (this.Messaging.EncounteredError) | ||
188 | { | ||
189 | return this.Messaging.LastErrorNumber; | ||
190 | } | ||
191 | |||
192 | if (this.ShowLogo) | ||
193 | { | ||
194 | HelpCommand.DisplayToolHeader(); | ||
195 | } | ||
196 | |||
197 | var heatCore = new HeatCore(this.ServiceProvider, this.ExtensionArgument); | ||
198 | |||
199 | // parse the extension's command line arguments | ||
200 | var extensionOptionsArray = this.ExtensionOptions.ToArray(); | ||
201 | foreach (var heatExtension in this.Extensions) | ||
202 | { | ||
203 | heatExtension.Core = heatCore; | ||
204 | heatExtension.ParseOptions(this.ExtensionType, extensionOptionsArray); | ||
205 | } | ||
206 | |||
207 | // exit if there was an error parsing the command line (otherwise the logo appears after error messages) | ||
208 | if (this.Messaging.EncounteredError) | ||
209 | { | ||
210 | return this.Messaging.LastErrorNumber; | ||
211 | } | ||
212 | |||
213 | // harvest the output | ||
214 | var wix = heatCore.Harvester.Harvest(this.ExtensionArgument); | ||
215 | if (null == wix) | ||
216 | { | ||
217 | return this.Messaging.LastErrorNumber; | ||
218 | } | ||
219 | |||
220 | // mutate the output | ||
221 | if (!heatCore.Mutator.Mutate(wix)) | ||
222 | { | ||
223 | return this.Messaging.LastErrorNumber; | ||
224 | } | ||
225 | |||
226 | var xmlSettings = new XmlWriterSettings(); | ||
227 | xmlSettings.Indent = true; | ||
228 | xmlSettings.IndentChars = new string(' ', this.Indent); | ||
229 | xmlSettings.OmitXmlDeclaration = true; | ||
230 | |||
231 | string wixString; | ||
232 | using (var stringWriter = new StringWriter()) | ||
233 | { | ||
234 | using (var xmlWriter = XmlWriter.Create(stringWriter, xmlSettings)) | ||
235 | { | ||
236 | wix.OutputXml(xmlWriter); | ||
237 | } | ||
238 | |||
239 | wixString = stringWriter.ToString(); | ||
240 | } | ||
241 | |||
242 | var mutatedWixString = heatCore.Mutator.Mutate(wixString); | ||
243 | if (String.IsNullOrEmpty(mutatedWixString)) | ||
244 | { | ||
245 | return this.Messaging.LastErrorNumber; | ||
246 | } | ||
247 | |||
248 | Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile)); | ||
249 | |||
250 | using (var streamWriter = new StreamWriter(this.OutputFile, false, System.Text.Encoding.UTF8)) | ||
251 | { | ||
252 | using (var xmlWriter = XmlWriter.Create(streamWriter, xmlSettings)) | ||
253 | { | ||
254 | xmlWriter.WriteStartDocument(); | ||
255 | xmlWriter.Flush(); | ||
256 | } | ||
257 | |||
258 | streamWriter.Write(mutatedWixString); | ||
259 | } | ||
260 | } | ||
261 | catch (WixException we) | ||
262 | { | ||
263 | this.Messaging.Write(we.Error); | ||
264 | } | ||
265 | catch (Exception e) | ||
266 | { | ||
267 | this.Messaging.Write(ErrorMessages.UnexpectedException(e)); | ||
268 | if (e is NullReferenceException || e is SEHException) | ||
269 | { | ||
270 | throw; | ||
271 | } | ||
272 | } | ||
273 | |||
274 | return this.Messaging.LastErrorNumber; | ||
275 | } | ||
276 | } | ||
277 | } | ||