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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Locates the ProjectInfo.xml files and uses the information in them to generate
        /// a sonar-scanner properties file
        /// </summary>
        /// <returns>Information about each of the project info files that was processed, together with
        /// the full path to generated file.
        /// Note: the path to the generated file will be null if the file could not be generated.</returns>
        public ProjectInfoAnalysisResult GenerateFile()
        {
            var projectPropertiesPath = Path.Combine(analysisConfig.SonarOutputDir, ProjectPropertiesFileName);

            logger.LogDebug(Resources.MSG_GeneratingProjectProperties, projectPropertiesPath);

            var result = new ProjectInfoAnalysisResult();

            var writer = new PropertiesWriter(analysisConfig);

            var success = TryWriteProperties(writer, out IEnumerable <ProjectData> projects);

            if (success)
            {
                var contents = writer.Flush();

                File.WriteAllText(projectPropertiesPath, contents, Encoding.ASCII);

                result.FullPropertiesFilePath = projectPropertiesPath;
            }

            result.Projects.AddRange(projects);

            return(result);
        }
        private static ProjectInfoAnalysisResult ProcessProjectInfoFiles(IEnumerable <ProjectInfo> projects, PropertiesWriter writer, ILogger logger, string projectBaseDir)
        {
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult();

            foreach (ProjectInfo projectInfo in projects)
            {
                ProjectInfoValidity status = ClassifyProject(projectInfo, projects, logger);

                if (status == ProjectInfoValidity.Valid)
                {
                    IEnumerable <string> files = GetFilesToAnalyze(projectInfo, logger, projectBaseDir, result);
                    if (files == null || !files.Any())
                    {
                        status = ProjectInfoValidity.NoFilesToAnalyze;
                    }
                    else
                    {
                        string fxCopReport      = TryGetFxCopReport(projectInfo, logger);
                        string vsCoverageReport = TryGetCodeCoverageReport(projectInfo, logger);
                        writer.WriteSettingsForProject(projectInfo, files, fxCopReport, vsCoverageReport);
                    }
                }

                result.Projects.Add(projectInfo, status);
            }
            return(result);
        }
Exemplo n.º 4
0
        public bool TryWriteProperties(PropertiesWriter writer, out IEnumerable <ProjectData> allProjects)
        {
            var projects = ProjectLoader.LoadFrom(analysisConfig.SonarOutputDir);

            if (projects == null || !projects.Any())
            {
                logger.LogError(Resources.ERR_NoProjectInfoFilesFound);
                allProjects = Enumerable.Empty <ProjectData>();
                return(false);
            }

            var projectPaths = projects.Select(p => p.GetProjectDirectory()).ToList();

            var analysisProperties = analysisConfig.ToAnalysisProperties(logger);

            FixSarifAndEncoding(projects, analysisProperties);

            allProjects = projects
                          .GroupBy(p => p.ProjectGuid)
                          .Select(ToProjectData)
                          .ToList();

            var validProjects = allProjects
                                .Where(d => d.Status == ProjectInfoValidity.Valid)
                                .ToList();

            if (validProjects.Count == 0)
            {
                logger.LogError(Resources.ERR_NoValidProjectInfoFiles);
                return(false);
            }

            var rootProjectBaseDir = ComputeRootProjectBaseDir(projectPaths);

            if (rootProjectBaseDir == null ||
                !Directory.Exists(rootProjectBaseDir))
            {
                logger.LogError(Resources.ERR_ProjectBaseDirDoesNotExist);
                return(false);
            }

            var rootModuleFiles = PutFilesToRightModuleOrRoot(validProjects, rootProjectBaseDir);

            PostProcessProjectStatus(validProjects);

            if (rootModuleFiles.Count == 0 && validProjects.All(p => p.Status == ProjectInfoValidity.NoFilesToAnalyze))
            {
                logger.LogError(Resources.ERR_NoValidProjectInfoFiles);
                return(false);
            }

            writer.WriteSonarProjectInfo(rootProjectBaseDir);
            writer.WriteSharedFiles(rootModuleFiles);
            validProjects.ForEach(p => writer.WriteSettingsForProject(p, p.CoverageAnalysisExists(logger)
                ? p.VisualStudioCoverageLocation : null));

            // Handle global settings
            writer.WriteGlobalSettings(analysisProperties);

            return(true);
        }
Exemplo n.º 5
0
        private static ProjectInfoAnalysisResult ProcessProjectInfoFiles(IEnumerable <ProjectInfo> projects, PropertiesWriter writer, ILogger logger, string rootProjectBaseDir, string globalSourceEncoding)
        {
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult();

            foreach (ProjectInfo projectInfo in projects)
            {
                ProjectInfoValidity status = ClassifyProject(projectInfo, projects, logger);

                if (status == ProjectInfoValidity.Valid)
                {
                    IEnumerable <string> files = GetFilesToAnalyze(projectInfo, logger, rootProjectBaseDir, result);
                    if (files == null || !files.Any())
                    {
                        status = ProjectInfoValidity.NoFilesToAnalyze;
                    }
                    else
                    {
                        string vsCoverageReport = TryGetCodeCoverageReport(projectInfo, logger);
                        FixEncoding(logger, globalSourceEncoding, projectInfo);
                        writer.WriteSettingsForProject(projectInfo, files, vsCoverageReport);
                    }
                }

                result.Projects.Add(projectInfo, status);
            }
            result.SharedFiles.RemoveWhere(s => IsPartOfAProject(projects, s));

            return(result);
        }