using System; using System.Collections.Generic; using System.Linq; using System.Text; using Codelyzer.Analysis; using Codelyzer.Analysis.Analyzer; using CTA.Rules.Config; namespace CTA.Rules.Models { public class PortSolutionResult : SolutionResult { public HashSet References { get; set; } public HashSet DownloadedFiles { get; set; } public string SolutionPath { get; set; } public PortSolutionResult(string solutionPath) : base() { SolutionPath = solutionPath; References = new HashSet(); DownloadedFiles = new HashSet(); } public PortSolutionResult(string solutionPath, SolutionResult solutionResult) : base() { SolutionPath = solutionPath; References = new HashSet(); DownloadedFiles = new HashSet(); this.ProjectResults = solutionResult.ProjectResults; } // Solution build errors grouped by project private Dictionary> _buildErrors; public Dictionary> BuildErrors { get { if (_buildErrors == null || !_buildErrors.Any()) { _buildErrors = GetSolutionBuildErrors(); } return _buildErrors; } set { _buildErrors = value; } } public void AddSolutionResult(SolutionResult solutionResult) { this.ProjectResults = solutionResult.ProjectResults; } private Dictionary> GetSolutionBuildErrors() { var buildErrorsByProject = new Dictionary>(); var codeAnalyzer = GetCodeAnalyzer(); var analyzerResults = new List(); try { if (ProjectResults.Any(p => p.MetaReferences != null)) { var metaReferences = ProjectResults.ToDictionary(p => p.ProjectFile, p => p.MetaReferences); analyzerResults = codeAnalyzer.AnalyzeSolution(SolutionPath, null, metaReferences).Result; } // Process build errors by project foreach (var analyzerResult in analyzerResults) { if (analyzerResult.ProjectResult != null) { // Count build errors by type var buildErrorCounts = new Dictionary(); var buildErrors = analyzerResult.ProjectResult.BuildErrors; buildErrors.ForEach(e => { string errorSeparator = ": error "; var index = e.IndexOf(errorSeparator, StringComparison.InvariantCulture); var s = e[(index + errorSeparator.Length)..]; if (buildErrorCounts.ContainsKey(s)) { buildErrorCounts[s]++; } else { buildErrorCounts.Add(s, 1); } }); var projectPath = analyzerResult.ProjectResult.ProjectFilePath; buildErrorsByProject[projectPath] = buildErrorCounts; } analyzerResult.Dispose(); } } catch (Exception) { foreach (var analyzerResult in analyzerResults) { analyzerResult.Dispose(); } } return buildErrorsByProject; } public override string ToString() { StringBuilder str = new StringBuilder(); // Print References str.AppendLine("=========="); str.AppendLine(nameof(References)); str.AppendLine("=========="); str.AppendLine(string.Join(Environment.NewLine, References)); str.AppendLine(); // Print Downloaded Files str.AppendLine("==============="); str.AppendLine(nameof(DownloadedFiles)); str.AppendLine("==============="); str.AppendLine(string.Join(Environment.NewLine, DownloadedFiles)); str.AppendLine(); // Print Project Results str.AppendLine("=============="); str.AppendLine(nameof(ProjectResults)); str.AppendLine("=============="); foreach (var projectResult in ProjectResults) { str.AppendLine("----------------------"); str.AppendLine($"Showing results for: {projectResult.ProjectFile}"); str.AppendLine("----------------------"); str.AppendLine(projectResult.ToString()); } str.AppendLine(); // Print Build Errors str.AppendLine("==========="); str.AppendLine(nameof(BuildErrors)); str.AppendLine("==========="); str.AppendLine(BuildErrorsToString()); return str.ToString(); } private string BuildErrorsToString() { StringBuilder errorReport = new StringBuilder(); foreach (var projectFile in BuildErrors.Keys) { errorReport.AppendLine("------------------"); errorReport.AppendLine($"BUILD ERRORS FOR: {projectFile}"); errorReport.AppendLine("------------------"); errorReport.AppendLine(); var buildErrorCounts = BuildErrors[projectFile]; foreach (var buildError in buildErrorCounts.Keys) { errorReport.AppendLine($"BuildError: {buildError}"); errorReport.AppendLine($"Count: {buildErrorCounts[buildError]}"); errorReport.AppendLine(); } errorReport.AppendLine(); } errorReport.AppendLine(); return errorReport.ToString(); } private CodeAnalyzerByLanguage GetCodeAnalyzer() { AnalyzerConfiguration analyzerConfiguration = new AnalyzerConfiguration(LanguageOptions.CSharp); ExportSettings exportSettings = new ExportSettings() { GenerateGremlinOutput = false, GenerateJsonOutput = false, GenerateRDFOutput = false }; MetaDataSettings metaDataSettings = new MetaDataSettings() { Annotations = false, DeclarationNodes = false, LambdaMethods = false, LiteralExpressions = false, LocationData = false, MethodInvocations = false, ReferenceData = false }; analyzerConfiguration.ExportSettings = exportSettings; analyzerConfiguration.MetaDataSettings = metaDataSettings; //CodeAnalyzer analyzer = CodeAnalyzerFactory.GetAnalyzer(analyzerConfiguration, LogHelper.Logger); CodeAnalyzerByLanguage analyzerByLanguage = new CodeAnalyzerByLanguage(analyzerConfiguration, LogHelper.Logger); return analyzerByLanguage; } } }