// 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.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
///
/// Entry point to parse Razor files and generate code.
///
public class RazorTemplateEngine
{
private RazorTemplateEngineOptions _options;
///
/// Initializes a new instance of .
///
/// The .
/// The .
public RazorTemplateEngine(
RazorEngine engine,
RazorProject project)
{
if (engine == null)
{
throw new ArgumentNullException(nameof(engine));
}
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
Engine = engine;
Project = project;
_options = new RazorTemplateEngineOptions();
}
///
/// Gets the .
///
public RazorEngine Engine { get; }
///
/// Gets the .
///
public RazorProject Project { get; }
///
/// Options to configure .
///
public RazorTemplateEngineOptions Options
{
get
{
return _options;
}
set
{
_options = value ?? throw new ArgumentNullException(nameof(value));
}
}
///
/// Parses the template specified by the project item .
///
/// The template path.
/// The .
public RazorCSharpDocument GenerateCode(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var projectItem = Project.GetItem(path);
return GenerateCode(projectItem);
}
///
/// Parses the template specified by .
///
/// The .
/// The .
public RazorCSharpDocument GenerateCode(RazorProjectItem projectItem)
{
if (projectItem == null)
{
throw new ArgumentNullException(nameof(projectItem));
}
if (!projectItem.Exists)
{
throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.FilePath));
}
var codeDocument = CreateCodeDocument(projectItem);
return GenerateCode(codeDocument);
}
///
/// Parses the template specified by .
///
/// The .
/// The .
public virtual RazorCSharpDocument GenerateCode(RazorCodeDocument codeDocument)
{
if (codeDocument == null)
{
throw new ArgumentNullException(nameof(codeDocument));
}
Engine.Process(codeDocument);
return codeDocument.GetCSharpDocument();
}
///
/// Generates a for the specified .
///
/// The template path.
/// The created .
public virtual RazorCodeDocument CreateCodeDocument(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var projectItem = Project.GetItem(path);
return CreateCodeDocument(projectItem);
}
///
/// Generates a for the specified .
///
/// The .
/// The created .
public virtual RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
{
if (projectItem == null)
{
throw new ArgumentNullException(nameof(projectItem));
}
if (!projectItem.Exists)
{
throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.FilePath));
}
var source = RazorSourceDocument.ReadFrom(projectItem);
var imports = GetImports(projectItem);
return RazorCodeDocument.Create(source, imports);
}
///
/// Gets that are applicable to the specified .
///
/// The template path.
/// The sequence of applicable .
public IEnumerable GetImports(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var projectItem = Project.GetItem(path);
return GetImports(projectItem);
}
///
/// Gets that are applicable to the specified .
///
/// The .
/// The sequence of applicable .
public virtual IEnumerable GetImports(RazorProjectItem projectItem)
{
if (projectItem == null)
{
throw new ArgumentNullException(nameof(projectItem));
}
var result = new List();
var importProjectItems = GetImportItems(projectItem);
foreach (var importItem in importProjectItems)
{
if (importItem.Exists)
{
// We want items in descending order. FindHierarchicalItems returns items in ascending order.
result.Insert(0, RazorSourceDocument.ReadFrom(importItem));
}
}
if (Options.DefaultImports != null)
{
result.Insert(0, Options.DefaultImports);
}
return result;
}
///
/// Gets the sequence of imports with the name specified by
/// that apply to .
///
/// The path to look up import items for.
/// A sequence of instances that apply to the
/// .
public IEnumerable GetImportItems(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var projectItem = Project.GetItem(path);
return GetImportItems(projectItem);
}
///
/// Gets the sequence of imports with the name specified by
/// that apply to .
///
/// The to look up import items for.
/// A sequence of instances that apply to the
/// .
public virtual IEnumerable GetImportItems(RazorProjectItem projectItem)
{
var importsFileName = Options.ImportsFileName;
if (!string.IsNullOrEmpty(importsFileName))
{
return Project.FindHierarchicalItems(projectItem.FilePath, importsFileName);
}
return Enumerable.Empty();
}
}
}