/// <summary> /// Run the report (validates settings against the generator given) /// </summary> /// <param name="report">The report object to perform the analysis</param> /// <param name="generator">Generator type given</param> /// <param name="outputXml">Output XML file name/path</param> /// <param name="quiet">Indicates if any console output should be available</param> /// <param name="violations">True to report each violation encountered</param> private static void RunReport(ReportBuilder report, string generator, string outputXml, bool quiet, bool violations) { var generatorType = Generator.Default; if (generator != null) { generatorType = (Generator)Enum.Parse(typeof(Generator), generator, true); } FileRunner runner; switch (generatorType) { case Generator.Xml: report = report.WithOutputEventHandler( (x, y) => { violationCount++; if (!quiet) { OutputGenerated(x, y); } }); runner = new XmlRunner(); break; default: EventHandler<StyleCop.ViolationEventArgs> callback = HadViolation; if (!quiet) { if (violations) { callback = ViolationEncountered; } else { report = report.WithOutputEventHandler(OutputGenerated); } } report = report.WithViolationEventHandler(callback); runner = new ConsoleRunner(); break; } runner.OutputFile = string.IsNullOrEmpty(outputXml) ? null : string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}.xml", System.IO.Path.GetFileNameWithoutExtension(outputXml)); report.Create(runner); }
/// <summary> /// The entry-point method for this application. /// </summary> /// <param name="args"> /// The command line arguments passed to this method. /// </param> private static void Main(string[] args) { IList<string> solutionFiles = new List<string>(); IList<string> projectFiles = new List<string>(); IList<string> ignorePatterns = new List<string>(); IList<string> directories = new List<string>(); IList<string> files = new List<string>(); IList<string> symbols = new List<string>(); string styleCopSettings = null; string outputXml = null; bool recurse = false; bool needHelp = false; bool violations = false; bool quiet = false; bool dedupe = false; opts = new OptionSet() { { "s=|solutionFiles=", "Visual studio solution files to check", opt => { solutionFiles.Add(opt); } }, { "p=|projectFiles=", "Visual Studio project files to check", opt => { projectFiles.Add(opt); } }, { "i=|ignoreFilePattern=", "Regular expression patterns to ignore files", opt => { ignorePatterns.Add(opt); } }, { "d=|directories=", "Directories to check for CSharp files", opt => { directories.Add(opt); } }, { "f=|files=", "Files to check", opt => { files.Add(opt); } }, { "r|recurse", "Recursive directory search", opt => { recurse = opt != null; } }, { "c=|styleCopSettingsFile=", "Use the given StyleCop settings file", opt => { styleCopSettings = opt; } }, { "o=|outputXmlFile=", "The file the XML output is written to", opt => { outputXml = opt; } }, { "x=|configurationSymbols=", "Configuration symbols to pass to StyleCop (ex. DEBUG, RELEASE)", opt => { symbols.Add(opt); } }, { "v|violations", "Print all violation information instead of the summary", opt => { violations = true; } }, { "q|quiet", "Do not print any information to console", opt => { quiet = true; } }, { "?|help", "Print the usage information", opt => { needHelp = true; } }, { "e|eliminate", "Eliminate checking duplicate files/projects", opt => { dedupe = true; } }, }; try { opts.Parse(args); needHelp = needHelp || (solutionFiles.Count == 0 && projectFiles.Count == 0 && directories.Count == 0 && files.Count == 0); } catch (OptionException error) { Console.WriteLine("Invalid arguments"); Console.WriteLine(error); needHelp = true; } if (needHelp) { PrintUsageAndHelp(); return; } var report = new ReportBuilder() .WithDedupe(dedupe) .WithStyleCopSettingsFile(styleCopSettings) .WithRecursion(recurse) .WithSolutionsFiles(solutionFiles) .WithProjectFiles(projectFiles) .WithDirectories(directories) .WithFiles(files) .WithIgnorePatterns(ignorePatterns); if (!quiet) { if (violations) { report = report.WithViolationEventHandler(ViolationEncountered); } else { report = report.WithOutputEventHandler(OutputGenerated); } } report.Create(outputXml); }