Ejemplo n.º 1
0
        public async Task GetMetadataAsync_WhenArgumentsAreValid_ReturnsProjectMetadata()
        {
            var    serviceBroker         = new Mock <IServiceBroker>();
            var    projectManagerService = new Mock <INuGetProjectManagerService>();
            var    project        = new Mock <IProjectContextInfo>();
            string projectId      = Guid.NewGuid().ToString();
            var    expectedResult = Mock.Of <IProjectMetadataContextInfo>();

            project.SetupGet(x => x.ProjectId)
            .Returns(projectId);

            projectManagerService.Setup(
                x => x.GetMetadataAsync(
                    It.Is <string>(id => string.Equals(projectId, id)),
                    It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <IProjectMetadataContextInfo>(expectedResult));

            serviceBroker.Setup(
#pragma warning disable ISB001 // Dispose of proxies
                x => x.GetProxyAsync <INuGetProjectManagerService>(
                    It.Is <ServiceRpcDescriptor>(descriptor => descriptor == NuGetServices.ProjectManagerService),
                    It.IsAny <ServiceActivationOptions>(),
                    It.IsAny <CancellationToken>()))
#pragma warning restore ISB001 // Dispose of proxies
            .Returns(new ValueTask <INuGetProjectManagerService>(projectManagerService.Object));

            IProjectMetadataContextInfo actualResult = await IProjectContextInfoExtensions.GetMetadataAsync(
                project.Object,
                serviceBroker.Object,
                CancellationToken.None);

            Assert.Same(expectedResult, actualResult);
        }
Ejemplo n.º 2
0
        // Returns the list of frameworks that we need to pass to the server during search
        public async Task <IReadOnlyCollection <string> > GetSupportedFrameworksAsync()
        {
            var frameworks = new HashSet <NuGetFramework>();

            foreach (IProjectContextInfo project in Projects)
            {
                if (project.ProjectStyle == ProjectModel.ProjectStyle.PackageReference)
                {
                    IReadOnlyCollection <NuGetFramework> targetFrameworks = await project.GetTargetFrameworksAsync(ServiceBroker, CancellationToken.None);

                    foreach (NuGetFramework targetFramework in targetFrameworks)
                    {
                        frameworks.Add(targetFramework);
                    }
                }
                else
                {
                    IProjectMetadataContextInfo projectMetadata = await project.GetMetadataAsync(
                        ServiceBroker,
                        CancellationToken.None);

                    NuGetFramework framework = projectMetadata.TargetFramework;

                    if (framework is null)
                    {
                        IReadOnlyCollection <NuGetFramework> supportedFrameworks = projectMetadata.SupportedFrameworks;

                        if (supportedFrameworks != null && supportedFrameworks.Count > 0)
                        {
                            foreach (NuGetFramework supportedFramework in supportedFrameworks)
                            {
                                if (supportedFramework.IsAny)
                                {
                                    return(Array.Empty <string>());
                                }

                                frameworks.Add(supportedFramework);
                            }
                        }
                    }
                    else
                    {
                        if (framework.IsAny)
                        {
                            return(Array.Empty <string>());
                        }

                        if (framework.IsSpecificFramework)
                        {
                            frameworks.Add(framework);
                        }
                    }
                }
            }

            return(frameworks.Select(framework => framework.ToString())
                   .ToArray());
        }
        public static async ValueTask <string?> GetUniqueNameOrNameAsync(
            this IProjectContextInfo projectContextInfo,
            IServiceBroker serviceBroker,
            CancellationToken cancellationToken)
        {
            IProjectMetadataContextInfo metadata = await GetMetadataAsync(projectContextInfo, serviceBroker, cancellationToken);

            return(metadata.UniqueName ?? metadata.Name);
        }
        public void SerializeThenDeserialize_WithValidArguments_RoundTrips(IProjectMetadataContextInfo expectedResult)
        {
            IProjectMetadataContextInfo actualResult = SerializeThenDeserialize(IProjectMetadataContextInfoFormatter.Instance, expectedResult);

            Assert.Equal(expectedResult.FullPath, actualResult.FullPath);
            Assert.Equal(expectedResult.Name, actualResult.Name);
            Assert.Equal(expectedResult.ProjectId, actualResult.ProjectId);
            Assert.Equal(expectedResult.SupportedFrameworks, actualResult.SupportedFrameworks);
            Assert.Equal(expectedResult.TargetFramework, actualResult.TargetFramework);
            Assert.Equal(expectedResult.UniqueName, actualResult.UniqueName);
        }
Ejemplo n.º 5
0
        private PackageManagerControl GetProjectPackageManagerControl(string projectUniqueName)
        {
            return(NuGetUIThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var uiShell = ServiceLocator.GetGlobalService <SVsUIShell, IVsUIShell>();
                foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
                {
                    object docView;
                    var hr = windowFrame.GetProperty(
                        (int)__VSFPROPID.VSFPROPID_DocView,
                        out docView);
                    if (hr == VSConstants.S_OK &&
                        docView is PackageManagerWindowPane)
                    {
                        var packageManagerWindowPane = (PackageManagerWindowPane)docView;
                        if (packageManagerWindowPane.Model.IsSolution)
                        {
                            // the window is the solution package manager
                            continue;
                        }

                        var projects = packageManagerWindowPane.Model.Context.Projects;
                        if (projects.Count() != 1)
                        {
                            continue;
                        }

                        IProjectContextInfo existingProject = projects.First();
                        IServiceBroker serviceBroker = packageManagerWindowPane.Model.Context.ServiceBroker;
                        IProjectMetadataContextInfo projectMetadata = await existingProject.GetMetadataAsync(
                            serviceBroker,
                            CancellationToken.None);

                        if (string.Equals(projectMetadata.Name, projectUniqueName, StringComparison.OrdinalIgnoreCase))
                        {
                            var packageManagerControl = VsUtility.GetPackageManagerControl(windowFrame);
                            if (packageManagerControl != null)
                            {
                                return packageManagerControl;
                            }
                        }
                    }
                }

                return null;
            }));
        }
