// Reinstall all packages in all projects
        public void ReinstallPackages(
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener eventListener)
        {
            //1) Reinstall solution packages first
            //2) On Each Project, call UninstallAllPackages(IProjectManager, Dictionary<Tuple<string, SemanticVersion>, bool>, out packagesInSourceRepository). And, create a dictionary <projectManager, packages>
            //3) Append all packagesInSourceRepository into allPackagesInSourceRepository
            //4) Call InstallWalker.ResolveOperations(allPackagesInSourceRepository, out IList<IPackage> packagesByDependencyOrder)
            //5) Call for each entry in Dictionary<projectManager, packages>
            //    InitializeLogger, RunSolutionAction( call projectManager.AddPackageReference(IPackage, ..., ...)

            // Change it to array so that the enumeration is not modified during enumeration to reinstall solution packages
            var packages = LocalRepository.GetPackages().ToArray();

            foreach (var package in packages)
            {
                if (!IsProjectLevel(package))
                {
                    ReinstallSolutionPackage(package, updateDependencies, allowPrereleaseVersions, logger);
                }
            }

            // Now, take care of project-level packages
            var packagesInProject = new Dictionary<IProjectManager, HashSet<IPackage>>();
            var verifiedPackagesInSourceRepository = new Dictionary<PackageName, IPackage>();
            HashSet<IPackage> allPackagesToBeReinstalled = new HashSet<IPackage>();

            // first uninstall all the packages from each project
            RunActionOnProjects(
                _solutionManager.GetProjects(),
                project =>
                    {
                        IProjectManager projectManager = GetProjectManager(project);
                        HashSet<IPackage> packagesToBeReinstalled;
                        UninstallPackagesForReinstall(projectManager, updateDependencies, logger, verifiedPackagesInSourceRepository, out packagesToBeReinstalled);

                        Debug.Assert(!packagesInProject.ContainsKey(projectManager));
                        packagesInProject[projectManager] = packagesToBeReinstalled;
                        allPackagesToBeReinstalled.AddRange(packagesToBeReinstalled);
                    },
                logger,
                eventListener ?? NullPackageOperationEventListener.Instance);

            // NOTE THAT allowPrereleaseVersions should be true for pre-release packages alone, even if the user did not specify it
            // since we are trying to reinstall packages here. However, ResolveOperations below will take care of this problem via allowPrereleaseVersionsBasedOnPackage parameter
            var installWalker = new InstallWalker(LocalRepository, SourceRepository, null, logger ?? NullLogger.Instance,
                ignoreDependencies: !updateDependencies, allowPrereleaseVersions: allowPrereleaseVersions,
                dependencyVersion: DependencyVersion);

            IList<IPackage> packagesUninstalledInDependencyOrder;
            var operations = installWalker.ResolveOperations(allPackagesToBeReinstalled, out packagesUninstalledInDependencyOrder, allowPrereleaseVersionsBasedOnPackage: true);

            ExecuteOperationsWithPackage(
                _solutionManager.GetProjects(),
                null,
                operations,
                projectManager =>
                {
                    foreach (var package in packagesUninstalledInDependencyOrder)
                    {
                        HashSet<IPackage> packagesToBeReinstalled;
                        if (packagesInProject.TryGetValue(projectManager, out packagesToBeReinstalled) && packagesToBeReinstalled.Contains(package))
                        {
                            AddPackageReference(projectManager, package, ignoreDependencies: !updateDependencies, allowPrereleaseVersions: allowPrereleaseVersions || !package.IsReleaseVersion());
                        }
                    }
                },
                logger,
                eventListener);
        }
 public void SafeUpdatePackage(string packageId, bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackage(packageId,
                   projectManager => UpdatePackageReference(projectManager, packageId, GetSafeRange(projectManager, packageId), updateDependencies, allowPrereleaseVersions),
                   () => SourceRepository.FindPackage(packageId, GetSafeRange(packageId), allowPrereleaseVersions: false, allowUnlisted: false),
                   updateDependencies,
                   allowPrereleaseVersions,
                   logger,
                   eventListener);
 }
 public void SafeUpdatePackages(bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackages(updateDependencies, safeUpdate: true, allowPrereleaseVersions: allowPrereleaseVersions, logger: logger, eventListener: eventListener);
 }
        public void UpdatePackage(
            IEnumerable<Project> projects,
            IPackage package,
            IEnumerable<PackageOperation> operations,
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener packageOperationEventListener)
        {
            if (operations == null)
            {
                throw new ArgumentNullException("operations");
            }

            if (projects == null)
            {
                throw new ArgumentNullException("projects");
            }

            using (StartUpdateOperation(package.Id, package.Version.ToString()))
            {
                ExecuteOperationsWithPackage(
                    projects,
                    package,
                    operations,
                    projectManager => UpdatePackageReference(projectManager, package.Id, package.Version, updateDependencies, allowPrereleaseVersions),
                    logger,
                    packageOperationEventListener);
            }
        }
        public void UpdateSolutionPackages(IEnumerable<IPackage> packages, IEnumerable<PackageOperation> operations, bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
        {
            if (packages == null)
            {
                throw new ArgumentNullException("packages");
            }

            if (operations == null)
            {
                throw new ArgumentNullException("operations");
            }

            try
            {
                InitializeLogger(logger, null);

                RunSolutionAction(() =>
                {
                    // update all packages in the 'packages' folder
                    foreach (var operation in operations)
                    {
                        Execute(operation);
                    }

                    if (eventListener == null)
                    {
                        eventListener = NullPackageOperationEventListener.Instance;
                    }

                    foreach (Project project in _solutionManager.GetProjects())
                    {
                        IProjectManager projectManager = GetProjectManager(project);
                        var oldWhatIfValue = projectManager.WhatIf;
                        try
                        {
                            eventListener.OnBeforeAddPackageReference(project);
                            InitializeLogger(logger, projectManager);

                            foreach (var package in packages)
                            {
                                // only perform update when the local package exists and has smaller version than the new version
                                var localPackage = projectManager.LocalRepository.FindPackage(package.Id);
                                if (localPackage != null && localPackage.Version < package.Version)
                                {
                                    UpdatePackageReference(projectManager, package, updateDependencies, allowPrereleaseVersions);
                                }
                            }
                            ClearLogger(projectManager);
                        }
                        catch (Exception ex)
                        {
                            eventListener.OnAddPackageReferenceError(project, ex);
                        }
                        finally
                        {
                            projectManager.WhatIf = oldWhatIfValue;
                            eventListener.OnAfterAddPackageReference(project);
                        }
                    }
                });
            }
            finally
            {
                ClearLogger(null);
            }
        }
Esempio n. 6
0
 public void UpdatePackageToSpecificVersion(string packageId, SemanticVersion version, bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackage(packageId,
                   projectManager => UpdatePackageReference(projectManager, packageId, version, updateDependencies, allowPrereleaseVersions),
                   () => SourceRepository.FindPackage(packageId, version, allowPrereleaseVersions, allowUnlisted: false),
                   updateDependencies,
                   allowPrereleaseVersions,
                   logger,
                   eventListener);
 }
 private void UpdatePackages(bool updateDependencies, bool safeUpdate, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackages(LocalRepository, package =>
     {
         if (safeUpdate)
         {
             SafeUpdatePackage(package.Id, updateDependencies, allowPrereleaseVersions, logger, eventListener);
         }
         else
         {
             UpdatePackage(package.Id, version: null, updateDependencies: updateDependencies, allowPrereleaseVersions: allowPrereleaseVersions, logger: logger, eventListener: eventListener);
         }
     },
     logger);
 }
        private void ExecuteOperationsWithPackage(IEnumerable<Project> projects, IPackage package, IEnumerable<PackageOperation> operations, Action<IProjectManager> projectAction, ILogger logger, IPackageOperationEventListener eventListener)
        {
            if (eventListener == null)
            {
                eventListener = NullPackageOperationEventListener.Instance;
            }

            ExecuteOperationsWithPackage(
                null,
                package,
                operations,
                () =>
                {
                    bool successfulAtLeastOnce = false;

                    foreach (var project in projects)
                    {
                        try
                        {
                            eventListener.OnBeforeAddPackageReference(project);

                            IProjectManager projectManager = GetProjectManager(project);
                            InitializeLogger(logger, projectManager);

                            projectAction(projectManager);
                            successfulAtLeastOnce = true;
                            ClearLogger(projectManager);
                        }
                        catch (Exception ex)
                        {
                            eventListener.OnAddPackageReferenceError(project, ex);
                        }
                        finally
                        {
                            eventListener.OnAfterAddPackageReference(project);
                        }
                    }

                    // Throw an exception only if all the update failed for all projects
                    // so we rollback any solution level operations that might have happened
                    if (projects.Any() && !successfulAtLeastOnce)
                    {
                        throw new InvalidOperationException(VsResources.OperationFailed);
                    }
                },
                logger);
        }
 /// <summary>
 /// Runs the action on projects and log any error that may occur.
 /// </summary>
 private void RunActionOnProjects(
     IEnumerable<Project> projects,
     Action<Project> action,
     ILogger logger,
     IPackageOperationEventListener eventListener)
 {
     foreach (var project in projects)
     {
         try
         {
             eventListener.OnBeforeAddPackageReference(project);
             action(project);
         }
         catch (Exception exception)
         {
             logger.Log(MessageLevel.Error, ExceptionUtility.Unwrap(exception).Message);
             eventListener.OnAddPackageReferenceError(project, exception);
         }
         finally
         {
             eventListener.OnAfterAddPackageReference(project);
         }
     }
 }
Esempio n. 10
0
        // Reinstall one package in all projects.
        // We need to uninstall the package from all projects BEFORE
        // reinstalling it back, so that the package will be refreshed from source repository.
        private void ReinstallPackageToAllProjects(
            string packageId,
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener eventListener)
        {
            logger = logger ?? NullLogger.Instance;
            eventListener = eventListener ?? NullPackageOperationEventListener.Instance;

            var projectsHasPackage = new Dictionary<Project, SemanticVersion>();

            // first uninstall from all projects that has the package installed
            RunActionOnProjects(
                _solutionManager.GetProjects(),
                project =>
                {
                    var projectManager = GetProjectManager(project);

                    // find the package version installed in this project
                    var projectPackage = projectManager.LocalRepository.FindPackage(packageId);
                    if (projectPackage != null)
                    {
                        // save the version installed in this project so that we can restore the correct version later
                        projectsHasPackage.Add(project, projectPackage.Version);

                        UninstallPackage(
                            projectManager,
                            packageId,
                            version: null,
                            forceRemove: true,
                            removeDependencies: updateDependencies,
                            logger: logger);
                    }
                },
                logger,
                eventListener);

            // now reinstall back to all the affected projects
            RunActionOnProjects(
               projectsHasPackage.Keys,
               project =>
               {
                   var projectManager = GetProjectManager(project);
                   if (!projectManager.LocalRepository.Exists(packageId))
                   {
                       SemanticVersion oldVersion = projectsHasPackage[project];
                       InstallPackage(
                           projectManager,
                           packageId,
                           version: oldVersion,
                           ignoreDependencies: !updateDependencies,
                           allowPrereleaseVersions: allowPrereleaseVersions || !String.IsNullOrEmpty(oldVersion.SpecialVersion),
                           logger: logger);
                   }
               },
               logger,
               eventListener);
        }
Esempio n. 11
0
        public void InstallPackage(
            IEnumerable<Project> projects,
            IPackage package,
            IEnumerable<PackageOperation> operations,
            bool ignoreDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener packageOperationEventListener)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            if (operations == null)
            {
                throw new ArgumentNullException("operations");
            }

            if (projects == null)
            {
                throw new ArgumentNullException("projects");
            }

            ExecuteOperationsWithPackage(
                projects,
                package,
                operations,
                projectManager => AddPackageReference(projectManager, package.Id, package.Version, ignoreDependencies, allowPrereleaseVersions),
                logger,
                packageOperationEventListener);
        }
