// perform the user selected action
        internal async void PerformAction(DetailControl detailControl)
        {
            ActivateOutputWindow();
            _outputConsole.Clear();
            var progressDialog = new ProgressDialog(_outputConsole);
            progressDialog.Owner = Window.GetWindow(this);
            progressDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            progressDialog.FileConflictAction = detailControl.FileConflictAction;
            progressDialog.Show();

            try
            {
                var actions = await detailControl.ResolveActionsAsync(progressDialog);

                // show preview
                var model = (DetailControlModel)_packageDetail.DataContext;
                if (model.Options.ShowPreviewWindow)
                {
                    var shouldContinue = PreviewActions(actions);
                    if (!shouldContinue)
                    {
                        return;
                    }
                }

                // show license agreeement
                bool acceptLicense = ShowLicenseAgreement(actions);
                if (!acceptLicense)
                {
                    return;
                }

                // Create the executor and execute the actions
                var userAction = detailControl.GetUserAction();                
                var executor = new ActionExecutor();
                await Task.Run(
                    () =>
                    {
                        executor.ExecuteActions(actions, progressDialog, userAction);
                    });

                UpdatePackageStatus();
                detailControl.Refresh();
            }
            catch (Exception ex)
            {
                var errorDialog = new ErrorReportingDialog(
                    ex.Message,
                    ex.ToString());
                errorDialog.ShowModal();
            }
            finally
            {
                progressDialog.CloseWindow();
            }
        }
Example #2
0
        private async Task InstallPackage(
            IFileSystem installPathFileSystem,
            string packageId,
            NuGetVersion version)
        {
            if (version == null)
            {
                NoCache = true;
            }
            var packageManager = CreatePackageManager(installPathFileSystem, useSideBySidePaths: true);
            
            // BUGBUG: When adding support for 'AllowMultipleVersions', remember to add PackageInstallNeeded method

            JObject packageMetadata;
            if (version == null)
            {
                packageMetadata = await SourceRepositoryHelper.GetLatestVersionMetadata(SourceRepository, packageId, prerelease: Prerelease);
                version = NuGetVersion.Parse(packageMetadata["version"].ToString());
            }
            else
            {
                packageMetadata = await SourceRepository.GetPackageMetadata(packageId, version);
            }

            if (packageMetadata == null)
            {
                throw new InvalidOperationException(
                        String.Format(CultureInfo.CurrentCulture,
                        NuGet.Resources.NuGetResources.UnknownPackageSpecificVersion, packageId, version));
            }

            var actionResolver = new ActionResolver(
                SourceRepository,
                SourceRepository,
                new ResolutionContext()
                {
                    AllowPrerelease = Prerelease,
                    DependencyBehavior = DependencyBehavior,
                });

            var packageActions = await actionResolver.ResolveActionsAsync(new PackageIdentity(packageId, version),
                PackageActionType.Install,
                new FilesystemInstallationTarget(packageManager));

            if (Verbosity == NuGet.Verbosity.Detailed)
            {
                Console.WriteLine("Actions returned by resolver");
                foreach (var action in packageActions)
                {
                    Console.WriteLine(action.ActionType.ToString() + "-" + action.PackageIdentity.ToString());
                }
            }

            packageActions = packageActions.Where(a => a.ActionType == PackageActionType.Download || a.ActionType == PackageActionType.Purge);

            if (Verbosity == NuGet.Verbosity.Detailed)
            {
                Console.WriteLine("After reducing actions to just Download and Purge");
                foreach (var action in packageActions)
                {
                    Console.WriteLine(action.ActionType.ToString() + "-" + action.PackageIdentity.ToString());
                }
            }

            var actionExecutor = new ActionExecutor();
            actionExecutor.ExecuteActions(packageActions, Console);
        }
        /// <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);
                }
            }
        }