Beispiel #1
0
        internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request)
        {
            var             found               = false;
            var             metaProviderName    = provider.GetMetaProviderName();
            FourPartVersion metaProviderVersion = provider.GetProviderVersion();

                                                                              if (!_metaProviders.ContainsKey(metaProviderName))
            {
                // Meta Providers can't be replaced at this point
                _metaProviders.AddOrSet(metaProviderName, provider);
            }

                                                                              try {
                provider.InitializeProvider(request.As <IRequest>());
                var metaProvider = provider;
                provider.GetProviderNames().ParallelForEach(name => {
                    // foreach (var name in provider.GetProviderNames()) {
                    var instance = metaProvider.CreateProvider(name);
                    if (instance != null)
                    {
                        // check if it's a Package Provider
                        if (typeof(IPackageProvider).CanDynamicCastFrom(instance))
                        {
                            try {
                                found = found | RegisterPackageProvider(instance.As <IPackageProvider>(), asmVersion, request);
                            } catch (Exception e) {
                                e.Dump();
                            }
                        }

                        // check if it's a Services Provider
                        if (typeof(IArchiver).CanDynamicCastFrom(instance))
                        {
                            try {
                                found = found | RegisterArchiver(instance.As <IArchiver>(), asmVersion, request);
                            } catch (Exception e) {
                                e.Dump();
                            }
                        }

                        if (typeof(IDownloader).CanDynamicCastFrom(instance))
                        {
                            try {
                                found = found | RegisterDownloader(instance.As <IDownloader>(), asmVersion, request);
                            } catch (Exception e) {
                                e.Dump();
                            }
                        }
                    }
                });
            } catch (Exception e) {
                e.Dump();
            }
                                                                              return(found);
        }
        private Downloader RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request)
        {
            string name = null;

            try {
                FourPartVersion ver     = provider.GetProviderVersion();
                var             version = ver == 0 ? asmVersion : ver;
                name = provider.GetDownloaderName();
                if (string.IsNullOrWhiteSpace(name))
                {
                    return(null);
                }

                // Initialize the provider before locking the collection
                // that way we're not blocking others on non-deterministic actions.
                request.Debug("Initializing provider '{0}'".format(name));
                provider.InitializeProvider(request.As <IRequest>());
                request.Debug("Provider '{0}' Initialized".format(name));

                lock (Downloaders) {
                    if (Downloaders.ContainsKey(name))
                    {
                        if (version > Downloaders[name].Version)
                        {
                            // remove the old provider first.
                            // todo: this won't remove the plugin domain and unload the code yet
                            // we'll have to do that later.

                            Downloaders.Remove(name);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    request.Debug("Using Downloader Provider {0}".format(name));

                    var downloader = new Downloader(provider)
                    {
                        Version = version
                    };

                    downloader.Initialize(request);
                    Downloaders.AddOrSet(name, downloader);
                    return(downloader);
                }
            } catch (Exception e) {
                request.Debug("Provider '{0}' Failed".format(name));
                e.Dump();
            }
            return(null);
        }
        internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request)
        {
            var found            = false;
            var metaProviderName = provider.GetMetaProviderName();

            lock (_metaProviders)
            {
                if (!_metaProviders.ContainsKey(metaProviderName))
                {
                    // Meta Providers can't be replaced at this point
                    _metaProviders.AddOrSet(metaProviderName, provider);
                }
            }

                  try {
                provider.InitializeProvider(request.As <IRequest>());
                provider.GetProviderNames().ParallelForEach(name => {
                    found = LoadViaMetaProvider(provider, name, asmVersion, request);
                });
            } catch (Exception e) {
                e.Dump();
            }
                  return(found);
        }
Beispiel #4
0
        private bool RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request)
        {
            try {
                FourPartVersion ver     = provider.GetProviderVersion();
                var             version = ver == 0 ? asmVersion : ver;
                var             name    = provider.GetDownloaderName();

                lock (Downloaders) {
                    if (Downloaders.ContainsKey(name))
                    {
                        if (version > Downloaders[name].Version)
                        {
                            // remove the old provider first.
                            // todo: this won't remove the plugin domain and unload the code yet
                            // we'll have to do that later.

                            Downloaders.Remove(name);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    request.Debug("Loading Downloader {0}".format(name, provider.GetDownloaderName()));
                    provider.InitializeProvider(request.As <IRequest>());
                    Downloaders.AddOrSet(name, new Downloader(provider)
                    {
                        Version = version
                    }).Initialize(request);
                }
                return(true);
            } catch (Exception e) {
                e.Dump();
            }
            return(false);
        }
        private PackageProvider RegisterPackageProvider(IPackageProvider provider, string name, FourPartVersion asmVersion, IHostApi request) {
           // string name = null;
            try {
                FourPartVersion ver = provider.GetProviderVersion();
                var version = ver == 0 ? asmVersion : ver;

                //TODO: Need to write a blog file name is the provider name. Provider is no longer required to impl ProvierName property
                //name = provider.GetPackageProviderName();
                //if (String.IsNullOrWhiteSpace(name)) {
                //    return null;
                //}

                // Initialize the provider before locking the collection
                // that way we're not blocking others on non-deterministic actions.
                request.Debug("Initializing provider '{0}'".format(name));
                provider.InitializeProvider(request.As<IRequest>());
                request.Debug("Provider '{0}' Initialized".format(name));


                lock (_packageProviders) {
                    if (_packageProviders.ContainsKey(name)) {
                        if (version > _packageProviders[name].Version) {
                            // remove the old provider first.
                            // todo: this won't remove the plugin domain and unload the code yet
                            // we'll have to do that later.

                            _packageProviders.Remove(name);
                        } else {
                            return null;
                        }
                    }
                }

                request.Debug("Using Package Provider {0}".format(name));
                var packageProvider = new PackageProvider(provider) {
                    Version = version,
                    IsLoaded = true
                };
                packageProvider.Initialize(request);

                // addOrSet locks the collection anyway.
                _packageProviders.AddOrSet(name, packageProvider);
                return packageProvider;
            } catch (Exception e) {
                request.Debug("Provider '{0}' Failed".format(name));
                e.Dump();
            }
            return null;
        }
        private PackageProvider ImportProvider(IHostApi request, string providerName, IMetaProvider powerShellMetaProvider)
        {


            //foreach (var providerName in providers)
            {
                //var matchedProviderName = provider.As<DefaultPackageProvider>().GetPackageProviderName();
                //TODO pass in Maxi, mini, and requiredVersion to LoadAvailableProvider
                var instance = powerShellMetaProvider.LoadAvailableProvider(request.As<IRequest>(), providerName);
                if (instance == null)
                {
                   return null;
                }

                //Register newly created provider
                if (typeof(IPackageProvider).CanDynamicCastFrom(instance))
                {

                    var packageProvider = RegisterPackageProvider(instance.As<IPackageProvider>(), providerName, String.Empty, request);

                    if (packageProvider != null)
                    {
                        packageProvider.ProviderPath = powerShellMetaProvider.GetProviderPath(providerName);
                        return packageProvider;
                    }
                }
                else
                {
                    //A provider is not found
                    request.Error(Constants.Messages.UnknownProvider, ErrorCategory.InvalidOperation.ToString(),
                        providerName, request.FormatMessageString(Constants.Messages.UnknownProvider, providerName));
                }
            }
        }
        /// <summary>
        /// Get available provider if -ListAvailable present;load a provider specified in providerName.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="providerName">provider to be loaded.</param>
        /// <param name="powerShellMetaProvider"></param>
        private IEnumerable<PackageProvider> GetAvailableProvider(IHostApi request, string providerName, IMetaProvider powerShellMetaProvider)
        {
            var results = _packageProviders.Values.Where(each => each.ProviderName.IsWildcardMatch(providerName)).ReEnumerable();

            if (results.Any()) {
                //the provider is already loaded and tracked in the loaded master list: _packageProviders 
                yield break;
            }

            //Get available powershell providers
            var availableProviders = powerShellMetaProvider.GetAvailableLocallyProviders(request.As<IRequest>()).ReEnumerable();                        

            if (!availableProviders.Any()) {
                //No available providers.
                if (!String.IsNullOrWhiteSpace(providerName)) {
                    //Error out if a user specific -Name
                    request.Error(Constants.Messages.UnknownProvider, ErrorCategory.InvalidOperation.ToString(),
                        providerName, request.FormatMessageString(Constants.Messages.UnknownProvider, providerName));
                }
                yield break;
            }

            if (String.IsNullOrWhiteSpace(providerName)) {
                // "-Name" does not exist, we return all we can find
                foreach (var module in availableProviders) {
                    yield return new PackageProvider(module.As<DefaultPackageProvider>());
                }
            } else {
                //A user inputs both -Name and -ListAvailable


                var matches = powerShellMetaProvider.GetProviderNames().Where(each => each.IsWildcardMatch(providerName)).ReEnumerable();
                if (!matches.Any()) {
                    request.Error(Constants.Messages.UnknownProvider, ErrorCategory.InvalidOperation.ToString(),
                        providerName, request.FormatMessageString(Constants.Messages.UnknownProvider, providerName));
                }

                foreach (var match in matches) {
                    yield return new PackageProvider(powerShellMetaProvider.GetAvailableLocallyProvider(match).As<DefaultPackageProvider>());
                    //yield return new PackageProvider(availableProviders[match].As<DefaultPackageProvider>());
                }
            }
        }
        internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request) {
            var found = false;
            var metaProviderName = provider.GetMetaProviderName();

            lock (_metaProviders) {
                if (!_metaProviders.ContainsKey(metaProviderName)) {
                    // Meta Providers can't be replaced at this point
                    _metaProviders.AddOrSet(metaProviderName.ToLowerInvariant(), provider);
                }
            }

            try {
                provider.InitializeProvider(request.As<IRequest>());
                provider.GetProviderNames().ParallelForEach(name => {
                    found = LoadViaMetaProvider(provider, name,asmVersion, request);
                });
            } catch (Exception e) {
                e.Dump();
            }
            return found;
        }
        private Downloader RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request) {
            string name = null;
            try {
                FourPartVersion ver = provider.GetProviderVersion();
                var version = ver == 0 ? asmVersion : ver;
                name = provider.GetDownloaderName();
                if (String.IsNullOrWhiteSpace(name)) {
                    return null;
                }

                // Initialize the provider before locking the collection
                // that way we're not blocking others on non-deterministic actions.
                request.Debug("Initializing provider '{0}'".format(name));
                provider.InitializeProvider(request.As<IRequest>());
                request.Debug("Provider '{0}' Initialized".format(name));

                lock (Downloaders) {
                    if (Downloaders.ContainsKey(name)) {
                        if (version > Downloaders[name].Version) {
                            // remove the old provider first.
                            // todo: this won't remove the plugin domain and unload the code yet
                            // we'll have to do that later.

                            Downloaders.Remove(name);
                        } else {
                            return null;
                        }
                    }
                    request.Debug("Using Downloader Provider {0}".format(name));

                    var downloader = new Downloader(provider) {
                        Version = version,
                        IsLoaded = true
                    };

                    downloader.Initialize(request);
                    Downloaders.AddOrSet(name, downloader);
                    return downloader;
                }
            } catch (Exception e) {
                request.Debug("Provider '{0}' Failed".format(name));
                e.Dump();
            }
            return null;
        }
        private IEnumerable<PackageProvider> ImportPowerShellProvider(IHostApi request, string modulePath, Version requiredVersion, bool shouldRefreshCache)
        {
            request.Debug(string.Format(CultureInfo.CurrentCulture, "Calling ImportPowerShellProvider. providerName = '{0}', requiredVersion='{1}'", 
                modulePath, requiredVersion));

            var powerShellMetaProvider = GetMetaProviderObject(request);
            if (powerShellMetaProvider == null) {
                yield break;
            }

            //providerName can be a file path or name.
            var instances = powerShellMetaProvider.LoadAvailableProvider(request.As<IRequest>(), modulePath, requiredVersion, shouldRefreshCache).ReEnumerable();

            if (!instances.Any()) {
                //A provider is not found
                request.Error(Constants.Messages.UnknownProvider, ErrorCategory.InvalidOperation.ToString(),
                    modulePath, string.Format(Resources.Messages.UnknownProvider, modulePath));
                yield break;

            }

            foreach (var instance in instances) {
                //Register the provider
                var provider = instance.As<PackageProvider>();
                if (provider != null) {
                    //initialize the actual powershell package provider
                    if (provider.Provider == null) {
                        continue;
                    }
                    provider.Provider.InitializeProvider(request.As<IRequest>());

                    AddToProviderCacheTable(provider.ProviderName, provider);

                    //initialize the warpper package provider
                    provider.Initialize(request);

                    // addOrSet locks the collection anyway.
                    _packageProviders.AddOrSet(provider.ProviderName, provider);

                    yield return provider;
                }
            }
        }
        private void ScanForAvailableProviders(IHostApi request,
            string providerName,
            Version requiredVersion,
            Version minimumVersion,
            Version maximumVersion,
            bool shouldRefreshCache = false) {

            ResetProviderCachetable();

            //search assemblies from the well-known locations and update the internal provider cache table 
            var providerAssemblies = ScanAllProvidersFromProviderAssembliesLocation(request, providerName, requiredVersion, minimumVersion, maximumVersion, ProviderOption.AllProvider).ToArray();

            //find out which one are from root directory. Because we cannot tell its version and provider name
            //we need to load it.

            var files = providerAssemblies.Where(each => ProviderAssembliesLocation.ContainsIgnoreCase(Path.GetDirectoryName(each))).ReEnumerable();

            //after the cache table gets cleaned, we need to load these assemblies sitting at the top level folder
            files.ParallelForEach(providerAssemblyName => {
                lock (_providerFiles) {
                    if (_providerFiles.ContainsKey(providerAssemblyName)) {
                        //remove the same file from the _providerFiles if any, so it gets reentered
                        //to the cache table.
                        _providerFiles.Remove(providerAssemblyName);
                    }
                }
                LoadProviderAssembly(request, providerAssemblyName, false);
            });

            var powerShellMetaProvider = GetMetaProviderObject(request);
            if (powerShellMetaProvider == null) {
                return;
            }

            //Get available powershell providers
            powerShellMetaProvider.RefreshProviders(request.As<IRequest>(), providerName, requiredVersion, minimumVersion, maximumVersion);
        }
        /// <summary>
        /// Register the package provider
        /// </summary>
        /// <param name="provider">Package provider object</param>
        /// <param name="asmVersion">assembly version info</param>
        /// <param name="request"></param>
        /// <param name="shouldRefreshCache">should refresh the internal provider list</param>
        /// <returns></returns>
        private PackageProvider RegisterPackageProvider(IPackageProvider provider,
            FourPartVersion asmVersion,
            IHostApi request,
            bool shouldRefreshCache) {

            string name = null;
            try {
                if (provider == null) {
                    return null;
                }
                FourPartVersion ver = provider.GetProviderVersion();
                var version = ver == 0 ? asmVersion : ver;
                name = provider.GetPackageProviderName();
                if (string.IsNullOrWhiteSpace(name)) {
                    return null;
                }

                // Initialize the provider before locking the collection
                // that way we're not blocking others on non-deterministic actions.
                request.Debug("Initializing provider '{0}'".format(name));
                provider.InitializeProvider(request.As<IRequest>());
                request.Debug("Provider '{0}' Initialized".format(name));

                lock (_packageProviders) {
                    //Check if the provider is loaded already.
                    if (_packageProviders.ContainsKey(name)) {
                        //if no -force, do nothing
                        if (!shouldRefreshCache) {
                            request.Verbose(string.Format(CultureInfo.CurrentCulture, Resources.Messages.SkipPreviousProcessedProvider, name));

                            //add the provider to the list
                            var pkgprovider = new PackageProvider(provider) {
                                Version = version,
                                IsLoaded = true
                            };
                            AddToProviderCacheTable(name, pkgprovider);
                            return pkgprovider;
                        } else {
                            //looks like -force is used, we need to remove the old provider first.
                            // this won't remove the plugin domain and unload the code yet
                            _packageProviders.Remove(name);
                        }
                    }
                }

                request.Debug("Using Package Provider {0}".format(name));
                var packageProvider = new PackageProvider(provider) {
                    Version = version
                };

                //initialize the package provider
                packageProvider.Initialize(request);

                // addOrSet locks the collection anyway.
                _packageProviders.AddOrSet(name, packageProvider);
                packageProvider.IsLoaded = true;
                request.Debug("The provider '{0}' is imported".format(name));

                //add the provider to the list
                AddToProviderCacheTable(name, packageProvider);
                return packageProvider;
            } catch (Exception e) {
                request.Debug("Provider '{0}' Failed to import".format(name));
                e.Dump();
            }
            return null;
        }