diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs index 48695a6f97..ae92e4089b 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs @@ -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 /// The . /// The collection of imports. /// The . - 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 _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 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); + } } } } diff --git a/src/RazorPageGenerator/Program.cs b/src/RazorPageGenerator/Program.cs index 1aae7c86ef..f3ae757886 100644 --- a/src/RazorPageGenerator/Program.cs +++ b/src/RazorPageGenerator/Program.cs @@ -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; diff --git a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultVisualStudioRazorParserIntegrationTest.cs b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultVisualStudioRazorParserIntegrationTest.cs index 2b07a09545..2102506035 100644 --- a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultVisualStudioRazorParserIntegrationTest.cs +++ b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultVisualStudioRazorParserIntegrationTest.cs @@ -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) { diff --git a/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxTreePartialParserTest.cs b/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxTreePartialParserTest.cs index a35ee3c13a..8b5ee4ef5a 100644 --- a/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxTreePartialParserTest.cs +++ b/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxTreePartialParserTest.cs @@ -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) {