Exemple #1
0
        private void InstallModuleProvider(DynamicElement provider, string fastPath, BootstrapRequest request)
        {
            // This is a prototype for a general provider installer
            // really, this is going away as soon as we have a real swidtag provider.

            // 'package' may not be the best rel= type.
            foreach (var link in provider.XPath("/swid:SoftwareIdentity/swid:Link[@rel = 'package']"))
            {
                var href = link.Attributes["href"];

                // NOT THIS -> at this point href should either be url to a location (that a provider will recognize)
                // JUST THIS -> or more likely should be a prototype canonical id: <provider>:<packagename>[/version][#source]
                //
                if (string.IsNullOrWhiteSpace(href) || !Uri.IsWellFormedUriString(href, UriKind.Absolute))
                {
                    request.Debug("Bad or missing uri: {0}", href);
                    continue;
                }

                var artifact = link.Attributes["artifact"];

                try {
                    var uri = new Uri(href);
                    // @futuregarrett this may die?
                    // todo make sure this works.
                    var providers = request.PackageManagementService.SelectProviders(uri.Scheme, request).ToArray();
                    if (providers.Length == 0)
                    {
                        // no known provider by that name right now.
                        continue;
                    }

                    var packageId      = uri.Host;
                    var packageVersion = uri.PathAndQuery;
                    var source         = uri.Fragment;

                    if (string.IsNullOrWhiteSpace(packageId))
                    {
                        continue;
                    }

                    var customRequest = request.Extend <Request>(new {
                        GetSources = new Func <IEnumerable <string> >(() => {
                            if (string.IsNullOrWhiteSpace(source))
                            {
                                return(new string[0]);
                            }
                            return(new string[] {
                                source
                            });
                        })
                    });

                    var packages = providers[0].FindPackage(packageId, packageVersion, null, null, 0, customRequest).Wait(60);

                    var pkgs = packages.ToArray();
                    if (pkgs.Length < 1)
                    {
                        if (string.IsNullOrWhiteSpace(packageVersion))
                        {
                            request.Warning("Unable to find package '{0}' to bootstrap", packageId);
                        }
                        else
                        {
                            request.Warning("Unable to find package '{0}/{1}' to bootstrap", packageId, packageVersion);
                        }
                        continue;
                    }
                    if (pkgs.Length > 1)
                    {
                        if (string.IsNullOrWhiteSpace(packageVersion))
                        {
                            request.Warning("Package '{0}' matched more than one package", packageId);
                        }
                        else
                        {
                            request.Warning("Package '{0}/{1}' matched more than one package", packageId, packageVersion);
                        }
                        continue;
                    }
                    var installedPackages = providers[0].InstallPackage(pkgs[0], customRequest).Wait(120).ToArray();
                    if (request.IsCanceled)
                    {
                        return;
                    }

                    bool installed = false;

                    foreach (var pkg in installedPackages)
                    {
                        installed = true;
                        request.YieldSoftwareIdentity(pkg.FastPackageReference, pkg.Name, pkg.Version, pkg.VersionScheme, pkg.Summary, pkg.Source, pkg.SearchKey, pkg.FullPath, pkg.PackageFilename);
                    }
                    if (request.IsCanceled)
                    {
                        return;
                    }

                    if (installed)
                    {
                        // it installed ok!
                        PackageManagementService.LoadProviders(request);
                    }
                    else
                    {
                        request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.FailedProviderBootstrap, fastPath);
                    }
                }
                catch (Exception e) {
                    e.Dump();
                }
            }
        }