Ejemplo n.º 6
0
        // Returns the list of frameworks that we need to pass to the server during search
        public async Task <IEnumerable <string> > GetSupportedFrameworksAsync()
        {
            var frameworks = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (IProjectContextInfo project in Projects)
            {
                IProjectMetadataContextInfo projectMetadata = await project.GetMetadataAsync(
                    ServiceBroker,
                    CancellationToken.None);

                NuGetFramework framework = projectMetadata.TargetFramework;

                if (framework != null)
                {
                    if (framework.IsAny)
                    {
                        // One of the project's target framework is AnyFramework. In this case,
                        // we don't need to pass the framework filter to the server.
                        return(Enumerable.Empty <string>());
                    }

                    if (framework.IsSpecificFramework)
                    {
                        frameworks.Add(framework.DotNetFrameworkName);
                    }
                }
                else
                {
                    // we also need to process SupportedFrameworks
                    IReadOnlyCollection <NuGetFramework> supportedFrameworks = projectMetadata.SupportedFrameworks;

                    if (supportedFrameworks != null && supportedFrameworks.Count > 0)
                    {
                        foreach (var f in supportedFrameworks)
                        {
                            if (f.IsAny)
                            {
                                return(Enumerable.Empty <string>());
                            }

                            frameworks.Add(f.DotNetFrameworkName);
                        }
                    }
                }
            }

            return(frameworks);
        }
Ejemplo n.º 7
0
        private async Task <IVsWindowFrame> FindExistingWindowFrameAsync(Project project)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));

            foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
            {
                object docView;
                var    hr = windowFrame.GetProperty(
                    (int)__VSFPROPID.VSFPROPID_DocView,
                    out docView);
                if (hr == VSConstants.S_OK &&
                    docView is PackageManagerWindowPane)
                {
                    var packageManagerWindowPane = (PackageManagerWindowPane)docView;
                    if (packageManagerWindowPane.Model.IsSolution)
                    {
                        // the window is the solution package manager
                        continue;
                    }

                    var projects = packageManagerWindowPane.Model.Context.Projects;
                    if (projects.Count() != 1)
                    {
                        continue;
                    }

                    IProjectContextInfo existingProject = projects.First();
                    IServiceBroker      serviceBroker   = await ServiceBrokerProvider.Value.GetAsync();

                    IProjectMetadataContextInfo projectMetadata = await existingProject.GetMetadataAsync(
                        serviceBroker,
                        CancellationToken.None);

                    if (string.Equals(projectMetadata.Name, project.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(windowFrame);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        public async ValueTask <string?> GetUniqueNameOrNameAsync(CancellationToken cancellationToken)
        {
            IProjectMetadataContextInfo metadata = await GetMetadataAsync(cancellationToken);

            return(metadata.UniqueName ?? metadata.Name);
        }