Changed AddDefaultImports to take strings instead of project items.
- Strings here was important because any import added to the system dynamically needs to eventually make its way back to being a project item. With strings we can state that they do exist (have content) but do not have any file paths associated. - Updated all call sites to use the new AddDefaultImports string based api. #2080
This commit is contained in:
parent
1c9088087b
commit
c5e83c61f8
|
|
@ -5,6 +5,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
|
||||
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
||||
|
||||
|
|
@ -151,7 +152,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param>
|
||||
/// <param name="imports">The collection of imports.</param>
|
||||
/// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns>
|
||||
public static RazorProjectEngineBuilder AddDefaultImports(this RazorProjectEngineBuilder builder, params RazorProjectItem[] imports)
|
||||
public static RazorProjectEngineBuilder AddDefaultImports(this RazorProjectEngineBuilder builder, params string[] imports)
|
||||
{
|
||||
if (builder == null)
|
||||
{
|
||||
|
|
@ -204,7 +205,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
private class AdditionalImportsProjectFeature : RazorProjectEngineFeatureBase, IImportProjectFeature
|
||||
{
|
||||
private readonly IImportProjectFeature _existingImportFeature;
|
||||
private readonly RazorProjectItem[] _imports;
|
||||
private readonly IEnumerable<RazorProjectItem> _imports;
|
||||
|
||||
public override RazorProjectEngine ProjectEngine
|
||||
{
|
||||
|
|
@ -216,10 +217,10 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
}
|
||||
}
|
||||
|
||||
public AdditionalImportsProjectFeature(IImportProjectFeature existingImportFeature, params RazorProjectItem[] imports)
|
||||
public AdditionalImportsProjectFeature(IImportProjectFeature existingImportFeature, params string[] imports)
|
||||
{
|
||||
_existingImportFeature = existingImportFeature;
|
||||
_imports = imports;
|
||||
_imports = imports.Select(import => new InMemoryProjectItem(import));
|
||||
}
|
||||
|
||||
public IReadOnlyList<RazorProjectItem> GetImports(RazorProjectItem projectItem)
|
||||
|
|
@ -229,6 +230,36 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
|
||||
return imports;
|
||||
}
|
||||
|
||||
private class InMemoryProjectItem : RazorProjectItem
|
||||
{
|
||||
private readonly byte[] _importBytes;
|
||||
|
||||
public InMemoryProjectItem(string content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(content))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(content));
|
||||
}
|
||||
|
||||
var preamble = Encoding.UTF8.GetPreamble();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(content);
|
||||
|
||||
_importBytes = new byte[preamble.Length + contentBytes.Length];
|
||||
preamble.CopyTo(_importBytes, 0);
|
||||
contentBytes.CopyTo(_importBytes, preamble.Length);
|
||||
}
|
||||
|
||||
public override string BasePath => null;
|
||||
|
||||
public override string FilePath => null;
|
||||
|
||||
public override string PhysicalPath => null;
|
||||
|
||||
public override bool Exists => true;
|
||||
|
||||
public override Stream Read() => new MemoryStream(_importBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,10 @@ Examples:
|
|||
configure(builder);
|
||||
}
|
||||
|
||||
builder.AddDefaultImports(DefaultImportItem.Instance);
|
||||
builder.AddDefaultImports(@"
|
||||
@using System
|
||||
@using System.Threading.Tasks
|
||||
");
|
||||
});
|
||||
return projectEngine;
|
||||
}
|
||||
|
|
@ -153,37 +156,6 @@ Examples:
|
|||
}
|
||||
}
|
||||
|
||||
private class DefaultImportItem : RazorProjectItem
|
||||
{
|
||||
private readonly byte[] _defaultImportBytes;
|
||||
|
||||
private DefaultImportItem()
|
||||
{
|
||||
var preamble = Encoding.UTF8.GetPreamble();
|
||||
var content = @"
|
||||
@using System
|
||||
@using System.Threading.Tasks
|
||||
";
|
||||
var contentBytes = Encoding.UTF8.GetBytes(content);
|
||||
|
||||
_defaultImportBytes = new byte[preamble.Length + contentBytes.Length];
|
||||
preamble.CopyTo(_defaultImportBytes, 0);
|
||||
contentBytes.CopyTo(_defaultImportBytes, preamble.Length);
|
||||
}
|
||||
|
||||
public override string BasePath => null;
|
||||
|
||||
public override string FilePath => null;
|
||||
|
||||
public override string PhysicalPath => null;
|
||||
|
||||
public override bool Exists => true;
|
||||
|
||||
public static DefaultImportItem Instance { get; } = new DefaultImportItem();
|
||||
|
||||
public override Stream Read() => new MemoryStream(_defaultImportBytes);
|
||||
}
|
||||
|
||||
private class FileSystemRazorProjectItemWrapper : RazorProjectItem
|
||||
{
|
||||
private readonly RazorProjectItem _source;
|
||||
|
|
|
|||
|
|
@ -533,7 +533,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
|
|||
{
|
||||
RazorExtensions.Register(builder);
|
||||
|
||||
builder.AddDefaultImports(new TestRazorProjectItem("_TestImports.cshtml") { Content = "@addTagHelper *, Test" });
|
||||
builder.AddDefaultImports("@addTagHelper *, Test");
|
||||
|
||||
if (tagHelpers != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -589,7 +589,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
|
|||
{
|
||||
RazorExtensions.Register(builder);
|
||||
|
||||
builder.AddDefaultImports(new TestRazorProjectItem("_TestImports.cshtml") { Content = "@addTagHelper *, Test" });
|
||||
builder.AddDefaultImports("@addTagHelper *, Test");
|
||||
|
||||
if (tagHelpers != null)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue