/// <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; } }
/// <summary> /// Clones the contents of the collection. /// </summary> /// <returns>Returns the cloned collection.</returns> public override PropertyCollection Clone() { AddInPropertyCollection clone = new AddInPropertyCollection(this.addIn); foreach (PropertyValue item in this.Properties) { clone.Add(item.Clone()); } return clone; }
/// <summary> /// Clones the contents of the collection. /// </summary> /// <returns>Returns the cloned collection.</returns> public override PropertyCollection Clone() { AddInPropertyCollection clone = new AddInPropertyCollection(this.addIn); foreach (PropertyValue item in this.Properties) { clone.Add(item.Clone()); } return(clone); }
/// <summary> /// Sets a setting for the given add-in. /// </summary> /// <param name="addIn"> /// The add-in. /// </param> /// <param name="property"> /// The setting property to set. /// </param> internal void SetAddInSettingInternal(StyleCopAddIn addIn, PropertyValue property) { Param.AssertNotNull(addIn, "addIn"); Param.AssertNotNull(property, "property"); AddInPropertyCollection properties = this.GetAddInSettings(addIn); if (properties == null) { properties = new AddInPropertyCollection(addIn); this.SetAddInSettings(properties); } properties.Add(property); }
/// <summary> /// Sets the settings for the given add-in. /// </summary> /// <param name="properties"> /// The properties to set. /// </param> /// <remarks> /// This overrides any existing settings for the add-in. /// </remarks> internal void SetAddInSettings(AddInPropertyCollection properties) { Param.AssertNotNull(properties, "properties"); Dictionary <string, AddInPropertyCollection> collection = this.GetPropertyCollectionDictionary(properties.AddIn); if (collection.ContainsKey(properties.AddIn.Id)) { collection[properties.AddIn.Id] = properties; } else { collection.Add(properties.AddIn.Id, properties); } }
/// <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="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); } } } } }
/// <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); } } } } }
/// <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 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); } } } } } }
/// <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; }
/// <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); }
/// <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); } } } } }
/// <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); } } } } } }
/// <summary> /// Sets the settings for the given add-in. /// </summary> /// <param name="properties"> /// The properties to set. /// </param> /// <remarks> /// This overrides any existing settings for the add-in. /// </remarks> internal void SetAddInSettings(AddInPropertyCollection properties) { Param.AssertNotNull(properties, "properties"); Dictionary<string, AddInPropertyCollection> collection = this.GetPropertyCollectionDictionary(properties.AddIn); if (collection.ContainsKey(properties.AddIn.Id)) { collection[properties.AddIn.Id] = properties; } else { collection.Add(properties.AddIn.Id, properties); } }
/// <summary> /// Initializes the collection of enabled analyzers and rules based on these settings. /// </summary> private void InitializeEnabledRules() { if (this.enabledRules == null) { LockCookie cookie = this.enabledRulesLock.UpgradeToWriterLock(Timeout.Infinite); try { if (this.enabledRules == null) { this.enabledRules = new Dictionary <StyleCopAddIn, Dictionary <string, Rule> >(); // Determine whether addins are enabled or disabled by default. bool enabledByDefault = this.RulesEnabledByDefault; // Iterate through all loaded parsers. foreach (SourceParser parser in this.core.Parsers) { // Iterate through each analyzer attached to this parser. foreach (SourceAnalyzer analyzer in parser.Analyzers) { // Create a dictionary to hold each enabled rule for the analyzer. Dictionary <string, Rule> enabledRulesForAnalyzer = new Dictionary <string, Rule>(); // Get the settings for this analyzer, if there are any. AddInPropertyCollection analyzerSettings = this.GetAddInSettings(analyzer); // Iterate through each of the analyzer's rules. foreach (Rule rule in analyzer.AddInRules) { // Determine whether the rule is currently enabled. bool ruleEnabled = enabledByDefault && rule.EnabledByDefault; // Determine whether there is a setting which enables or disables the rules. // If the rule is set to CanDisable = false, then ignore the setting unless // we are in disabled by default mode. if (analyzerSettings != null && (!ruleEnabled || rule.CanDisable)) { BooleanProperty property = analyzerSettings[rule.Name + "#Enabled"] as BooleanProperty; if (property != null) { ruleEnabled = property.Value; } } // If the rule is enabled, add it to the enabled rules dictionary. if (ruleEnabled) { enabledRulesForAnalyzer.Add(rule.Name, rule); } } // If the analyzer has at least one enabled rule, add the analyzer to the list // of enabled analyzers. if (enabledRulesForAnalyzer.Count > 0) { // The rules list should not already be set for this project on this analyzer. // If so, something is wrong. Debug.Assert(!this.enabledRules.ContainsKey(analyzer), "The rule list for this analyzer should not be set yet."); this.enabledRules.Add(analyzer, enabledRulesForAnalyzer); } } } } } finally { this.enabledRulesLock.DowngradeFromWriterLock(ref cookie); } } }