public async Task UpgradeNuGetProjectAsync(INuGetUI uiService, NuGetProject nuGetProject) { var context = uiService.UIContext; // Restore the project before proceeding var solutionDirectory = context.SolutionManager.SolutionDirectory; await context.PackageRestoreManager.RestoreMissingPackagesInSolutionAsync(solutionDirectory, uiService.ProjectContext, CancellationToken.None); var packagesDependencyInfo = await context.PackageManager.GetInstalledPackagesDependencyInfo(nuGetProject, CancellationToken.None, includeUnresolved : true); var upgradeInformationWindowModel = new NuGetProjectUpgradeWindowModel(nuGetProject, packagesDependencyInfo.ToList()); var result = uiService.ShowNuGetUpgradeWindow(upgradeInformationWindowModel); if (!result) { // raise upgrade telemetry event with Cancelled status var packagesCount = upgradeInformationWindowModel.UpgradeDependencyItems.Count; var upgradeTelemetryEvent = VSTelemetryServiceUtility.GetUpgradeTelemetryEvent( uiService.Projects, NuGetOperationStatus.Cancelled, packagesCount); TelemetryActivity.EmitTelemetryEvent(upgradeTelemetryEvent); return; } var progressDialogData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage); string backupPath; using (var progressDialogSession = context.StartModalProgressDialog(Resources.WindowTitle_NuGetMigrator, progressDialogData, uiService)) { backupPath = await PackagesConfigToPackageReferenceMigrator.DoUpgradeAsync( context, uiService, nuGetProject, upgradeInformationWindowModel.UpgradeDependencyItems, upgradeInformationWindowModel.NotFoundPackages, progressDialogSession.Progress, progressDialogSession.UserCancellationToken); } if (!string.IsNullOrEmpty(backupPath)) { var htmlLogFile = GenerateUpgradeReport(nuGetProject, backupPath, upgradeInformationWindowModel); Process process = null; try { process = Process.Start(htmlLogFile); } catch { } } }
public IModalProgressDialogSession StartModalProgressDialog(string caption, ProgressDialogData initialData, INuGetUI uiService) { var waitForDialogFactory = (IVsThreadedWaitDialogFactory)Package.GetGlobalService(typeof(SVsThreadedWaitDialogFactory)); var progressData = new ThreadedWaitDialogProgressData( initialData.WaitMessage, initialData.ProgressText, null, initialData.IsCancelable, initialData.CurrentStep, initialData.TotalSteps); var session = waitForDialogFactory.StartWaitDialog(caption, progressData); return(new VisualStudioProgressDialogSession(session)); }
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 { } } }
internal static async Task <string> DoUpgradeAsync( INuGetUIContext context, INuGetUI uiService, IProjectContextInfo project, IEnumerable <NuGetProjectUpgradeDependencyItem> upgradeDependencyItems, IEnumerable <PackageIdentity> notFoundPackages, IProgress <ProgressDialogData> progress, CancellationToken token) { var startTime = DateTimeOffset.Now; var packagesCount = 0; var status = NuGetOperationStatus.Succeeded; var upgradeInformationTelemetryEvent = new UpgradeInformationTelemetryEvent(); using (var telemetry = TelemetryActivity.Create(upgradeInformationTelemetryEvent)) { try { // 0. Fail if any package was not found if (notFoundPackages.Any()) { status = NuGetOperationStatus.Failed; var notFoundPackageIds = string.Join(",", notFoundPackages.Select(t => t.Id)); uiService.ProjectContext.Log(MessageLevel.Error, string.Format(CultureInfo.CurrentCulture, Resources.Migrator_PackageNotFound, notFoundPackageIds)); return(null); } IServiceBroker serviceBroker = await BrokeredServicesUtilities.GetRemoteServiceBrokerAsync(); using (INuGetProjectUpgraderService projectUpgrader = await serviceBroker.GetProxyAsync <INuGetProjectUpgraderService>( NuGetServices.ProjectUpgraderService, token)) { Assumes.NotNull(projectUpgrader); string backupPath; // 1. Backup files (csproj and packages.config) that will change try { backupPath = await projectUpgrader.BackupProjectAsync(project.ProjectId, token); } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); uiService.ProjectContext.Log( MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrader_BackupFailed)); return(null); } // 2. Uninstall all packages currently in packages.config var progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Uninstalling); progress.Report(progressData); // Don't uninstall packages we couldn't find - that will just fail PackageIdentity[] packagesToUninstall = upgradeDependencyItems.Select(d => d.Identity) .Where(p => !notFoundPackages.Contains(p)) .ToArray(); try { await projectUpgrader.UninstallPackagesAsync(project.ProjectId, packagesToUninstall, token); } catch (Exception ex) { status = NuGetOperationStatus.Failed; // log error message uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_UninstallFailed)); return(null); } // Reload the project, and get a reference to the reloaded project await projectUpgrader.SaveProjectAsync(project.ProjectId, token); IProjectContextInfo upgradedProject = await projectUpgrader.UpgradeProjectToPackageReferenceAsync( project.ProjectId, token); // Ensure we use the updated project for installing, and don't display preview or license acceptance windows. context.Projects = new[] { upgradedProject }; var nuGetUI = (NuGetUI)uiService; nuGetUI.Projects = new[] { upgradedProject }; nuGetUI.DisplayPreviewWindow = false; // 4. Install the requested packages var ideExecutionContext = uiService.ProjectContext.ExecutionContext as IDEExecutionContext; if (ideExecutionContext != null) { await ideExecutionContext.SaveExpandedNodeStates(context.SolutionManager); } progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Installing); progress.Report(progressData); List <PackageIdentity> packagesToInstall = GetPackagesToInstall(upgradeDependencyItems).ToList(); packagesCount = packagesToInstall.Count; try { await projectUpgrader.InstallPackagesAsync( project.ProjectId, packagesToInstall, token); if (ideExecutionContext != null) { await ideExecutionContext.CollapseAllNodes(context.SolutionManager); } return(backupPath); } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_InstallFailed, backupPath)); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_RevertSteps, "https://aka.ms/nugetupgraderevertv1")); return(null); } } } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); return(null); } finally { IEnumerable <string> projectIds = await ProjectUtility.GetSortedProjectIdsAsync(uiService.Projects, token); upgradeInformationTelemetryEvent.SetResult(projectIds, status, packagesCount); } } }
internal static async Task <string> DoUpgradeAsync( INuGetUIContext context, INuGetUI uiService, NuGetProject nuGetProject, IEnumerable <NuGetProjectUpgradeDependencyItem> upgradeDependencyItems, IEnumerable <PackageIdentity> notFoundPackages, IProgress <ProgressDialogData> progress, CancellationToken token) { var startTime = DateTimeOffset.Now; var packagesCount = 0; var status = NuGetOperationStatus.Succeeded; using (var telemetry = new TelemetryActivity(Guid.Empty)) { try { // 0. Fail if any package was not found if (notFoundPackages.Any()) { status = NuGetOperationStatus.Failed; var notFoundPackageIds = string.Join(",", notFoundPackages.Select(t => t.Id)); uiService.ProjectContext.Log(MessageLevel.Error, string.Format(CultureInfo.CurrentCulture, Resources.Migrator_PackageNotFound, notFoundPackageIds)); return(null); } // 1. Backup files (csproj and packages.config) that will change var solutionManager = context.SolutionManager; var msBuildNuGetProject = (MSBuildNuGetProject)nuGetProject; var msBuildNuGetProjectSystem = msBuildNuGetProject.ProjectSystem; var backupPath = string.Empty; try { backupPath = CreateBackup(msBuildNuGetProject, solutionManager.SolutionDirectory); } catch (Exception ex) { status = NuGetOperationStatus.Failed; // log error message uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrader_BackupFailed)); return(null); } // 2. Uninstall all packages currently in packages.config var progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Uninstalling); progress.Report(progressData); // Don't uninstall packages we couldn't find - that will just fail var actions = upgradeDependencyItems.Select(d => d.Package) .Where(p => !notFoundPackages.Contains(p)) .Select(t => NuGetProjectAction.CreateUninstallProjectAction(t, nuGetProject)); try { await context.PackageManager.ExecuteNuGetProjectActionsAsync(nuGetProject, actions, uiService.ProjectContext, NullSourceCacheContext.Instance, CancellationToken.None); } catch (Exception ex) { status = NuGetOperationStatus.Failed; // log error message uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_UninstallFailed)); return(null); } // Reload the project, and get a reference to the reloaded project var uniqueName = msBuildNuGetProjectSystem.ProjectUniqueName; await msBuildNuGetProject.SaveAsync(token); nuGetProject = await solutionManager.GetNuGetProjectAsync(uniqueName); nuGetProject = await solutionManager.UpgradeProjectToPackageReferenceAsync(nuGetProject); // Ensure we use the updated project for installing, and don't display preview or license acceptance windows. context.Projects = new[] { nuGetProject }; var nuGetUI = (NuGetUI)uiService; nuGetUI.Projects = new[] { nuGetProject }; nuGetUI.DisplayPreviewWindow = false; // 4. Install the requested packages var ideExecutionContext = uiService.ProjectContext.ExecutionContext as IDEExecutionContext; if (ideExecutionContext != null) { await ideExecutionContext.SaveExpandedNodeStates(solutionManager); } progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Installing); progress.Report(progressData); var activeSources = new List <SourceRepository>(); PackageSourceMoniker .PopulateList(context.SourceProvider) .ForEach(s => activeSources.AddRange(s.SourceRepositories)); var packagesToInstall = GetPackagesToInstall(upgradeDependencyItems).ToList(); packagesCount = packagesToInstall.Count; // create low level NuGet actions based on number of packages being installed var lowLevelActions = new List <NuGetProjectAction>(); foreach (var packageIdentity in packagesToInstall) { lowLevelActions.Add(NuGetProjectAction.CreateInstallProjectAction(packageIdentity, activeSources.FirstOrDefault(), nuGetProject)); } try { var buildIntegratedProject = nuGetProject as BuildIntegratedNuGetProject; await context.PackageManager.ExecuteBuildIntegratedProjectActionsAsync( buildIntegratedProject, lowLevelActions, uiService.ProjectContext, token); if (ideExecutionContext != null) { await ideExecutionContext.CollapseAllNodes(solutionManager); } return(backupPath); } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_InstallFailed, backupPath)); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_RevertSteps, "https://aka.ms/nugetupgraderevertv1")); return(null); } } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); return(null); } finally { telemetry.TelemetryEvent = VSTelemetryServiceUtility.GetUpgradeTelemetryEvent( uiService.Projects, status, packagesCount); } } }
public async Task <IModalProgressDialogSession> StartModalProgressDialogAsync(string caption, ProgressDialogData initialData, INuGetUI uiService) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var waitForDialogFactory = (IVsThreadedWaitDialogFactory)Package.GetGlobalService(typeof(SVsThreadedWaitDialogFactory)); var progressData = new ThreadedWaitDialogProgressData( initialData.WaitMessage, initialData.ProgressText, null, initialData.IsCancelable, initialData.CurrentStep, initialData.TotalSteps); var session = waitForDialogFactory.StartWaitDialog(caption, progressData); return(new VisualStudioProgressDialogSession(session)); }