public void Prepare(CancellationToken token)
        {
            Debug.Assert(this.SolutionFullPath != null, "Expected to be initialized");

            foreach (var keyValue in this.bindingConfigInformationMap)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                ConfigFileInformation info = keyValue.Value;
                Debug.Assert(!string.IsNullOrWhiteSpace(info.NewFilePath), "Expected to be set during registration time");

                this.sourceControlledFileSystem.QueueFileWrite(info.NewFilePath, () =>
                {
                    var ruleSetDirectoryPath = Path.GetDirectoryName(info.NewFilePath);

                    fileSystem.Directory.CreateDirectory(ruleSetDirectoryPath); // will no-op if exists

                    // Create or overwrite existing rule set
                    info.BindingConfigFile.Save(info.NewFilePath);

                    return(true);
                });

                Debug.Assert(this.sourceControlledFileSystem.FileExistOrQueuedToBeWritten(info.NewFilePath), "Expected a rule set to pend pended");
            }

            foreach (IBindingOperation binder in this.childBinder)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                binder.Prepare(token);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Queues a write to a project-level SonarQube <see cref="RuleSet"/> file in the <param name="projectRoot"/> directory.
        /// </summary>
        /// <param name="projectFullPath">The absolute full path to the project</param>
        /// <param name="ruleSetFileName">The rule set file name</param>
        /// <param name="solutionRuleSet\">Full path of the parent solution-level SonarQube rule set</param>
        /// <returns>Full file path of the file that we expect to write to</returns>
        internal /*for testing purposes*/ string QueueWriteProjectLevelRuleSet(string projectFullPath, string ruleSetFileName, ConfigFileInformation solutionRuleSet, string currentRuleSetPath)
        {
            Debug.Assert(!string.IsNullOrWhiteSpace(projectFullPath));
            Debug.Assert(!string.IsNullOrWhiteSpace(ruleSetFileName));
            Debug.Assert(solutionRuleSet != null);

            // TODO: clean up to remove down-cast to check for RuleSet
            solutionRuleSet.BindingConfigFile.TryGetRuleSet(out var dotNetRuleSet);
            Debug.Assert(dotNetRuleSet != null, "Only expecting this method to be called for projects that have a VS RuleSet");

            string projectRoot = Path.GetDirectoryName(projectFullPath);
            string ruleSetRoot = PathHelper.ForceDirectoryEnding(projectRoot);

            string  existingRuleSetPath;
            RuleSet existingRuleSet;

            if (this.TryUpdateExistingProjectRuleSet(solutionRuleSet.NewFilePath, ruleSetRoot, currentRuleSetPath, out existingRuleSetPath, out existingRuleSet))
            {
                Debug.Assert(existingRuleSetPath != null);
                Debug.Assert(existingRuleSet != null);

                // Pend update
                this.sourceControlledFileSystem.QueueFileWrite(existingRuleSetPath, () =>
                {
                    existingRuleSet.WriteToFile(existingRuleSetPath);
                    return(true);
                });

                return(existingRuleSetPath);
            }

            // Create a new project level rule set
            string solutionIncludePath = PathHelper.CalculateRelativePath(ruleSetRoot, solutionRuleSet.NewFilePath);

            RuleSet newRuleSet     = GenerateNewProjectRuleSet(solutionIncludePath, currentRuleSetPath, dotNetRuleSet.DisplayName);
            string  newRuleSetPath = this.GenerateNewProjectRuleSetPath(ruleSetRoot, ruleSetFileName);

            // Pend new
            this.sourceControlledFileSystem.QueueFileWrite(newRuleSetPath, () =>
            {
                this.ruleSetSerializer.WriteRuleSetFile(newRuleSet, newRuleSetPath);
                return(true);
            });

            return(newRuleSetPath);
        }