async Task ExecuteActions(
     NuGetProject project,
     IEnumerable <NuGetProjectAction> actions,
     ConsoleHostNuGetPackageManager packageManager,
     SourceCacheContext cacheContext)
 {
     if (actions.Any())
     {
         if (WhatIf.IsPresent)
         {
             // For -WhatIf, only preview the actions
             PreviewNuGetPackageActions(actions);
         }
         else
         {
             // Execute project actions by Package Manager
             await packageManager.ExecuteNuGetProjectActionsAsync(
                 project,
                 actions,
                 this,
                 cacheContext,
                 ConsoleHost.Token);
         }
     }
     else
     {
         Log(MessageLevel.Info,
             GettextCatalog.GetString("No package updates are available from the current package source for project '{0}'.",
                                      project.GetMetadata <string> (NuGetProjectMetadataKeys.Name)));
     }
 }
        protected async Task UninstallPackageByIdAsync(
            NuGetProject project,
            string packageId,
            UninstallationContext uninstallContext,
            INuGetProjectContext projectContext,
            bool isPreview)
        {
            ConsoleHostNuGetPackageManager   packageManager = ConsoleHost.CreatePackageManager();
            IEnumerable <NuGetProjectAction> actions        = await packageManager.PreviewUninstallPackageAsync(project, packageId, uninstallContext, projectContext, ConsoleHost.Token);

            if (isPreview)
            {
                PreviewNuGetPackageActions(actions);
            }
            else
            {
                await packageManager.ExecuteNuGetProjectActionsAsync(project, actions, projectContext, ConsoleHost.Token);
            }
        }
        async Task PreviewAndExecuteUpdateActionsforSinglePackage(NuGetProject project)
        {
            ConsoleHostNuGetPackageManager packageManager = null;

            var installedPackage = (await project.GetInstalledPackagesAsync(ConsoleHost.Token))
                                   .FirstOrDefault(p => string.Equals(p.PackageIdentity.Id, Id, StringComparison.OrdinalIgnoreCase));

            if (installedPackage != null)
            {
                // set _installed to true, if package to update is installed.
                isPackageInstalled = true;

                packageManager = ConsoleHost.CreatePackageManager();
                var actions = Enumerable.Empty <NuGetProjectAction> ();

                var resolutionContext = CreateResolutionContext();
                // If -Version switch is specified
                if (!string.IsNullOrEmpty(Version))
                {
                    actions = await packageManager.PreviewUpdatePackagesAsync(
                        new PackageIdentity (installedPackage.PackageIdentity.Id, PowerShellCmdletsUtility.GetNuGetVersionFromString(Version)),
                        project,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        ConsoleHost.Token);
                }
                else
                {
                    actions = await packageManager.PreviewUpdatePackagesAsync(
                        installedPackage.PackageIdentity.Id,
                        project,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        ConsoleHost.Token);
                }

                await ExecuteActions(project, actions, packageManager, resolutionContext.SourceCacheContext);
            }
        }
        async Task ExecuteInitPs1ForPackagesConfig(
            ConsoleHostNuGetPackageManager packageManager,
            Dictionary <NuGetFramework, HashSet <PackageIdentity> > packagesConfigInstalled,
            HashSet <PackageIdentity> finishedPackages)
        {
            // Get the path to the Packages folder.
            var packagesFolderPath  = packageManager.PackageManager.PackagesFolderSourceRepository.PackageSource.Source;
            var packagePathResolver = new PackagePathResolver(packagesFolderPath);

            var packagesToSort   = new HashSet <ResolverPackage> ();
            var resolvedPackages = new HashSet <PackageIdentity> ();

            var dependencyInfoResource = await packageManager
                                         .PackageManager
                                         .PackagesFolderSourceRepository
                                         .GetResourceAsync <DependencyInfoResource> ();

            using (var sourceCacheContext = new SourceCacheContext()) {
                // Order by the highest framework first to make this deterministic
                // Process each framework/id/version once to avoid duplicate work
                // Packages may have different dependendcy orders depending on the framework, but there is
                // no way to fully solve this across an entire solution so we make a best effort here.
                foreach (var framework in packagesConfigInstalled.Keys.OrderByDescending(fw => fw, new NuGetFrameworkSorter()))
                {
                    foreach (var package in packagesConfigInstalled[framework])
                    {
                        if (resolvedPackages.Add(package))
                        {
                            var dependencyInfo = await dependencyInfoResource.ResolvePackage(
                                package,
                                framework,
                                sourceCacheContext,
                                NullLogger.Instance,
                                CancellationToken.None);

                            // This will be null for unrestored packages
                            if (dependencyInfo != null)
                            {
                                packagesToSort.Add(new ResolverPackage(dependencyInfo, listed: true, absent: false));
                            }
                        }
                    }
                }
            }

            // Order packages by dependency order
            var sortedPackages = ResolverUtility.TopologicalSort(packagesToSort);

            foreach (var package in sortedPackages)
            {
                if (finishedPackages.Add(package))
                {
                    // Find the package path in the packages folder.
                    var installPath = packagePathResolver.GetInstalledPath(package);

                    if (string.IsNullOrEmpty(installPath))
                    {
                        continue;
                    }

                    ExecuteInitPs1(installPath, package);
                }
            }
        }