Exemplo n.º 1
0
        public async ValueTask <Stream?> GetPackageIconAsync(PackageIdentity packageIdentity, CancellationToken cancellationToken)
        {
            Assumes.NotNull(packageIdentity);
            string key = NuGetPackageFileService.IconPrefix + packageIdentity.ToString();
            Uri?   uri = IdentityToUriCache.Get(key) as Uri;

            if (uri == null)
            {
                var exception = new CacheMissException();
                await _nuGetTelemetryProvider.PostFaultAsync(exception, typeof(NuGetPackageFileService).FullName, nameof(NuGetPackageFileService.GetPackageIconAsync));

                return(null);
            }

            Stream?stream;

            if (IsEmbeddedUri(uri))
            {
                stream = await GetEmbeddedFileAsync(uri, cancellationToken);
            }
            else
            {
                stream = await GetStream(uri);
            }

            return(stream);
        }
        public async Task <object> MigrateProjectJsonToPackageReferenceAsync(string projectUniqueName)
        {
            const string eventName = nameof(IVsProjectJsonToPackageReferenceMigrator) + "." + nameof(MigrateProjectJsonToPackageReferenceAsync);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName);

            try
            {
                if (string.IsNullOrEmpty(projectUniqueName))
                {
                    throw new ArgumentNullException(nameof(projectUniqueName));
                }

                if (!File.Exists(projectUniqueName))
                {
                    throw new FileNotFoundException(string.Format(VsResources.Error_FileNotExists, projectUniqueName));
                }

                return(await MigrateProjectToPackageRefAsync(projectUniqueName));
            }
            catch (Exception ex)
            {
                await _telemetryProvider.PostFaultAsync(ex, nameof(VsProjectJsonToPackageReferenceMigrator));

                throw;
            }
        }
        private bool IsPackageInstalled(Project project, string packageId, NuGetVersion nugetVersion)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId");
            }

            // We simply use ThreadHelper.JoinableTaskFactory.Run instead of PumpingJTF.Run, unlike,
            // VsPackageInstaller and VsPackageUninstaller. Because, no powershell scripts get executed
            // as part of the operations performed below. Powershell scripts need to be executed on the
            // pipeline execution thread and they might try to access DTE. Doing that under
            // ThreadHelper.JoinableTaskFactory.Run will consistently result in a hang
            return(_threadingService.JoinableTaskFactory.Run(async delegate
            {
                try
                {
                    IEnumerable <PackageReference> installedPackageReferences = await GetInstalledPackageReferencesAsync(project);

                    return PackageServiceUtilities.IsPackageInList(installedPackageReferences, packageId, nugetVersion);
                }
                catch (Exception exception)
                {
                    await _telemetryProvider.PostFaultAsync(exception, typeof(VsPackageInstallerServices).FullName);
                    throw;
                }
            }));
        }
        public async Task <bool> ExecuteInitScriptAsync(string packageId, string packageVersion)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(packageId));
            }

            if (string.IsNullOrEmpty(packageVersion))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(packageVersion));
            }

            // Exceptions from parsing package id or version should not be logged as faults
            var version         = new NuGetVersion(packageVersion);
            var packageIdentity = new PackageIdentity(packageId, version);

            try
            {
                return(await _scriptExecutor.ExecuteInitScriptAsync(packageIdentity));
            }
            catch (Exception exception)
            {
                await _telemetryProvider.PostFaultAsync(exception, typeof(VsGlobalPackagesInitScriptExecutor).FullName);

                throw;
            }
        }
        public async Task <InstalledPackagesResult> GetInstalledPackagesAsync(Guid projectId, CancellationToken cancellationToken)
        {
            const string etwEventName = nameof(INuGetProjectService) + "." + nameof(GetInstalledPackagesAsync);
            var          eventData    = new GetInstalledPackagesAsyncEventData()
            {
                Project = projectId
            };

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(etwEventName, eventData);

            try
            {
                // Just in case we're on the UI thread, switch to background thread. Very low cost (does not schedule new task) if already on background thread.
                await TaskScheduler.Default;

                NuGetProject project = await _solutionManager.GetNuGetProjectAsync(projectId.ToString());

                if (project == null)
                {
                    return(NuGetContractsFactory.CreateInstalledPackagesResult(InstalledPackageResultStatus.ProjectNotReady, packages: null));
                }

                InstalledPackageResultStatus status;
                IReadOnlyCollection <NuGetInstalledPackage> installedPackages;

                switch (project)
                {
                case BuildIntegratedNuGetProject packageReferenceProject:
                    (status, installedPackages) = await GetInstalledPackagesAsync(packageReferenceProject, cancellationToken);

                    break;

                case MSBuildNuGetProject packagesConfigProject:
                    (status, installedPackages) = await GetInstalledPackagesAsync(packagesConfigProject, cancellationToken);

                    break;

                default:
                    (status, installedPackages) = await GetInstalledPackagesAsync(project, cancellationToken);

                    break;
                }

                return(NuGetContractsFactory.CreateInstalledPackagesResult(status, installedPackages));
            }
            catch (Exception exception)
            {
                var extraProperties = new Dictionary <string, object>();
                extraProperties["projectId"] = projectId.ToString();
                await _telemetryProvider.PostFaultAsync(exception, typeof(NuGetProjectService).FullName, extraProperties : extraProperties);

                throw;
            }
        }
        /// <summary>
        /// True if all projects have been nominated and the restore worker has completed all work.
        /// </summary>
        public async Task <bool> IsRestoreCompleteAsync(CancellationToken token)
        {
            const string eventName = nameof(IVsSolutionRestoreStatusProvider) + "." + nameof(IsRestoreCompleteAsync);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName);

            try
            {
                var complete = true;

                // Check if the solution is open, if there are no projects then consider it restored.
                if (await _solutionManager.Value.IsSolutionOpenAsync())
                {
                    var graphContext = new DependencyGraphCacheContext();
                    var projects     = (await _solutionManager.Value.GetNuGetProjectsAsync()).AsList();

                    // It could be that the project added to the solution has not yet been updated.
                    if (projects == null || projects.Count == 0)
                    {
                        return(false);
                    }

                    // Empty solutions with no projects are considered restored.
                    foreach (var project in projects)
                    {
                        token.ThrowIfCancellationRequested();

                        // Check if the project has a spec to see if nomination is complete.
                        complete &= await HasSpecAsync(project, graphContext);
                    }

                    // Check if the restore worker is currently active.
                    complete &= !_restoreWorker.Value.IsRunning;
                }

                return(complete);
            }
            catch (Exception ex)
            {
                await _telemetryProvider.PostFaultAsync(ex, nameof(VsSolutionRestoreStatusProvider));

                throw;
            }
        }
        public async Task <bool> ExecuteInitScriptAsync(string packageId, string packageVersion)
        {
            const string eventName = nameof(IVsGlobalPackagesInitScriptExecutor) + "." + nameof(ExecuteInitScriptAsync);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName,
                                                                           new
            {
                PackageId      = packageId,
                PackageVersion = packageVersion
            });

            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(packageId));
            }

            if (string.IsNullOrEmpty(packageVersion))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(packageVersion));
            }

            // Exceptions from parsing package id or version should not be logged as faults
            var version         = new NuGetVersion(packageVersion);
            var packageIdentity = new PackageIdentity(packageId, version);

            try
            {
                return(await _scriptExecutor.ExecuteInitScriptAsync(packageIdentity));
            }
            catch (Exception exception)
            {
                await _telemetryProvider.PostFaultAsync(exception, typeof(VsGlobalPackagesInitScriptExecutor).FullName);

                throw;
            }
        }