Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhili1208 committed Feb 9, 2016
1 parent 1912ec2 commit 2cef46a
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 628 deletions.
8 changes: 1 addition & 7 deletions src/NuGet.Clients/NuGet.CommandLine/PackageSourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ internal static class PackageSourceBuilder
{
internal static Configuration.PackageSourceProvider CreateSourceProvider(Configuration.ISettings settings)
{
var defaultPackageSource = new Configuration.PackageSource(
NuGetConstants.V3FeedUrl, NuGetConstants.FeedName);

var packageSourceProvider = new Configuration.PackageSourceProvider(
settings,
new[] { defaultPackageSource });
return packageSourceProvider;
return new Configuration.PackageSourceProvider(settings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ public static class NuGetConstants


public static readonly string FeedName = "nuget.org";

public static readonly string DefaultConfigContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget.org"" value=""https://api.nuget.org/v3/index.json"" protocolVersion=""3"" />
</packageSources>
</configuration>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,25 @@ public class PackageSourceProvider : IPackageSourceProvider
private const int MaxSupportedProtocolVersion = 3;

private ISettings Settings { get; set; }
private readonly IEnumerable<PackageSource> _providerDefaultPrimarySources;
private readonly IEnumerable<PackageSource> _providerDefaultSecondarySources;
private readonly IDictionary<PackageSource, PackageSource> _migratePackageSources;
private readonly IEnumerable<PackageSource> _configurationDefaultSources;

public PackageSourceProvider(ISettings settings)
: this(settings, providerDefaultPrimarySources: null, providerDefaultSecondarySources: null)
{
}

/// <summary>
/// Creates a new PackageSourceProvider instance.
/// </summary>
/// <param name="settings">Specifies the settings file to use to read package sources.</param>
/// <param name="providerDefaultPrimarySources">The primary default sources you would like to use</param>
public PackageSourceProvider(ISettings settings, IEnumerable<PackageSource> providerDefaultPrimarySources)
: this(settings, providerDefaultPrimarySources, providerDefaultSecondarySources: null, migratePackageSources: null)
{
}

/// <summary>
/// Creates a new PackageSourceProvider instance.
/// </summary>
/// <param name="settings">Specifies the settings file to use to read package sources.</param>
/// <param name="providerDefaultPrimarySources">The primary default sources you would like to use</param>
/// <param name="providerDefaultSecondarySources">The secondary default sources you would like to use</param>
public PackageSourceProvider(ISettings settings, IEnumerable<PackageSource> providerDefaultPrimarySources, IEnumerable<PackageSource> providerDefaultSecondarySources)
: this(settings, providerDefaultPrimarySources, providerDefaultSecondarySources, migratePackageSources: null)
: this(settings, migratePackageSources: null)
{
}

public PackageSourceProvider(
ISettings settings,
IEnumerable<PackageSource> providerDefaultPrimarySources,
IEnumerable<PackageSource> providerDefaultSecondarySources,
IDictionary<PackageSource, PackageSource> migratePackageSources)
: this(settings,
providerDefaultPrimarySources,
providerDefaultSecondarySources,
migratePackageSources,
ConfigurationDefaults.Instance.DefaultPackageSources)
{
}

public PackageSourceProvider(
ISettings settings,
IEnumerable<PackageSource> providerDefaultPrimarySources,
IEnumerable<PackageSource> providerDefaultSecondarySources,
IDictionary<PackageSource, PackageSource> migratePackageSources,
IEnumerable<PackageSource> configurationDefaultSources
)
Expand All @@ -73,8 +44,6 @@ IEnumerable<PackageSource> configurationDefaultSources
}
Settings = settings;
Settings.SettingsChanged += (_, __) => { OnPackageSourcesChanged(); };
_providerDefaultPrimarySources = providerDefaultPrimarySources ?? Enumerable.Empty<PackageSource>();
_providerDefaultSecondarySources = providerDefaultSecondarySources ?? Enumerable.Empty<PackageSource>();
_migratePackageSources = migratePackageSources;
_configurationDefaultSources = LoadConfigurationDefaultSources(configurationDefaultSources);
}
Expand Down Expand Up @@ -160,13 +129,6 @@ public IEnumerable<PackageSource> LoadPackageSources()
MigrateSources(loadedPackageSources);
}

