示例#1
0
        public async Task UpdatePackagesAsync(IPackageDetails package, bool allowedPrerelease = false, CancellationToken token = default)
        {
            Argument.IsNotNull(() => package);

            var updateIdentity = package.GetIdentity();
            var installPath    = _defaultProject.GetInstallPath(updateIdentity);

            // create current version identity
            var currentVersion = await _nuGetPackageManager.GetVersionInstalledAsync(_defaultProject, updateIdentity.Id, token);

            var currentIdentity = new PackageIdentity(updateIdentity.Id, currentVersion);

            if (currentIdentity.Version is null)
            {
                // Can be because of mismatch between packages.config and package files

                _logger.LogWarning($"Could not find existing local files for installed '{package.Id}'. Continue update to version '{updateIdentity.Version}'");

                ValidatePackage(package);
                _packageOperationNotificationService.NotifyOperationStarting(installPath, PackageOperationType.Install, package);

                await _nuGetPackageManager.UpdatePackageForProjectAsync(_defaultProject, updateIdentity.Id, updateIdentity.Version, token);

                return;
            }
            var uninstallPath = _defaultProject.GetInstallPath(currentIdentity);

            try
            {
                ValidatePackage(package);

                _packageOperationNotificationService.NotifyOperationStarting(installPath, PackageOperationType.Update, package); // install path is same as update

                // notify about uninstall and install because update in fact is combination of these actions
                // this also allow us provide different InstallPaths on notifications

                _packageOperationNotificationService.NotifyOperationStarting(uninstallPath, PackageOperationType.Uninstall, package);

                _packageOperationNotificationService.NotifyOperationStarting(installPath, PackageOperationType.Install, package);

                await _nuGetPackageManager.UpdatePackageForProjectAsync(_defaultProject, updateIdentity.Id, updateIdentity.Version, token);
            }
            catch (Exception ex)
            {
                await _logger.LogAsync(LogLevel.Error, ex.Message);

                _packageOperationContextService.CurrentContext.Exceptions.Add(ex);
            }
            finally
            {
                FinishOperation(PackageOperationType.Uninstall, uninstallPath, package);
                FinishOperation(PackageOperationType.Install, installPath, package);
                FinishOperation(PackageOperationType.Update, installPath, package); // The install path the same for update;
            }
        }
        /// <summary>
        /// Get installed local metadata based on package.config
        /// </summary>
        /// <param name="token"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <DeferToken> GetMetadataFromLocalSourcesAsync(DeferToken token, CancellationToken cancellationToken)
        {
            var    project   = _projectProvider.GetDefaultProject();
            string packageId = token.Package.Identity.Id;

            if (project is null)
            {
                return(token);
            }

            var installedVersion = await _projectManager.GetVersionInstalledAsync(project, packageId, cancellationToken);

            if (installedVersion is null)
            {
                return(token);
            }

            var metadata = await _packageMetadataProvider.GetLocalPackageMetadataAsync(new PackageIdentity(packageId, installedVersion), true, cancellationToken);

            token.Result = metadata;

            return(token);
        }