Esempio n. 12
0
 // Reinstall all packages in all projects
 public void ReinstallPackages(
     bool updateDependencies,
     bool allowPrereleaseVersions,
     ILogger logger,
     IPackageOperationEventListener eventListener)
 {
     UpdatePackages(
         LocalRepository,
         package => ReinstallPackage(
                         package.Id,
                         updateDependencies: updateDependencies,
                         allowPrereleaseVersions: allowPrereleaseVersions,
                         logger: logger,
                         eventListener: eventListener),
         logger);
 }
Esempio n. 13
0
        private void UpdatePackage(string packageId, Action<IProjectManager> projectAction, Func<IPackage> resolvePackage, bool updateDependencies, bool allowPrereleaseVersions,
                ILogger logger, IPackageOperationEventListener eventListener)
        {
            bool appliesToProject;
            IPackage package = FindLocalPackage(packageId, out appliesToProject);

            if (appliesToProject)
            {
                eventListener = eventListener ?? NullPackageOperationEventListener.Instance;

                IPackage newPackage = null;

                foreach (var project in _solutionManager.GetProjects())
                {
                    IProjectManager projectManager = GetProjectManager(project);
                    try
                    {
                        InitializeLogger(logger, projectManager);

                        if (projectManager.LocalRepository.Exists(packageId))
                        {
                            eventListener.OnBeforeAddPackageReference(project);
                            try
                            {
                                RunSolutionAction(() => projectAction(projectManager));

                                if (newPackage == null)
                                {
                                    // after the update, get the new version to add to the recent package repository
                                    newPackage = projectManager.LocalRepository.FindPackage(packageId);
                                }
                            }
                            catch (Exception e)
                            {
                                logger.Log(MessageLevel.Error, ExceptionUtility.Unwrap(e).Message);
                                eventListener.OnAddPackageReferenceError(project, e);
                            }
                            finally
                            {
                                eventListener.OnAfterAddPackageReference(project);
                            }
                        }
                    }
                    finally
                    {
                        ClearLogger(projectManager);
                    }
                }

                if (newPackage != null)
                {
                    AddPackageToRecentRepository(newPackage);
                }
            }
            else
            {
                // Find the package we're going to update to
                IPackage newPackage = resolvePackage();

                if (newPackage != null && package.Version != newPackage.Version)
                {
                    try
                    {
                        InitializeLogger(logger, projectManager: null);

                        // We might be updating a solution only package
                        UpdatePackage(newPackage, updateDependencies, allowPrereleaseVersions);
                    }
                    finally
                    {
                        ClearLogger(projectManager: null);
                    }

                    // Add package to recent repository
                    AddPackageToRecentRepository(newPackage);
                }
                else
                {
                    logger.Log(MessageLevel.Info, VsResources.NoUpdatesAvailable, packageId);
                }
            }
        }
