예제 #1
0
        public void TryResolveConfiguration_MvcWithVersion_ReturnsTrue()
        {
            // Arrange
            var context  = BuildContext("/some/path/to/some.dll", "/another/path/to/" + FallbackConfigurationProvider.MvcAssemblyFileName);
            var provider = new TestLegacyConfigurationProvider(MvcAssemblyVersion);
            var expectedConfiguration = FallbackRazorConfiguration.SelectConfiguration(MvcAssemblyVersion);

            // Act
            var result = provider.TryResolveConfiguration(context, out var projectConfiguration);

            // Assert
            Assert.True(result);
            Assert.Same(expectedConfiguration, projectConfiguration.Configuration);
            Assert.Empty(projectConfiguration.Documents);
        }
        public override bool TryResolveConfiguration(ProjectConfigurationProviderContext context, out ProjectConfiguration configuration)
        {
            if (!HasRazorCoreCapability(context))
            {
                configuration = null;
                return(false);
            }

            var    compilationReferences = context.ProjectInstance.GetItems(ReferencePathWithRefAssembliesItemType);
            string mvcReferenceFullPath  = null;

            foreach (var compilationReference in compilationReferences)
            {
                var assemblyFullPath = compilationReference.EvaluatedInclude;
                if (assemblyFullPath.EndsWith(MvcAssemblyFileName, FilePathComparison.Instance))
                {
                    var potentialPathSeparator = assemblyFullPath[assemblyFullPath.Length - MvcAssemblyFileName.Length - 1];
                    if (potentialPathSeparator == '/' || potentialPathSeparator == '\\')
                    {
                        mvcReferenceFullPath = assemblyFullPath;
                        break;
                    }
                }
            }

            if (mvcReferenceFullPath == null)
            {
                configuration = null;
                return(false);
            }

            var version = GetAssemblyVersion(mvcReferenceFullPath);

            if (version == null)
            {
                configuration = null;
                return(false);
            }

            var razorConfiguration = FallbackRazorConfiguration.SelectConfiguration(version);

            configuration = new ProjectConfiguration(razorConfiguration, Array.Empty <OmniSharpHostDocument>(), rootNamespace: null);
            return(true);
        }