aboutsummaryrefslogtreecommitdiff
path: root/src/wixcop
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2018-10-04 13:33:08 -0700
committerBob Arnson <bob@firegiant.com>2018-10-04 18:20:52 -0400
commit240f3594db6f633ece6818afd28dde1e1ef6b36c (patch)
treeab75f516217afa280944b2198a42c69b9a1b9cc0 /src/wixcop
parent917bfd43985af3afaac0b3a1d97a89a58bde1f2f (diff)
downloadwix-240f3594db6f633ece6818afd28dde1e1ef6b36c.tar.gz
wix-240f3594db6f633ece6818afd28dde1e1ef6b36c.tar.bz2
wix-240f3594db6f633ece6818afd28dde1e1ef6b36c.zip
Fix WixCop namespaces and some C# modernization
Diffstat (limited to 'src/wixcop')
-rw-r--r--src/wixcop/CommandLine/ConvertCommand.cs21
-rw-r--r--src/wixcop/CommandLine/HelpCommand.cs8
-rw-r--r--src/wixcop/CommandLine/WixCopCommandLineParser.cs82
-rw-r--r--src/wixcop/Converter.cs56
-rw-r--r--src/wixcop/Interfaces/IWixCopCommandLineParser.cs6
-rw-r--r--src/wixcop/Program.cs6
6 files changed, 90 insertions, 89 deletions
diff --git a/src/wixcop/CommandLine/ConvertCommand.cs b/src/wixcop/CommandLine/ConvertCommand.cs
index 6af7d4ca..ab7cd359 100644
--- a/src/wixcop/CommandLine/ConvertCommand.cs
+++ b/src/wixcop/CommandLine/ConvertCommand.cs
@@ -1,13 +1,14 @@
1using System; 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.
2using System.Collections.Generic; 2
3using System.IO; 3namespace WixToolset.Tools.WixCop.CommandLine
4using System.Linq;
5using System.Xml;
6using WixToolset.Extensibility.Data;
7using WixToolset.Extensibility.Services;
8
9namespace WixCop.CommandLine
10{ 4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9 using WixToolset.Extensibility.Data;
10 using WixToolset.Extensibility.Services;
11
11 internal class ConvertCommand : ICommandLineCommand 12 internal class ConvertCommand : ICommandLineCommand
12 { 13 {
13 private const string SettingsFileDefault = "wixcop.settings.xml"; 14 private const string SettingsFileDefault = "wixcop.settings.xml";
@@ -69,7 +70,7 @@ namespace WixCop.CommandLine
69 70
70 var errors = this.InspectSubDirectories(converter, Path.GetFullPath(".")); 71 var errors = this.InspectSubDirectories(converter, Path.GetFullPath("."));
71 72
72 foreach (string searchPattern in this.SearchPatterns) 73 foreach (var searchPattern in this.SearchPatterns)
73 { 74 {
74 if (!this.SearchPatternResults.Contains(searchPattern)) 75 if (!this.SearchPatternResults.Contains(searchPattern))
75 { 76 {
diff --git a/src/wixcop/CommandLine/HelpCommand.cs b/src/wixcop/CommandLine/HelpCommand.cs
index a75dac5c..bfb784b0 100644
--- a/src/wixcop/CommandLine/HelpCommand.cs
+++ b/src/wixcop/CommandLine/HelpCommand.cs
@@ -1,8 +1,10 @@
1using System; 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.
2using WixToolset.Extensibility.Data;
3 2
4namespace WixCop.CommandLine 3namespace WixToolset.Tools.WixCop.CommandLine
5{ 4{
5 using System;
6 using WixToolset.Extensibility.Data;
7
6 internal class HelpCommand : ICommandLineCommand 8 internal class HelpCommand : ICommandLineCommand
7 { 9 {
8 public int Execute() 10 public int Execute()
diff --git a/src/wixcop/CommandLine/WixCopCommandLineParser.cs b/src/wixcop/CommandLine/WixCopCommandLineParser.cs
index 53012cfd..ae826d4f 100644
--- a/src/wixcop/CommandLine/WixCopCommandLineParser.cs
+++ b/src/wixcop/CommandLine/WixCopCommandLineParser.cs
@@ -1,12 +1,14 @@
1using System; 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.
2using System.Collections.Generic;
3using WixCop.Interfaces;
4using WixToolset.Core;
5using WixToolset.Extensibility.Data;
6using WixToolset.Extensibility.Services;
7 2
8namespace WixCop.CommandLine 3namespace WixToolset.Tools.WixCop.CommandLine
9{ 4{
5 using System;
6 using System.Collections.Generic;
7 using WixToolset.Core;
8 using WixToolset.Extensibility.Data;
9 using WixToolset.Extensibility.Services;
10 using WixToolset.Tools.WixCop.Interfaces;
11
10 public sealed class WixCopCommandLineParser : IWixCopCommandLineParser 12 public sealed class WixCopCommandLineParser : IWixCopCommandLineParser
11 { 13 {
12 private bool fixErrors; 14 private bool fixErrors;
@@ -89,43 +91,43 @@ namespace WixCop.CommandLine
89 91
90 switch (parameter.ToLowerInvariant()) 92 switch (parameter.ToLowerInvariant())
91 { 93 {
92 case "?": 94 case "?":
93 this.showHelp = true; 95 this.showHelp = true;
94 return true; 96 return true;
95 case "f": 97 case "f":
96 this.fixErrors = true; 98 this.fixErrors = true;
97 return true; 99 return true;
98 case "nologo": 100 case "nologo":
99 this.showLogo = false; 101 this.showLogo = false;
100 return true; 102 return true;
101 case "s": 103 case "s":
102 this.subDirectories = true; 104 this.subDirectories = true;
103 return true; 105 return true;
104 default: // other parameters 106 default: // other parameters
105 if (parameter.StartsWith("set1", StringComparison.Ordinal)) 107 if (parameter.StartsWith("set1", StringComparison.Ordinal))
106 { 108 {
107 this.settingsFile1 = parameter.Substring(4); 109 this.settingsFile1 = parameter.Substring(4);
108 } 110 }
109 else if (parameter.StartsWith("set2", StringComparison.Ordinal)) 111 else if (parameter.StartsWith("set2", StringComparison.Ordinal))
110 { 112 {
111 this.settingsFile2 = parameter.Substring(4); 113 this.settingsFile2 = parameter.Substring(4);
112 } 114 }
113 else if (parameter.StartsWith("indent:", StringComparison.Ordinal)) 115 else if (parameter.StartsWith("indent:", StringComparison.Ordinal))
116 {
117 try
114 { 118 {
115 try 119 this.indentationAmount = Convert.ToInt32(parameter.Substring(7));
116 {
117 this.indentationAmount = Convert.ToInt32(parameter.Substring(7));
118 }
119 catch
120 {
121 throw new ArgumentException("Invalid numeric argument.", parameter);
122 }
123 } 120 }
124 else 121 catch
125 { 122 {
126 throw new ArgumentException("Invalid argument.", parameter); 123 throw new ArgumentException("Invalid numeric argument.", parameter);
127 } 124 }
128 return true; 125 }
126 else
127 {
128 throw new ArgumentException("Invalid argument.", parameter);
129 }
130 return true;
129 } 131 }
130 } 132 }
131 } 133 }
diff --git a/src/wixcop/Converter.cs b/src/wixcop/Converter.cs
index a204ebe0..7e8486ab 100644
--- a/src/wixcop/Converter.cs
+++ b/src/wixcop/Converter.cs
@@ -1,6 +1,6 @@
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. 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 2
3namespace WixCop 3namespace WixToolset.Tools.WixCop
4{ 4{
5 using System; 5 using System;
6 using System.Collections.Generic; 6 using System.Collections.Generic;
@@ -141,7 +141,7 @@ namespace WixCop
141 { 141 {
142 try 142 try
143 { 143 {
144 using (StreamWriter writer = File.CreateText(this.SourceFile)) 144 using (var writer = File.CreateText(this.SourceFile))
145 { 145 {
146 document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); 146 document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces);
147 } 147 }
@@ -162,7 +162,7 @@ namespace WixCop
162 /// <returns>The number of errors found.</returns> 162 /// <returns>The number of errors found.</returns>
163 public int ConvertDocument(XDocument document) 163 public int ConvertDocument(XDocument document)
164 { 164 {
165 XDeclaration declaration = document.Declaration; 165 var declaration = document.Declaration;
166 166
167 // Convert the declaration. 167 // Convert the declaration.
168 if (null != declaration) 168 if (null != declaration)
@@ -206,7 +206,7 @@ namespace WixCop
206 } 206 }
207 207
208 // Convert this node if it is an element. 208 // Convert this node if it is an element.
209 XElement element = node as XElement; 209 var element = node as XElement;
210 210
211 if (null != element) 211 if (null != element)
212 { 212 {
@@ -215,7 +215,7 @@ namespace WixCop
215 // Convert all children of this element. 215 // Convert all children of this element.
216 IEnumerable<XNode> children = element.Nodes().ToList(); 216 IEnumerable<XNode> children = element.Nodes().ToList();
217 217
218 foreach (XNode child in children) 218 foreach (var child in children)
219 { 219 {
220 this.ConvertNode(child, level + 1); 220 this.ConvertNode(child, level + 1);
221 } 221 }
@@ -225,9 +225,9 @@ namespace WixCop
225 private void ConvertElement(XElement element) 225 private void ConvertElement(XElement element)
226 { 226 {
227 // Gather any deprecated namespaces, then update this element tree based on those deprecations. 227 // Gather any deprecated namespaces, then update this element tree based on those deprecations.
228 Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>(); 228 var deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>();
229 229
230 foreach (XAttribute declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration)) 230 foreach (var declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration))
231 { 231 {
232 XNamespace ns; 232 XNamespace ns;
233 233
@@ -258,7 +258,7 @@ namespace WixCop
258 { 258 {
259 if (null == element.Attribute("Id")) 259 if (null == element.Attribute("Id"))
260 { 260 {
261 XAttribute attribute = element.Attribute("Name"); 261 var attribute = element.Attribute("Name");
262 262
263 if (null == attribute) 263 if (null == attribute)
264 { 264 {
@@ -267,7 +267,7 @@ namespace WixCop
267 267
268 if (null != attribute) 268 if (null != attribute)
269 { 269 {
270 string name = Path.GetFileName(attribute.Value); 270 var name = Path.GetFileName(attribute.Value);
271 271
272 if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name)) 272 if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name))
273 { 273 {
@@ -282,7 +282,7 @@ namespace WixCop
282 282
283 private void ConvertSuppressSignatureValidation(XElement element) 283 private void ConvertSuppressSignatureValidation(XElement element)
284 { 284 {
285 XAttribute suppressSignatureValidation = element.Attribute("SuppressSignatureValidation"); 285 var suppressSignatureValidation = element.Attribute("SuppressSignatureValidation");
286 286
287 if (null != suppressSignatureValidation) 287 if (null != suppressSignatureValidation)
288 { 288 {
@@ -311,7 +311,7 @@ namespace WixCop
311 311
312 element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace. 312 element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace.
313 313
314 foreach (XElement elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace)) 314 foreach (var elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace))
315 { 315 {
316 elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName); 316 elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName);
317 } 317 }
@@ -326,9 +326,7 @@ namespace WixCop
326 private void ConvertWhitespace(XNode node, int level) 326 private void ConvertWhitespace(XNode node, int level)
327 { 327 {
328 // Fix the whitespace before this node. 328 // Fix the whitespace before this node.
329 XText whitespace = node.PreviousNode as XText; 329 if (node.PreviousNode is XText whitespace)
330
331 if (null != whitespace)
332 { 330 {
333 if (XmlNodeType.CDATA == node.NodeType) 331 if (XmlNodeType.CDATA == node.NodeType)
334 { 332 {
@@ -351,9 +349,7 @@ namespace WixCop
351 } 349 }
352 350
353 // Fix the whitespace after CDATA nodes. 351 // Fix the whitespace after CDATA nodes.
354 XCData cdata = node as XCData; 352 if (node is XCData cdata)
355
356 if (null != cdata)
357 { 353 {
358 whitespace = cdata.NextNode as XText; 354 whitespace = cdata.NextNode as XText;
359 355
@@ -368,9 +364,7 @@ namespace WixCop
368 else 364 else
369 { 365 {
370 // Fix the whitespace inside and after this node (except for Error which may contain just whitespace). 366 // Fix the whitespace inside and after this node (except for Error which may contain just whitespace).
371 XElement element = node as XElement; 367 if (node is XElement element && "Error" != element.Name.LocalName)
372
373 if (null != element && "Error" != element.Name.LocalName)
374 { 368 {
375 if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim())) 369 if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim()))
376 { 370 {
@@ -403,7 +397,7 @@ namespace WixCop
403 { 397 {
404 if (null != types) 398 if (null != types)
405 { 399 {
406 foreach (string type in types) 400 foreach (var type in types)
407 { 401 {
408 ConverterTestType itt; 402 ConverterTestType itt;
409 403
@@ -421,7 +415,7 @@ namespace WixCop
421 415
422 private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces) 416 private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces)
423 { 417 {
424 foreach (XElement element in elements) 418 foreach (var element in elements)
425 { 419 {
426 XNamespace ns; 420 XNamespace ns;
427 421
@@ -434,9 +428,9 @@ namespace WixCop
434 IEnumerable<XAttribute> attributes = element.Attributes().ToList(); 428 IEnumerable<XAttribute> attributes = element.Attributes().ToList();
435 element.RemoveAttributes(); 429 element.RemoveAttributes();
436 430
437 foreach (XAttribute attribute in attributes) 431 foreach (var attribute in attributes)
438 { 432 {
439 XAttribute convertedAttribute = attribute; 433 var convertedAttribute = attribute;
440 434
441 if (attribute.IsNamespaceDeclaration) 435 if (attribute.IsNamespaceDeclaration)
442 { 436 {
@@ -477,7 +471,7 @@ namespace WixCop
477 } 471 }
478 472
479 // check the spaces 473 // check the spaces
480 foreach (char character in whitespace) 474 foreach (var character in whitespace)
481 { 475 {
482 if (' ' != character) 476 if (' ' != character)
483 { 477 {
@@ -496,9 +490,9 @@ namespace WixCop
496 /// <param name="whitespace">The whitespace node to fix.</param> 490 /// <param name="whitespace">The whitespace node to fix.</param>
497 private static void FixWhitespace(int indentationAmount, int level, XText whitespace) 491 private static void FixWhitespace(int indentationAmount, int level, XText whitespace)
498 { 492 {
499 int newLineCount = 0; 493 var newLineCount = 0;
500 494
501 for (int i = 0; i + 1 < whitespace.Value.Length; ++i) 495 for (var i = 0; i + 1 < whitespace.Value.Length; ++i)
502 { 496 {
503 if (XDocumentNewLine == whitespace.Value.Substring(i, 2)) 497 if (XDocumentNewLine == whitespace.Value.Substring(i, 2))
504 { 498 {
@@ -516,7 +510,7 @@ namespace WixCop
516 whitespace.Value = String.Empty; 510 whitespace.Value = String.Empty;
517 511
518 // add the correct number of newlines 512 // add the correct number of newlines
519 for (int i = 0; i < newLineCount; ++i) 513 for (var i = 0; i < newLineCount; ++i)
520 { 514 {
521 whitespace.Value = String.Concat(whitespace.Value, XDocumentNewLine); 515 whitespace.Value = String.Concat(whitespace.Value, XDocumentNewLine);
522 } 516 }
@@ -543,9 +537,9 @@ namespace WixCop
543 // Increase the error count. 537 // Increase the error count.
544 this.Errors++; 538 this.Errors++;
545 539
546 SourceLineNumber sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber); 540 var sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber);
547 bool warning = this.ErrorsAsWarnings.Contains(converterTestType); 541 var warning = this.ErrorsAsWarnings.Contains(converterTestType);
548 string display = String.Format(CultureInfo.CurrentCulture, message, args); 542 var display = String.Format(CultureInfo.CurrentCulture, message, args);
549 543
550 var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString()); 544 var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString());
551 545
diff --git a/src/wixcop/Interfaces/IWixCopCommandLineParser.cs b/src/wixcop/Interfaces/IWixCopCommandLineParser.cs
index 2093f5d8..44c75289 100644
--- a/src/wixcop/Interfaces/IWixCopCommandLineParser.cs
+++ b/src/wixcop/Interfaces/IWixCopCommandLineParser.cs
@@ -1,7 +1,9 @@
1using WixToolset.Extensibility.Data; 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 2
3namespace WixCop.Interfaces 3namespace WixToolset.Tools.WixCop.Interfaces
4{ 4{
5 using WixToolset.Extensibility.Data;
6
5 public interface IWixCopCommandLineParser 7 public interface IWixCopCommandLineParser
6 { 8 {
7 ICommandLineArguments Arguments { get; set; } 9 ICommandLineArguments Arguments { get; set; }
diff --git a/src/wixcop/Program.cs b/src/wixcop/Program.cs
index b26bd6c9..8e9534f6 100644
--- a/src/wixcop/Program.cs
+++ b/src/wixcop/Program.cs
@@ -1,15 +1,15 @@
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. 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 2
3namespace WixCop 3namespace WixToolset.Tools.WixCop
4{ 4{
5 using System; 5 using System;
6 using WixCop.CommandLine;
7 using WixCop.Interfaces;
8 using WixToolset.Core; 6 using WixToolset.Core;
9 using WixToolset.Extensibility; 7 using WixToolset.Extensibility;
10 using WixToolset.Extensibility.Data; 8 using WixToolset.Extensibility.Data;
11 using WixToolset.Extensibility.Services; 9 using WixToolset.Extensibility.Services;
12 using WixToolset.Tools.Core; 10 using WixToolset.Tools.Core;
11 using WixToolset.Tools.WixCop.CommandLine;
12 using WixToolset.Tools.WixCop.Interfaces;
13 13
14 /// <summary> 14 /// <summary>
15 /// Wix source code style inspector and converter. 15 /// Wix source code style inspector and converter.