/// <summary> /// Creates a StyleCop report. /// </summary> /// <param name="outputXmlFile"> /// The fully-qualified path to write the output of the report to. /// </param> public void Create(string outputXmlFile) { // Create a StyleCop configuration specifying the configuration // symbols to use for this report. var cfg = new Configuration( this.ProcessorSymbols != null ? this.ProcessorSymbols.ToArray() : null); // Create a new StyleCop console used to do the check. var scc = new StyleCopConsole( this.StyleCopSettingsFile, true, GetViolationsFile(outputXmlFile), this.AddInDirectories, true); // Process solution files if (this.SolutionFiles != null) { foreach (var i in this.SolutionFiles) { this.AddSolutionFile(i); } } // Process project files if (this.ProjectFiles != null) { foreach (var i in this.ProjectFiles) { this.AddProjectFile( i, null); } } // Process directories if (this.Directories != null) { foreach (var i in this.Directories) { this.AddDirectory(i); } } // Process files if (this.Files != null) { foreach (var i in this.Files) { this.AddFile( i, null, null); } } // Create a list of code projects from the data set. var cps = this.Report.Projects.Select( r => new CodeProject( r.ID, r.Location, cfg)).ToList(); // Add the source code files to the style cop checker foreach (var f in this.Report.SourceCodeFiles) { // ReSharper disable AccessToModifiedClosure var cp = cps.SingleOrDefault(i => i.Key == f.CodeProjectID); scc.Core.Environment.AddSourceCode( cp, f.Path, null); // ReSharper restore AccessToModifiedClosure } if (this.OutputGenerated != null) { scc.OutputGenerated += this.OutputGenerated; } scc.ViolationEncountered += this.ViolationEncountered; scc.Start( cps, true); if (this.OutputGenerated != null) { scc.OutputGenerated -= this.OutputGenerated; } scc.ViolationEncountered -= this.ViolationEncountered; scc.Dispose(); // Write the report to the output XML file. this.Report.WriteXml(outputXmlFile); if (!string.IsNullOrEmpty(this.TransformFile)) { this.Transform(outputXmlFile); } }
/// <summary> /// Processes a collection of files. /// </summary> /// <param name="settingsPath">The path to the settings file.</param> /// <param name="files">The files to process.</param> /// <param name="autoFix">Indicates whether to automatically fix violations.</param> private static void Process(string settingsPath, IEnumerable <string> files, bool autoFix) { StyleCopConsole console = null; CodeProject project = null; try { Configuration configuration = new Configuration(new string[] { "DEBUG" }); project = new CodeProject(1, "default", configuration); console = new StyleCopConsole(settingsPath, false, null, null, true, autoFix, null); foreach (string file in files) { console.Core.Environment.AddSourceCode(project, file, null); } filesCount += project.SourceCodeInstances.Count; if (!estimateMode) { if (fuzzMode) { console.ViolationEncountered += OnFuzzViolationEncountered; console.OutputGenerated += OnFuzzOutputGenerated; } else { console.ViolationEncountered += OnViolationEncountered; console.OutputGenerated += OnOutputGenerated; } } console.Start(new CodeProject[] { project }, true); } catch (Exception e) { Console.WriteLine("Unhandled exception in StyleCop.\r\n{0}", e.ToString()); if (project != null) { if (project.SourceCodeInstances.Count == 1) { Console.WriteLine(project.SourceCodeInstances[0]); } } } finally { if (console != null) { if (!estimateMode) { if (fuzzMode) { console.ViolationEncountered -= OnFuzzViolationEncountered; console.OutputGenerated -= OnFuzzOutputGenerated; } else { console.ViolationEncountered -= OnViolationEncountered; console.OutputGenerated -= OnOutputGenerated; } } console.Dispose(); } } }
/// <summary> /// Disposes the style cop console. /// </summary> /// <param name="console">The console.</param> private static void DisposeStyleCopConsole(StyleCopConsole console) { console.Dispose(); }