private async Task InstallPackagesInternalAsync( IEnumerable <ProjectInstallActionSummaryModel> summaries, INuGetProjectContext projectContext, bool shouldThrow, CancellationToken token) { projectContext.ActionType = NuGetActionType.Install; projectContext.OperationId = Guid.NewGuid(); List <NuGetProject> projects = summaries.Select(t => t.Project).ToList(); List <PackageIdentity> packages = summaries.SelectMany(t => t.NeedInstall).Distinct().ToList(); using (SourceCacheContext sourceCacheContext = new SourceCacheContext()) { foreach (PackageIdentity packageIdentity in packages) { IReadOnlyList <ResolvedAction> actions = await GetActionsForInstallAsync(projects, packageIdentity, projectContext, sourceCacheContext, token); if (actions.Count != 0) { PackageInstalling?.Invoke(packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); try { await ExecuteActionsAsync(actions, packageIdentity, projectContext, sourceCacheContext, token); } catch (Exception ex) { Debug.WriteLine(ex); if (shouldThrow) { throw; } } finally { PackageInstalled?.Invoke(packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); } } } } }
/// <summary> /// Installs the package. /// </summary> /// <param name="package">The package.</param> /// <param name="isPlugin">if set to <c>true</c> [is plugin].</param> /// <param name="progress">The progress.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> /// <exception cref="ArgumentNullException">package</exception> public async Task InstallPackage(PackageVersionInfo package, bool isPlugin, IProgress <double> progress, CancellationToken cancellationToken) { if (package == null) { throw new ArgumentNullException(nameof(package)); } if (progress == null) { throw new ArgumentNullException(nameof(progress)); } var installationInfo = new InstallationInfo { Id = Guid.NewGuid(), Name = package.name, AssemblyGuid = package.guid, UpdateClass = package.classification, Version = package.versionStr }; var innerCancellationTokenSource = new CancellationTokenSource(); var tuple = new Tuple <InstallationInfo, CancellationTokenSource>(installationInfo, innerCancellationTokenSource); // Add it to the in-progress list lock (CurrentInstallations) { CurrentInstallations.Add(tuple); } var innerProgress = new ActionableProgress <double>(); // Whenever the progress updates, update the outer progress object and InstallationInfo innerProgress.RegisterAction(percent => { progress.Report(percent); installationInfo.PercentComplete = percent; }); var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationTokenSource.Token).Token; var installationEventArgs = new InstallationEventArgs { InstallationInfo = installationInfo, PackageVersionInfo = package }; PackageInstalling?.Invoke(this, installationEventArgs); try { await InstallPackageInternal(package, isPlugin, innerProgress, linkedToken).ConfigureAwait(false); lock (CurrentInstallations) { CurrentInstallations.Remove(tuple); } CompletedInstallationsInternal.Add(installationInfo); PackageInstallationCompleted?.Invoke(this, installationEventArgs); } catch (OperationCanceledException) { lock (CurrentInstallations) { CurrentInstallations.Remove(tuple); } _logger.LogInformation("Package installation cancelled: {0} {1}", package.name, package.versionStr); PackageInstallationCancelled?.Invoke(this, installationEventArgs); throw; } catch (Exception ex) { _logger.LogError(ex, "Package installation failed"); lock (CurrentInstallations) { CurrentInstallations.Remove(tuple); } PackageInstallationFailed?.Invoke(this, new InstallationFailedEventArgs { InstallationInfo = installationInfo, Exception = ex }); throw; } finally { // Dispose the progress object and remove the installation from the in-progress list tuple.Item2.Dispose(); } }
private void NuGetPackageInstalling(string packageId, string version) { PackageInstalling?.Invoke(packageId, version); }
private async Task UpdatePackagesInternalAsync( IEnumerable <ProjectInstallActionSummaryModel> summaries, INuGetProjectContext projectContext, bool shouldThrow, CancellationToken token) { projectContext.ActionType = NuGetActionType.UpdateAll; projectContext.OperationId = Guid.NewGuid(); List <NuGetProject> projects = summaries.Select(t => t.Project).ToList(); List <PackageIdentity> packages = summaries.SelectMany(t => t.NeedInstall).Distinct().ToList(); using (SourceCacheContext sourceCacheContext = new SourceCacheContext()) { foreach (PackageIdentity packageIdentity in packages) { IReadOnlyList <ResolvedAction> actions = await GetActionsForUpdateAsync(projects, packageIdentity, projectContext, sourceCacheContext, token); if (actions.Count != 0) { PackageInstalling?.Invoke(packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); try { await ExecuteActionsAsync(actions, packageIdentity, projectContext, sourceCacheContext, token); } catch (PackageReferenceRollbackException rollbackException) { StringBuilder sb = new StringBuilder(); sb.AppendLine(rollbackException.Message); if (rollbackException.LogMessages != null) { foreach (var item in rollbackException.LogMessages) { sb.AppendLine(item.FormatWithCode()); } } throw new Exception(sb.ToString()); } catch (Exception ex) { Debug.WriteLine(ex); if (shouldThrow) { throw; } } finally { PackageInstalled?.Invoke(packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); } } } } }