public /*for testing*/ PropertiesFileGenerator(AnalysisConfig analysisConfig, ILogger logger, IRoslynV1SarifFixer fixer) { this.analysisConfig = analysisConfig ?? throw new ArgumentNullException(nameof(analysisConfig)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); this.fixer = fixer ?? throw new ArgumentNullException(nameof(fixer)); }
private static void TryFixSarifReport(ILogger logger, ProjectInfo project, IRoslynV1SarifFixer fixer, string language, string reportFilePropertyKey) { Property reportPathProperty; bool tryResult = project.TryGetAnalysisSetting(reportFilePropertyKey, out reportPathProperty); if (tryResult) { string reportPath = reportPathProperty.Value; string fixedPath = fixer.LoadAndFixFile(reportPath, language, logger); if (!reportPath.Equals(fixedPath)) // only need to alter the property if there was no change { // remove the property ahead of changing it // if the new path is null, the file was unfixable and we should leave the property out project.AnalysisSettings.Remove(reportPathProperty); if (fixedPath != null) { // otherwise, set the property value (results in no change if the file was already valid) Property newReportPathProperty = new Property(); newReportPathProperty.Id = reportFilePropertyKey; newReportPathProperty.Value = fixedPath; project.AnalysisSettings.Add(newReportPathProperty); } } } }
public /* for test */ static ProjectInfoAnalysisResult GenerateFile(AnalysisConfig config, ILogger logger, IRoslynV1SarifFixer fixer) { if (config == null) { throw new ArgumentNullException("config"); } if (logger == null) { throw new ArgumentNullException("logger"); } string fileName = Path.Combine(config.SonarOutputDir, ProjectPropertiesFileName); logger.LogDebug(Resources.MSG_GeneratingProjectProperties, fileName); IEnumerable<ProjectInfo> projects = ProjectLoader.LoadFrom(config.SonarOutputDir); if (projects == null || !projects.Any()) { logger.LogError(Resources.ERR_NoProjectInfoFilesFound); return new ProjectInfoAnalysisResult(); } FixSarifReport(logger, projects, fixer); PropertiesWriter writer = new PropertiesWriter(config); ProjectInfoAnalysisResult result = ProcessProjectInfoFiles(projects, writer, logger); IEnumerable<ProjectInfo> validProjects = result.GetProjectsByStatus(ProjectInfoValidity.Valid); if (validProjects.Any()) { // Handle global settings AnalysisProperties properties = GetAnalysisPropertiesToWrite(config, logger); writer.WriteGlobalSettings(properties); string contents = writer.Flush(); result.FullPropertiesFilePath = fileName; File.WriteAllText(result.FullPropertiesFilePath, contents, Encoding.ASCII); } else { // if the user tries to build multiple configurations at once there will be duplicate projects if (result.GetProjectsByStatus(ProjectInfoValidity.DuplicateGuid).Any()) { logger.LogError(Resources.ERR_NoValidButDuplicateProjects); } else { logger.LogError(Resources.ERR_NoValidProjectInfoFiles); } } return result; }
/// <summary> /// Loads SARIF reports from the given projects and attempts to fix /// improper escaping from Roslyn V1 (VS 2015 RTM) where appropriate. /// </summary> private static void FixSarifReport(ILogger logger, IEnumerable<ProjectInfo> projects, IRoslynV1SarifFixer fixer /* for test */) { // attempt to fix invalid project-level SARIF emitted by Roslyn 1.0 (VS 2015 RTM) foreach (ProjectInfo project in projects) { Property reportPathProperty; bool tryResult = project.TryGetAnalysisSetting(RoslynV1SarifFixer.ReportFilePropertyKey, out reportPathProperty); if (tryResult) { string reportPath = reportPathProperty.Value; string fixedPath = fixer.LoadAndFixFile(reportPath, logger); if (!reportPath.Equals(fixedPath)) // only need to alter the property if there was no change { // remove the property ahead of changing it // if the new path is null, the file was unfixable and we should leave the property out project.AnalysisSettings.Remove(reportPathProperty); if (fixedPath != null) { // otherwise, set the property value (results in no change if the file was already valid) Property newReportPathProperty = new Property(); newReportPathProperty.Id = RoslynV1SarifFixer.ReportFilePropertyKey; newReportPathProperty.Value = fixedPath; project.AnalysisSettings.Add(newReportPathProperty); } } } } }
public /* for test */ static ProjectInfoAnalysisResult GenerateFile(AnalysisConfig config, ILogger logger, IRoslynV1SarifFixer fixer) { if (config == null) { throw new ArgumentNullException("config"); } if (logger == null) { throw new ArgumentNullException("logger"); } string fileName = Path.Combine(config.SonarOutputDir, ProjectPropertiesFileName); logger.LogDebug(Resources.MSG_GeneratingProjectProperties, fileName); IEnumerable <ProjectInfo> projects = ProjectLoader.LoadFrom(config.SonarOutputDir); if (projects == null || !projects.Any()) { logger.LogError(Resources.ERR_NoProjectInfoFilesFound); return(new ProjectInfoAnalysisResult()); } TryFixSarifReports(logger, projects, fixer); string projectBaseDir = ComputeProjectBaseDir(config, projects); PropertiesWriter writer = new PropertiesWriter(config); ProjectInfoAnalysisResult result = ProcessProjectInfoFiles(projects, writer, logger, projectBaseDir); writer.WriteSonarProjectInfo(projectBaseDir, result.SharedFiles); IEnumerable <ProjectInfo> validProjects = result.GetProjectsByStatus(ProjectInfoValidity.Valid); if (validProjects.Any() || result.SharedFiles.Any()) { // Handle global settings AnalysisProperties properties = GetAnalysisPropertiesToWrite(config, logger); writer.WriteGlobalSettings(properties); string contents = writer.Flush(); result.FullPropertiesFilePath = fileName; File.WriteAllText(result.FullPropertiesFilePath, contents, Encoding.ASCII); } else { // if the user tries to build multiple configurations at once there will be duplicate projects if (result.GetProjectsByStatus(ProjectInfoValidity.DuplicateGuid).Any()) { logger.LogError(Resources.ERR_NoValidButDuplicateProjects); } else { logger.LogError(Resources.ERR_NoValidProjectInfoFiles); } } return(result); }
/// <summary> /// Loads SARIF reports from the given projects and attempts to fix /// improper escaping from Roslyn V1 (VS 2015 RTM) where appropriate. /// </summary> private static void TryFixSarifReports(ILogger logger, IEnumerable <ProjectInfo> projects, IRoslynV1SarifFixer fixer /* for test */) { // attempt to fix invalid project-level SARIF emitted by Roslyn 1.0 (VS 2015 RTM) foreach (ProjectInfo project in projects) { TryFixSarifReport(logger, project, fixer, RoslynV1SarifFixer.CSharpLanguage, ReportFileCsharpPropertyKey); TryFixSarifReport(logger, project, fixer, RoslynV1SarifFixer.VBNetLanguage, ReportFileVbnetPropertyKey); } }
/// <summary> /// Loads SARIF reports from the given projects and attempts to fix /// improper escaping from Roslyn V1 (VS 2015 RTM) where appropriate. /// </summary> private static void FixSarifReport(ILogger logger, IEnumerable <ProjectInfo> projects, IRoslynV1SarifFixer fixer /* for test */) { // attempt to fix invalid project-level SARIF emitted by Roslyn 1.0 (VS 2015 RTM) foreach (ProjectInfo project in projects) { Property reportPathProperty; bool tryResult = project.TryGetAnalysisSetting(RoslynV1SarifFixer.ReportFilePropertyKey, out reportPathProperty); if (tryResult) { string reportPath = reportPathProperty.Value; string fixedPath = fixer.LoadAndFixFile(reportPath, logger); if (!reportPath.Equals(fixedPath)) // only need to alter the property if there was no change { // remove the property ahead of changing it // if the new path is null, the file was unfixable and we should leave the property out project.AnalysisSettings.Remove(reportPathProperty); if (fixedPath != null) { // otherwise, set the property value (results in no change if the file was already valid) Property newReportPathProperty = new Property(); newReportPathProperty.Id = RoslynV1SarifFixer.ReportFilePropertyKey; newReportPathProperty.Value = fixedPath; project.AnalysisSettings.Add(newReportPathProperty); } } } } }