Exemplo n.º 1
0
        // When isPath is true, then the setting value is checked to see if it can be interpreted
        // as relative path. If it can, the returned value will be the full path of the relative path.
        // If it cannot be interpreted as relative path, the value is returned as-is.
        private SettingValue ReadSettingsValue(XElement element, bool isPath)
        {
            var keyAttribute   = element.Attribute("key");
            var valueAttribute = element.Attribute("value");

            if (keyAttribute == null || String.IsNullOrEmpty(keyAttribute.Value) || valueAttribute == null)
            {
                throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.UserSettings_UnableToParseConfigFile, ConfigFilePath));
            }

            var value = valueAttribute.Value;
            Uri uri;

            if (isPath && Uri.TryCreate(value, UriKind.Relative, out uri))
            {
                string configDirectory = Path.GetDirectoryName(ConfigFilePath);
                value = _fileSystem.GetFullPath(Path.Combine(configDirectory, value));
            }

            var settingValue = new SettingValue(keyAttribute.Value, value, IsMachineWideSettings, _priority);

            foreach (var attribute in element.Attributes())
            {
                // Add all attributes other than ConfigurationContants.KeyAttribute and ConfigurationContants.ValueAttribute to AdditionalValues
                if (!string.Equals(attribute.Name.LocalName, "key", StringComparison.Ordinal) &&
                    !string.Equals(attribute.Name.LocalName, "value", StringComparison.Ordinal))
                {
                    settingValue.AdditionalData[attribute.Name.LocalName] = attribute.Value;
                }
            }

            return(settingValue);
        }
Exemplo n.º 2
0
        private static int ReadProtocolVersion(SettingValue setting)
        {
            string protocolVersionString;
            int    protocolVersion;

            if (setting.AdditionalData.TryGetValue(ProtocolVersionAttribute, out protocolVersionString) &&
                int.TryParse(protocolVersionString, out protocolVersion))
            {
                return(protocolVersion);
            }

            return(PackageSource.DefaultProtocolVersion);
        }
Exemplo n.º 3
0
        private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled)
        {
            var name          = setting.Key;
            var packageSource = new PackageSource(setting.Value, name, isEnabled)
            {
                IsMachineWide = setting.IsMachineWide
            };

            var credentials = ReadCredential(name);

            if (credentials != null)
            {
                packageSource.UserName            = credentials.Username;
                packageSource.Password            = credentials.Password;
                packageSource.IsPasswordClearText = credentials.IsPasswordClearText;
            }

            packageSource.ProtocolVersion = ReadProtocolVersion(setting);

            return(packageSource);
        }
Exemplo n.º 4
0
 public void SetFakeActivePackageSource(PackageSource packageSource)
 {
     ActivePackageSourceSettings.Clear();
     var setting = new SettingValue(packageSource.Name, packageSource.Source, false);
     ActivePackageSourceSettings.Add(setting);
 }
Exemplo n.º 5
0
 public void AddFakePackageSource(PackageSource packageSource)
 {
     var setting = new SettingValue (packageSource.Name, packageSource.Source, false);
     PackageSources.Add(setting);
 }
 static PackageSource CreatePackageSourceFromSetting(SettingValue savedPackageSource)
 {
     string source = savedPackageSource.Value;
     string name = savedPackageSource.Key;
     return new PackageSource(source, name);
 }
		public void AddFakePackageSource (PackageSource packageSource)
		{
			var valuePair = new SettingValue (packageSource.Name, packageSource.Source, false);
			PackageSources.Add (valuePair);
		}
Exemplo n.º 8
0
        public override bool Equals(object obj)
        {
            SettingValue value2 = obj as SettingValue;

            return((value2 != null) ? ((value2.Key == this.Key) && ((value2.Value == this.Value) && (value2.IsMachineWide == value2.IsMachineWide))) : false);
        }
		public void ActivePackageSource_SourceChanged_ActivePackageSourceUpdatedInSettings ()
		{
			CreateOptions ();
			var packageSource = new PackageSource ("http://sharpdevelop.com", "Test");

			options.ActivePackageSource = packageSource;

			var expectedSetting = new SettingValue ("Test", "http://sharpdevelop.com", false);
			SettingValue actualSetting = fakeSettings.GetValuePassedToSetValueForActivePackageSourceSection ();

			Assert.AreEqual (expectedSetting, actualSetting);
		}
