public static void Execute(ICakeContext context, FilePath solutionFile, FilePath settingsFile) { if (solutionFile == null) { throw new ArgumentNullException(nameof(solutionFile), "Solution file path is null."); } var solutionParser = new SolutionParser(context.FileSystem, context.Environment); var projectParser = new ProjectParser(context.FileSystem, context.Environment); var stylecopSettingsFile = settingsFile == null ? null : settingsFile.ToString(); var projectPath = Cake.Common.IO.DirectoryAliases.Directory(context, solutionFile.MakeAbsolute(context.Environment).GetDirectory().FullPath); Cake.Common.Diagnostics.LoggingAliases.Information(context, string.Format("Project Path: {0}", projectPath.Path.FullPath)); var styleCopConsole = new StyleCop.StyleCopConsole( stylecopSettingsFile, false, /* Input Cache Result */ null, /* Output file */ null, true); var styleCopProjects = new List <CodeProject>(); var solution = solutionParser.Parse(solutionFile); foreach (var solutionProject in solution.Projects) { var project = projectParser.Parse(solutionProject.Path); var styleCopProject = new CodeProject(0, solutionProject.Path.GetDirectory().ToString(), new Configuration(null)); styleCopProjects.Add(styleCopProject); foreach (var projectFile in project.Files) { if (projectFile.FilePath.GetExtension() == ".cs") { styleCopConsole.Core.Environment.AddSourceCode( styleCopProject, projectFile.FilePath.ToString(), null); } } } var handler = new StylecopHandlers(context); styleCopConsole.OutputGenerated += handler.OnOutputGenerated; styleCopConsole.ViolationEncountered += handler.ViolationEncountered; styleCopConsole.Start(styleCopProjects.ToArray(), true); styleCopConsole.OutputGenerated -= handler.OnOutputGenerated; styleCopConsole.ViolationEncountered -= handler.ViolationEncountered; if (handler.TotalViolations > 0) { throw new Exception(string.Format("{0} StyleCop violations encountered.", handler.TotalViolations)); } }
/// <summary> /// Starts an analysis run. /// </summary> /// <param name="context">The cake context.</param> /// <param name="settingsDelegate">The stylecop setting to use during the analysis.</param> public static void Execute(ICakeContext context, SettingsDelegate settingsDelegate) { settings = settingsDelegate(new StyleCopSettings()); // need to get pwd for stylecop.dll for stylesheet var assemblyDirectory = AssemblyDirectory(Assembly.GetAssembly(typeof(StyleCopSettings))); var toolPath = context.File(assemblyDirectory).Path.GetDirectory(); var defaultStyleSheet = context.File(toolPath + "/StyleCopStyleSheet.xslt"); context.Log.Information($"Stylecop: Default stylesheet {context.MakeAbsolute(defaultStyleSheet)}"); var solutionFile = settings.SolutionFile; var settingsFile = settings.SettingsFile?.ToString(); var outputPath = settings.ResultsFile?.ToString(); var addins = settings.Addins.Count == 0 ? null : settings.Addins.Select(x => x.FullPath).ToList(); var solutionParser = new SolutionParser(context.FileSystem, context.Environment); var projectParser = new ProjectParser(context.FileSystem, context.Environment); var projectPath = solutionFile.MakeAbsolute(context.Environment).GetDirectory(); context.Log.Information($"Stylecop: Found solution {projectPath.FullPath}"); StyleCopConsole styleCopConsole = null; try { styleCopConsole = new StyleCopConsole( settingsFile, settings.WriteResultsCache, /* Input Cache Result */ outputPath, /* Output file */ addins, settings.LoadFromDefaultPath); } catch (TypeLoadException typeLoadException) { context.Log.Error($"Error: Stylecop was unable to load an Addin .dll. {typeLoadException.Message}"); throw; } var styleCopProjects = new List <CodeProject>(); var solution = solutionParser.Parse(solutionFile); foreach (var solutionProject in solution.Projects.Where(p => p.Type != FOLDER_PROJECT_TYPE_GUID)) { context.Log.Information($"Stylecop: Found project {solutionProject.Path}"); var project = projectParser.Parse(solutionProject.Path); var styleCopProject = new CodeProject(0, solutionProject.Path.GetDirectory().ToString(), new Configuration(null)); styleCopProjects.Add(styleCopProject); foreach (var projectFile in project.Files) { if (projectFile.FilePath.GetExtension() != ".cs") { continue; } context.Log.Debug($"Stylecop: Found file {projectFile.FilePath}"); styleCopConsole.Core.Environment.AddSourceCode(styleCopProject, projectFile.FilePath.ToString(), null); } } var handler = new StylecopHandlers(context, settings); styleCopConsole.OutputGenerated += handler.OnOutputGenerated; styleCopConsole.ViolationEncountered += handler.ViolationEncountered; context.Log.Information($"Stylecop: Starting analysis"); styleCopConsole.Start(styleCopProjects.ToArray(), settings.FullAnalyze); context.Log.Information($"Stylecop: Finished analysis"); styleCopConsole.OutputGenerated -= handler.OnOutputGenerated; styleCopConsole.ViolationEncountered -= handler.ViolationEncountered; if (settings.HtmlReportFile != null) { settings.HtmlReportFile = settings.HtmlReportFile.MakeAbsolute(context.Environment); // copy default resources to output folder context.CopyDirectory(context.Directory(toolPath + "/resources"), settings.HtmlReportFile.GetDirectory() + "/resources"); context.Log.Information($"Stylecop: Creating html report {settings.HtmlReportFile.FullPath}"); Transform(context, settings.HtmlReportFile, settings.ResultsFile.MakeAbsolute(context.Environment), settings.StyleSheet ?? context.MakeAbsolute(defaultStyleSheet)); } if (handler.TotalViolations > 0 && settings.FailTask) { throw new Exception($"{handler.TotalViolations} StyleCop violations encountered."); } }
/// <summary> /// Starts an analysis run. /// </summary> /// <param name="context">The cake context.</param> /// <param name="settingsDelegate">The stylecop setting to use during the analysis.</param> public static void Execute(ICakeContext context, SettingsDelegate settingsDelegate) { settings = settingsDelegate(new StyleCopSettings()); // need to get pwd for stylecop.dll for stylesheet var assemblyDirectory = AssemblyDirectory(Assembly.GetAssembly(typeof(StyleCopSettings))); var toolPath = context.File(assemblyDirectory).Path.GetDirectory(); var defaultStyleSheet = context.File(toolPath + "/StyleCopStyleSheet.xslt"); context.Log.Information($"Stylecop: Default stylesheet {context.MakeAbsolute(defaultStyleSheet)}"); var styleCopConsole = InitializeStyleCopConsole(context); var styleCopProjects = new List <CodeProject>(); var projectParser = new ProjectParser(context.FileSystem, context.Environment); if (settings.SolutionFile != null) { var solutionFile = settings.SolutionFile; var projectPath = solutionFile.MakeAbsolute(context.Environment).GetDirectory(); context.Log.Information($"Stylecop: Found solution {projectPath.FullPath}"); var solutionParser = new SolutionParser(context.FileSystem, context.Environment); var solution = solutionParser.Parse(solutionFile); styleCopProjects .AddRange( solution.Projects .Where(p => p.Type != FOLDER_PROJECT_TYPE_GUID) .Select(solutionProject => InitializeProject(context, solutionProject.Path, projectParser, styleCopConsole))); } else if (settings.ProjectFiles.Count != 0) { styleCopProjects .AddRange( settings.ProjectFiles .Select( projectFile => InitializeProject(context, projectFile, projectParser, styleCopConsole))); } else { throw new ArgumentException("Solution or Project file was not included"); } var handler = new StylecopHandlers(context); styleCopConsole.OutputGenerated += handler.OnOutputGenerated; styleCopConsole.ViolationEncountered += handler.ViolationEncountered; context.Log.Information($"Stylecop: Starting analysis"); styleCopConsole.Start(styleCopProjects.ToArray(), settings.FullAnalyze); context.Log.Information($"Stylecop: Finished analysis"); styleCopConsole.OutputGenerated -= handler.OnOutputGenerated; styleCopConsole.ViolationEncountered -= handler.ViolationEncountered; if (settings.HtmlReportFile != null) { settings.HtmlReportFile = settings.HtmlReportFile.MakeAbsolute(context.Environment); // copy default resources to output folder context.CopyDirectory(context.Directory(toolPath + "/resources"), settings.HtmlReportFile.GetDirectory() + "/resources"); context.Log.Information($"Stylecop: Creating html report {settings.HtmlReportFile.FullPath}"); Transform(context, settings.HtmlReportFile, settings.ResultsFile.MakeAbsolute(context.Environment), settings.StyleSheet ?? context.MakeAbsolute(defaultStyleSheet)); } if (handler.TotalViolations > 0) { throw new Exception($"{handler.TotalViolations} StyleCop violations encountered."); } }