示例#1
0
 public IEnumerable <string> GetOptionValues(string key)
 {
     if (CanCallHost)
     {
         return(_hostApi.GetOptionValues(key));
     }
     return(new string[0]);
 }
示例#2
0
 public IEnumerable <string> GetOptionValues(string key)
 {
     return(_hostApi.GetOptionValues(key));
 }
        public IEnumerable <SoftwareIdentity> FindPackageByCanonicalId(string packageId, IHostApi hostApi)
        {
            Uri pkgId;

            if (Uri.TryCreate(packageId, UriKind.Absolute, out pkgId))
            {
                var segments = pkgId.Segments;
                if (segments.Length > 0)
                {
                    var provider = SelectProviders(pkgId.Scheme, hostApi).FirstOrDefault();
                    if (provider != null)
                    {
                        var name    = Uri.UnescapeDataString(segments[0].Trim('/', '\\'));
                        var version = (segments.Length > 1) ? Uri.UnescapeDataString(segments[1]) : null;
                        var source  = pkgId.Fragment.TrimStart('#');
                        var sources = (string.IsNullOrWhiteSpace(source) ? hostApi.Sources : Uri.UnescapeDataString(source).SingleItemAsEnumerable()).ToArray();

                        var host = new object[] {
                            new {
                                GetSources      = new Func <IEnumerable <string> >(() => sources),
                                GetOptionValues = new Func <string, IEnumerable <string> >(key => key.EqualsIgnoreCase("FindByCanonicalId") ? new[] { "true" } : hostApi.GetOptionValues(key)),
                                GetOptionKeys   = new Func <IEnumerable <string> >(() => hostApi.OptionKeys.ConcatSingleItem("FindByCanonicalId")),
                            },
                            hostApi,
                        }.As <IHostApi>();

                        return(provider.FindPackage(name, version, null, null, host).Select(each => {
                            each.Status = Constants.PackageStatus.Dependency;
                            return each;
                        }).ReEnumerable());
                    }
                }
            }
            return(new SoftwareIdentity[0]);
        }
        public IEnumerable<SoftwareIdentity> FindPackageByCanonicalId(string packageId, IHostApi hostApi) {
            Uri pkgId;
            if (Uri.TryCreate(packageId, UriKind.Absolute, out pkgId)) {
                var segments = pkgId.Segments;
                if (segments.Length > 0) {
                    var provider = SelectProviders(pkgId.Scheme, hostApi).FirstOrDefault();
                    if (provider != null) {
                        var name = Uri.UnescapeDataString(segments[0].Trim('/', '\\'));
                        var version = (segments.Length > 1) ? Uri.UnescapeDataString(segments[1]) : null;
                        var source = pkgId.Fragment.TrimStart('#');
                        var sources = (String.IsNullOrWhiteSpace(source) ? hostApi.Sources : Uri.UnescapeDataString(source).SingleItemAsEnumerable()).ToArray();

                        var host = new object[] {
                            new {
                                GetSources = new Func<IEnumerable<string>>(() => sources),
                                GetOptionValues = new Func<string, IEnumerable<string>>(key => key.EqualsIgnoreCase("FindByCanonicalId") ? new[] {"true"} : hostApi.GetOptionValues(key)),
                                GetOptionKeys = new Func<IEnumerable<string>>(() => hostApi.OptionKeys.ConcatSingleItem("FindByCanonicalId")),
                            },
                            hostApi,
                        }.As<IHostApi>();

                        return provider.FindPackage(name, version, null, null, host).Select(each => {
                            each.Status = Constants.PackageStatus.Dependency;
                            return each;
                        }).ReEnumerable();
                    }
                }
            }
            return new SoftwareIdentity[0];
        }
        public IEnumerable<PackageProvider> SelectProviders(string providerName, IHostApi hostApi) {
            if (!string.IsNullOrWhiteSpace(providerName)) {
                // match with wildcards
                var results = _packageProviders.Values.Where(each => each.ProviderName.IsWildcardMatch(providerName)).ReEnumerable();
                if (results.Any()) {
                    return results;
                }

                // If the provider is installed but not imported, let's import it 
                // we don't want import package provider via name to write errors, because in that case the subsequent call to bootstrapper provider will get cancelled.
                var provider = ImportPackageProviderHelper(hostApi, providerName, null, null, null, false, false, false).ToArray();
                if (provider.Any()) {
                    return provider;
                }

                if (hostApi != null && !providerName.ContainsWildcards()) {
                    // if the end user requested a provider that's not there. perhaps the bootstrap provider can find it.
                    if (RequirePackageProvider(null, providerName, Constants.MinVersion, hostApi)) {
                        // seems to think we found it.
                        if (_packageProviders.ContainsKey(providerName)) {
                            return _packageProviders[providerName].SingleItemAsEnumerable();
                        }
                    }

                    // SelectProviders() is iterating through the loaded provider list. As we still need to go through the
                    // unloaded provider list, we should not warn users yet at this point of time.
                    // If the provider is not found, eventually we will error out in SelectProviders()/cmdletbase.cs(). 

                    //hostApi.Warn(hostApi.FormatMessageString(Constants.Messages.UnknownProvider, providerName));                   
                }
                return Enumerable.Empty<PackageProvider>();
            } else {
                // If a user does not specify -provider or -provider name, we will bootstrap the nuget provider if it does not exist.                
                //Only find, install, uninstall, and save cmdlets requires the bootstrap.                   
                var bootstrapNuGet = hostApi.GetOptionValues(Constants.BootstrapNuGet).FirstOrDefault();

                if ((bootstrapNuGet != null) && bootstrapNuGet.EqualsIgnoreCase("true")) {
                    //check if the NuGet provider is already loaded                   
                    if (!_packageProviders.Keys.Any(each => each.EqualsIgnoreCase(_nuget))) {
                        //we'll bootstrap NuGet provider under the following cases:
                        //case 1: on a clean VM, type install-package foobar
                        //case 2: on a existing VM, if the nuget provider does not exist and type install-package foobar
                        //case 3: An existing VM has a old version of the NuGet installed, no bootstrap will occur. This means there is no changes
                        //        to the user, unless he does 'install-packageprovider -name nuget -force'.
                        if (RequirePackageProvider(null, _nuget, Constants.MinVersion, hostApi)) {
                            // seems to think we found it.
                            if (_packageProviders.ContainsKey(_nuget)) {
                                return PackageProviders.Concat(_packageProviders[_nuget].SingleItemAsEnumerable());
                            }
                        }
                    }
                }
            }

            return PackageProviders;
        }