private async Task <ViewCompilerWorkItem> CreateRuntimeCompilationWorkItem(string templateKey)
        {
            RazorLightProjectItem projectItem = null;

            if (_razorLightOptions.DynamicTemplates.TryGetValue(templateKey, out string templateContent))
            {
                projectItem = new TextSourceRazorProjectItem(templateKey, templateContent);
            }
            else
            {
                string normalizedKey = GetNormalizedKey(templateKey);
                projectItem = await _razorProject.GetItemAsync(normalizedKey);
            }

            if (!projectItem.Exists)
            {
                throw new TemplateNotFoundException($"Project can not find template with key {projectItem.Key}");
            }

            return(new ViewCompilerWorkItem()
            {
                SupportsCompilation = true,

                ProjectItem = projectItem,
                NormalizedKey = projectItem.Key,
                ExpirationToken = projectItem.ExpirationToken,
            });
        }
        public void Ensure_ConstructorParams_Applied()
        {
            string templateKey = "key";
            string content     = "content";

            var item = new TextSourceRazorProjectItem(templateKey, content);

            Assert.NotNull(item);
            Assert.Equal(templateKey, item.Key);
            Assert.Equal(content, item.Content);
        }
Exemple #3
0
        public async Task GetImports_Returns_EmptyCollection_On_Empty_Project_WhenResolving_Content_ByKey()
        {
            //Assign
            var generator = new RazorSourceGenerator(RazorEngine.Create(), new EmbeddedRazorProject(typeof(Root)));

            //Act
            var projectItem = new TextSourceRazorProjectItem("key", "some content");
            var result      = await generator.GetImportsAsync(projectItem);

            Assert.NotNull(result);
            Assert.Empty(result);
        }
Exemple #4
0
        public async Task Return_Empty_Imports_ForTextSource_ProjectItem()
        {
            //Assign
            var generator = new RazorSourceGenerator(RazorEngine.Create(), new EmbeddedRazorProject(typeof(Root)));

            //Act
            var projectItem = new TextSourceRazorProjectItem("key", "some content");
            IEnumerable <RazorSourceDocument> result = await generator.GetImportsAsync(projectItem);

            //Assert
            Assert.NotNull(result);
            Assert.Empty(result);
        }
        public void Read_Returns_Content()
        {
            string content = "Test content here";

            var item = new TextSourceRazorProjectItem("key", content);

            string projectContent = null;

            using (var stringWriter = new StreamReader(item.Read()))
            {
                projectContent = stringWriter.ReadToEnd();
            }

            Assert.NotNull(projectContent);
            Assert.Equal(projectContent, content);
        }
Exemple #6
0
        private async Task <ViewCompilerWorkItem> CreateRuntimeCompilationWorkItem(string normalizedKey)
        {
            RazorLightProjectItem projectItem = null;

            if (_razorLightOptions.DynamicTemplates.TryGetValue(normalizedKey, out string templateContent))
            {
                projectItem = new TextSourceRazorProjectItem(normalizedKey, templateContent);
            }
            else
            {
                projectItem = await _razorProject.GetItemAsync(normalizedKey);
            }

            if (!projectItem.Exists)
            {
                // If the file doesn't exist, we can't do compilation right now - we still want to cache
                // the fact that we tried. This will allow us to retrigger compilation if the view file
                // is added.
                return(new ViewCompilerWorkItem()
                {
                    // We don't have enough information to compile
                    SupportsCompilation = false,

                    Descriptor = new CompiledTemplateDescriptor()
                    {
                        TemplateKey = normalizedKey,
                        ExpirationToken = projectItem.ExpirationToken,
                    },

                    // We can try again if the file gets created.
                    ExpirationToken = projectItem.ExpirationToken,
                });
            }

            return(new ViewCompilerWorkItem()
            {
                SupportsCompilation = true,

                ProjectItem = projectItem,
                NormalizedKey = normalizedKey,
                ExpirationToken = projectItem.ExpirationToken,
            });
        }
        public async Task <TemplateFactoryResult> CreateFactoryAsync(string templateKey)
        {
            if (templateKey == null)
            {
                throw new ArgumentNullException(nameof(templateKey));
            }

            IGeneratedRazorTemplate razorTemplate = null;

            if (options.DynamicTemplates.TryGetValue(templateKey, out string templateContent))
            {
                var projectItem = new TextSourceRazorProjectItem(templateKey, templateContent);
                razorTemplate = await sourceGenerator.GenerateCodeAsync(projectItem).ConfigureAwait(false);
            }
            else
            {
                razorTemplate = await sourceGenerator.GenerateCodeAsync(templateKey).ConfigureAwait(false);
            }

            return(await CompileAsync(razorTemplate));
        }
        public void Exist_AlwaysReturnsTrue()
        {
            var item = new TextSourceRazorProjectItem("some", "some");

            Assert.True(item.Exists);
        }