Exemplo 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));
        }
Exemplo n.º 2
0
        internal async Task <TemplateNotFoundException> CreateTemplateNotFoundException(RazorLightProjectItem projectItem)
        {
            var msg = $"{nameof(RazorLightProjectItem)} of type {projectItem.GetType().FullName} with key {projectItem.Key} could not be found by the " +
                      $"{ nameof(RazorLightProject)} of type { _razorProject.GetType().FullName} and does not exist in dynamic templates. ";

            var propNames = $"\"{nameof(TemplateNotFoundException.KnownDynamicTemplateKeys)}\" and \"{nameof(TemplateNotFoundException.KnownProjectTemplateKeys)}\"";

            if (_razorLightOptions.EnableDebugMode ?? false)
            {
                msg += $"See the {propNames} properties for known template keys.";

                var dynamicKeys = _razorLightOptions.DynamicTemplates.Keys.ToList();

                var projectKeys = await _razorProject.GetKnownKeysAsync();

                projectKeys = projectKeys == null?Enumerable.Empty <string>() : projectKeys.ToList();

                return(new TemplateNotFoundException(msg, dynamicKeys, projectKeys));
            }
            else
            {
                msg += $"Set {nameof(RazorLightOptions)}.{nameof(RazorLightOptions.EnableDebugMode)} to true to allow " +
                       $"the {propNames} properties on this exception to be set.";

                return(new TemplateNotFoundException(msg));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates a <see cref="RazorCodeDocument"/> for the specified <paramref name="projectItem"/>.
        /// </summary>
        /// <param name="projectItem">The <see cref="RazorLightProjectItem"/>.</param>
        /// <returns>The created <see cref="RazorCodeDocument"/>.</returns>
        public virtual async Task <RazorCodeDocument> CreateCodeDocumentAsync(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.");
            }

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

                return(RazorCodeDocument.Create(source, imports));
            }
        }