private IEnumerable <PackageSource> LoadConfigurationDefaultSources()
        {
            var baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "NuGet");
            var settings      = new Settings(baseDirectory, ConfigurationDefaultsFile);

            var sources = new List <PackageSource>();
            IList <SettingValue> disabledPackageSources = settings.GetSettingValues("disabledPackageSources");
            IList <SettingValue> packageSources         = settings.GetSettingValues("packageSources");

            foreach (var settingValue in packageSources)
            {
                // In a SettingValue representing a package source, the Key represents the name of the package source and the Value its source
                sources.Add(new PackageSource(settingValue.Value,
                                              settingValue.Key,
                                              isEnabled: !disabledPackageSources.Any <SettingValue>(p => p.Key.Equals(settingValue.Key, StringComparison.CurrentCultureIgnoreCase)),
                                              isOfficial: true));
            }

            return(sources);
        }
        private IEnumerable <PackageSource> LoadConfigurationDefaultSources()
        {
#if !DNXCORE50
            // Global default NuGet source doesn't make sense on Mono
            if (_isMono.Value)
            {
                return(Enumerable.Empty <PackageSource>());
            }

            var baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "NuGet");
#else
            var baseDirectory = Path.Combine(Environment.GetEnvironmentVariable("ProgramData"), "NuGet");
#endif
            var settings = new Settings(baseDirectory, ConfigurationContants.ConfigurationDefaultsFile);

            var disabledPackageSources = settings.GetSettingValues(ConfigurationContants.DisabledPackageSources);
            var packageSources         = settings.GetSettingValues(ConfigurationContants.PackageSources);

            var packageSourceLookup = new Dictionary <string, IndexedPackageSource>(StringComparer.OrdinalIgnoreCase);
            var packageIndex        = 0;

            foreach (var settingValue in packageSources)
            {
                // In a SettingValue representing a package source, the Key represents the name of the package source and the Value its source
                var isEnabled = !disabledPackageSources.Any(p => p.Key.Equals(settingValue.Key, StringComparison.OrdinalIgnoreCase));

                var packageSource = ReadPackageSource(settingValue, isEnabled);
                packageSource.IsOfficial = true;

                packageIndex = AddOrUpdateIndexedSource(packageSourceLookup, packageIndex, packageSource);
            }

            return(packageSourceLookup.Values
                   .OrderBy(source => source.Index)
                   .Select(source => source.PackageSource));
        }
        private IEnumerable<PackageSource> LoadConfigurationDefaultSources()
        {
#if !DNXCORE50
            var baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "NuGet");
#else
            var baseDirectory = Path.Combine(Environment.GetEnvironmentVariable("ProgramData"), "NuGet");
#endif
            var settings = new Settings(baseDirectory, ConfigurationDefaultsFile);

            var sources = new List<PackageSource>();
            IList<SettingValue> disabledPackageSources = settings.GetSettingValues("disabledPackageSources");
            IList<SettingValue> packageSources = settings.GetSettingValues("packageSources");

            foreach (var settingValue in packageSources)
            {
                // In a SettingValue representing a package source, the Key represents the name of the package source and the Value its source
                sources.Add(new PackageSource(settingValue.Value,
                    settingValue.Key,
                    isEnabled: !disabledPackageSources.Any<SettingValue>(p => p.Key.Equals(settingValue.Key, StringComparison.CurrentCultureIgnoreCase)),
                    isOfficial: true));
            }

            return sources;
        }
        private IEnumerable<PackageSource> LoadConfigurationDefaultSources()
        {
#if !DNXCORE50
            // Global default NuGet source doesn't make sense on Mono
            if (_isMono.Value)
            {
                return Enumerable.Empty<PackageSource>();
            }

            var baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "NuGet");
#else
            var baseDirectory = Path.Combine(Environment.GetEnvironmentVariable("ProgramData"), "NuGet");
#endif
            var settings = new Settings(baseDirectory, ConfigurationContants.ConfigurationDefaultsFile);

            var disabledPackageSources = settings.GetSettingValues(ConfigurationContants.DisabledPackageSources);
            var packageSources = settings.GetSettingValues(ConfigurationContants.PackageSources);

            var packageSourceLookup = new Dictionary<string, IndexedPackageSource>(StringComparer.OrdinalIgnoreCase);
            var packageIndex = 0;

            foreach (var settingValue in packageSources)
            {
                // In a SettingValue representing a package source, the Key represents the name of the package source and the Value its source
                var isEnabled = !disabledPackageSources.Any(p => p.Key.Equals(settingValue.Key, StringComparison.OrdinalIgnoreCase));

                var packageSource = ReadPackageSource(settingValue, isEnabled);
                packageSource.IsOfficial = true;

                packageIndex = AddOrUpdateIndexedSource(packageSourceLookup, packageIndex, packageSource);
            }

            return packageSourceLookup.Values
                .OrderBy(source => source.Index)
                .Select(source => source.PackageSource);
        }
        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);
            }
        }
Beispiel #6
0
        public async Task DownloadPackage(string packageID, string version, string[] targetFrameworkSets)
        {
            Settings settings = new Settings(Environment.CurrentDirectory, "NuGet.config");
            var sources = settings.GetSettingValues("activePackageSource");
            output.WriteLine("Sources :" + string.Join(",", sources.Select(x => x.Value)));
            Assert.True(sources.Count > 0);


            var repo = Repository.Factory.GetVisualStudio("http://api.nuget.org/v3/index.json");
            var downloadResource = await repo.GetResourceAsync<DownloadResource>();
            var package = new PackageIdentity(packageID, NuGetVersion.Parse(version));
            var info = await downloadResource.GetDownloadResourceResultAsync(package, settings, CancellationToken.None);

            // RefItems provides us only those frameworks for which there is a folder in lib\...
            // so safer to use that
            var refItems = info.PackageReader.GetReferenceItems().ToList();
            foreach (var item in refItems)
            {
                output.WriteLine("Framework JSON: " + JsonConvert.SerializeObject(item.TargetFramework, Formatting.Indented));
            }

            var fx = info.PackageReader.GetSupportedFrameworks().ToList();
            foreach (var item in fx)
            {
                output.WriteLine("Framework JSON: " + JsonConvert.SerializeObject(item, Formatting.Indented));
            }

            foreach (var item in refItems)
            {
                Assert.True(targetFrameworkSets.Any(x => item.TargetFramework.DotNetFrameworkName.Equals(x)));
            }

            //var packageName = ((System.IO.FileStream)depInfo.PackageStream).Name;
            //Directory.Delete(Path.GetDirectoryName(packageName), true);

            Assert.NotNull(refItems.Count == targetFrameworkSets.Length);
        }