public async Task TestGetMissingPackagesForSolution() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) using (var randomPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder()) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var packageIdentity = new PackageIdentity("packageA", new NuGetVersion("1.0.0")); var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomPackageSourcePath, packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); var testNuGetProjectContext = new TestNuGetProjectContext(); var token = CancellationToken.None; using (var packageStream = GetDownloadResult(packageFileInfo)) { // Act await projectA.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); await projectB.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); } var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); // Act var packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); var packagesFromSolutionList = packagesFromSolution.ToList(); var missingPackagesFromSolutionList = packagesFromSolution.Where(p => p.IsMissing).ToList(); Assert.Equal(1, packagesFromSolutionList.Count); Assert.Equal(0, missingPackagesFromSolutionList.Count); // Delete packages folder TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages")); packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); packagesFromSolutionList = packagesFromSolution.ToList(); missingPackagesFromSolutionList = packagesFromSolution.Where(p => p.IsMissing).ToList(); Assert.Equal(1, missingPackagesFromSolutionList.Count); } }
public async Task TestGetMissingPackagesForSolution_NoPackagesInstalled() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var testNuGetProjectContext = new TestNuGetProjectContext(); var token = CancellationToken.None; var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); // Act var packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); Assert.False(packagesFromSolution.Any()); } }
protected async Task CheckMissingPackagesAsync() { var solutionDirectory = VsSolutionManager.SolutionDirectory; var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None); if (packages.Any(p => p.IsMissing)) { var packageRestoreConsent = new VisualStudio.PackageRestoreConsent(ConfigSettings); if (packageRestoreConsent.IsGranted) { await TaskScheduler.Default; var result = await PackageRestoreManager.RestoreMissingPackagesAsync(solutionDirectory, packages, this, Token); if (result.Restored) { await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None); return; } } ErrorHandler.HandleException( new InvalidOperationException(Resources.Cmdlet_MissingPackages), terminating: true, errorId: NuGetErrorId.MissingPackages, category: ErrorCategory.InvalidOperation); } }
protected async Task CheckMissingPackagesAsync() { var solutionDirectory = VsSolutionManager.SolutionDirectory; var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None); if (packages.Any(p => p.IsMissing)) { var packageRestoreConsent = new PackageRestoreConsent(ConfigSettings); if (packageRestoreConsent.IsGranted) { await TaskScheduler.Default; using (var cacheContext = new SourceCacheContext()) { var logger = new LoggerAdapter(this); var signedPackageVerifier = new PackageSignatureVerifier(SignatureVerificationProviderFactory.GetSignatureVerificationProviders()); var downloadContext = new PackageDownloadContext(cacheContext) { ParentId = OperationId, ExtractionContext = new PackageExtractionContext( PackageSaveMode.Defaultv3, PackageExtractionBehavior.XmlDocFileSaveMode, logger, signedPackageVerifier, SignedPackageVerifierSettings.GetClientPolicy(ConfigSettings, logger)) }; var result = await PackageRestoreManager.RestoreMissingPackagesAsync( solutionDirectory, packages, this, downloadContext, logger, Token); if (result.Restored) { await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None); return; } } } ErrorHandler.HandleException( new InvalidOperationException(Resources.Cmdlet_MissingPackages), terminating: true, errorId: NuGetErrorId.MissingPackages, category: ErrorCategory.InvalidOperation); } }
protected async Task CheckMissingPackagesAsync() { var solutionDirectory = await VsSolutionManager.GetSolutionDirectoryAsync(); var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None); if (packages.Any(p => p.IsMissing)) { var packageRestoreConsent = new PackageRestoreConsent(ConfigSettings); if (packageRestoreConsent.IsGranted) { await TaskScheduler.Default; using (var cacheContext = new SourceCacheContext()) { var logger = new LoggerAdapter(this); var downloadContext = new PackageDownloadContext(cacheContext) { ParentId = OperationId, ClientPolicyContext = ClientPolicyContext.GetClientPolicy(ConfigSettings, logger) }; var result = await PackageRestoreManager.RestoreMissingPackagesAsync( solutionDirectory, packages, this, downloadContext, logger, Token); if (result.Restored) { await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None); return; } } } ErrorHandler.HandleException( new InvalidOperationException(Resources.Cmdlet_MissingPackages), terminating: true, errorId: NuGetErrorId.MissingPackages, category: ErrorCategory.InvalidOperation); } }
private async Task RestorePackagesOrCheckForMissingPackagesAsync( string solutionDirectory, bool isSolutionAvailable) { // To be sure, switch to main thread before doing anything on this method await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var waitDialogFactory = ServiceLocator.GetGlobalService <SVsThreadedWaitDialogFactory, IVsThreadedWaitDialogFactory>(); if (string.IsNullOrEmpty(solutionDirectory)) { // If the solution is closed, SolutionDirectory will be unavailable. Just return. Do nothing return; } var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None); if (IsConsentGranted(Settings)) { CurrentCount = 0; if (!packages.Any()) { if (!isSolutionAvailable && GetProjectFolderPath().Any(p => CheckPackagesConfig(p.ProjectPath, p.ProjectName))) { MessageHelper.ShowError(_errorListProvider, TaskErrorCategory.Error, TaskPriority.High, NuGet.PackageManagement.VisualStudio.Strings.SolutionIsNotSaved, hierarchyItem: null); WriteLine( VerbosityLevel.Quiet, NuGet.PackageManagement.VisualStudio.Strings.SolutionIsNotSaved); } // Restore is not applicable, since, there is no project with installed packages return; } var missingPackagesList = packages.Where(p => p.IsMissing).ToList(); TotalCount = missingPackagesList.Count; if (TotalCount > 0) { // Only show the wait dialog, when there are some packages to restore using (var threadedWaitDialogSession = waitDialogFactory.StartWaitDialog( waitCaption: Resources.DialogTitle, initialProgress: new ThreadedWaitDialogProgressData( Resources.RestoringPackages, string.Empty, string.Empty, isCancelable: true, currentStep: 0, totalSteps: 0))) { Token = threadedWaitDialogSession.UserCancellationToken; ThreadedWaitDialogProgress = threadedWaitDialogSession.Progress; // Display the restore opt out message if it has not been shown yet DisplayOptOutMessage(); await RestoreMissingPackagesInSolutionAsync(solutionDirectory, packages, Token); // Mark that work is being done during this restore _hasMissingPackages = true; _displayRestoreSummary = true; } } } else { // When the user consent is not granted, missing packages may not be restored. // So, we just check for them, and report them as warning(s) on the error list window using (var twd = waitDialogFactory.StartWaitDialog( waitCaption: Resources.DialogTitle, initialProgress: new ThreadedWaitDialogProgressData( Resources.RestoringPackages, string.Empty, string.Empty, isCancelable: true, currentStep: 0, totalSteps: 0))) { CheckForMissingPackages(packages); } } await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None); }
public Task <IEnumerable <PackageRestoreData> > GetPackagesInSolutionAsync( string solutionDirectory, CancellationToken token) { return(restoreManager.GetPackagesInSolutionAsync(solutionDirectory, token)); }