// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Linq; using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language { /// /// Extension methods to . /// public static class RazorEngineBuilderExtensions { /// /// Adds the specified . /// /// The . /// The to add. /// The . public static IRazorEngineBuilder AddDirective(this IRazorEngineBuilder builder, DirectiveDescriptor directive) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (directive == null) { throw new ArgumentNullException(nameof(directive)); } var directiveFeature = GetDirectiveFeature(builder); directiveFeature.Directives.Add(directive); return builder; } /// /// Adds the specified . /// /// The . /// The to add. /// The . public static IRazorEngineBuilder AddTargetExtension(this IRazorEngineBuilder builder, ICodeTargetExtension extension) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (extension == null) { throw new ArgumentNullException(nameof(extension)); } var targetExtensionFeature = GetTargetExtensionFeature(builder); targetExtensionFeature.TargetExtensions.Add(extension); return builder; } /// /// Sets the base type for generated types. /// /// The . /// The name of the base type. /// The . public static IRazorEngineBuilder SetBaseType(this IRazorEngineBuilder builder, string baseType) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var configurationFeature = GetDefaultDocumentClassifierPassFeature(builder); configurationFeature.ConfigureClass.Add((document, @class) => @class.BaseType = baseType); return builder; } /// /// Registers a class configuration delegate that gets invoked during code generation. /// /// The . /// invoked to configure /// during code generation. /// The . public static IRazorEngineBuilder ConfigureClass( this IRazorEngineBuilder builder, Action configureClass) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (configureClass == null) { throw new ArgumentNullException(nameof(configureClass)); } var configurationFeature = GetDefaultDocumentClassifierPassFeature(builder); configurationFeature.ConfigureClass.Add(configureClass); return builder; } /// /// Sets the namespace for generated types. /// /// The . /// The name of the namespace. /// The . public static IRazorEngineBuilder SetNamespace(this IRazorEngineBuilder builder, string namespaceName) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var configurationFeature = GetDefaultDocumentClassifierPassFeature(builder); configurationFeature.ConfigureNamespace.Add((document, @namespace) => @namespace.Content = namespaceName); return builder; } private static IRazorDirectiveFeature GetDirectiveFeature(IRazorEngineBuilder builder) { var directiveFeature = builder.Features.OfType().FirstOrDefault(); if (directiveFeature == null) { directiveFeature = new DefaultRazorDirectiveFeature(); builder.Features.Add(directiveFeature); } return directiveFeature; } private static IRazorTargetExtensionFeature GetTargetExtensionFeature(IRazorEngineBuilder builder) { var targetExtensionFeature = builder.Features.OfType().FirstOrDefault(); if (targetExtensionFeature == null) { targetExtensionFeature = new DefaultRazorTargetExtensionFeature(); builder.Features.Add(targetExtensionFeature); } return targetExtensionFeature; } private static DefaultDocumentClassifierPassFeature GetDefaultDocumentClassifierPassFeature(IRazorEngineBuilder builder) { var configurationFeature = builder.Features.OfType().FirstOrDefault(); if (configurationFeature == null) { configurationFeature = new DefaultDocumentClassifierPassFeature(); builder.Features.Add(configurationFeature); } return configurationFeature; } } }