Exemplo n.º 1
0
        public void SavePackageAgents(IEnumerable <PackageAgent> agents)
        {
            if (agents == null)
            {
                throw new ArgumentNullException(nameof(agents));
            }

            var isDirty = false;
            var existingSettingsLookup = GetExistingSettingsLookup();

            var disabledAgentsSection        = Settings.GetSection(ConfigurationConstants2.DisabledPackageAgents);
            var existingDisabledAgents       = disabledAgentsSection?.Items.OfType <AddItem>();
            var existingDisabledAgentsLookup = existingDisabledAgents?.ToDictionary(setting => setting.Key, StringComparer.OrdinalIgnoreCase);

            foreach (var agent in agents)
            {
                AddItem   existingDisabledAgentItem = null;
                AgentItem existingAgentItem         = null;

                var existingAgentIsEnabled = existingDisabledAgentsLookup == null || existingDisabledAgentsLookup.TryGetValue(agent.Name, out existingDisabledAgentItem);

                if (existingSettingsLookup != null &&
                    existingSettingsLookup.TryGetValue(agent.Name, out existingAgentItem) &&
                    ReadProtocolVersion(existingAgentItem) == agent.ProtocolVersion)
                {
                    var oldPackageAgent = ReadPackageAgent(existingAgentItem, existingAgentIsEnabled);

                    UpdatePackageAgent(
                        agent,
                        oldPackageAgent,
                        existingDisabledAgentItem,
                        updateEnabled: true,
                        shouldSkipSave: true,
                        isDirty: ref isDirty);
                }
                else
                {
                    AddPackageAgent(agent, shouldSkipSave: true, isDirty: ref isDirty);
                }

                if (existingAgentItem != null)
                {
                    existingSettingsLookup.Remove(agent.Name);
                }
            }

            if (existingSettingsLookup != null)
            {
                foreach (var agentItem in existingSettingsLookup)
                {
                    if (existingDisabledAgentsLookup != null && existingDisabledAgentsLookup.TryGetValue(agentItem.Value.Key, out var existingDisabledAgentItem))
                    {
                        Settings.Remove(ConfigurationConstants2.DisabledPackageAgents, existingDisabledAgentItem);
                        isDirty = true;
                    }

                    Settings.Remove(ConfigurationConstants2.PackageAgents, agentItem.Value);
                    isDirty = true;
                }
            }

            if (isDirty)
            {
                Settings.SaveToDisk();
                OnPackageAgentsChanged();
                isDirty = false;
            }
        }
Exemplo n.º 2
0
 static int ReadProtocolVersion(AgentItem setting) => int.TryParse(setting.ProtocolVersion, out var protocolVersion) ? protocolVersion : PackageSource.DefaultProtocolVersion;