コード例 #1
0
        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;
                    }
                    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);
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
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);
                }
            }
        }
コード例 #5
0
 /// <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);
         }
     }
 }
コード例 #6
0
        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);
        }
コード例 #7
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);
                }
            }
        }