aboutsummaryrefslogtreecommitdiff
path: root/src/wixcop/CommandLine/ConvertCommand.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wixcop/CommandLine/ConvertCommand.cs')
-rw-r--r--src/wixcop/CommandLine/ConvertCommand.cs224
1 files changed, 0 insertions, 224 deletions
diff --git a/src/wixcop/CommandLine/ConvertCommand.cs b/src/wixcop/CommandLine/ConvertCommand.cs
deleted file mode 100644
index 0a765771..00000000
--- a/src/wixcop/CommandLine/ConvertCommand.cs
+++ /dev/null
@@ -1,224 +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
3namespace WixToolset.Tools.WixCop.CommandLine
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9 using WixToolset.Converters;
10 using WixToolset.Extensibility.Data;
11 using WixToolset.Extensibility.Services;
12
13 internal class ConvertCommand : ICommandLineCommand
14 {
15 private const string SettingsFileDefault = "wixcop.settings.xml";
16
17 public ConvertCommand(IWixToolsetServiceProvider serviceProvider, bool showLogo, bool fixErrors, int indentationAmount, List<string> searchPatterns, bool subDirectories, string settingsFile1, string settingsFile2)
18 {
19 this.ErrorsAsWarnings = new HashSet<string>();
20 this.ExemptFiles = new HashSet<string>();
21 this.FixErrors = fixErrors;
22 this.IndentationAmount = indentationAmount;
23 this.IgnoreErrors = new HashSet<string>();
24 this.SearchPatternResults = new HashSet<string>();
25 this.SearchPatterns = searchPatterns;
26 this.ServiceProvider = serviceProvider;
27 this.SettingsFile1 = settingsFile1;
28 this.SettingsFile2 = settingsFile2;
29 this.ShowLogo = showLogo;
30 this.SubDirectories = subDirectories;
31 }
32
33 private HashSet<string> ErrorsAsWarnings { get; }
34
35 private HashSet<string> ExemptFiles { get; }
36
37 private bool FixErrors { get; }
38
39 private int IndentationAmount { get; }
40
41 private HashSet<string> IgnoreErrors { get; }
42
43 private HashSet<string> SearchPatternResults { get; }
44
45 private List<string> SearchPatterns { get; }
46
47 private IWixToolsetServiceProvider ServiceProvider { get; }
48
49 private string SettingsFile1 { get; }
50
51 private string SettingsFile2 { get; }
52
53 private bool SubDirectories { get; }
54
55 public bool ShowLogo { get; }
56
57 public bool StopParsing => throw new NotImplementedException();
58
59 public bool TryParseArgument(ICommandLineParser parser, string argument)
60 {
61 throw new NotImplementedException();
62 }
63
64 public int Execute()
65 {
66 // parse the settings if any were specified
67 if (null != this.SettingsFile1 || null != this.SettingsFile2)
68 {
69 this.ParseSettingsFiles(this.SettingsFile1, this.SettingsFile2);
70 }
71 else
72 {
73 if (File.Exists(ConvertCommand.SettingsFileDefault))
74 {
75 this.ParseSettingsFiles(ConvertCommand.SettingsFileDefault, null);
76 }
77 }
78
79 var messaging = this.ServiceProvider.GetService<IMessaging>();
80 var converter = new Wix3Converter(messaging, this.IndentationAmount, this.ErrorsAsWarnings, this.IgnoreErrors);
81
82 var errors = this.InspectSubDirectories(converter, Path.GetFullPath("."));
83
84 foreach (var searchPattern in this.SearchPatterns)
85 {
86 if (!this.SearchPatternResults.Contains(searchPattern))
87 {
88 Console.Error.WriteLine("Could not find file \"{0}\"", searchPattern);
89 errors++;
90 }
91 }
92
93 return errors != 0 ? 2 : 0;
94 }
95
96 /// <summary>
97 /// Get the files that match a search path pattern.
98 /// </summary>
99 /// <param name="baseDir">The base directory at which to begin the search.</param>
100 /// <param name="searchPath">The search path pattern.</param>
101 /// <returns>The files matching the pattern.</returns>
102 private static string[] GetFiles(string baseDir, string searchPath)
103 {
104 // convert alternate directory separators to the standard one
105 var filePath = searchPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
106 var lastSeparator = filePath.LastIndexOf(Path.DirectorySeparatorChar);
107 string[] files = null;
108
109 try
110 {
111 if (0 > lastSeparator)
112 {
113 files = Directory.GetFiles(baseDir, filePath);
114 }
115 else // found directory separator
116 {
117 var searchPattern = filePath.Substring(lastSeparator + 1);
118
119 files = Directory.GetFiles(filePath.Substring(0, lastSeparator + 1), searchPattern);
120 }
121 }
122 catch (DirectoryNotFoundException)
123 {
124 // don't let this function throw the DirectoryNotFoundException. (this exception
125 // occurs for non-existant directories and invalid characters in the searchPattern)
126 }
127
128 return files;
129 }
130
131 /// <summary>
132 /// Inspect sub-directories.
133 /// </summary>
134 /// <param name="directory">The directory whose sub-directories will be inspected.</param>
135 /// <returns>The number of errors that were found.</returns>
136 private int InspectSubDirectories(Wix3Converter converter, string directory)
137 {
138 var errors = 0;
139
140 foreach (var searchPattern in this.SearchPatterns)
141 {
142 foreach (var sourceFilePath in GetFiles(directory, searchPattern))
143 {
144 var file = new FileInfo(sourceFilePath);
145
146 if (!this.ExemptFiles.Contains(file.Name.ToUpperInvariant()))
147 {
148 this.SearchPatternResults.Add(searchPattern);
149 errors += converter.ConvertFile(file.FullName, this.FixErrors);
150 }
151 }
152 }
153
154 if (this.SubDirectories)
155 {
156 foreach (var childDirectoryPath in Directory.GetDirectories(directory))
157 {
158 errors += this.InspectSubDirectories(converter, childDirectoryPath);
159 }
160 }
161
162 return errors;
163 }
164
165 /// <summary>
166 /// Parse the primary and secondary settings files.
167 /// </summary>
168 /// <param name="localSettingsFile1">The primary settings file.</param>
169 /// <param name="localSettingsFile2">The secondary settings file.</param>
170 private void ParseSettingsFiles(string localSettingsFile1, string localSettingsFile2)
171 {
172 if (null == localSettingsFile1 && null != localSettingsFile2)
173 {
174 throw new ArgumentException("Cannot specify a secondary settings file (set2) without a primary settings file (set1).", "localSettingsFile2");
175 }
176
177 var settingsFile = localSettingsFile1;
178 while (null != settingsFile)
179 {
180 XmlTextReader reader = null;
181 try
182 {
183 reader = new XmlTextReader(settingsFile);
184 var doc = new XmlDocument();
185 doc.Load(reader);
186
187 // get the types of tests that will have their errors displayed as warnings
188 var testsIgnoredElements = doc.SelectNodes("/Settings/IgnoreErrors/Test");
189 foreach (XmlElement test in testsIgnoredElements)
190 {
191 var key = test.GetAttribute("Id");
192 this.IgnoreErrors.Add(key);
193 }
194
195 // get the types of tests that will have their errors displayed as warnings
196 var testsAsWarningsElements = doc.SelectNodes("/Settings/ErrorsAsWarnings/Test");
197 foreach (XmlElement test in testsAsWarningsElements)
198 {
199 var key = test.GetAttribute("Id");
200 this.ErrorsAsWarnings.Add(key);
201 }
202
203 // get the exempt files
204 var localExemptFiles = doc.SelectNodes("/Settings/ExemptFiles/File");
205 foreach (XmlElement file in localExemptFiles)
206 {
207 var key = file.GetAttribute("Name").ToUpperInvariant();
208 this.ExemptFiles.Add(key);
209 }
210 }
211 finally
212 {
213 if (null != reader)
214 {
215 reader.Close();
216 }
217 }
218
219 settingsFile = localSettingsFile2;
220 localSettingsFile2 = null;
221 }
222 }
223 }
224}