Esempio n. 1
0
        /// <summary>
        /// Parses the template specified by <paramref name="projectItem"/>.
        /// </summary>
        /// <param name="projectItem">The <see cref="RazorLightProjectItem"/>.</param>
        /// <returns>The <see cref="IGeneratedRazorTemplate"/>.</returns>
        public async Task <IGeneratedRazorTemplate> GenerateCodeAsync(RazorLightProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            if (!projectItem.Exists)
            {
                throw new InvalidOperationException($"{nameof(RazorLightProjectItem)} of type " +
                                                    $"{projectItem.GetType().FullName} with key {projectItem.Key} does not exist.");
            }

            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);
            }

            return(new GeneratedRazorTemplate(projectItem, document));
        }
Esempio n. 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 void CodeGeneration_Runtime_LargeStaticFile()
        {
            var codeDocument = ProjectEngine.Process(MSN);
            var generated    = codeDocument.GetCSharpDocument();

            if (generated.Diagnostics.Count != 0)
            {
                throw new Exception("Error!" + Environment.NewLine + string.Join(Environment.NewLine, generated.Diagnostics));
            }
        }