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

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

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

            return(new ViewCompilerWorkItem()
            {
                SupportsCompilation = true,
                ProjectItem = projectItem,
                NormalizedKey = projectItem.Key,
                ExpirationToken = projectItem.ExpirationToken,
            });
        }
Beispiel #2
0
        public async Task <IGeneratedRazorTemplate> GenerateCodeAsync(TkRazorProjectItem projectItem)
        {
            TkDebug.AssertArgumentNull(projectItem, nameof(projectItem), this);

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

            RazorCodeDocument codeDocument = await CreateCodeDocumentAsync(projectItem);

            ProjectEngine.Process(codeDocument);

            RazorCSharpDocument document = codeDocument.GetCSharpDocument();

            if (document.Diagnostics.Count > 0)
            {
                var builder = new StringBuilder();
                builder.AppendLine("Failed to generate Razor template. See \"Diagnostics\" property for more details");

                foreach (RazorDiagnostic d in document.Diagnostics)
                {
                    builder.AppendLine($"- {d.GetMessage()}");
                }

                throw new TemplateGenerationException(builder.ToString(), document.Diagnostics, this);
            }

            return(new GeneratedRazorTemplate(projectItem, document));
        }
        public GeneratedRazorTemplate(TkRazorProjectItem projectItem, RazorCSharpDocument cSharpDocument)
        {
            TkDebug.AssertArgumentNull(projectItem, nameof(projectItem), null);
            TkDebug.AssertArgumentNull(cSharpDocument, nameof(cSharpDocument), null);

            ProjectItem    = projectItem;
            CSharpDocument = cSharpDocument;
        }
Beispiel #4
0
        public virtual async Task <RazorCodeDocument> CreateCodeDocumentAsync(TkRazorProjectItem projectItem)
        {
            TkDebug.AssertArgumentNull(projectItem, nameof(projectItem), this);
            TkDebug.AssertArgument(projectItem.Exists, nameof(projectItem),
                                   $"Project can not find template with key {projectItem.Key}", this);

            using (var stream = projectItem.Read())
            {
                RazorSourceDocument source = RazorSourceDocument.ReadFrom(stream, projectItem.Key);
                IEnumerable <RazorSourceDocument> imports = await GetImportsAsync(projectItem);

                return(RazorCodeDocument.Create(source, imports));
            }
        }
Beispiel #5
0
        public async Task <IGeneratedRazorTemplate> GenerateCodeAsync(string key)
        {
            TkDebug.AssertArgumentNullOrEmpty(key, nameof(key), this);

            if (Project == null)
            {
                string _message = "Can not resolve a content for the template \"{0}\" as there is no project set." +
                                  "You can only render a template by passing it's content directly via string using coresponding function overload";

                throw new InvalidOperationException(_message);
            }

            TkRazorProjectItem projectItem = await Project.GetItemAsync(key).ConfigureAwait(false);

            return(await GenerateCodeAsync(projectItem));
        }
        protected virtual CompiledTemplateDescriptor CompileAndEmit(TkRazorProjectItem projectItem)
        {
            Assembly assembly = projectItem.TryLoadAssembly();

            if (assembly == null)
            {
                IGeneratedRazorTemplate generatedTemplate
                         = fRazorSourceGenerator.GenerateCodeAsync(projectItem).GetAwaiter().GetResult();
                assembly = fCompiler.CompileAndEmit(generatedTemplate);
            }

            // Anything we compile from source will use Razor 2.1 and so should have the new metadata.
            var loader    = new RazorCompiledItemLoader();
            var item      = loader.LoadItems(assembly).SingleOrDefault();
            var attribute = assembly.GetCustomAttribute <RazorTemplateAttribute>();

            return(new CompiledTemplateDescriptor()
            {
                Item = item,
                TemplateKey = projectItem.Key,
                TemplateAttribute = attribute
            });
        }
Beispiel #7
0
        public virtual async Task <IEnumerable <RazorSourceDocument> > GetImportsAsync(TkRazorProjectItem projectItem)
        {
            TkDebug.AssertArgumentNull(projectItem, nameof(projectItem), this);

            if (projectItem is TextSourceRazorProjectItem)
            {
                return(Enumerable.Empty <RazorSourceDocument>());
            }

            var result = new List <RazorSourceDocument>();

            IEnumerable <TkRazorProjectItem> importProjectItems = await Project.GetImportsAsync(projectItem.Key);

            foreach (var importItem in importProjectItems)
            {
                if (importItem.Exists)
                {
                    using (var stream = importItem.Read())
                    {
                        result.Insert(0, RazorSourceDocument.ReadFrom(stream, null));
                    }
                }
            }

            if (Namespaces != null)
            {
                RazorSourceDocument namespacesImports = GetNamespacesImports();
                if (namespacesImports != null)
                {
                    result.Insert(0, namespacesImports);
                }
            }

            if (DefaultImports != null)
            {
                result.Insert(0, DefaultImports);
            }

            return(result);
        }