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()); } }
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()); } }
private static void LoadUserSpecificSettings( List <Settings> validSettingFiles, string root, string configFileName, IMachineWideSettings machineWideSettings, bool useTestingGlobalPath ) { if (root == null) { // Path.Combine is performed with root so it should not be null // However, it is legal for it be empty in this method root = String.Empty; } // for the default location, allow case where file does not exist, in which case it'll end // up being created if needed Settings appDataSettings = null; if (configFileName == null) { var defaultSettingsFilePath = String.Empty; if (useTestingGlobalPath) { defaultSettingsFilePath = Path.Combine(root, "TestingGlobalPath", DefaultSettingsFileName); } else { var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); // If there is no user settings directory, return no appdata settings if (userSettingsDir == null) { return; } defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); } if (!File.Exists(defaultSettingsFilePath) && machineWideSettings != null) { // Since defaultSettingsFilePath is a full path, so it doesn't matter what value is // used as root for the PhysicalFileSystem. appDataSettings = ReadSettings( root, defaultSettingsFilePath); // Disable machinewide sources to improve perf var disabledSources = new List <SettingValue>(); foreach (var setting in machineWideSettings.Settings) { var values = setting.GetSettingValues(ConfigurationConstants.PackageSources, isPath: true); foreach (var value in values) { var packageSource = new PackageSource(value.Value); // if the machine wide package source is http source, disable it by default if (packageSource.IsHttp) { disabledSources.Add(new SettingValue(value.Key, "true", origin: setting, isMachineWide: true, priority: 0)); } } } appDataSettings.UpdateSections(ConfigurationConstants.DisabledPackageSources, disabledSources); } else { appDataSettings = ReadSettings(root, defaultSettingsFilePath); bool IsEmptyConfig = !appDataSettings.GetSettingValues(ConfigurationConstants.PackageSources).Any(); if (IsEmptyConfig) { var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile); if (!File.Exists(trackFilePath)) { File.Create(trackFilePath).Dispose(); var defaultPackageSource = new SettingValue(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, isMachineWide: false); defaultPackageSource.AdditionalData.Add(ConfigurationConstants.ProtocolVersionAttribute, "3"); appDataSettings.UpdateSections(ConfigurationConstants.PackageSources, new List <SettingValue> { defaultPackageSource }); } } } } else { if (!FileSystemUtility.DoesFileExistIn(root, configFileName)) { var message = String.Format(CultureInfo.CurrentCulture, Resources.FileDoesNotExist, Path.Combine(root, configFileName)); throw new InvalidOperationException(message); } appDataSettings = ReadSettings(root, configFileName); } if (appDataSettings != null) { validSettingFiles.Add(appDataSettings); } }