Beispiel #1
0
        private void SetValueInternal(XElement sectionElement, string key, string value, IDictionary <string, string> attributes)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, ConfigurationContants.KeyAttribute);
            }
            if (value == null)
            {
                throw new ArgumentNullException(ConfigurationContants.ValueAttribute);
            }

            var element = FindElementByKey(sectionElement, key, null);

            if (element != null)
            {
                SetElementValues(element, key, value, attributes);
                Save();
            }
            else
            {
                element = new XElement("add");
                SetElementValues(element, key, value, attributes);
                XElementUtility.AddIndented(sectionElement, element);
            }
        }
Beispiel #2
0
        public bool DeleteSection(string section)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings)
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(Resources.Error_NoWritableConfig);
                }

                return(_next.DeleteSection(section));
            }

            if (String.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement == null)
            {
                return(false);
            }

            XElementUtility.RemoveIndented(sectionElement);
            Save();
            return(true);
        }
Beispiel #3
0
        internal override void Update(SettingItem other)
        {
            var owners = other as OwnersItem;

            if (!owners.Content.Any())
            {
                throw new InvalidOperationException(Resources.OwnersItemMustHaveAtLeastOneOwner);
            }

            base.Update(other);

            if (!Equals(owners))
            {
                XElementUtility.RemoveIndented(_content.Node);
                Content = owners.Content;

                _content = new SettingText(string.Join(OwnersListSeparator.ToString(), Content));

                if (Origin != null)
                {
                    _content.SetOrigin(Origin);

                    if (Node != null)
                    {
                        _content.SetNode(_content.AsXNode());
                        (Node as XElement).Add(_content.Node);
                        Origin.IsDirty = true;
                    }
                }
            }
        }
        internal virtual bool Add(T setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (Origin.IsMachineWide)
            {
                throw new InvalidOperationException(Resources.CannotUpdateMachineWide);
            }

            if (Origin.IsReadOnly)
            {
                throw new InvalidOperationException(Resources.CannotUpdateReadOnlyConfig);
            }

            if (!Children.Contains(setting) && !setting.IsEmpty())
            {
                Children.Add(setting);

                setting.SetOrigin(Origin);
                setting.SetNode(setting.AsXNode());

                XElementUtility.AddIndented(Node as XElement, setting.Node);
                Origin.IsDirty = true;

                setting.Parent = this;

                return(true);
            }

            return(false);
        }
Beispiel #5
0
        private void SetValueInternal(XElement sectionElement, string key, string value)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, "key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var element = FindElementByKey(sectionElement, key, null);

            if (element != null)
            {
                element.SetAttributeValue("value", value);
                Save();
            }
            else
            {
                XElementUtility.AddIndented(sectionElement, new XElement("add",
                                                                         new XAttribute("key", key),
                                                                         new XAttribute("value", value)));
            }
        }
Beispiel #6
0
        private static XElement GetOrCreateSection(XElement parentElement, string sectionName)
        {
            sectionName = XmlConvert.EncodeLocalName(sectionName);
            var section = parentElement.Element(sectionName);

            if (section == null)
            {
                section = new XElement(sectionName);
                XElementUtility.AddIndented(parentElement, section);
            }
            return(section);
        }