SetDefaultPackageSources(loadedPackageSources);

foreach (var source in loadedPackageSources)
{
source.Description = GetDescription(source);
}

return loadedPackageSources;
}

Expand Down Expand Up @@ -231,20 +193,6 @@ private static int AddOrUpdateIndexedSource(
return packageIndex;
}

// Gets the description of the source if it matches a default source.
// Returns null if it does not match a default source
private string GetDescription(PackageSource source)
{
var matchingSource = _providerDefaultPrimarySources.FirstOrDefault(
s => StringComparer.OrdinalIgnoreCase.Equals(s.Source, source.Source));
if (matchingSource != null)
{
return matchingSource.Description;
}

return null;
}

private PackageSourceCredential ReadCredential(string sourceName)
{
var environmentCredentials = ReadCredentialFromEnvironment(sourceName);
Expand Down Expand Up @@ -332,113 +280,6 @@ private void MigrateSources(List<PackageSource> loadedPackageSources)
}
}

private void SetDefaultPackageSources(List<PackageSource> loadedPackageSources)
{
var defaultPackageSourcesToBeAdded = new List<PackageSource>();

if (_configurationDefaultSources == null
|| !_configurationDefaultSources.Any<PackageSource>())
{
// Update provider default sources and use provider default sources since _configurationDefaultSources is empty
UpdateProviderDefaultSources(loadedPackageSources);
defaultPackageSourcesToBeAdded = GetPackageSourcesToBeAdded(
loadedPackageSources,
Enumerable.Concat(_providerDefaultPrimarySources, _providerDefaultSecondarySources));
}
else
{
defaultPackageSourcesToBeAdded = GetPackageSourcesToBeAdded(loadedPackageSources, _configurationDefaultSources);
}

var defaultSourcesInsertIndex = loadedPackageSources.FindIndex(source => source.IsMachineWide);
if (defaultSourcesInsertIndex == -1)
{
defaultSourcesInsertIndex = loadedPackageSources.Count;
}

// Default package sources go ahead of machine wide sources
loadedPackageSources.InsertRange(defaultSourcesInsertIndex, defaultPackageSourcesToBeAdded);
}

