diff --git a/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs b/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs index 36dce2c3633..61e6055212f 100644 --- a/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs +++ b/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs @@ -24,6 +24,8 @@ public static class NuGetConstants public static readonly string FeedName = "nuget.org"; + public static readonly string AddV3TrackFile = "AddV3Track.config"; + public static readonly string DefaultConfigContent = @" diff --git a/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs b/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs index ed20d538643..b250725e9cf 100644 --- a/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs +++ b/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs @@ -296,6 +296,19 @@ bool useTestingGlobalPath else { appDataSettings = ReadSettings(root, defaultSettingsFilePath); + bool IsEmptyConfig = !appDataSettings.GetSettingValues(ConfigurationConstants.PackageSources).Any(); + + if (IsEmptyConfig) + { + var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile); + + if (appDataSettings.GetorCreateAddV3TrackFileValue(trackFilePath)) + { + var defaultPackageSource = new SettingValue(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, isMachineWide: false); + defaultPackageSource.AdditionalData.Add(ConfigurationConstants.ProtocolVersionAttribute, "3"); + appDataSettings.UpdateSections(ConfigurationConstants.PackageSources, new List { defaultPackageSource }); + } + } } } else @@ -1192,5 +1205,38 @@ private void CheckConfigRoot() string.Format(Resources.ShowError_ConfigRootInvalid, ConfigFilePath)); } } + + private bool GetorCreateAddV3TrackFileValue(string path) + { + bool FileNotExist = false; + if (!File.Exists(path)) + { + FileNotExist = true; + } + var content = new XDocument(new XElement(ConfigurationConstants.AddV3Track, "false")); + XDocument doc = null; + ExecuteSynchronized(() => doc = XmlUtility.GetOrCreateDocument(content, path)); + + if (FileNotExist) + { + return true; + } + else + { + if (doc != null) + { + try + { + return Convert.ToBoolean(doc.Elements(ConfigurationConstants.AddV3Track).FirstOrDefault().Value); + } + catch (FormatException) + { + return false; + } + } + + return false; + } + } } } diff --git a/src/NuGet.Core/NuGet.Configuration/Utility/ConfigurationContants.cs b/src/NuGet.Core/NuGet.Configuration/Utility/ConfigurationContants.cs index 9f2027bcc99..153274f1360 100644 --- a/src/NuGet.Core/NuGet.Configuration/Utility/ConfigurationContants.cs +++ b/src/NuGet.Core/NuGet.Configuration/Utility/ConfigurationContants.cs @@ -47,5 +47,7 @@ public static class ConfigurationConstants public static readonly string BeginIgnoreMarker = "NUGET: BEGIN LICENSE TEXT"; public static readonly string EndIgnoreMarker = "NUGET: END LICENSE TEXT"; + + public static string AddV3Track = "AddV3Track"; } } diff --git a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs index eceaa168502..016b52dc3fd 100644 --- a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs @@ -2235,6 +2235,46 @@ public void CreateNewConfigFileIfNoConfig() } } + [Fact] + public void AddV3ToEmptyConfigFile() + { + using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) + { + // Arrange + var config = @" + +"; + + var nugetConfigPath = "NuGet.Config"; + ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, + Path.Combine(mockBaseDirectory, "TestingGlobalPath"), config); + + // Act + var settings = Settings.LoadDefaultSettings(mockBaseDirectory, null, null, true, true); + + // Assert + var text = File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")).Replace("\r\n", "\n"); + var result = @" + + + + +".Replace("\r\n", "\n"); + Assert.Equal(result, text); + + // Act + settings.DeleteSection("packageSources"); + settings = Settings.LoadDefaultSettings(mockBaseDirectory, null, null, true, true); + + // Assert + text = File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath" , "NuGet.Config")).Replace("\r\n", "\n"); + result = @" + +".Replace("\r\n", "\n"); + Assert.Equal(result, text); + } + } + [Fact] public void LoadNuGetConfig_InvalidXmlThrowException() {