Esempio n. 1
0
        private IPythonInterpreterFactoryProvider[] LoadProviders(
            SettingsStore store,
            IServiceProvider serviceProvider
            )
        {
            var seen    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var catalog = new List <ComposablePartCatalog>();

            if (store.CollectionExists(SuppressFactoryProvidersCollection))
            {
                return(new IPythonInterpreterFactoryProvider[0]);
            }

            if (store.CollectionExists(FactoryProvidersCollection))
            {
                foreach (var idStr in store.GetSubCollectionNames(FactoryProvidersCollection))
                {
                    var key = FactoryProvidersCollection + "\\" + idStr;
                    LoadOneProvider(
                        store.GetString(key, FactoryProviderCodeBaseSetting, ""),
                        seen,
                        catalog,
                        _activityLog
                        );
                }
            }

            foreach (var baseKey in new[] { Registry.CurrentUser, Registry.LocalMachine })
            {
                using (var key = baseKey.OpenSubKey(FactoryProvidersRegKey)) {
                    if (key != null)
                    {
                        foreach (var idStr in key.GetSubKeyNames())
                        {
                            using (var subkey = key.OpenSubKey(idStr)) {
                                if (subkey != null)
                                {
                                    LoadOneProvider(
                                        subkey.GetValue(FactoryProviderCodeBaseSetting, "") as string,
                                        seen,
                                        catalog,
                                        _activityLog
                                        );
                                }
                            }
                        }
                    }
                }
            }

            if (!catalog.Any())
            {
                LoadOneProvider(
                    typeof(CPythonInterpreterFactoryConstants).Assembly.Location,
                    seen,
                    catalog,
                    _activityLog
                    );
            }

            const string FailedToImportMessage   = "Failed to import factory providers";
            var          providers               = new List <IPythonInterpreterFactoryProvider>();
            var          serviceProviderProvider = new MockExportProvider();

            if (serviceProvider != null)
            {
                serviceProviderProvider.SetExport(typeof(SVsServiceProvider), () => serviceProvider);
            }

            foreach (var part in catalog)
            {
                var container = new CompositionContainer(part, serviceProviderProvider);
                try {
                    foreach (var provider in container.GetExports <IPythonInterpreterFactoryProvider>())
                    {
                        if (provider.Value != null)
                        {
                            providers.Add(provider.Value);
                        }
                    }
                } catch (CompositionException ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex, ex.Errors);
                } catch (ReflectionTypeLoadException ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex, ex.LoaderExceptions);
                } catch (Exception ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex);
                }
            }

            return(providers.ToArray());
        }
Esempio n. 2
0
        private IPythonInterpreterFactoryProvider[] LoadProviders(
            SettingsStore store,
            IServiceProvider serviceProvider
        ) {
            var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            var catalog = new List<ComposablePartCatalog>();

            if (store.CollectionExists(SuppressFactoryProvidersCollection)) {
                return new IPythonInterpreterFactoryProvider[0];
            }

            if (store.CollectionExists(FactoryProvidersCollection)) {
                foreach (var idStr in store.GetSubCollectionNames(FactoryProvidersCollection)) {
                    var key = FactoryProvidersCollection + "\\" + idStr;
                    LoadOneProvider(
                        store.GetString(key, FactoryProviderCodeBaseSetting, ""),
                        seen,
                        catalog,
                        _activityLog
                    );
                }
            }

            foreach (var baseKey in new[] { Registry.CurrentUser, Registry.LocalMachine }) {
                using (var key = baseKey.OpenSubKey(FactoryProvidersRegKey)) {
                    if (key != null) {
                        foreach (var idStr in key.GetSubKeyNames()) {
                            using (var subkey = key.OpenSubKey(idStr)) {
                                if (subkey != null) {
                                    LoadOneProvider(
                                        subkey.GetValue(FactoryProviderCodeBaseSetting, "") as string,
                                        seen,
                                        catalog,
                                        _activityLog
                                    );
                                }
                            }
                        }
                    }
                }
            }

            if (!catalog.Any()) {
                LoadOneProvider(
                    typeof(CPythonInterpreterFactoryConstants).Assembly.Location,
                    seen,
                    catalog,
                    _activityLog
                );
            }

            const string FailedToImportMessage = "Failed to import factory providers";
            var providers = new List<IPythonInterpreterFactoryProvider>();
            var serviceProviderProvider = new MockExportProvider();
            if (serviceProvider != null) {
                serviceProviderProvider.SetExport(typeof(SVsServiceProvider), () => serviceProvider);
            }

            foreach (var part in catalog) {
                var container = new CompositionContainer(part, serviceProviderProvider);
                try {
                    foreach (var provider in container.GetExports<IPythonInterpreterFactoryProvider>()) {
                        if (provider.Value != null) {
                            providers.Add(provider.Value);
                        }
                    }
                } catch (CompositionException ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex, ex.Errors);
                } catch (ReflectionTypeLoadException ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex, ex.LoaderExceptions);
                } catch (Exception ex) {
                    LogException(_activityLog, FailedToImportMessage, null, ex);
                }
            }

            return providers.ToArray();
        }