Beispiel #7
0
        public void UpdateSections(string section, IReadOnlyList <SettingValue> values)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings)
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(Resources.Error_NoWritableConfig);
                }

                _next.UpdateSections(section, values);
                return;
            }

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

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

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement != null)
            {
                XElementUtility.RemoveIndented(sectionElement);
            }

            var valuesToWrite = _next == null ? values : values.Where(v => v.Priority < _next._priority);

            if (valuesToWrite.Any())
            {
                sectionElement = GetOrCreateSection(ConfigXDocument.Root, section);
            }

            foreach (var value in valuesToWrite)
            {
                var element = new XElement("add");
                SetElementValues(element, value.Key, value.Value, value.AdditionalData);
                XElementUtility.AddIndented(sectionElement, element);
            }

            Save();

            if (_next != null)
            {
                _next.UpdateSections(section, values.Where(v => v.Priority >= _next._priority).ToList());
            }
        }
        public void UpdateSections(string section, IReadOnlyList <SettingValue> values)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings ||
                ((section == ConfigurationConstants.PackageSources || section == ConfigurationConstants.DisabledPackageSources) && Cleared))
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(Resources.Error_NoWritableConfig);
                }

                _next.UpdateSections(section, values);
                return;
            }

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

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

            var valuesToWrite = _next == null ? values : values.Where(v => v.Priority < _next._priority);

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement == null && valuesToWrite.Any())
            {
                sectionElement = GetOrCreateSection(ConfigXDocument.Root, section);
            }

            // When updating attempt to preserve the clear tag (and any sources that appear prior to it)
            // to avoid creating extra diffs in the source.
            RemoveElementAfterClearTag(sectionElement);

            foreach (var value in valuesToWrite)
            {
                var element = new XElement("add");
                SetElementValues(element, value.Key, value.OriginalValue, value.AdditionalData);
                XElementUtility.AddIndented(sectionElement, element);
            }

            Save();

            if (_next != null)
            {
                _next.UpdateSections(section, values.Where(v => v.Priority >= _next._priority).ToList());
            }
        }
Beispiel #9
0
        /// <summary>
        /// Convenience method to remove an element from it's origin and convert to abstract
        /// </summary>
        /// <remarks>Each setting can override this method to remove any descendants from their origin</remarks>
        internal virtual void RemoveFromSettings()
        {
            if (!IsCopy() && !IsAbstract())
            {
                XElementUtility.RemoveIndented(Node);
                Origin.IsDirty = true;

                Node = null;
            }

            Origin = null;
            Parent = null;
        }
Beispiel #10
0
        internal static void AddIndented(XContainer container, XContainer content)
        {
            string oneIndentLevel = ComputeOneLevelOfIndentation(container);

            XText  leadingText  = container.PreviousNode as XText;
            string parentIndent = leadingText != null ? leadingText.Value : Environment.NewLine;

            XElementUtility.IndentChildrenElements(content, parentIndent + oneIndentLevel, oneIndentLevel);

            XElementUtility.AddLeadingIndentation(container, parentIndent, oneIndentLevel);
            container.Add(content);
            AddTrailingIndentation(container, parentIndent);
        }
Beispiel #11
0
        internal override void Update(SettingItem other)
        {
            var trustedSigner = other as TrustedSignerItem;

            if (!trustedSigner.Certificates.Any())
            {
                throw new InvalidOperationException(Resources.TrustedSignerMustHaveCertificates);
            }

            base.Update(other);

            var otherCerts     = trustedSigner.Certificates.ToDictionary(c => c, c => c);
            var immutableCerts = new List <CertificateItem>(Certificates);

            foreach (var cert in immutableCerts)
            {
                if (otherCerts.TryGetValue(cert, out var otherChild))
                {
                    otherCerts.Remove(cert);
                }

                if (otherChild == null)
                {
                    Certificates.Remove(cert);
                    cert.RemoveFromSettings();
                }
                else if (cert is SettingItem item)
                {
                    item.Update(otherChild as SettingItem);
                }
            }

            foreach (var newCert in otherCerts)
            {
                var certToAdd = newCert.Value;
                Certificates.Add(certToAdd);

                if (Origin != null)
                {
                    certToAdd.SetOrigin(Origin);

                    if (Node != null)
                    {
                        certToAdd.SetNode(certToAdd.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, certToAdd.Node);
                        Origin.IsDirty = true;
                    }
                }
            }
        }
