Esempio n. 1
0
        /// <summary>
        /// Loads a property which no longer exists, and translates it into an enabled or
        /// disabled rule.
        /// </summary>
        /// <param name="settings">
        /// The settings.
        /// </param>
        /// <param name="analyzerId">
        /// The ID of the analyzer owning the property.
        /// </param>
        /// <param name="propertyName">
        /// The name of legacy property.
        /// </param>
        /// <param name="nodeText">
        /// The property value.
        /// </param>
        private static void LoadLegacyAnalyzerSetting(Settings settings, string analyzerId, string propertyName, string nodeText)
        {
            Param.AssertNotNull(settings, "settings");
            Param.AssertValidString(analyzerId, "analyzerId");
            Param.AssertValidString(propertyName, "propertyName");
            Param.AssertNotNull(nodeText, "nodeText");

            switch (analyzerId)
            {
            case "StyleCop.CSharp.DocumentationRules":
                if (propertyName == "RequireValueTags")
                {
                    SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId);
                    if (analyzer != null)
                    {
                        AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer);

                        if (settingsForAnalyzer == null)
                        {
                            settingsForAnalyzer = new AddInPropertyCollection(analyzer);
                            settings.SetAddInSettings(settingsForAnalyzer);
                        }

                        bool enabled = nodeText != "0";

                        AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValue", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);

                        AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValueText", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);
                    }
                }

                break;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads parser settings from the document.
        /// </summary>
        /// <param name="document">
        /// The settings document.
        /// </param>
        /// <param name="settings">
        /// Stores the settings.
        /// </param>
        private static void LoadParserSettings(XmlDocument document, Settings settings)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList parsersNodes = document.DocumentElement.SelectNodes("Parsers/Parser");

            if (parsersNodes != null && parsersNodes.Count > 0)
            {
                foreach (XmlNode parserNode in parsersNodes)
                {
                    XmlAttribute parserId = parserNode.Attributes["ParserId"];
                    if (parserId != null && !string.IsNullOrEmpty(parserId.Value))
                    {
                        string parserName = parserId.Value;
                        if (parserName.Equals("Microsoft.SourceAnalysis.CSharp.CsParser", StringComparison.Ordinal))
                        {
                            parserName = "StyleCop.CSharp.CsParser";
                        }

                        // Get the parser instance.
                        SourceParser parserInstance = settings.Core.GetParser(parserName);
                        if (parserInstance != null)
                        {
                            // Get the parser settings object for this parser or create a new one.
                            AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance);

                            if (settingsForParser == null)
                            {
                                settingsForParser = new AddInPropertyCollection(parserInstance);
                                settings.SetAddInSettings(settingsForParser);
                            }

                            // Load the settings for this parser.
                            XmlNode parserSettingsNode = parserNode["ParserSettings"];
                            if (parserSettingsNode != null)
                            {
                                LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null);
                            }

                            // Load any rule settings for the parser.
                            LoadRulesSettings(parserNode, settingsForParser, parserInstance.PropertyDescriptors);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Loads parser settings from the document.
        /// </summary>
        /// <param name="documentRoot">The root node of the settings document.</param>
        /// <param name="settings">Stores the settings.</param>
        private static void LoadParserSettings(XmlNode documentRoot, Settings settings)
        {
            Param.AssertNotNull(documentRoot, "documentRoot");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList parsersNodes = documentRoot.SelectNodes("Parsers/Parser");

            if (parsersNodes != null && parsersNodes.Count > 0)
            {
                foreach (XmlNode parserNode in parsersNodes)
                {
                    XmlAttribute parserId = parserNode.Attributes["ParserId"];
                    if (parserId != null && !string.IsNullOrEmpty(parserId.Value))
                    {
                        string parserName = ConvertLegacyAddInName(parserId.Value);

                        // Get the parser instance.
                        SourceParser parserInstance = settings.Core.GetParser(parserName);
                        if (parserInstance != null)
                        {
                            // Get the parser settings object for this parser or create a new one.
                            AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance);

                            if (settingsForParser == null)
                            {
                                settingsForParser = new AddInPropertyCollection(parserInstance);
                                settings.SetAddInSettings(settingsForParser);
                            }

                            // Load the settings for this parser.
                            XmlNode parserSettingsNode = parserNode["ParserSettings"];
                            if (parserSettingsNode != null)
                            {
                                LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null);
                            }

                            // Load any rule settings for the parser.
                            LoadRulesSettings(parserNode, settingsForParser, parserInstance.PropertyDescriptors);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Loads analyzer settings from the document.
        /// </summary>
        /// <param name="documentRoot">The root node of the settings document.</param>
        /// <param name="settings">Stores the settings.</param>
        private static void LoadAnalyzerSettings(XmlNode documentRoot, Settings settings)
        {
            Param.AssertNotNull(documentRoot, "documentRoot");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList analyzerNodes = documentRoot.SelectNodes("Analyzers/Analyzer");

            if (analyzerNodes != null && analyzerNodes.Count > 0)
            {
                foreach (XmlNode analyzerNode in analyzerNodes)
                {
                    XmlAttribute analyzerId = analyzerNode.Attributes["AnalyzerId"];
                    if (analyzerId != null && !string.IsNullOrEmpty(analyzerId.Value))
                    {
                        string analyzerName = analyzerId.Value;

                        // Get the analyzer instance.
                        SourceAnalyzer analyzerInstance = settings.Core.GetAnalyzer(analyzerName);
                        if (analyzerInstance != null)
                        {
                            // Get the analyzer settings object for this analyzer or create a new one.
                            AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzerInstance);

                            if (settingsForAnalyzer == null)
                            {
                                settingsForAnalyzer = new AddInPropertyCollection(analyzerInstance);
                                settings.SetAddInSettings(settingsForAnalyzer);
                            }

                            // Load the settings for this analyzer.
                            XmlNode analyzerSettingsNode = analyzerNode["AnalyzerSettings"];
                            if (analyzerSettingsNode != null)
                            {
                                LoadPropertyCollection(analyzerSettingsNode, settingsForAnalyzer, analyzerInstance.PropertyDescriptors, null);
                            }

                            // Load any rule settings for the analyzer.
                            LoadRulesSettings(analyzerNode, settingsForAnalyzer, analyzerInstance.PropertyDescriptors);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Enables or disables all ruels for the given analyzers.
        /// </summary>
        /// <param name="disabledAnalyzersNode">The node representing the analyzer.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="enabled">Indicates whether to enable or disable the rules.</param>
        private static void EnableDisableAnalyzerRules(XmlNode disabledAnalyzersNode, Settings settings, bool enabled)
        {
            Param.AssertNotNull(disabledAnalyzersNode, "disabledAnalyzersNode");
            Param.AssertNotNull(settings, "settings");
            Param.Ignore(enabled);

            string[] ids = disabledAnalyzersNode.InnerText.Split(',');

            foreach (string id in ids)
            {
                string analyzerId43 = MapAnalyzerId(id);

                if (analyzerId43 != null)
                {
                    SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId43);
                    if (analyzer != null)
                    {
                        ICollection <string> rules = MapAnalyzerToRules(id);
                        if (rules != null)
                        {
                            AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer);

                            if (settingsForAnalyzer == null)
                            {
                                settingsForAnalyzer = new AddInPropertyCollection(analyzer);
                                settings.SetAddInSettings(settingsForAnalyzer);
                            }

                            foreach (string rule in rules)
                            {
                                AddBooleanProperty(rule + "#Enabled", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Loads parser settings from the document.
        /// </summary>
        /// <param name="document">
        /// The settings document.
        /// </param>
        /// <param name="settings">
        /// Stores the settings.
        /// </param>
        private static void LoadParserSettings(XmlDocument document, Settings settings)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList parsersNodes = document.DocumentElement.SelectNodes("Parsers/Parser");
            if (parsersNodes != null && parsersNodes.Count > 0)
            {
                foreach (XmlNode parserNode in parsersNodes)
                {
                    XmlAttribute parserId = parserNode.Attributes["ParserId"];
                    if (parserId != null && !string.IsNullOrEmpty(parserId.Value))
                    {
                        string parserName = parserId.Value;
                        if (parserName.Equals("Microsoft.SourceAnalysis.CSharp.CsParser", StringComparison.Ordinal))
                        {
                            parserName = "StyleCop.CSharp.CsParser";
                        }

                        // Get the parser instance.
                        SourceParser parserInstance = settings.Core.GetParser(parserName);
                        if (parserInstance != null)
                        {
                            // Get the parser settings object for this parser or create a new one.
                            AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance);

                            if (settingsForParser == null)
                            {
                                settingsForParser = new AddInPropertyCollection(parserInstance);
                                settings.SetAddInSettings(settingsForParser);
                            }

                            // Load the settings for this parser.
                            XmlNode parserSettingsNode = parserNode["ParserSettings"];
                            if (parserSettingsNode != null)
                            {
                                LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null);
                            }

                            // Load any rule settings for the parser.
                            LoadRulesSettings(parserNode, settingsForParser, parserInstance.PropertyDescriptors);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Loads analyzer settings from the document.
        /// </summary>
        /// <param name="document">
        /// The settings document.
        /// </param>
        /// <param name="settings">
        /// Stores the settings.
        /// </param>
        private static void LoadAnalyzerSettings(XmlDocument document, Settings settings)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList analyzerNodes = document.DocumentElement.SelectNodes("Analyzers/Analyzer");
            if (analyzerNodes != null && analyzerNodes.Count > 0)
            {
                foreach (XmlNode analyzerNode in analyzerNodes)
                {
                    XmlAttribute analyzerId = analyzerNode.Attributes["AnalyzerId"];
                    if (analyzerId != null && !string.IsNullOrEmpty(analyzerId.Value))
                    {
                        string analyzerId43 = MapAnalyzerId(analyzerId.Value);

                        // Get the analyzer instance for this mapped analyzer ID.
                        SourceAnalyzer analyzerInstance = settings.Core.GetAnalyzer(analyzerId43);
                        if (analyzerInstance != null)
                        {
                            // Get the analyzer settings object for this analyzer or create a new one.
                            AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzerInstance);

                            if (settingsForAnalyzer == null)
                            {
                                settingsForAnalyzer = new AddInPropertyCollection(analyzerInstance);
                                settings.SetAddInSettings(settingsForAnalyzer);
                            }

                            // Load the settings for this analyzer.
                            XmlNode analyzerSettingsNode = analyzerNode["AnalyzerSettings"];
                            if (analyzerSettingsNode != null)
                            {
                                LoadPropertyCollection(analyzerSettingsNode, settingsForAnalyzer, analyzerInstance.PropertyDescriptors, analyzerId.Value);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Merges two settings files together.
        /// </summary>
        /// <param name="originalSettings">
        /// The original settings.
        /// </param>
        /// <param name="overridingSettings">
        /// The settings which are overriding the original settings.
        /// </param>
        /// <returns>
        /// Returns the merged settings.
        /// </returns>
        private static Settings MergeSettings(Settings originalSettings, Settings overridingSettings)
        {
            Param.AssertNotNull(originalSettings, "originalSettings");
            Param.AssertNotNull(overridingSettings, "overridingSettings");

            //// TODO Not sure why this has to be true
            //// TODO Also where are we getting a different core from?
            Debug.Assert(originalSettings.Core == overridingSettings.Core, "The settings must come from the same core instance.");

            // Create a new merged settings object.
            Settings mergedSettings = new Settings(originalSettings.Core);

            // Merge the global settings together.
            MergePropertyCollections(originalSettings.GlobalSettings, overridingSettings.GlobalSettings, mergedSettings.GlobalSettings);

            // Merge the parser settings together. Loop through the settings for each parser in the original settings.
            foreach (AddInPropertyCollection originalParserSettings in originalSettings.ParserSettings)
            {
                // Try to find settings for this parser in the overriding settings.
                AddInPropertyCollection overridingParserSettings = overridingSettings.GetAddInSettings(originalParserSettings.AddIn);

                // Create a new merged parser settings object.
                AddInPropertyCollection mergedParserSettings = new AddInPropertyCollection(originalParserSettings.AddIn);
                mergedSettings.SetAddInSettings(mergedParserSettings);

                // Merge the parser settings together.
                MergePropertyCollections(originalParserSettings, overridingParserSettings, mergedParserSettings);
            }

            // Now loop through the settings for each parser in the overriding settings. If there are any parser
            // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings.
            // This means that there were no settings for this parser in the original settings.
            foreach (AddInPropertyCollection overridingParserSettings in overridingSettings.ParserSettings)
            {
                AddInPropertyCollection mergedParserSettings = mergedSettings.GetAddInSettings(overridingParserSettings.AddIn);
                if (mergedParserSettings == null)
                {
                    mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingParserSettings.Clone());
                }
            }

            // Merge the analyzer settings together. Loop through the settings for each analyzer in the original settings.
            foreach (AddInPropertyCollection originalAnalyzerSettings in originalSettings.AnalyzerSettings)
            {
                // Try to find settings for this analyzer in the overriding settings.
                AddInPropertyCollection overridingAnalyzerSettings = overridingSettings.GetAddInSettings(originalAnalyzerSettings.AddIn);

                // Create a new merged analyzer settings object.
                AddInPropertyCollection mergedAnalyzerSettings = new AddInPropertyCollection(originalAnalyzerSettings.AddIn);
                mergedSettings.SetAddInSettings(mergedAnalyzerSettings);

                // Merge the analyer settings together.
                MergePropertyCollections(originalAnalyzerSettings, overridingAnalyzerSettings, mergedAnalyzerSettings);
            }

            // Now loop through the settings for each analyzer in the overriding settings. If there are any analyzer
            // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings.
            // This means that there were no settings for this analyzer in the original settings.
            foreach (AddInPropertyCollection overridingAnalyzerSettings in overridingSettings.AnalyzerSettings)
            {
                AddInPropertyCollection mergedAnalyzerSettings = mergedSettings.GetAddInSettings(overridingAnalyzerSettings.AddIn);
                if (mergedAnalyzerSettings == null)
                {
                    mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingAnalyzerSettings.Clone());
                }
            }

            // Merge the write times together. Set the write time of the merged settings file to the most
            // recent write time of the two file which were merged.
            if (originalSettings.WriteTime.CompareTo(overridingSettings.WriteTime) > 0)
            {
                mergedSettings.WriteTime = originalSettings.WriteTime;
            }
            else
            {
                mergedSettings.WriteTime = overridingSettings.WriteTime;
            }

            return mergedSettings;
        }
Esempio n. 9
0
        /// <summary>
        /// Loads parser settings from the document.
        /// </summary>
        /// <param name="documentRoot">
        /// The root node of the settings document.
        /// </param>
        /// <param name="settings">
        /// Stores the settings.
        /// </param>
        private static void LoadParserSettings(XmlNode documentRoot, Settings settings)
        {
            Param.AssertNotNull(documentRoot, "documentRoot");
            Param.AssertNotNull(settings, "settings");

            XmlNodeList parsersNodes = documentRoot.SelectNodes("Parsers/Parser");
            if (parsersNodes != null && parsersNodes.Count > 0)
            {
                foreach (XmlNode parserNode in parsersNodes)
                {
                    XmlAttribute parserId = parserNode.Attributes["ParserId"];
                    if (parserId != null && !string.IsNullOrEmpty(parserId.Value))
                    {
                        string parserName = ConvertLegacyAddInName(parserId.Value);

                        // Get the parser instance.
                        SourceParser parserInstance = settings.Core.GetParser(parserName);
                        if (parserInstance != null)
                        {
                            // Get the parser settings object for this parser or create a new one.
                            AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance);

                            if (settingsForParser == null)
                            {
                                settingsForParser = new AddInPropertyCollection(parserInstance);
                                settings.SetAddInSettings(settingsForParser);
                            }

                            // Load the settings for this parser.
                            XmlNode parserSettingsNode = parserNode["ParserSettings"];
                            if (parserSettingsNode != null)
                            {
                                LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null);
                            }

                            // Load any rule settings for the parser.
                            LoadRulesSettings(parserNode, settingsForParser, parserInstance.PropertyDescriptors);
                        }
                    }
                }
            }
        }
Esempio n. 10
0
        public XmlDocument WriteSettingsToDocument(StyleCopEnvironment environment)
        {
            Param.RequireNotNull(environment, "environment");

            // Create a new document for the settings.
            XmlDocument document = WritableSettings.NewDocument();

            // Get the parent settings if there are any.
            SettingsMerger merger         = new SettingsMerger(this, environment);
            Settings       parentSettings = merger.ParentMergedSettings;

            // Add the global settings if there are any.
            if (this.GlobalSettings != null && this.GlobalSettings.Count > 0)
            {
                // Get the global settings from the parent.
                PropertyCollection parentGlobalSettings = null;
                if (parentSettings != null)
                {
                    parentGlobalSettings = parentSettings.GlobalSettings;
                }

                SavePropertyCollection(document.DocumentElement, "GlobalSettings", this.GlobalSettings, parentGlobalSettings, true, null);
            }

            // Add the parser settings if there are any.
            if (this.ParserSettings.Count > 0)
            {
                bool       parserSettingsAdded = false;
                XmlElement parsersNode         = document.CreateElement("Parsers");

                foreach (AddInPropertyCollection parserSettings in this.ParserSettings)
                {
                    // Add the settings for this parser if there are any.
                    if (parserSettings.Count > 0)
                    {
                        // Create a node for this parser.
                        XmlElement   parserNode        = document.CreateElement("Parser");
                        XmlAttribute parserIdAttribute = document.CreateAttribute("ParserId");
                        parserIdAttribute.Value = parserSettings.AddIn.Id;
                        parserNode.Attributes.Append(parserIdAttribute);

                        // Get the parser settings from the parent.
                        PropertyCollection parentParserSettings = null;
                        if (parentSettings != null)
                        {
                            parentParserSettings = parentSettings.GetAddInSettings(parserSettings.AddIn);
                        }

                        if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null))
                        {
                            parsersNode.AppendChild(parserNode);
                            parserSettingsAdded = true;
                        }
                    }
                }

                if (parserSettingsAdded)
                {
                    document.DocumentElement.AppendChild(parsersNode);
                }
            }

            // Add the analyzer settings if there are any.
            if (this.AnalyzerSettings.Count > 0)
            {
                bool       analyzerSettingsAdded = false;
                XmlElement analyzersNode         = document.CreateElement("Analyzers");

                foreach (AddInPropertyCollection analyzerSettings in this.AnalyzerSettings)
                {
                    // Add the settings for this analyzer if there are any.
                    if (analyzerSettings.Count > 0)
                    {
                        // Create a node for this analzyer.
                        XmlElement   analyzerNode        = document.CreateElement("Analyzer");
                        XmlAttribute analyzerIdAttribute = document.CreateAttribute("AnalyzerId");
                        analyzerIdAttribute.Value = analyzerSettings.AddIn.Id;
                        analyzerNode.Attributes.Append(analyzerIdAttribute);

                        // Get the analyzer settings from the parent.
                        PropertyCollection parentAnalyzerSettings = null;
                        if (parentSettings != null)
                        {
                            parentAnalyzerSettings = parentSettings.GetAddInSettings(analyzerSettings.AddIn);
                        }

                        if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null))
                        {
                            analyzersNode.AppendChild(analyzerNode);
                            analyzerSettingsAdded = true;
                        }
                    }
                }

                if (analyzerSettingsAdded)
                {
                    document.DocumentElement.AppendChild(analyzersNode);
                }
            }

            return(document);
        }
Esempio n. 11
0
        /// <summary>
        /// Merges two settings files together.
        /// </summary>
        /// <param name="originalSettings">
        /// The original settings.
        /// </param>
        /// <param name="overridingSettings">
        /// The settings which are overriding the original settings.
        /// </param>
        /// <returns>
        /// Returns the merged settings.
        /// </returns>
        private static Settings MergeSettings(Settings originalSettings, Settings overridingSettings)
        {
            Param.AssertNotNull(originalSettings, "originalSettings");
            Param.AssertNotNull(overridingSettings, "overridingSettings");

            //// TODO Not sure why this has to be true
            //// TODO Also where are we getting a different core from?
            Debug.Assert(originalSettings.Core == overridingSettings.Core, "The settings must come from the same core instance.");

            // Create a new merged settings object.
            Settings mergedSettings = new Settings(originalSettings.Core);

            // Merge the global settings together.
            MergePropertyCollections(originalSettings.GlobalSettings, overridingSettings.GlobalSettings, mergedSettings.GlobalSettings);

            // Merge the parser settings together. Loop through the settings for each parser in the original settings.
            foreach (AddInPropertyCollection originalParserSettings in originalSettings.ParserSettings)
            {
                // Try to find settings for this parser in the overriding settings.
                AddInPropertyCollection overridingParserSettings = overridingSettings.GetAddInSettings(originalParserSettings.AddIn);

                // Create a new merged parser settings object.
                AddInPropertyCollection mergedParserSettings = new AddInPropertyCollection(originalParserSettings.AddIn);
                mergedSettings.SetAddInSettings(mergedParserSettings);

                // Merge the parser settings together.
                MergePropertyCollections(originalParserSettings, overridingParserSettings, mergedParserSettings);
            }

            // Now loop through the settings for each parser in the overriding settings. If there are any parser
            // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings.
            // This means that there were no settings for this parser in the original settings.
            foreach (AddInPropertyCollection overridingParserSettings in overridingSettings.ParserSettings)
            {
                AddInPropertyCollection mergedParserSettings = mergedSettings.GetAddInSettings(overridingParserSettings.AddIn);
                if (mergedParserSettings == null)
                {
                    mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingParserSettings.Clone());
                }
            }

            // Merge the analyzer settings together. Loop through the settings for each analyzer in the original settings.
            foreach (AddInPropertyCollection originalAnalyzerSettings in originalSettings.AnalyzerSettings)
            {
                // Try to find settings for this analyzer in the overriding settings.
                AddInPropertyCollection overridingAnalyzerSettings = overridingSettings.GetAddInSettings(originalAnalyzerSettings.AddIn);

                // Create a new merged analyzer settings object.
                AddInPropertyCollection mergedAnalyzerSettings = new AddInPropertyCollection(originalAnalyzerSettings.AddIn);
                mergedSettings.SetAddInSettings(mergedAnalyzerSettings);

                // Merge the analyer settings together.
                MergePropertyCollections(originalAnalyzerSettings, overridingAnalyzerSettings, mergedAnalyzerSettings);
            }

            // Now loop through the settings for each analyzer in the overriding settings. If there are any analyzer
            // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings.
            // This means that there were no settings for this analyzer in the original settings.
            foreach (AddInPropertyCollection overridingAnalyzerSettings in overridingSettings.AnalyzerSettings)
            {
                AddInPropertyCollection mergedAnalyzerSettings = mergedSettings.GetAddInSettings(overridingAnalyzerSettings.AddIn);
                if (mergedAnalyzerSettings == null)
                {
                    mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingAnalyzerSettings.Clone());
                }
            }

            // Merge the write times together. Set the write time of the merged settings file to the most
            // recent write time of the two file which were merged.
            if (originalSettings.WriteTime.CompareTo(overridingSettings.WriteTime) > 0)
            {
                mergedSettings.WriteTime = originalSettings.WriteTime;
            }
            else
            {
                mergedSettings.WriteTime = overridingSettings.WriteTime;
            }

            return(mergedSettings);
        }
Esempio n. 12
0
        /// <summary>
        /// Loads a property which no longer exists, and translates it into an enabled or
        /// disabled rule.
        /// </summary>
        /// <param name="settings">
        /// The settings.
        /// </param>
        /// <param name="analyzerId">
        /// The ID of the analyzer owning the property.
        /// </param>
        /// <param name="propertyName">
        /// The name of legacy property.
        /// </param>
        /// <param name="nodeText">
        /// The property value.
        /// </param>
        private static void LoadLegacyAnalyzerSetting(Settings settings, string analyzerId, string propertyName, string nodeText)
        {
            Param.AssertNotNull(settings, "settings");
            Param.AssertValidString(analyzerId, "analyzerId");
            Param.AssertValidString(propertyName, "propertyName");
            Param.AssertNotNull(nodeText, "nodeText");

            switch (analyzerId)
            {
                case "StyleCop.CSharp.DocumentationRules":
                    if (propertyName == "RequireValueTags")
                    {
                        SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId);
                        if (analyzer != null)
                        {
                            AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer);

                            if (settingsForAnalyzer == null)
                            {
                                settingsForAnalyzer = new AddInPropertyCollection(analyzer);
                                settings.SetAddInSettings(settingsForAnalyzer);
                            }

                            bool enabled = nodeText != "0";

                            AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValue", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);

                            AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValueText", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);
                        }
                    }

                    break;
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Enables or disables all rules for the given analyzers.
        /// </summary>
        /// <param name="disabledAnalyzersNode">
        /// The node representing the analyzer.
        /// </param>
        /// <param name="settings">
        /// The settings.
        /// </param>
        /// <param name="enabled">
        /// Indicates whether to enable or disable the rules.
        /// </param>
        private static void EnableDisableAnalyzerRules(XmlNode disabledAnalyzersNode, Settings settings, bool enabled)
        {
            Param.AssertNotNull(disabledAnalyzersNode, "disabledAnalyzersNode");
            Param.AssertNotNull(settings, "settings");
            Param.Ignore(enabled);

            string[] ids = disabledAnalyzersNode.InnerText.Split(',');

            foreach (string id in ids)
            {
                string analyzerId43 = MapAnalyzerId(id);

                if (analyzerId43 != null)
                {
                    SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId43);
                    if (analyzer != null)
                    {
                        ICollection<string> rules = MapAnalyzerToRules(id);
                        if (rules != null)
                        {
                            AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer);

                            if (settingsForAnalyzer == null)
                            {
                                settingsForAnalyzer = new AddInPropertyCollection(analyzer);
                                settings.SetAddInSettings(settingsForAnalyzer);
                            }

                            foreach (string rule in rules)
                            {
                                AddBooleanProperty(rule + "#Enabled", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Saves the Settings provided into the XmlDocument.
        /// </summary>
        /// <param name="document">
        /// The root document.
        /// </param>
        /// <param name="environment">
        /// The environment that StyleCop is running under, if any.
        /// </param>
        /// <param name="rootElement">
        /// The element to save our settings to.
        /// </param>
        /// <param name="settingsToSave">
        /// The settings to save.
        /// </param>
        private void SaveSettingsIntoXmlDocument(XmlDocument document, StyleCopEnvironment environment, XmlElement rootElement, Settings settingsToSave)
        {
            // Get the parent settings if there are any.
            SettingsMerger merger         = new SettingsMerger(settingsToSave, environment);
            Settings       parentSettings = merger.ParentMergedSettings;

            // Add the global settings if there are any.
            if (settingsToSave.GlobalSettings != null && settingsToSave.GlobalSettings.Count > 0)
            {
                // Get the global settings from the parent.
                PropertyCollection parentGlobalSettings = null;
                if (parentSettings != null)
                {
                    parentGlobalSettings = parentSettings.GlobalSettings;
                }

                SavePropertyCollection(rootElement, "GlobalSettings", settingsToSave.GlobalSettings, parentGlobalSettings, true, null);
            }

            // Add the parser settings if there are any.
            if (settingsToSave.ParserSettings.Count > 0)
            {
                bool       parserSettingsAdded = false;
                XmlElement parsersNode         = document.CreateElement("Parsers");

                foreach (AddInPropertyCollection parserSettings in settingsToSave.ParserSettings)
                {
                    // Add the settings for this parser if there are any.
                    if (parserSettings.Count > 0)
                    {
                        // Create a node for this parser.
                        XmlElement   parserNode        = document.CreateElement("Parser");
                        XmlAttribute parserIdAttribute = document.CreateAttribute("ParserId");
                        parserIdAttribute.Value = parserSettings.AddIn.Id;
                        parserNode.Attributes.Append(parserIdAttribute);

                        // Get the parser settings from the parent.
                        PropertyCollection parentParserSettings = null;
                        if (parentSettings != null)
                        {
                            parentParserSettings = parentSettings.GetAddInSettings(parserSettings.AddIn);
                        }

                        if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null))
                        {
                            parsersNode.AppendChild(parserNode);
                            parserSettingsAdded = true;
                        }
                    }
                }

                if (parserSettingsAdded)
                {
                    rootElement.AppendChild(parsersNode);
                }
            }

            // Add the analyzer settings if there are any.
            if (settingsToSave.AnalyzerSettings.Count > 0)
            {
                bool       analyzerSettingsAdded = false;
                XmlElement analyzersNode         = document.CreateElement("Analyzers");

                foreach (AddInPropertyCollection analyzerSettings in settingsToSave.AnalyzerSettings)
                {
                    // Add the settings for this analyzer if there are any.
                    if (analyzerSettings.Count > 0)
                    {
                        // Create a node for this analzyer.
                        XmlElement   analyzerNode        = document.CreateElement("Analyzer");
                        XmlAttribute analyzerIdAttribute = document.CreateAttribute("AnalyzerId");
                        analyzerIdAttribute.Value = analyzerSettings.AddIn.Id;
                        analyzerNode.Attributes.Append(analyzerIdAttribute);

                        // Get the analyzer settings from the parent.
                        PropertyCollection parentAnalyzerSettings = null;
                        if (parentSettings != null)
                        {
                            parentAnalyzerSettings = parentSettings.GetAddInSettings(analyzerSettings.AddIn);
                        }

                        if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null))
                        {
                            analyzersNode.AppendChild(analyzerNode);
                            analyzerSettingsAdded = true;
                        }
                    }
                }

                if (analyzerSettingsAdded)
                {
                    rootElement.AppendChild(analyzersNode);
                }
            }

            // Add the sourcefilelists settings if there are any.
            if (settingsToSave.SourceFileLists.Count > 0)
            {
                foreach (SourceFileListSettings sourceFileListSettings in settingsToSave.SourceFileLists)
                {
                    XmlElement sourceFileListNode = document.CreateElement("SourceFileList");

                    foreach (string sourceFileListSetting in sourceFileListSettings.SourceFiles)
                    {
                        XmlElement sourceFileNode = document.CreateElement("SourceFile");
                        sourceFileNode.InnerText = sourceFileListSetting;
                        sourceFileListNode.AppendChild(sourceFileNode);
                    }

                    XmlElement settingsNode = document.CreateElement("Settings");

                    this.SaveSettingsIntoXmlDocument(document, environment, settingsNode, sourceFileListSettings.Settings);

                    sourceFileListNode.AppendChild(settingsNode);
                    rootElement.AppendChild(sourceFileListNode);
                }
            }
        }