コード例 #1
0
        public FrameworkName ParseFrameworkName(string shortOrFullName)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            const string eventName = nameof(IVsFrameworkParser) + "." + nameof(ParseFrameworkName);
#pragma warning restore CS0618 // Type or member is obsolete
            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName,
                                                                           new
            {
                Framework = shortOrFullName
            });

            if (shortOrFullName == null)
            {
                throw new ArgumentNullException(nameof(shortOrFullName));
            }

            try
            {
                var nuGetFramework = NuGetFramework.Parse(shortOrFullName);
                return(new FrameworkName(nuGetFramework.DotNetFrameworkName));
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkParser).FullName);
                throw;
            }
        }
コード例 #2
0
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            try
            {
                // invoke async operation from within synchronous method
                outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                    async() =>
                {
                    var nuGetProject = await CreateNuGetProjectAsync(projectUniqueName);

                    // It's possible the project isn't a NuGet-compatible project at all.
                    if (nuGetProject == null)
                    {
                        return(null);
                    }

                    return(await CreatePathContextAsync(nuGetProject, CancellationToken.None));
                });

                return(outputPathContext != null);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
                throw;
            }
        }
コード例 #3
0
        public void InstallLatestPackage(
            string source,
            Project project,
            string packageId,
            bool includePrerelease,
            bool ignoreDependencies)
        {
            const string eventName = nameof(IVsPackageInstaller2) + "." + nameof(InstallLatestPackage);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName);
            try
            {
                PumpingJTF.Run(() => InstallPackageAsync(
                                   source,
                                   project,
                                   packageId,
                                   version: null,
                                   includePrerelease: includePrerelease,
                                   ignoreDependencies: ignoreDependencies));
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPackageInstaller).FullName);
                throw;
            }
        }
コード例 #4
0
 public bool IsUserConsentGranted()
 {
     try
     {
         var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);
         return(packageRestoreConsent.IsGranted);
     }
     catch (Exception exception)
     {
         _telemetryProvider.PostFault(exception, typeof(IVsPackageRestorer).FullName);
         throw;
     }
 }
コード例 #5
0
 public IEnumerable <FrameworkName> GetNetStandardFrameworks()
 {
     try
     {
         return(DefaultFrameworkNameProvider
                .Instance
                .GetNetStandardVersions()
                .Select(framework => new FrameworkName(framework.DotNetFrameworkName))
                .ToList());
     }
     catch (Exception exception)
     {
         _telemetryProvider.PostFault(exception, typeof(VsFrameworkCompatibility).FullName);
         throw;
     }
 }
コード例 #6
0
        public VsPathContext(string userPackageFolder, IEnumerable <string> fallbackPackageFolders, INuGetTelemetryProvider telemetryProvider)
        {
            if (userPackageFolder == null)
            {
                throw new ArgumentNullException(nameof(userPackageFolder));
            }

            if (fallbackPackageFolders == null)
            {
                throw new ArgumentNullException(nameof(fallbackPackageFolders));
            }

            _telemetryProvider = telemetryProvider ?? throw new ArgumentNullException(nameof(telemetryProvider));

            try
            {
                _userPackageFolder      = userPackageFolder;
                _fallbackPackageFolders = fallbackPackageFolders.ToList();
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContext).FullName);
                throw;
            }
        }
コード例 #7
0
        public IEnumerable <KeyValuePair <string, string> > GetSources(bool includeUnOfficial, bool includeDisabled)
        {
            var sources = new List <KeyValuePair <string, string> >();

            try
            {
                foreach (var source in _packageSourceProvider.LoadPackageSources())
                {
                    if ((IsOfficial(source) || includeUnOfficial) &&
                        (source.IsEnabled || includeDisabled))
                    {
                        // Name -> Source Uri
                        var pair = new KeyValuePair <string, string>(source.Name, source.Source);
                        sources.Add(pair);
                    }
                }
            }
            catch (Exception ex) when(!IsExpected(ex))
            {
                _telemetryProvider.PostFault(ex, typeof(VsPackageSourceProvider).FullName);
                throw new InvalidOperationException(ex.Message, ex);
            }

            return(sources);
        }
