/// <summary> /// Load the user specific settings /// </summary> /// <param name="root"></param> /// <param name="configFileName"></param> /// <param name="useTestingGlobalPath"></param> /// <param name="settingsLoadingContext"></param> /// <returns></returns> internal static IEnumerable <SettingsFile> LoadUserSpecificSettings( string root, string configFileName, bool useTestingGlobalPath, SettingsLoadingContext settingsLoadingContext = null) { // Path.Combine is performed with root so it should not be null // However, it is legal for it be empty in this method var rootDirectory = root ?? string.Empty; if (configFileName == null) { string userSettingsDir = GetUserSettingsDirectory(rootDirectory, useTestingGlobalPath); if (userSettingsDir == null) { yield break; } var defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); // ReadSettings will try to create the default config file if it doesn't exist SettingsFile userSpecificSettings = ReadSettings(rootDirectory, defaultSettingsFilePath, settingsLoadingContext: settingsLoadingContext); yield return(userSpecificSettings); // For backwards compatibility, we first return default user specific the non-default configs and then the additional files from the nested `config` directory var additionalConfigurationPath = GetAdditionalUserWideConfigurationDirectory(userSettingsDir); foreach (var file in FileSystemUtility .GetFilesRelativeToRoot(root: additionalConfigurationPath, filters: SupportedMachineWideConfigExtension, searchOption: SearchOption.TopDirectoryOnly) .OrderBy(e => e, PathUtility.GetStringComparerBasedOnOS())) { if (!PathUtility.GetStringComparerBasedOnOS().Equals(DefaultSettingsFileName, file)) { var settings = ReadSettings(additionalConfigurationPath, file, isMachineWideSettings: false, isAdditionalUserWideConfig: true); if (settings != null) { yield return(settings); } } } } else { if (!FileSystemUtility.DoesFileExistIn(rootDirectory, configFileName)) { var message = string.Format(CultureInfo.CurrentCulture, Resources.FileDoesNotExist, Path.Combine(rootDirectory, configFileName)); throw new InvalidOperationException(message); } yield return(ReadSettings(rootDirectory, configFileName, settingsLoadingContext: settingsLoadingContext)); } }
/// <summary> /// Loads the machine wide settings. /// </summary> /// <remarks> /// For example, if <paramref name="paths" /> is {"IDE", "Version", "SKU" }, then /// the files loaded are (in the order that they are loaded): /// %programdata%\NuGet\Config\IDE\Version\SKU\*.config /// %programdata%\NuGet\Config\IDE\Version\*.config /// %programdata%\NuGet\Config\IDE\*.config /// %programdata%\NuGet\Config\*.config /// </remarks> /// <param name="root">The file system in which the settings files are read.</param> /// <param name="paths">The additional paths under which to look for settings files.</param> /// <returns>The list of settings read.</returns> public static ISettings LoadMachineWideSettings( string root, params string[] paths) { if (string.IsNullOrEmpty(root)) { throw new ArgumentException("root cannot be null or empty"); } var settingFiles = new List <SettingsFile>(); var combinedPath = Path.Combine(paths); while (true) { // load setting files in directory foreach (var file in FileSystemUtility.GetFilesRelativeToRoot(root, combinedPath, SupportedMachineWideConfigExtension, SearchOption.TopDirectoryOnly)) { var settings = ReadSettings(root, file, isMachineWideSettings: true); if (settings != null) { settingFiles.Add(settings); } } if (combinedPath.Length == 0) { break; } var index = combinedPath.LastIndexOf(Path.DirectorySeparatorChar); if (index < 0) { index = 0; } combinedPath = combinedPath.Substring(0, index); } if (settingFiles.Any()) { SettingsFile.ConnectSettingsFilesLinkedList(settingFiles); return(new Settings(settingFiles.Last())); } return(NullSettings.Instance); }
/// <summary> /// Loads the machine wide settings. /// </summary> /// <remarks> /// For example, if <paramref name="paths"/> is {"IDE", "Version", "SKU" }, then /// the files loaded are (in the order that they are loaded): /// %programdata%\NuGet\Config\IDE\Version\SKU\*.config /// %programdata%\NuGet\Config\IDE\Version\*.config /// %programdata%\NuGet\Config\IDE\*.config /// %programdata%\NuGet\Config\*.config /// </remarks> /// <param name="root">The file system in which the settings files are read.</param> /// <param name="paths">The additional paths under which to look for settings files.</param> /// <returns>The list of settings read.</returns> public static IEnumerable <Settings> LoadMachineWideSettings( string root, params string[] paths) { if (String.IsNullOrEmpty(root)) { throw new ArgumentException("root cannot be null or empty"); } List <Settings> settingFiles = new List <Settings>(); string basePath = @"NuGet\Config"; string combinedPath = Path.Combine(paths); while (true) { string directory = Path.Combine(basePath, combinedPath); // load setting files in directory foreach (var file in FileSystemUtility.GetFilesRelativeToRoot(root, directory, "*.config", SearchOption.TopDirectoryOnly)) { var settings = ReadSettings(root, file, true); if (settings != null) { settingFiles.Add(settings); } } if (combinedPath.Length == 0) { break; } int index = combinedPath.LastIndexOf(Path.DirectorySeparatorChar); if (index < 0) { index = 0; } combinedPath = combinedPath.Substring(0, index); } return(settingFiles); }
/// <summary> /// Load the user specific settings /// </summary> /// <param name="root"></param> /// <param name="configFileName"></param> /// <param name="useTestingGlobalPath"></param> /// <param name="settingsLoadingContext"></param> /// <returns></returns> internal static IEnumerable <SettingsFile> LoadUserSpecificSettings( string root, string configFileName, bool useTestingGlobalPath, SettingsLoadingContext settingsLoadingContext = null) { // Path.Combine is performed with root so it should not be null // However, it is legal for it be empty in this method var rootDirectory = root ?? string.Empty; if (configFileName == null) { string userSettingsDir = GetUserSettingsDirectory(rootDirectory, useTestingGlobalPath); if (userSettingsDir == null) { yield break; } // If the default user config NuGet.Config does not exist, we need to create it. var defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); SettingsFile userSpecificSettings = ReadSettings(rootDirectory, defaultSettingsFilePath, settingsLoadingContext: settingsLoadingContext); if (File.Exists(defaultSettingsFilePath) && userSpecificSettings.IsEmpty()) { var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile); if (!File.Exists(trackFilePath)) { File.Create(trackFilePath).Dispose(); var defaultSource = new SourceItem(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, protocolVersion: "3"); userSpecificSettings.AddOrUpdate(ConfigurationConstants.PackageSources, defaultSource); userSpecificSettings.SaveToDisk(); } } yield return(userSpecificSettings); // For backwards compatibility, we first return default user specific the non-default configs and then the additional files from the nested `config` directory var additionalConfigurationPath = GetAdditionalUserWideConfigurationDirectory(userSettingsDir); foreach (var file in FileSystemUtility .GetFilesRelativeToRoot(root: additionalConfigurationPath, filters: SupportedMachineWideConfigExtension, searchOption: SearchOption.TopDirectoryOnly) .OrderBy(e => e, PathUtility.GetStringComparerBasedOnOS())) { if (!PathUtility.GetStringComparerBasedOnOS().Equals(DefaultSettingsFileName, file)) { var settings = ReadSettings(additionalConfigurationPath, file, isMachineWideSettings: false, isAdditionalUserWideConfig: true); if (settings != null) { yield return(settings); } } } } else { if (!FileSystemUtility.DoesFileExistIn(rootDirectory, configFileName)) { var message = string.Format(CultureInfo.CurrentCulture, Resources.FileDoesNotExist, Path.Combine(rootDirectory, configFileName)); throw new InvalidOperationException(message); } yield return(ReadSettings(rootDirectory, configFileName, settingsLoadingContext: settingsLoadingContext)); } }