Exemplo n.º 10
0
        public void SavePackageSources(IEnumerable <PackageSource> sources)
        {
            // clear the old values
            // and write the new ones
            var sourcesToWrite = sources.Where(s => !s.IsMachineWide);

            var existingSettings = (_settingsManager.GetValues(PackageSourcesSectionName, isPath: true) ??
                                    Enumerable.Empty <SettingValue>()).Where(setting => !setting.IsMachineWide).ToList();

            var existingSettingsLookup  = existingSettings.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase);
            var existingDisabledSources = _settingsManager.GetValues(DisabledPackageSourcesSectionName, isPath: false) ??
                                          Enumerable.Empty <SettingValue>();
            var existingDisabledSourcesLookup = existingDisabledSources.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase);

            var sourceSettings   = new List <SettingValue>();
            var sourcesToDisable = new List <SettingValue>();

            foreach (var source in sourcesToWrite)
            {
                var foundSettingWithSourcePriority = false;
                var settingPriority          = 0;
                var existingSettingForSource = existingSettingsLookup[source.Name];

                // Preserve packageSource entries from low priority settings.
                foreach (var existingSetting in existingSettingForSource)
                {
                    settingPriority = Math.Max(settingPriority, existingSetting.Priority);

                    // Write all settings other than the currently written one to the current NuGet.config.
                    if (ReadProtocolVersion(existingSetting) == source.ProtocolVersion)
                    {
                        // Update the source value of all settings with the same protocol version.
                        existingSetting.Value          = source.Source;
                        foundSettingWithSourcePriority = true;
                    }
                    sourceSettings.Add(existingSetting);
                }

                if (!foundSettingWithSourcePriority)
                {
                    // This is a new source, add it to the Setting with the lowest priority.
                    var settingValue = new SettingValue(source.Name, source.Source, isMachineWide: false);
                    if (source.ProtocolVersion != PackageSource.DefaultProtocolVersion)
                    {
                        settingValue.AdditionalData[ProtocolVersionAttribute] =
                            source.ProtocolVersion.ToString(CultureInfo.InvariantCulture);
                    }

                    sourceSettings.Add(settingValue);
                }

                // settingValue contains the setting with the highest priority.

                var existingDisabledSettings = existingDisabledSourcesLookup[source.Name];
                // Preserve disabledPackageSource entries from low priority settings.
                foreach (var setting in existingDisabledSettings.Where(s => s.Priority < settingPriority))
                {
                    sourcesToDisable.Add(setting);
                }

                if (!source.IsEnabled)
                {
                    // Add an entry to the disabledPackageSource in the file that contains
                    sourcesToDisable.Add(new SettingValue(source.Name, "true", isMachineWide: false, priority: settingPriority));
                }
            }

            // Re-add all settings with a higher protocol version that weren't listed. Skip any settings where the source is
            // already being written and any setting with any source with a protocol version < 2 (the max supported version).
            // The latter indicates a deleted source.
            var sourcesWithHigherProtocolVersion = existingSettingsLookup
                                                   .Where(item =>
                                                          !sourcesToWrite.Any(s => string.Equals(s.Name, item.Key, StringComparison.OrdinalIgnoreCase)) &&
                                                          !item.Any(s => ReadProtocolVersion(s) <= MaxSupportedProtocolVersion))
                                                   .SelectMany(s => s);

            sourceSettings.AddRange(sourcesWithHigherProtocolVersion);

            // Add disabled machine wide sources
            foreach (var source in sources.Where(s => s.IsMachineWide && !s.IsEnabled))
            {
                sourcesToDisable.Add(new SettingValue(source.Name, "true", isMachineWide: false));
            }

            // Write the updates to the nearest settings file.
            _settingsManager.UpdateSections(PackageSourcesSectionName, sourceSettings);

            // overwrite new values for the <disabledPackageSources> section
            _settingsManager.UpdateSections(DisabledPackageSourcesSectionName, sourcesToDisable);

            // Overwrite the <packageSourceCredentials> section
            _settingsManager.DeleteSection(CredentialsSectionName);

            var sourceWithCredentials = sources.Where(s => !String.IsNullOrEmpty(s.UserName) && !String.IsNullOrEmpty(s.Password));

            foreach (var source in sourceWithCredentials)
            {
                _settingsManager.SetNestedValues(CredentialsSectionName, source.Name, new[] {
                    new KeyValuePair <string, string>(UsernameToken, source.UserName),
                    ReadPasswordValues(source)
                });
            }
        }