Exemplo n.º 1
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);
        }
        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).ToArray();

            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);
            AnalysisProperties properties = GetAnalysisProperties(config);
            var globalSourceEncoding      = GetSourceEncoding(properties, new EncodingProvider());

            ProjectInfoAnalysisResult result = ProcessProjectInfoFiles(projects, writer, logger, projectBaseDir, globalSourceEncoding);

            writer.WriteSonarProjectInfo(projectBaseDir, result.SharedFiles);

            IEnumerable <ProjectInfo> validProjects = result.GetProjectsByStatus(ProjectInfoValidity.Valid);

            if (validProjects.Any() || result.SharedFiles.Any())
            {
                // Handle global settings
                properties = GetAnalysisPropertiesToWrite(properties, 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);
        }