Esempio n. 1
0
        private static RazorProjectEngine GetDeclarationProjectEngine(
            SourceGeneratorProjectItem item,
            IEnumerable <SourceGeneratorProjectItem> imports,
            RazorSourceGenerationOptions razorSourceGeneratorOptions)
        {
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(item);
            foreach (var import in imports)
            {
                fileSystem.Add(import);
            }

            var discoveryProjectEngine = RazorProjectEngine.Create(razorSourceGeneratorOptions.Configuration, fileSystem, b =>
            {
                b.Features.Add(new DefaultTypeNameFeature());
                b.Features.Add(new ConfigureRazorCodeGenerationOptions(options =>
                {
                    options.SuppressPrimaryMethodBody = true;
                    options.SuppressChecksum          = true;
                }));

                b.SetRootNamespace(razorSourceGeneratorOptions.RootNamespace);

                CompilerFeatures.Register(b);
                RazorExtensions.Register(b);

                b.SetCSharpLanguageVersion(razorSourceGeneratorOptions.CSharpLanguageVersion);
            });

            return(discoveryProjectEngine);
        }
Esempio n. 2
0
        public void FileName_ReturnsFileNameWithExtension(string path, string expected)
        {
            // Arrange
            var projectItem = new SourceGeneratorProjectItem(
                filePath: path,
                basePath: "/",
                relativePhysicalPath: "/foo",
                fileKind: FileKinds.Legacy,
                additionalText: new TestAdditionalText(string.Empty),
                cssScope: null);

            // Act
            var fileName = projectItem.FileName;

            // Assert
            Assert.Equal(expected, fileName);
        }
Esempio n. 3
0
        public void Extension_ReturnsNullIfFileDoesNotHaveExtension(string path)
        {
            // Arrange
            var projectItem = new SourceGeneratorProjectItem(
                filePath: path,
                basePath: "/views",
                relativePhysicalPath: "/foo",
                fileKind: FileKinds.Legacy,
                additionalText: new TestAdditionalText(string.Empty),
                cssScope: null);

            // Act
            var extension = projectItem.Extension;

            // Assert
            Assert.Null(extension);
        }
        public void Extension_ReturnsFileExtension(string path, string expected)
        {
            // Arrange
            var projectItem = new SourceGeneratorProjectItem(
                filePath: path,
                basePath: "/views",
                relativePhysicalPath: "/foo",
                fileKind: FileKinds.Legacy,
                additionalText: new TestAdditionalText(string.Empty),
                cssScope: null,
                context: new GeneratorExecutionContext());

            // Act
            var extension = projectItem.Extension;

            // Assert
            Assert.Equal(expected, extension);
        }
        public void PathWithoutExtension_ExcludesExtension(string path, string expected)
        {
            // Arrange
            var projectItem = new SourceGeneratorProjectItem(
                filePath: path,
                basePath: "/",
                relativePhysicalPath: "/foo",
                fileKind: FileKinds.Legacy,
                additionalText: new TestAdditionalText(string.Empty),
                cssScope: null,
                context: new GeneratorExecutionContext());

            // Act
            var fileName = projectItem.FilePathWithoutExtension;

            // Assert
            Assert.Equal(expected, fileName);
        }
Esempio n. 6
0
        public void PhysicalPath_ReturnsSourceTextPath()
        {
            // Arrange
            var emptyBasePath = "/";
            var path          = "/foo/bar.cshtml";
            var projectItem   = new SourceGeneratorProjectItem(
                filePath: path,
                basePath: emptyBasePath,
                relativePhysicalPath: "/foo",
                fileKind: FileKinds.Legacy,
                additionalText: new TestAdditionalText(string.Empty),
                cssScope: null);

            // Act
            var physicalPath = projectItem.PhysicalPath;

            // Assert
            Assert.Equal("dummy", physicalPath);
        }
        private static RazorProjectEngine GetGenerationProjectEngine(
            IReadOnlyList <TagHelperDescriptor> tagHelpers,
            SourceGeneratorProjectItem item,
            IEnumerable <SourceGeneratorProjectItem> imports,
            RazorSourceGenerationOptions razorSourceGeneratorOptions)
        {
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(item);
            foreach (var import in imports)
            {
                fileSystem.Add(import);
            }

            var projectEngine = RazorProjectEngine.Create(razorSourceGeneratorOptions.Configuration, fileSystem, b =>
            {
                b.Features.Add(new DefaultTypeNameFeature());
                b.SetRootNamespace(razorSourceGeneratorOptions.RootNamespace);

                b.Features.Add(new ConfigureRazorCodeGenerationOptions(options =>
                {
                    options.SuppressMetadataSourceChecksumAttributes = !razorSourceGeneratorOptions.GenerateMetadataSourceChecksumAttributes;
                    options.SupportLocalizedComponentNames           = razorSourceGeneratorOptions.SupportLocalizedComponentNames;
                }));

                b.Features.Add(new StaticTagHelperFeature {
                    TagHelpers = tagHelpers
                });
                b.Features.Add(new DefaultTagHelperDescriptorProvider());

                CompilerFeatures.Register(b);
                RazorExtensions.Register(b);

                b.SetCSharpLanguageVersion(razorSourceGeneratorOptions.CSharpLanguageVersion);
            });

            return(projectEngine);
        }