private IEnumerable <PowerShellPreviewResult> AddToPowerShellPreviewResult(List <PowerShellPreviewResult> list,
                                                                                   PreviewResult result, PowerShellPackageAction action, VsProject proj)
        {
            IEnumerable <PackageIdentity>     identities = null;
            IEnumerable <UpdatePreviewResult> updates    = null;

            switch (action)
            {
            case PowerShellPackageAction.Install:
            {
                identities = result.Added;
                break;
            }

            case PowerShellPackageAction.Uninstall:
            {
                identities = result.Deleted;
                break;
            }

            case PowerShellPackageAction.Update:
            {
                updates = result.Updated;
                break;
            }
            }

            if (identities != null)
            {
                foreach (PackageIdentity identity in identities)
                {
                    PowerShellPreviewResult previewRes = new PowerShellPreviewResult();
                    previewRes.Id     = identity.Id;
                    previewRes.Action = string.Format("{0} ({1})", action.ToString(), identity.Version.ToNormalizedString());
                    list.Add(previewRes);
                    previewRes.ProjectName = proj.Name;
                }
            }

            if (updates != null)
            {
                foreach (UpdatePreviewResult updateResult in updates)
                {
                    PowerShellPreviewResult previewRes = new PowerShellPreviewResult();
                    previewRes.Id     = updateResult.Old.Id;
                    previewRes.Action = string.Format("{0} ({1} => {2})",
                                                      action.ToString(), updateResult.Old.Version.ToNormalizedString(), updateResult.New.Version.ToNormalizedString());
                    list.Add(previewRes);
                    previewRes.ProjectName = proj.Name;
                }
            }

            return(list);
        }
        private IEnumerable<PowerShellPreviewResult> AddToPowerShellPreviewResult(List<PowerShellPreviewResult> list, 
            PreviewResult result, PowerShellPackageAction action, VsProject proj)
        {
            IEnumerable<PackageIdentity> identities = null;
            IEnumerable<UpdatePreviewResult> updates = null;
            switch (action)
            {
                case PowerShellPackageAction.Install:
                    {
                        identities = result.Added;
                        break;
                    }
                case PowerShellPackageAction.Uninstall:
                    {
                        identities = result.Deleted;
                        break;
                    }
                case PowerShellPackageAction.Update:
                    {
                        updates = result.Updated;
                        break;
                    }
            }

            if (identities != null)
            {
                foreach (PackageIdentity identity in identities)
                {
                    PowerShellPreviewResult previewRes = new PowerShellPreviewResult();
                    previewRes.Id = identity.Id;
                    previewRes.Action = string.Format("{0} ({1})", action.ToString(), identity.Version.ToNormalizedString());
                    list.Add(previewRes);
                    previewRes.ProjectName = proj.Name;
                }
            }

            if (updates != null)
            {
                foreach (UpdatePreviewResult updateResult in updates)
                {
                    PowerShellPreviewResult previewRes = new PowerShellPreviewResult();
                    previewRes.Id = updateResult.Old.Id;
                    previewRes.Action = string.Format("{0} ({1} => {2})", 
                        action.ToString(), updateResult.Old.Version.ToNormalizedString(), updateResult.New.Version.ToNormalizedString());
                    list.Add(previewRes);
                    previewRes.ProjectName = proj.Name;
                }
            }

            return list;
        }
        /// <summary>
        /// Resolve and execute actions for a single package for specified package action type.
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="projects"></param>
        /// <param name="actionType"></param>
        protected void ExecuteSinglePackageAction(PackageIdentity identity, IEnumerable <VsProject> projects, PackageActionType actionType)
        {
            if (identity == null)
            {
                return;
            }

            try
            {
                // Resolve Actions
                List <VsProject> targetProjects = projects.ToList();
                Task <IEnumerable <Client.Resolution.PackageAction> > resolverAction =
                    PackageActionResolver.ResolveActionsAsync(identity, actionType, targetProjects, Solution);

                IEnumerable <Client.Resolution.PackageAction> actions = resolverAction.Result;

                if (WhatIf.IsPresent)
                {
                    foreach (VsProject proj in targetProjects)
                    {
                        IEnumerable <PreviewResult> previewResults = PreviewResult.CreatePreview(actions, proj);
                        if (previewResults.Count() == 0)
                        {
                            PowerShellPreviewResult prResult = new PowerShellPreviewResult();
                            prResult.Id          = identity.Id;
                            prResult.Action      = Resources.Log_NoActionsWhatIf;
                            prResult.ProjectName = proj.Name;
                            WriteObject(prResult);
                        }
                        else
                        {
                            foreach (var p in previewResults)
                            {
                                LogPreviewResult(p, proj);
                            }
                        }
                    }

                    return;
                }

                // Execute Actions
                if (actions.Count() == 0 && actionType == PackageActionType.Install)
                {
                    Log(MessageLevel.Info, NuGetResources.Log_PackageAlreadyInstalled, identity.Id);
                }
                else
                {
                    var            userAction = new UserAction(_actionType, identity);
                    ActionExecutor executor   = new ActionExecutor();
                    executor.ExecuteActions(actions, this, userAction);
                }
            }
            // TODO: Consider adding the rollback behavior if exception is thrown.
            catch (Exception ex)
            {
                if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                {
                    WriteError(ex.InnerException.Message);
                }
                else
                {
                    WriteError(ex.Message);
                }
            }
        }
        /// <summary>
        /// Resolve and execute actions for a single package for specified package action type.
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="projects"></param>
        /// <param name="actionType"></param>
        protected void ExecuteSinglePackageAction(PackageIdentity identity, IEnumerable<VsProject> projects, PackageActionType actionType)
        {
            if (identity == null)
            {
                return;
            }

            try
            {
                // Resolve Actions
                List<VsProject> targetProjects = projects.ToList();
                Task<IEnumerable<Client.Resolution.PackageAction>> resolverAction =
                    PackageActionResolver.ResolveActionsAsync(identity, actionType, targetProjects, Solution);

                IEnumerable<Client.Resolution.PackageAction> actions = resolverAction.Result;

                if (WhatIf.IsPresent)
                {
                    foreach (VsProject proj in targetProjects)
                    {
                        IEnumerable<PreviewResult> previewResults = PreviewResult.CreatePreview(actions, proj);
                        if (previewResults.Count() == 0)
                        {
                            PowerShellPreviewResult prResult = new PowerShellPreviewResult();
                            prResult.Id = identity.Id;
                            prResult.Action = Resources.Log_NoActionsWhatIf;
                            prResult.ProjectName = proj.Name;
                            WriteObject(prResult);
                        }
                        else
                        {
                            foreach (var p in previewResults)
                            {
                                LogPreviewResult(p, proj);
                            }
                        }
                    }

                    return;
                }

                // Execute Actions
                if (actions.Count() == 0 && actionType == PackageActionType.Install)
                {
                    Log(MessageLevel.Info, NuGetResources.Log_PackageAlreadyInstalled, identity.Id);
                }
                else
                {
                    var userAction = new UserAction(_actionType, identity);
                    ActionExecutor executor = new ActionExecutor();
                    executor.ExecuteActions(actions, this, userAction);
                }
            }
            // TODO: Consider adding the rollback behavior if exception is thrown.
            catch (Exception ex)
            {
                if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                {
                    WriteError(ex.InnerException.Message);
                }
                else
                {
                    WriteError(ex.Message);
                }
            }
        }