コード例 #8
0
        public IEnumerable <KeyValuePair <string, string> > GetSources(bool includeUnOfficial, bool includeDisabled)
        {
            const string eventName = nameof(IVsPackageSourceProvider) + "." + nameof(GetSources);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName,
                                                                           new
            {
                IncludeUnOfficial = includeUnOfficial,
                IncludeDisabled   = includeDisabled
            });

            try
            {
                var sources = new List <KeyValuePair <string, string> >();

                foreach (var source in _packageSourceProvider.LoadPackageSources())
                {
                    if ((IsOfficial(source) || includeUnOfficial) &&
                        (source.IsEnabled || includeDisabled))
                    {
                        // Name -> Source Uri
                        var pair = new KeyValuePair <string, string>(source.Name, source.Source);
                        sources.Add(pair);
                    }
                }

                return(sources);
            }
            catch (Exception ex) when(!IsExpected(ex))
            {
                _telemetryProvider.PostFault(ex, typeof(VsPackageSourceProvider).FullName);
                throw new InvalidOperationException(ex.Message, ex);
            }
        }
コード例 #9
0
        public bool IsUserConsentGranted()
        {
            const string eventName = nameof(IVsPackageRestorer) + "." + nameof(IsUserConsentGranted);

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

            try
            {
                var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);
                return(packageRestoreConsent.IsGranted);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(IVsPackageRestorer).FullName);
                throw;
            }
        }
コード例 #10
0
        public void UninstallPackage(Project project, string packageId, bool removeDependencies)
        {
            const string eventName = nameof(IVsPackageUninstaller) + "." + nameof(UninstallPackage);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName,
                                                                           new
            {
                // Can't add project information, since it's a COM object that this method might be called on a background thread
                PackageId          = packageId,
                RemoveDependencies = removeDependencies
            });

            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

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

            try
            {
                PumpingJTF.Run(async delegate
                {
                    NuGetPackageManager packageManager =
                        new NuGetPackageManager(
                            _sourceRepositoryProvider,
                            _settings,
                            _solutionManager,
                            _deleteOnRestartManager,
                            _restoreProgressReporter);

                    var uninstallContext = new UninstallationContext(removeDependencies, forceRemove: false);
                    var projectContext   = new VSAPIProjectContext
                    {
                        PackageExtractionContext = new PackageExtractionContext(
                            PackageSaveMode.Defaultv2,
                            PackageExtractionBehavior.XmlDocFileSaveMode,
                            ClientPolicyContext.GetClientPolicy(_settings, NullLogger.Instance),
                            NullLogger.Instance)
                    };

                    // find the project
                    NuGetProject nuGetProject = await _solutionManager.GetOrCreateProjectAsync(project, projectContext);

                    // uninstall the package
                    await packageManager.UninstallPackageAsync(nuGetProject, packageId, uninstallContext, projectContext, CancellationToken.None);
                });
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPackageUninstaller).FullName);
                throw;
            }
        }
コード例 #11
0
        public IEnumerable <FrameworkName> GetNetStandardFrameworks()
        {
            const string eventName = nameof(IVsFrameworkCompatibility) + "." + nameof(GetNetStandardFrameworks);

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

            try
            {
                return(DefaultFrameworkNameProvider
                       .Instance
                       .GetNetStandardVersions()
                       .Select(framework => new FrameworkName(framework.DotNetFrameworkName))
                       .ToList());
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkCompatibility).FullName);
                throw;
            }
        }
コード例 #12
0
        public FrameworkName ParseFrameworkName(string shortOrFullName)
        {
            if (shortOrFullName == null)
            {
                throw new ArgumentNullException(nameof(shortOrFullName));
            }

            try
            {
                var nuGetFramework = NuGetFramework.Parse(shortOrFullName);
                return(new FrameworkName(nuGetFramework.DotNetFrameworkName));
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkParser).FullName);
                throw;
            }
        }
コード例 #13
0
 public void InstallLatestPackage(
     string source,
     Project project,
     string packageId,
     bool includePrerelease,
     bool ignoreDependencies)
 {
     try
     {
         RunJTFWithCorrectContext(project, () => InstallPackageAsync(
                                      source,
                                      project,
                                      packageId,
                                      version: null,
                                      includePrerelease: includePrerelease,
                                      ignoreDependencies: ignoreDependencies));
     }
     catch (Exception exception)
     {
         _telemetryProvider.PostFault(exception, typeof(VsPackageInstaller).FullName);
         throw;
     }
 }