private List<PackageSource> GetPackageSourcesToBeAdded(List<PackageSource> loadedPackageSources, IEnumerable<PackageSource> allDefaultPackageSources)
{
// There are 4 different cases to consider for primary/ secondary package sources
// Case 1. primary/ secondary Package Source is already present matching both feed source and the feed name. Set IsOfficial to true
// Case 2. primary/ secondary Package Source is already present matching feed source but with a different feed name. DO NOTHING
// Case 3. primary/ secondary Package Source is not present, but there is another feed source with the same feed name. Override that feed entirely
// Case 4. primary/ secondary Package Source is not present, simply, add it. In addition, if Primary is getting added
// for the first time, promote Primary to Enabled and demote secondary to disabled, if it is already enabled

var defaultPackageSourcesToBeAdded = new List<PackageSource>();
foreach (var packageSource in allDefaultPackageSources)
{
var existingIndex = defaultPackageSourcesToBeAdded.FindIndex(
source => string.Equals(source.Name, packageSource.Name, StringComparison.OrdinalIgnoreCase));

// Ignore sources with the same name but lower protocol versions that are already added.
if (existingIndex != -1)
{
var existingSource = defaultPackageSourcesToBeAdded[existingIndex];
if (existingSource.ProtocolVersion < packageSource.ProtocolVersion)
{
defaultPackageSourcesToBeAdded.RemoveAt(existingIndex);
defaultPackageSourcesToBeAdded.Insert(existingIndex, packageSource);
}
continue;
}

var sourceMatchingIndex = loadedPackageSources.FindIndex(p => p.Source.Equals(packageSource.Source, StringComparison.OrdinalIgnoreCase));
if (sourceMatchingIndex != -1)
{
if (loadedPackageSources[sourceMatchingIndex].Name.Equals(packageSource.Name, StringComparison.CurrentCultureIgnoreCase))
{
// Case 1: Both the feed name and source matches. DO NOTHING except set IsOfficial to true
loadedPackageSources[sourceMatchingIndex].IsOfficial = true;
}
else
{
// Case 2: Only feed source matches but name is different. DO NOTHING
}
}
else
{
var nameMatchingIndex = loadedPackageSources.FindIndex(p => p.Name.Equals(packageSource.Name, StringComparison.CurrentCultureIgnoreCase));
if (nameMatchingIndex != -1)
{
// Case 3: Only feed name matches but source is different. Override it entirely
//DO NOTHING
}
else
{
// Case 4: Default package source is not present. Add it to the temp list. Later, the temp listed is inserted above the machine wide sources
defaultPackageSourcesToBeAdded.Add(packageSource);
packageSource.IsOfficial = true;
}
}
}
return defaultPackageSourcesToBeAdded;
}

private void UpdateProviderDefaultSources(List<PackageSource> loadedSources)
{
// If there are NO other non-machine wide sources, providerDefaultPrimarySource should be enabled
var areProviderDefaultSourcesEnabled = loadedSources.Count == 0 || loadedSources.Where(p => !p.IsMachineWide).Count() == 0
|| loadedSources.Where(p => p.IsEnabled).Count() == 0;

foreach (var packageSource in _providerDefaultPrimarySources)
{
packageSource.IsEnabled = areProviderDefaultSourcesEnabled;
packageSource.IsOfficial = true;
}

//Mark secondary sources as official but not enable them
foreach (var secondaryPackageSource in _providerDefaultSecondarySources)
{
secondaryPackageSource.IsEnabled = areProviderDefaultSourcesEnabled;
secondaryPackageSource.IsOfficial = true;
}
}

public void SavePackageSources(IEnumerable<PackageSource> sources)
{
// clear the old values
Expand Down
35 changes: 28 additions & 7 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Settings(string root, string fileName, bool isMachineWideSettings)
Root = root;
FileName = fileName;
XDocument config = null;
ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument("configuration", ConfigFilePath));
ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument(CreateDefaultConfig(), ConfigFilePath));
ConfigXDocument = config;
IsMachineWideSettings = isMachineWideSettings;
}
Expand Down Expand Up @@ -155,7 +155,8 @@ public static ISettings LoadDefaultSettings(
root,
configFileName,
machineWideSettings,
loadAppDataSettings: true);
loadAppDataSettings: true,
useTestingGlobalPath : false);
}

/// <summary>
Expand All @@ -165,7 +166,8 @@ public static ISettings LoadDefaultSettings(
string root,
string configFileName,
IMachineWideSettings machineWideSettings,
bool loadAppDataSettings)
bool loadAppDataSettings,
bool useTestingGlobalPath)
{
{
// Walk up the tree to find a config file; also look in .nuget subdirectories
Expand All @@ -183,7 +185,7 @@ public static ISettings LoadDefaultSettings(

if (loadAppDataSettings)
{
LoadUserSpecificSettings(validSettingFiles, root, configFileName, machineWideSettings);
LoadUserSpecificSettings(validSettingFiles, root, configFileName, machineWideSettings, useTestingGlobalPath);
}

if (machineWideSettings != null)
Expand Down Expand Up @@ -224,7 +226,8 @@ private static void LoadUserSpecificSettings(
List<Settings> validSettingFiles,
string root,
string configFileName,
IMachineWideSettings machineWideSettings
IMachineWideSettings machineWideSettings,
bool useTestingGlobalPath
)
{
if (root == null)
Expand All @@ -238,8 +241,16 @@ IMachineWideSettings machineWideSettings
Settings appDataSettings = null;
if (configFileName == null)
{
var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory);
var defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName);
var defaultSettingsFilePath = String.Empty;
if (useTestingGlobalPath)
{
defaultSettingsFilePath = Path.Combine(root, "TestingGlobalPath");
}
else
{
var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory);
defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName);
}

if (!File.Exists(defaultSettingsFilePath) && machineWideSettings != null)
{
Expand Down Expand Up @@ -1083,5 +1094,15 @@ private static bool ConfigPathComparer(string path1, string path2)
return path1.Equals(path2);
}
}

