Пример #1
0
        /// <summary>
        /// Warns the user about the fact that the dotnet TFM is deprecated.
        /// </summary>
        /// <returns>Returns true if the user wants to ignore the warning or if the warning does not apply.</returns>
        private bool ShouldContinueDueToDotnetDeprecation(
            INuGetUI uiService,
            IEnumerable <ResolvedAction> actions,
            CancellationToken token)
        {
            var projects = DotnetDeprecatedPrompt.GetAffectedProjects(actions);

            TelemetryUtility.StopTimer();

            if (projects.Any())
            {
                return(uiService.WarnAboutDotnetDeprecation(projects));
            }

            return(true);
        }
Пример #2
0
        // Returns false if user doesn't accept license agreements.
        private async Task <bool> CheckLicenseAcceptanceAsync(
            INuGetUI uiService,
            IEnumerable <PreviewResult> results,
            CancellationToken token)
        {
            // find all the packages that might need a license acceptance
            var licenseCheck = new HashSet <PackageIdentity>(PackageIdentity.Comparer);

            foreach (var result in results)
            {
                foreach (var pkg in result.Added)
                {
                    licenseCheck.Add(pkg);
                }

                foreach (var pkg in result.Updated)
                {
                    licenseCheck.Add(pkg.New);
                }
            }
            var sources         = _sourceProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled);
            var licenseMetadata = await GetPackageMetadataAsync(sources, licenseCheck, token);

            TelemetryUtility.StopTimer();

            // show license agreement
            if (licenseMetadata.Any(e => e.RequireLicenseAcceptance))
            {
                var licenseInfoItems = licenseMetadata
                                       .Where(p => p.RequireLicenseAcceptance)
                                       .Select(e => new PackageLicenseInfo(e.Identity.Id, e.LicenseUrl, e.Authors));
                return(uiService.PromptForLicenseAcceptance(licenseInfoItems));
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// The internal implementation to perform user action.
        /// </summary>
        /// <param name="resolveActionsAsync">A function that returns a task that resolves the user
        /// action into project actions.</param>
        /// <param name="executeActionsAsync">A function that returns a task that executes
        /// the project actions.</param>
        private async Task PerformActionImplAsync(
            INuGetUI uiService,
            Func <Task <IReadOnlyList <ResolvedAction> > > resolveActionsAsync,
            Func <IReadOnlyList <ResolvedAction>, Task> executeActionsAsync,
            NuGetOperationType operationType,
            CancellationToken token)
        {
            var status       = NuGetOperationStatus.Succeeded;
            var startTime    = DateTimeOffset.Now;
            var packageCount = 0;
            var operationId  = Guid.NewGuid().ToString();

            // Enable granular level telemetry events for nuget ui operation
            var telemetryService = new TelemetryServiceHelper();

            uiService.ProjectContext.TelemetryService = telemetryService;

            await _lockService.ExecuteNuGetOperationAsync(async() =>
            {
                try
                {
                    uiService.BeginOperation();

                    var acceptedFormat = await CheckPackageManagementFormat(uiService, token);
                    if (!acceptedFormat)
                    {
                        return;
                    }

                    TelemetryUtility.StartorResumeTimer();

                    var actions = await resolveActionsAsync();
                    var results = GetPreviewResults(actions);

                    if (operationType == NuGetOperationType.Uninstall)
                    {
                        packageCount = results.SelectMany(result => result.Deleted).
                                       Select(package => package.Id).Distinct().Count();
                    }
                    else
                    {
                        var addCount = results.SelectMany(result => result.Added).
                                       Select(package => package.Id).Distinct().Count();

                        var updateCount = results.SelectMany(result => result.Updated).
                                          Select(result => result.New.Id).Distinct().Count();

                        // update packages count
                        packageCount = addCount + updateCount;

                        if (updateCount > 0)
                        {
                            // set operation type to update when there are packages being updated
                            operationType = NuGetOperationType.Update;
                        }
                    }

                    TelemetryUtility.StopTimer();

                    // Show the preview window.
                    if (uiService.DisplayPreviewWindow)
                    {
                        var shouldContinue = uiService.PromptForPreviewAcceptance(results);
                        if (!shouldContinue)
                        {
                            return;
                        }
                    }

                    TelemetryUtility.StartorResumeTimer();

                    // Show the license acceptance window.
                    var accepted = await CheckLicenseAcceptanceAsync(uiService, results, token);

                    TelemetryUtility.StartorResumeTimer();

                    if (!accepted)
                    {
                        return;
                    }

                    // Warn about the fact that the "dotnet" TFM is deprecated.
                    if (uiService.DisplayDeprecatedFrameworkWindow)
                    {
                        var shouldContinue = ShouldContinueDueToDotnetDeprecation(uiService, actions, token);

                        TelemetryUtility.StartorResumeTimer();

                        if (!shouldContinue)
                        {
                            return;
                        }
                    }

                    if (!token.IsCancellationRequested)
                    {
                        // execute the actions
                        await executeActionsAsync(actions);

                        // fires ActionsExecuted event to update the UI
                        uiService.OnActionsExecuted(actions);
                    }
                }
                catch (System.Net.Http.HttpRequestException ex)
                {
                    status = NuGetOperationStatus.Failed;
                    if (ex.InnerException != null)
                    {
                        uiService.ShowError(ex.InnerException);
                    }
                    else
                    {
                        uiService.ShowError(ex);
                    }
                }
                catch (Exception ex)
                {
                    status = NuGetOperationStatus.Failed;
                    uiService.ShowError(ex);
                }
                finally
                {
                    TelemetryUtility.StopTimer();

                    var duration = TelemetryUtility.GetTimerElapsedTime();

                    uiService.ProjectContext.Log(MessageLevel.Info,
                                                 string.Format(CultureInfo.CurrentCulture, Resources.Operation_TotalTime, duration));

                    uiService.EndOperation();

                    var actionTelemetryEvent = TelemetryUtility.GetActionTelemetryEvent(
                        uiService.Projects,
                        operationType,
                        OperationSource.UI,
                        startTime,
                        status,
                        packageCount,
                        duration.TotalSeconds);

                    ActionsTelemetryService.Instance.EmitActionEvent(actionTelemetryEvent, telemetryService.TelemetryEvents);
                }
            }, token);
        }