Esempio n. 14
0
 public void UpdatePackages(PackageUpdateMode updateMode, bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackages(packageRepository: LocalRepository, updateMode: updateMode, updateDependencies: updateDependencies, allowPrereleaseVersions: allowPrereleaseVersions, logger: logger, eventListener: eventListener);
 }
Esempio n. 15
0
        /// <summary>
        /// Reinstall the specified package in all projects.
        /// </summary>
        public void ReinstallPackage(
            string packageId,
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener eventListener)
        {
            bool appliesToProject;
            IPackage package = FindLocalPackage(packageId, out appliesToProject);

            if (appliesToProject)
            {
                ReinstallPackageToAllProjects(packageId, updateDependencies, allowPrereleaseVersions, logger, eventListener);
            }
            else
            {
                ReinstallSolutionPackage(package, updateDependencies, allowPrereleaseVersions, logger);
            }
        }
Esempio n. 16
0
        private void UpdatePackage(
            string packageId,
            Action<IProjectManager> projectAction,
            Func<IPackage> resolvePackage,
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener eventListener)
        {
            bool appliesToProject;
            IPackage package = FindLocalPackage(packageId, out appliesToProject);

            if (appliesToProject)
            {
                eventListener = eventListener ?? NullPackageOperationEventListener.Instance;

                foreach (var project in _solutionManager.GetProjects())
                {
                    IProjectManager projectManager = GetProjectManager(project);
                    var oldWhatIfValue = projectManager.WhatIf;
                    try
                    {
                        InitializeLogger(logger, projectManager);
                        projectManager.WhatIf = WhatIf;

                        if (projectManager.LocalRepository.Exists(packageId))
                        {
                            eventListener.OnBeforeAddPackageReference(project);
                            try
                            {
                                RunSolutionAction(() => projectAction(projectManager));
                            }
                            catch (Exception e)
                            {
                                logger.Log(MessageLevel.Error, ExceptionUtility.Unwrap(e).Message);
                                eventListener.OnAddPackageReferenceError(project, e);
                            }
                            finally
                            {
                                eventListener.OnAfterAddPackageReference(project);
                            }
                        }
                    }
                    finally
                    {
                        projectManager.WhatIf = oldWhatIfValue;
                        ClearLogger(projectManager);
                    }
                }
            }
            else
            {
                // Find the package we're going to update to
                IPackage newPackage = resolvePackage();

                if (newPackage != null && package.Version != newPackage.Version)
                {
                    IDisposable operationDisposable = StartUpdateOperation(newPackage.Id, newPackage.Version.ToString());

                    try
                    {
                        InitializeLogger(logger, projectManager: null);

                        // We might be updating a solution only package
                        UpdatePackage(newPackage, updateDependencies, allowPrereleaseVersions);
                    }
                    finally
                    {
                        ClearLogger(projectManager: null);
                        operationDisposable.Dispose();
                    }
                }
                else
                {
                    logger.Log(MessageLevel.Info, VsResources.NoUpdatesAvailable, packageId);
                }
            }
        }