private static XDocument CreateDefaultConfig()
{
return new XDocument(new XElement("configuration",
new XElement(ConfigurationConstants.PackageSources,
new XElement("add",
new XAttribute(ConfigurationConstants.KeyAttribute, NuGetConstants.FeedName),
new XAttribute(ConfigurationConstants.ValueAttribute, NuGetConstants.V3FeedUrl),
new XAttribute(ConfigurationConstants.ProtocolVersionAttribute, "3")))));
}
}
}
13 changes: 6 additions & 7 deletions src/NuGet.Core/NuGet.Configuration/Utility/XmlUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NuGet.Configuration
{
internal static class XmlUtility
{
internal static XDocument GetOrCreateDocument(XName rootName, string fullPath)
internal static XDocument GetOrCreateDocument(XDocument content, string fullPath)
{
if (File.Exists(fullPath))
{
Expand All @@ -21,18 +21,17 @@ internal static XDocument GetOrCreateDocument(XName rootName, string fullPath)
}
catch (FileNotFoundException)
{
return CreateDocument(rootName, fullPath);
return CreateDocument(content, fullPath);
}
}
return CreateDocument(rootName, fullPath);
return CreateDocument(content, fullPath);
}

private static XDocument CreateDocument(XName rootName, string fullPath)
private static XDocument CreateDocument(XDocument content, string fullPath)
{
var document = new XDocument(new XElement(rootName));
// Add it to the file system
FileSystemUtility.AddFile(fullPath, document.Save);
return document;
FileSystemUtility.AddFile(fullPath, content.Save);
return content;
}

private static XDocument GetDocument(string fullPath)
Expand Down
28 changes: 0 additions & 28 deletions src/NuGet.Core/NuGet.Protocol.Core.Types/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,6 @@ public static ISourceRepositoryProvider CreateProvider(IEnumerable<INuGetResourc
return new SourceRepositoryProvider(Settings.LoadDefaultSettings(rootPath, null, null), CreateLazy(resourceProviders));
}

/// <summary>
/// Create a source provider for the given sources
/// </summary>
public static ISourceRepositoryProvider CreateProvider(IEnumerable<INuGetResourceProvider> resourceProviders, IEnumerable<string> sources)
{
return CreateProvider(resourceProviders, sources.Select(s => new PackageSource(s)));
}

/// <summary>
/// Create a source provider for the given sources and with the extra providers.
/// </summary>
public static ISourceRepositoryProvider CreateProvider(IEnumerable<INuGetResourceProvider> resourceProviders, IEnumerable<PackageSource> sources)
{
if (sources == null)
{
throw new ArgumentNullException("sources");
}

if (resourceProviders == null)
{
throw new ArgumentNullException("resourceProviders");
}

var sourceProvider = new PackageSourceProvider(NullSettings.Instance, sources, Enumerable.Empty<PackageSource>());

return new SourceRepositoryProvider(sourceProvider, CreateLazy(resourceProviders));
}

/// <summary>
/// Create a SourceRepository
/// </summary>
Expand Down
Loading

0 comments on commit 2cef46a

Please sign in to comment.