コード例 #1
0
        internal override void Remove(SettingItem setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (TryGetChild(setting, out var currentSetting))
            {
                Debug.Assert(!currentSetting.IsAbstract());

                if (currentSetting.Origin != null && currentSetting.Origin.IsMachineWide)
                {
                    throw new InvalidOperationException(Resources.CannotUpdateMachineWide);
                }

                if (currentSetting.Origin != null && currentSetting.Origin.IsReadOnly)
                {
                    throw new InvalidOperationException(Resources.CannotUpdateReadOnlyConfig);
                }

                if (Children.Remove(currentSetting))
                {
                    // Remove it from the appropriate config
                    if (currentSetting.Parent != null && currentSetting.Parent != this)
                    {
                        currentSetting.Parent.Remove(currentSetting);
                    }
                }

                if (currentSetting.MergedWith != null)
                {
                    // Add that back to the set since, we should leave the machine wide setting intact.
                    if (!TryRemoveAllMergedWith(currentSetting, out var undeletedItem))
                    {
                        Children.Add(undeletedItem);
                    }
                }
            }
        }
コード例 #2
0
        public void AddOrUpdate(string sectionName, SettingItem item)
        {
            if (string.IsNullOrEmpty(sectionName))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(sectionName));
            }

            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (Sections.TryGetValue(sectionName, out var section))
            {
                // section exists, update or add the element on it
                if (section.Update(item) || section.Add(item))
                {
                    return;
                }
            }

            // The section is new, add it with the item
            Add(new ParsedSettingSection(sectionName, item));
        }
コード例 #3
0
ファイル: NullSettings.cs プロジェクト: rytmis/NuGet.Client
 public void Remove(string sectionName, SettingItem item) => throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidNullSettingsOperation, nameof(Remove)));
コード例 #4
0
 /// <summary>
 /// Removes the given <paramref name="item"/> from the settings.
 /// If the <paramref name="item"/> is the last item in the section, the section will also be removed.
 /// </summary>
 /// <param name="sectionName">Section where the <paramref name="item"/> is stored. If this section does not exist, the method will throw</param>
 /// <param name="item">item to be removed from the settings</param>
 /// <remarks> If the SettingsFile is a machine wide config this method will throw</remarks>
 internal void Remove(string sectionName, SettingItem item)
 {
     _rootElement.Remove(sectionName, item);
 }
コード例 #5
0
 /// <summary>
 /// Adds or updates the given <paramref name="item"/> to the settings.
 /// </summary>
 /// <param name="sectionName">section where the <paramref name="item"/> has to be added. If this section does not exist, one will be created.</param>
 /// <param name="item">item to be added to the settings.</param>
 /// <returns>true if the item was successfully updated or added in the settings</returns>
 internal void AddOrUpdate(string sectionName, SettingItem item)
 {
     _rootElement.AddOrUpdate(sectionName, item);
 }
コード例 #6
0
 public void Remove(string sectionName, SettingItem item)
 {
     throw new NotSupportedException();
 }
コード例 #7
0
 public void AddOrUpdate(string sectionName, SettingItem item)
 {
     throw new NotSupportedException();
 }
コード例 #8
0
        /// <remarks>
        /// This method is internal because it updates directly the xElement behind this abstraction.
        /// It should only be called whenever the underlying config file is intended to be changed.
        /// To persist changes to disk one must save the corresponding setting files
        /// </remarks>
        internal virtual void Update(SettingItem setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (Origin != null && Origin.IsMachineWide)
            {
                throw new InvalidOperationException(Resources.CannotUpdateMachineWide);
            }

            if (Origin != null && Origin.IsReadOnly)
            {
                throw new InvalidOperationException(Resources.CannotUpdateReadOnlyConfig);
            }

            if (setting.GetType() != GetType())
            {
                throw new InvalidOperationException(Resources.CannotUpdateDifferentItems);
            }

            var xElement            = Node as XElement;
            var otherAttributes     = setting.Attributes.ToDictionary(a => a.Key, a => a.Value);
            var attributesImmutable = new Dictionary <string, string>(MutableAttributes);

            foreach (var attribute in attributesImmutable)
            {
                if (otherAttributes.TryGetValue(attribute.Key, out var otherValue))
                {
                    otherAttributes.Remove(attribute.Key);
                }

                string value = null;
                if (otherValue != null)
                {
                    value = otherValue;
                }

                if (!string.Equals(value, attribute.Value, StringComparison.Ordinal))
                {
                    if (xElement != null && Origin != null)
                    {
                        // Update or remove any existing item that has changed
                        xElement.SetAttributeValue(attribute.Key, value);
                        Origin.IsDirty = true;
                    }

                    AddOrUpdateAttribute(attribute.Key, value);
                }
            }

            foreach (var attribute in otherAttributes)
            {
                if (xElement != null && Origin != null)
                {
                    xElement.SetAttributeValue(attribute.Key, attribute.Value);
                    Origin.IsDirty = true;
                }

                AddOrUpdateAttribute(attribute.Key, attribute.Value);
            }
        }