Data-only class that represents rule set information for specific configuration
Exemplo n.º 1
0
        public bool TryGetProjectRuleSetFilePath(Project project, RuleSetDeclaration declaration, out string fullFilePath)
        {
            List <string> options = new List <string>();

            options.Add(declaration.RuleSetPath);                                                   // Might be a full path
            options.Add(PathHelper.ResolveRelativePath(declaration.RuleSetPath, project.FullName)); // Relative to project
            // Note: currently we don't search in rule set directories since we expect the project rule set
            // to be relative to the project. We can add this in the future if it will be needed.

            fullFilePath = options.FirstOrDefault(fileSystem.File.Exists);

            return(!string.IsNullOrWhiteSpace(fullFilePath));
        }
        private IEnumerable <RuleSetDeclaration> GetProjectRuleSetsDeclarationsIterator(Project project)
        {
            /* This method walks through all of the available configurations (e.g. Debug, Release, Foo) and
             * attempts to fetch the values of a couple of properties from the project (CodeAnalysisRuleSet
             * and CodeAnalysisRuleSetDirectories). The collected data is put into a data object
             * and returned to the caller. The collected data includes the DTE Property object itself, which
             * is used later to update the ruleset value.
             *
             * TODO: consider refactoring. The code seems over-complicated: it finds the "ruleset"
             * property for all configurations, then backtracks to find the configuration, then looks
             * for the corresponding "ruleset directories" property.
             * Note: we are now fetching the "ruleset directories" property from the MSBuild project,
             * rather than through the DTE (the previous version of this code that used the DTE fails
             * for C# and VB projects that use the new project system).
             */

            var declarations = new List <RuleSetDeclaration>();

            var projectSystem = this.serviceProvider.GetService <IProjectSystemHelper>();

            var ruleSetProperties = VsShellUtils.GetProjectProperties(project, Constants.CodeAnalysisRuleSetPropertyKey);

            Debug.Assert(ruleSetProperties != null);
            Debug.Assert(ruleSetProperties.All(p => p != null), "Not expecting nulls in the list of properties");

            if (!ruleSetProperties.Any())
            {
                logger.WriteLine(Strings.CouldNotFindCodeAnalysisRuleSetPropertyOnProject, project.UniqueName);
            }

            foreach (Property ruleSetProperty in ruleSetProperties)
            {
                string   activationContext       = TryGetPropertyConfiguration(ruleSetProperty)?.ConfigurationName ?? string.Empty;
                string   ruleSetDirectoriesValue = projectSystem.GetProjectProperty(project, Constants.CodeAnalysisRuleSetDirectoriesPropertyKey, activationContext);
                string[] ruleSetDirectories      = ruleSetDirectoriesValue?.Split(new[] { RuleSetDirectoriesValueSpliter }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                string   ruleSetValue            = ruleSetProperty.Value as string;

                var declaration = new RuleSetDeclaration(project, ruleSetProperty, ruleSetValue, activationContext, ruleSetDirectories);
                declarations.Add(declaration);
            }

            return(declarations);
        }
Exemplo n.º 3
0
        private bool HasAnySonarRule(RuleSetDeclaration ruleSetDeclaration)
        {
            var projectDirectoryFullPath = Path.GetDirectoryName(ruleSetDeclaration.RuleSetProjectFullName);
            var projectRuleSetFullPath   = GetFullPath(ruleSetDeclaration.RuleSetPath, projectDirectoryFullPath);

            if (!File.Exists(projectRuleSetFullPath))
            {
                return(false);
            }

            var projectRuleSet = RuleSet.LoadFromFile(projectRuleSetFullPath);

            // 1. Collect all paths (current ruleset + includes)
            var ruleSetIncludeFullPaths = projectRuleSet.RuleSetIncludes
                                          .Select(include => GetFullPath(include.FilePath, projectDirectoryFullPath))
                                          .ToList();

            // 2. Look if any of the effective rules is from SonarAnalyzer and initialize dictionary with this result.
            return(projectRuleSet
                   .GetEffectiveRules(ruleSetIncludeFullPaths, new RuleInfoProvider[0])
                   .Any(rule => rule.AnalyzerId.StartsWith("SonarAnalyzer.", StringComparison.OrdinalIgnoreCase)));
        }
        private RuleSet FindDeclarationRuleSet(Dictionary <string, RuleSet> cache, Project project, RuleSetDeclaration declaration)
        {
            var ruleSetInfoProvider = this.serviceProvider.GetService <ISolutionRuleSetsInformationProvider>();

            ruleSetInfoProvider.AssertLocalServiceIsNotNull();

            var ruleSetSerializer = this.serviceProvider.GetService <IRuleSetSerializer>();

            ruleSetSerializer.AssertLocalServiceIsNotNull();

            string ruleSetFilePath;

            // Check if project rule set is found (we treat missing/erroneous rule set settings as not found)
            if (!ruleSetInfoProvider.TryGetProjectRuleSetFilePath(project, declaration, out ruleSetFilePath))
            {
                return(null);
            }

            RuleSet projectRuleSet;

            if (!cache.TryGetValue(ruleSetFilePath, out projectRuleSet))
            {
                // We treat corrupted rulesets as not found
                projectRuleSet         = ruleSetSerializer.LoadRuleSet(ruleSetFilePath);
                cache[ruleSetFilePath] = projectRuleSet;
            }

            return(projectRuleSet);
        }
        private bool IsRuleSetBound(Dictionary <string, RuleSet> cache, Project project, RuleSetDeclaration declaration, RuleSet sonarQubeRuleSet)
        {
            RuleSet projectRuleSet = this.FindDeclarationRuleSet(cache, project, declaration);

            return(projectRuleSet != null && RuleSetHelper.FindInclude(projectRuleSet, sonarQubeRuleSet) != null);
        }
        private RuleSet FindDeclarationRuleSet(IRuleSetSerializer ruleSetSerializer, Project project, RuleSetDeclaration declaration)
        {
            string ruleSetFilePath;

            // Check if project rule set is found (we treat missing/erroneous rule set settings as not found)
            if (!ruleSetInfoProvider.TryGetProjectRuleSetFilePath(project, declaration, out ruleSetFilePath))
            {
                return(null);
            }

            return(ruleSetSerializer.LoadRuleSet(ruleSetFilePath));
        }
        private bool IsRuleSetBound(IRuleSetSerializer ruleSetSerializer, Project project, RuleSetDeclaration declaration, RuleSet sonarQubeRuleSet)
        {
            RuleSet projectRuleSet = this.FindDeclarationRuleSet(ruleSetSerializer, project, declaration);

            return(projectRuleSet != null && RuleSetIncludeChecker.HasInclude(projectRuleSet, sonarQubeRuleSet));
        }
        public bool TryGetProjectRuleSetFilePath(Project project, RuleSetDeclaration declaration, out string fullFilePath)
        {
            List<string> options = new List<string>();
            options.Add(declaration.RuleSetPath); // Might be a full path
            options.Add(PathHelper.ResolveRelativePath(declaration.RuleSetPath, project.FullName)); // Relative to project
            // Note: currently we don't search in rule set directories since we expect the project rule set
            // to be relative to the project. We can add this in the future if it will be needed.

            IFileSystem fileSystem = this.serviceProvider.GetService<IFileSystem>();
            fileSystem.AssertLocalServiceIsNotNull();

            fullFilePath = options.FirstOrDefault(fileSystem.FileExist);

            return !string.IsNullOrWhiteSpace(fullFilePath);
        }
        private RuleSet FindDeclarationRuleSet(Dictionary<string, RuleSet> cache, Project project, RuleSetDeclaration declaration)
        {
            var ruleSetInfoProvider = this.serviceProvider.GetService<ISolutionRuleSetsInformationProvider>();
            ruleSetInfoProvider.AssertLocalServiceIsNotNull();

            var ruleSetSerializer = this.serviceProvider.GetService<IRuleSetSerializer>();
            ruleSetSerializer.AssertLocalServiceIsNotNull();

            string ruleSetFilePath;

            // Check if project rule set is found (we treat missing/erroneous rule set settings as not found)
            if (!ruleSetInfoProvider.TryGetProjectRuleSetFilePath(project, declaration, out ruleSetFilePath))
            {
                return null;
            }

            RuleSet projectRuleSet;
            if (!cache.TryGetValue(ruleSetFilePath, out projectRuleSet))
            {
                // We treat corrupted rulesets as not found
                projectRuleSet = ruleSetSerializer.LoadRuleSet(ruleSetFilePath);
                cache[ruleSetFilePath] = projectRuleSet;
            }

            return projectRuleSet;
        }
        private bool IsRuleSetBound(Dictionary<string, RuleSet> cache, Project project, RuleSetDeclaration declaration, RuleSet sonarQubeRuleSet)
        {
            RuleSet projectRuleSet = this.FindDeclarationRuleSet(cache, project, declaration);

            return (projectRuleSet != null && RuleSetHelper.FindInclude(projectRuleSet, sonarQubeRuleSet) != null);
        }