Beispiel #12
0
        internal override void Update(SettingItem other)
        {
            var packageSourceMappingSourceItem = other as PackageSourceMappingSourceItem;

            if (!packageSourceMappingSourceItem.Patterns.Any())
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Error_ItemNeedsAtLeastOnePackagePattern, packageSourceMappingSourceItem.Key));
            }

            base.Update(other);

            Dictionary <PackagePatternItem, PackagePatternItem> otherPatterns = packageSourceMappingSourceItem.Patterns.ToDictionary(c => c, c => c);
            var immutablePatterns = new List <PackagePatternItem>(Patterns);

            foreach (PackagePatternItem packagePatternItem in immutablePatterns)
            {
                if (otherPatterns.TryGetValue(packagePatternItem, out PackagePatternItem otherChild))
                {
                    otherPatterns.Remove(packagePatternItem);
                }

                if (otherChild == null)
                {
                    Patterns.Remove(packagePatternItem);
                    packagePatternItem.RemoveFromSettings();
                }
                else if (packagePatternItem is SettingItem item)
                {
                    item.Update(otherChild);
                }
            }

            foreach (var newPackagePatternItem in otherPatterns)
            {
                var itemToAdd = newPackagePatternItem.Value;
                Patterns.Add(itemToAdd);

                if (Origin != null)
                {
                    itemToAdd.SetOrigin(Origin);

                    if (Node != null)
                    {
                        itemToAdd.SetNode(itemToAdd.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, itemToAdd.Node);
                        Origin.IsDirty = true;
                    }
                }
            }
        }
Beispiel #13
0
        private string ElementToValue(XElement element, bool isPath)
        {
            if (element == null)
            {
                return(null);
            }

            // Return the optional value which if not there will be null;
            string value = XElementUtility.GetOptionalAttributeValue(element, ConfigurationContants.ValueAttribute);

            if (!isPath || String.IsNullOrEmpty(value))
            {
                return(value);
            }
            return(Path.Combine(Root, ResolvePath(Path.GetDirectoryName(ConfigFilePath), value)));
        }
Beispiel #14
0
        private static XElement FindElementByKey(XElement sectionElement, string key, XElement curr)
        {
            XElement result = curr;

            foreach (var element in sectionElement.Elements())
            {
                string elementName = element.Name.LocalName;
                if (elementName.Equals("clear", StringComparison.OrdinalIgnoreCase))
                {
                    result = null;
                }
                else if (elementName.Equals("add", StringComparison.OrdinalIgnoreCase) &&
                         XElementUtility.GetOptionalAttributeValue(element, ConfigurationContants.KeyAttribute).Equals(key, StringComparison.OrdinalIgnoreCase))
                {
                    result = element;
                }
            }
            return(result);
        }
Beispiel #15
0
        public bool DeleteValue(string section, string key)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings)
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(NuGet_Configuration_Resources.Error_NoWritableConfig);
                }

                return(_next.DeleteValue(section, key));
            }

            if (String.IsNullOrEmpty(section))
            {
                throw new ArgumentException(NuGet_Configuration_Resources.Argument_Cannot_Be_Null_Or_Empty, "section");
            }
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(NuGet_Configuration_Resources.Argument_Cannot_Be_Null_Or_Empty, "key");
            }

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement == null)
            {
                return(false);
            }

            var elementToDelete = FindElementByKey(sectionElement, key, null);

            if (elementToDelete == null)
            {
                return(false);
            }
            XElementUtility.RemoveIndented(elementToDelete);
            Save();
            return(true);
        }
Beispiel #16
0
        internal bool Add(SettingBase setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

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

            if (!_mutableChildren.ContainsKey(setting) && !setting.IsEmpty())
            {
                _mutableChildren.Add(setting, setting);

                if (Origin != null)
                {
                    setting.SetOrigin(Origin);

                    if (Node != null)
                    {
                        setting.SetNode(setting.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, setting.Node);
                        Origin.IsDirty = true;
                    }
                }

                setting.Parent = this;

                return(true);
            }

            return(false);
        }