Exemple #1
0
        /// <summary>
        /// Returns PackageSources if specified in the config file. Else returns the default sources specified in the constructor.
        /// If no default values were specified, returns an empty sequence.
        /// </summary>
        public IEnumerable <PackageSource> LoadPackageSources()
        {
            var sources                 = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var settingsValue           = new List <SettingValue>();
            IList <SettingValue> values = Settings.GetSettingValues(PackageSourcesSectionName);
            var machineWideSourcesCount = 0;

            if (values != null && values.Any())
            {
                var machineWideSources = new List <SettingValue>();

                // remove duplicate sources. Pick the one with the highest priority.
                // note that Reverse() is needed because items in 'values' is in
                // ascending priority order.
                foreach (var settingValue in values.Reverse())
                {
                    if (!sources.Contains(settingValue.Key))
                    {
                        if (settingValue.IsMachineWide)
                        {
                            machineWideSources.Add(settingValue);
                        }
                        else
                        {
                            settingsValue.Add(settingValue);
                        }

                        sources.Add(settingValue.Key);
                    }
                }

                // Reverse the the list to be backward compatible
                settingsValue.Reverse();
                machineWideSourcesCount = machineWideSources.Count;

                // Add machine wide sources at the end
                settingsValue.AddRange(machineWideSources);
            }

            var loadedPackageSources = new List <PackageSource>();

            if (settingsValue != null && settingsValue.Any())
            {
                // get list of disabled packages
                var disabledSetting = Settings.GetSettingValues(DisabledPackageSourcesSectionName) ?? Enumerable.Empty <SettingValue>();

                Dictionary <string, SettingValue> disabledSources = new Dictionary <string, SettingValue>();
                foreach (var setting in disabledSetting)
                {
                    if (disabledSources.ContainsKey(setting.Key.ToLower()))
                    {
                        disabledSources[setting.Key.ToLower()] = setting;
                    }
                    else
                    {
                        disabledSources.Add(setting.Key.ToLower(), setting);
                    }
                }
                loadedPackageSources = settingsValue.
                                       Select(p =>
                {
                    string name = p.Key;
                    string src  = p.Value;
                    PackageSourceCredential creds = ReadCredential(name);

                    bool isEnabled = true;
                    SettingValue disabledSource;
                    if (disabledSources.TryGetValue(name, out disabledSource) &&
                        disabledSource.Priority >= p.Priority)
                    {
                        isEnabled = false;
                    }

                    return(new PackageSource(src, name, isEnabled)
                    {
                        UserName = creds != null ? creds.Username : null,
                        Password = creds != null ? creds.Password : null,
                        IsPasswordClearText = creds != null && creds.IsPasswordClearText,
                        IsMachineWide = p.IsMachineWide
                    });
                }).ToList();

                if (_migratePackageSources != null)
                {
                    MigrateSources(loadedPackageSources);
                }
            }

            SetDefaultPackageSources(loadedPackageSources, machineWideSourcesCount);

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

            return(loadedPackageSources);
        }