예제 #1
0
        public static async Task <NuGetProjectUpgradeWindowModel> CreateAsync(
            IServiceBroker serviceBroker,
            IProjectContextInfo project,
            IList <PackageDependencyInfo> packageDependencyInfos,
            CancellationToken cancellationToken)
        {
            Assumes.NotNull(project);
            Assumes.NotNull(packageDependencyInfos);

            cancellationToken.ThrowIfCancellationRequested();

            string projectName = await project.GetUniqueNameOrNameAsync(serviceBroker, CancellationToken.None);

            string title            = string.Format(CultureInfo.CurrentCulture, Resources.WindowTitle_NuGetMigrator, projectName);
            var    notFoundPackages = new HashSet <PackageIdentity>();
            var    hasIssues        = false;

            IEnumerable <NuGetProjectUpgradeDependencyItem> dependencyItems =
                PackageGraphAnalysisUtilities.GetPackagesWithDependants(packageDependencyInfos)
                .Select(e => new NuGetProjectUpgradeDependencyItem(e.Identity, e));

            foreach (NuGetProjectUpgradeDependencyItem dependencyItem in dependencyItems)
            {
                (bool _, string packagePath) = await project.TryGetInstalledPackageFilePathAsync(
                    serviceBroker,
                    dependencyItem.Identity,
                    cancellationToken);

                InitPackageUpgradeIssues(packagePath, dependencyItem, notFoundPackages, ref hasIssues);
            }

            var upgradeDependencyItems = new ObservableCollection <NuGetProjectUpgradeDependencyItem>(dependencyItems);

            return(new NuGetProjectUpgradeWindowModel(
                       title,
                       upgradeDependencyItems,
                       notFoundPackages,
                       hasIssues));
        }
예제 #2
0
        public async Task UpgradeNuGetProjectAsync(INuGetUI uiService, IProjectContextInfo project)
        {
            Assumes.NotNull(uiService);
            Assumes.NotNull(project);

            INuGetUIContext context = uiService.UIContext;
            // Restore the project before proceeding
            string solutionDirectory = await context.SolutionManagerService.GetSolutionDirectoryAsync(CancellationToken.None);

            await context.PackageRestoreManager.RestoreMissingPackagesInSolutionAsync(
                solutionDirectory,
                uiService.ProjectContext,
                new LoggerAdapter(uiService.ProjectContext),
                CancellationToken.None);

            IServiceBroker serviceBroker = context.ServiceBroker;
            NuGetProjectUpgradeWindowModel upgradeInformationWindowModel;

            using (INuGetProjectManagerService projectManager = await serviceBroker.GetProxyAsync <INuGetProjectManagerService>(
                       NuGetServices.ProjectManagerService,
                       CancellationToken.None))
            {
                Assumes.NotNull(projectManager);

                IReadOnlyCollection <PackageDependencyInfo> packagesDependencyInfo = await projectManager.GetInstalledPackagesDependencyInfoAsync(
                    project.ProjectId,
                    includeUnresolved : true,
                    CancellationToken.None);

                upgradeInformationWindowModel = await NuGetProjectUpgradeWindowModel.CreateAsync(
                    serviceBroker,
                    project,
                    packagesDependencyInfo.ToList(),
                    CancellationToken.None);
            }

            var result = uiService.ShowNuGetUpgradeWindow(upgradeInformationWindowModel);

            if (!result)
            {
                // raise upgrade telemetry event with Cancelled status
                var packagesCount = upgradeInformationWindowModel.UpgradeDependencyItems.Count;

                var upgradeTelemetryEvent       = new UpgradeInformationTelemetryEvent();
                IEnumerable <string> projectIds = await ProjectUtility.GetSortedProjectIdsAsync(
                    uiService.UIContext.ServiceBroker,
                    uiService.Projects,
                    CancellationToken.None);

                upgradeTelemetryEvent.SetResult(
                    projectIds,
                    NuGetOperationStatus.Cancelled,
                    packagesCount);

                TelemetryActivity.EmitTelemetryEvent(upgradeTelemetryEvent);

                return;
            }

            var    progressDialogData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage);
            string projectName        = await project.GetUniqueNameOrNameAsync(
                uiService.UIContext.ServiceBroker,
                CancellationToken.None);

            string backupPath;

            var windowTitle = string.Format(
                CultureInfo.CurrentCulture,
                Resources.WindowTitle_NuGetMigrator,
                projectName);

            using (IModalProgressDialogSession progressDialogSession = await context.StartModalProgressDialogAsync(windowTitle, progressDialogData, uiService))
            {
                backupPath = await PackagesConfigToPackageReferenceMigrator.DoUpgradeAsync(
                    context,
                    uiService,
                    project,
                    upgradeInformationWindowModel.UpgradeDependencyItems,
                    upgradeInformationWindowModel.NotFoundPackages,
                    progressDialogSession.Progress,
                    progressDialogSession.UserCancellationToken);
            }

            if (!string.IsNullOrEmpty(backupPath))
            {
                string htmlLogFile = GenerateUpgradeReport(projectName, backupPath, upgradeInformationWindowModel);

                Process process = null;
                try
                {
                    process = Process.Start(htmlLogFile);
                }
                catch { }
            }
        }