示例#1
0
        public void InstallPackage(Workspace workspace, Document document, TypeInfo typeInfo, CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.Assert(typeInfo != null);

            ThreadHelper.JoinableTaskFactory.RunAsync(async delegate {
                // Switch to main thread
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

                try
                {
                    var container    = _serviceProvider.GetService <IComponentModel, SComponentModel>();
                    var docHierarchy = document.GetVsHierarchy(_serviceProvider);
                    if (docHierarchy == null)
                    {
                        return;
                    }

                    var project = docHierarchy.GetDTEProject();
                    var projectSpecificInstallers = container.DefaultExportProvider.GetExportedValues <IProjectPackageInstaller>();
                    if (projectSpecificInstallers != null && projectSpecificInstallers.Any())
                    {
                        var supportedInstaller = projectSpecificInstallers.FirstOrDefault(x => x.SupportsProject(project));
                        if (supportedInstaller != null)
                        {
                            if (await SafeExecuteActionAsync(
                                    delegate {
                                var frameworks = typeInfo.TargetFrameworks == null
                                                        ? null
                                                        : typeInfo.TargetFrameworks.Select(x => VersionUtility.ParseFrameworkName(x)).ToList();
                                return(supportedInstaller.InstallPackageAsync(project, typeInfo.PackageName, typeInfo.PackageVersion, frameworks, cancellationToken));
                            }))
                            {
                                return; // package installed successfully
                            }
                        }
                    }

                    // if there no project specific installer use nuget default IVsPackageInstaller
                    var installer             = container.DefaultExportProvider.GetExportedValue <IVsPackageInstaller>() as IVsPackageInstaller2;
                    var packageSourceProvider = container.DefaultExportProvider.GetExportedValue <IVsPackageSourceProvider>();
                    var sources = packageSourceProvider.GetSources(includeUnOfficial: true, includeDisabled: false).Select(x => x.Value).ToList();

                    // if pass all sources to InstallPackageAsync it would throw Central Directory corrupt
                    // if one of the feeds is not supported and stop installation - nuget's bug
                    foreach (var source in sources)
                    {
                        if (await SafeExecuteActionAsync(
                                delegate {
                            return(installer.InstallPackageAsync(project, new[] { source }, typeInfo.PackageName, typeInfo.PackageVersion, true, cancellationToken));
                        }))
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    // we should not throw here, since it would create an exception that may be
                    // visible to the user, instead just dump into debugger output or to package
                    // manager console.
                    // TODO Package manager console?
                    Debug.Write(string.Format("{0} \r\n {1}", e.Message, e.StackTrace));
                }
            });
        }