コード例 #14
0
        public void UninstallPackage(Project project, string packageId, bool removeDependencies)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

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

            try
            {
                PumpingJTF.Run(async delegate
                {
                    NuGetPackageManager packageManager =
                        new NuGetPackageManager(
                            _sourceRepositoryProvider,
                            _settings,
                            _solutionManager,
                            _deleteOnRestartManager);

                    var uninstallContext = new UninstallationContext(removeDependencies, forceRemove: false);
                    var projectContext   = new VSAPIProjectContext
                    {
                        PackageExtractionContext = new PackageExtractionContext(
                            PackageSaveMode.Defaultv2,
                            PackageExtractionBehavior.XmlDocFileSaveMode,
                            ClientPolicyContext.GetClientPolicy(_settings, NullLogger.Instance),
                            NullLogger.Instance)
                    };

                    // find the project
                    NuGetProject nuGetProject = await _solutionManager.GetOrCreateProjectAsync(project, projectContext);

                    // uninstall the package
                    await packageManager.UninstallPackageAsync(nuGetProject, packageId, uninstallContext, projectContext, CancellationToken.None);
                });
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPackageUninstaller).FullName);
                throw;
            }
        }
コード例 #15
0
 public bool TryResolvePackageAsset(string packageAssetPath, out string packageDirectoryPath)
 {
     try
     {
         packageDirectoryPath = _referenceLookupIndex[packageAssetPath];
         return(true);
     }
     catch (KeyNotFoundException)
     {
         packageDirectoryPath = null;
         return(false);
     }
     catch (Exception exception)
     {
         _telemetryProvider.PostFault(exception, typeof(VsIndexedPathContext).FullName);
         throw;
     }
 }
コード例 #16
0
        public VsPathContext(NuGetPathContext pathContext, INuGetTelemetryProvider telemetryProvider, string solutionPackageFolder = null)
        {
            if (pathContext == null)
            {
                throw new ArgumentNullException(nameof(pathContext));
            }

            _telemetryProvider = telemetryProvider ?? throw new ArgumentNullException(nameof(telemetryProvider));

            try
            {
                _userPackageFolder      = pathContext.UserPackageFolder;
                _fallbackPackageFolders = pathContext.FallbackPackageFolders;
                _solutionPackageFolder  = solutionPackageFolder;
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContext).FullName);
                throw;
            }
        }
コード例 #17
0
        public bool TryResolvePackageAsset(string packageAssetPath, out string packageDirectoryPath)
        {
            const string eventName = nameof(IVsPathContext) + "." + nameof(TryResolvePackageAsset);

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

            try
            {
                packageDirectoryPath = _referenceLookupIndex[packageAssetPath];
                return(true);
            }
            catch (KeyNotFoundException)
            {
                packageDirectoryPath = null;
                return(false);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsIndexedPathContext).FullName);
                throw;
            }
        }
コード例 #18
0
        public IEnumerable <IVsPackageMetadata> GetInstalledPackages()
        {
            try
            {
                var packages = new HashSet <IVsPackageMetadata>(new VsPackageMetadataComparer());

                return(_threadingService.JoinableTaskFactory.Run(async delegate
                {
                    // Calls may occur in the template wizard before the solution is actually created,
                    // in that case return no projects
                    if (_solutionManager != null &&
                        !string.IsNullOrEmpty(_solutionManager.SolutionDirectory))
                    {
                        //switch to background thread
                        await TaskScheduler.Default;

                        NuGetPackageManager nuGetPackageManager = CreateNuGetPackageManager();

                        foreach (var project in (await _solutionManager.GetNuGetProjectsAsync()))
                        {
                            FallbackPackagePathResolver pathResolver = null;
                            var buildIntegratedProject = project as BuildIntegratedNuGetProject;
                            if (buildIntegratedProject != null)
                            {
                                pathResolver = await GetPackagesPathResolverAsync(buildIntegratedProject);
                            }

                            var installedPackages = await project.GetInstalledPackagesAsync(CancellationToken.None);

                            foreach (var package in installedPackages)
                            {
                                var identity = package.PackageIdentity;

                                if (!identity.HasVersion)
                                {
                                    // Currently we are not supporting floating versions
                                    // because of that we will skip this package
                                    continue;
                                }

                                // find packages using the solution level packages folder
                                string installPath;
                                if (buildIntegratedProject != null)
                                {
                                    installPath = pathResolver.GetPackageDirectory(identity.Id, identity.Version);
                                }
                                else
                                {
                                    installPath = nuGetPackageManager
                                                  .PackagesFolderNuGetProject
                                                  .GetInstalledPath(identity);
                                }

                                var metadata = new VsPackageMetadata(package.PackageIdentity, installPath);

                                packages.Add(metadata);
                            }
                        }
                    }

                    return packages;
                }));
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPackageInstallerServices).FullName);
                throw;
            }
        }