Esempio n. 17
0
        // Reinstall one package in all projects.
        // We need to uninstall the package from all projects BEFORE
        // reinstalling it back, so that the package will be refreshed from source repository.
        private void ReinstallPackageToAllProjects(
            string packageId,
            bool updateDependencies,
            bool allowPrereleaseVersions,
            ILogger logger,
            IPackageOperationEventListener eventListener)
        {
            logger = logger ?? NullLogger.Instance;

            eventListener = eventListener ?? NullPackageOperationEventListener.Instance;

            var projectsHasPackage = new Dictionary<Project, SemanticVersion>();
            var versionsChecked = new Dictionary<SemanticVersion, bool>();

            // first uninstall from all projects that has the package installed
            RunActionOnProjects(
                _solutionManager.GetProjects(),
                project =>
                {
                    IProjectManager projectManager = GetProjectManager(project);

                    // find the package version installed in this project
                    IPackage projectPackage = projectManager.LocalRepository.FindPackage(packageId);
                    if (projectPackage != null)
                    {
                        bool packageExistInSource;
                        if (!versionsChecked.TryGetValue(projectPackage.Version, out packageExistInSource))
                        {
                            // version has not been checked, so check it here
                            packageExistInSource = SourceRepository.Exists(packageId, projectPackage.Version);

                            // mark the version as checked so that we don't have to check again if we
                            // encounter another project with the same version.
                            versionsChecked[projectPackage.Version] = packageExistInSource;
                        }

                        if (packageExistInSource)
                        {
                            // save the version installed in this project so that we can restore the correct version later
                            projectsHasPackage.Add(project, projectPackage.Version);
                            UninstallPackage(
                                projectManager,
                                packageId,
                                version: null,
                                forceRemove: true,
                                removeDependencies: updateDependencies,
                                logger: logger);
                        }
                        else
                        {
                            logger.Log(
                                MessageLevel.Warning,
                                VsResources.PackageRestoreSkipForProject,
                                projectPackage.GetFullName(),
                                project.Name);
                        }
                    }
                },
                logger,
                eventListener);

            // now reinstall back to all the affected projects
            RunActionOnProjects(
               projectsHasPackage.Keys,
               project =>
               {
                   var projectManager = GetProjectManager(project);
                   if (!projectManager.LocalRepository.Exists(packageId))
                   {
                       SemanticVersion oldVersion = projectsHasPackage[project];
                       using (StartReinstallOperation(packageId, oldVersion.ToString()))
                       {
                           InstallPackage(
                               projectManager,
                               packageId,
                               version: oldVersion,
                               ignoreDependencies: !updateDependencies,
                               allowPrereleaseVersions: allowPrereleaseVersions || !String.IsNullOrEmpty(oldVersion.SpecialVersion),
                               logger: logger);
                       }
                   }
               },
               logger,
               eventListener);
        }
Esempio n. 18
0
 private void UpdatePackages(IPackageRepository packageRepository, PackageUpdateMode updateMode, bool updateDependencies, bool allowPrereleaseVersions, ILogger logger, IPackageOperationEventListener eventListener)
 {
     UpdatePackages(
         packageRepository,
         package => UpdatePackage(package.Id, updateMode, updateDependencies, allowPrereleaseVersions, logger, eventListener),
         logger);
 }