From 7b53ba1f6be9b3759cf22a43c1e05477bda2a13f Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 13 Mar 2017 09:45:50 -0700 Subject: [PATCH] Reacting to https://github.com/aspnet/Razor/issues/1063 --- .../MvcViewDocumentClassifierPass.cs | 2 +- .../RazorPageDocumentClassifierPass.cs | 2 +- .../DefaultRoslynCompilationService.cs | 10 +++---- .../Internal/RazorCompilationService.cs | 2 +- .../PageActionDescriptorProvider.cs | 4 +-- .../InjectDirectiveTest.cs | 11 ++----- .../ModelDirectiveTest.cs | 11 ++----- .../ModelExpressionPassTest.cs | 11 ++----- .../MvcViewDocumentClassifierPassTest.cs | 8 ++--- .../RazorPageDocumentClassifierPassTest.cs | 8 ++--- .../ViewComponentTagHelperPassTest.cs | 11 ++----- .../DefaultRoslynCompilationServiceTest.cs | 30 +++++-------------- .../Internal/RazorCompilationServiceTest.cs | 2 +- 13 files changed, 31 insertions(+), 81 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcViewDocumentClassifierPass.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcViewDocumentClassifierPass.cs index a8c38782fb..81fb630d7f 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcViewDocumentClassifierPass.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcViewDocumentClassifierPass.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host ClassDeclarationIRNode @class, RazorMethodDeclarationIRNode method) { - var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.Filename; + var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FileName; base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method); @class.Name = ClassName.GetClassNameFromPath(filePath); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Host/RazorPageDocumentClassifierPass.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Host/RazorPageDocumentClassifierPass.cs index ee104cc408..3996062968 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Host/RazorPageDocumentClassifierPass.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Host/RazorPageDocumentClassifierPass.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host ClassDeclarationIRNode @class, RazorMethodDeclarationIRNode method) { - var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.Filename; + var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FileName; base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method); @class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page"; diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs index af09864471..09446264ef 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs @@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal throw new ArgumentNullException(nameof(codeDocument)); } - _logger.GeneratedCodeToAssemblyCompilationStart(codeDocument.Source.Filename); + _logger.GeneratedCodeToAssemblyCompilationStart(codeDocument.Source.FileName); var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0; @@ -96,7 +96,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal var assembly = LoadAssembly(assemblyStream, pdbStream); var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested); - _logger.GeneratedCodeToAssemblyCompilationEnd(codeDocument.Source.Filename, startTimestamp); + _logger.GeneratedCodeToAssemblyCompilationEnd(codeDocument.Source.FileName, startTimestamp); return new CompilationResult(type); } @@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { if (diagnostic.Location == Location.None) { - return codeDocument.Source.Filename; + return codeDocument.Source.FileName; } return diagnostic.Location.GetMappedLineSpan().Path; @@ -196,7 +196,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal private static string GetContent(RazorCodeDocument codeDocument, string filePath) { - if (filePath == codeDocument.Source.Filename) + if (filePath == codeDocument.Source.FileName) { var chars = new char[codeDocument.Source.Length]; codeDocument.Source.CopyTo(0, chars, 0, chars.Length); @@ -206,7 +206,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal for (var i = 0; i < codeDocument.Imports.Count; i++) { var import = codeDocument.Imports[i]; - if (filePath == import.Filename) + if (filePath == import.FileName) { var chars = new char[codeDocument.Source.Length]; codeDocument.Source.CopyTo(0, chars, 0, chars.Length); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs index 85f271668a..4fafa26f4a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs @@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal writer.Flush(); stream.Seek(0L, SeekOrigin.Begin); - GlobalImports = RazorSourceDocument.ReadFrom(stream, filename: null, encoding: Encoding.UTF8); + GlobalImports = RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8); } public RazorSourceDocument GlobalImports { get; } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs index 1e06952d10..3b90d9e58e 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs @@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory)) { - if (item.Filename.StartsWith("_")) + if (item.FileName.StartsWith("_")) { // Pages like _PageImports should not be routable. continue; @@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure var routePrefix = item.PathWithoutExtension; model.Selectors.Add(CreateSelectorModel(routePrefix, template)); - if (string.Equals(IndexFileName, item.Filename, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(IndexFileName, item.FileName, StringComparison.OrdinalIgnoreCase)) { var parentDirectoryPath = item.Path; var index = parentDirectoryPath.LastIndexOf('/'); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs index 67a1218830..b66b2b26db 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs @@ -161,15 +161,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host private RazorCodeDocument CreateDocument(string content) { - using (var stream = new MemoryStream()) - { - var bytes = Encoding.UTF8.GetBytes(content); - stream.Write(bytes, 0, bytes.Length); - stream.Seek(0L, SeekOrigin.Begin); - - var source = RazorSourceDocument.ReadFrom(stream, "test.cshtml"); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, "test.cshtml"); + return RazorCodeDocument.Create(source); } private ClassDeclarationIRNode FindClassNode(RazorIRNode node) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs index 5e269bf175..dc8a18c0ad 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs @@ -156,15 +156,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host private RazorCodeDocument CreateDocument(string content) { - using (var stream = new MemoryStream()) - { - var bytes = Encoding.UTF8.GetBytes(content); - stream.Write(bytes, 0, bytes.Length); - stream.Seek(0L, SeekOrigin.Begin); - - var source = RazorSourceDocument.ReadFrom(stream, "test.cshtml"); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, "test.cshtml"); + return RazorCodeDocument.Create(source); } private ClassDeclarationIRNode FindClassNode(RazorIRNode node) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs index 0eda58c8d1..daaf6d0b5a 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs @@ -167,15 +167,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host private RazorCodeDocument CreateDocument(string content) { - using (var stream = new MemoryStream()) - { - var bytes = Encoding.UTF8.GetBytes(content); - stream.Write(bytes, 0, bytes.Length); - stream.Seek(0L, SeekOrigin.Begin); - - var source = RazorSourceDocument.ReadFrom(stream, "test.cshtml"); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, "test.cshtml"); + return RazorCodeDocument.Create(source); } private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcViewDocumentClassifierPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcViewDocumentClassifierPassTest.cs index a13fc414cd..2fe80e60c4 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcViewDocumentClassifierPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcViewDocumentClassifierPassTest.cs @@ -195,12 +195,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host { filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml"); - var bytes = Encoding.UTF8.GetBytes(content); - using (var stream = new MemoryStream(bytes)) - { - var source = RazorSourceDocument.ReadFrom(stream, filePath); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, filePath); + return RazorCodeDocument.Create(source); } private static RazorEngine CreateEngine() => RazorEngine.Create(); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/RazorPageDocumentClassifierPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/RazorPageDocumentClassifierPassTest.cs index 62fbd0776e..66fcd8b6a3 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/RazorPageDocumentClassifierPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/RazorPageDocumentClassifierPassTest.cs @@ -215,12 +215,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host { filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml"); - var bytes = Encoding.UTF8.GetBytes(content); - using (var stream = new MemoryStream(bytes)) - { - var source = RazorSourceDocument.ReadFrom(stream, filePath); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, filePath); + return RazorCodeDocument.Create(source); } private static RazorEngine CreateEngine() diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs index fc3a2ea5c2..e71a1554f9 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs @@ -319,15 +319,8 @@ public class __Generated__TagCloudViewComponentTagHelper : Microsoft.AspNetCore. private RazorCodeDocument CreateDocument(string content) { - using (var stream = new MemoryStream()) - { - var bytes = Encoding.UTF8.GetBytes(content); - stream.Write(bytes, 0, bytes.Length); - stream.Seek(0L, SeekOrigin.Begin); - - var source = RazorSourceDocument.ReadFrom(stream, "test.cshtml"); - return RazorCodeDocument.Create(source); - } + var source = RazorSourceDocument.Create(content, "test.cshtml"); + return RazorCodeDocument.Create(source); } private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs index 3afa05e69b..dcc2051178 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs @@ -28,7 +28,7 @@ public class MyTestType {}"; var compilationService = GetRoslynCompilationService(); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(), "test.cshtml")); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "test.cshtml")); var csharpDocument = new RazorCSharpDocument() { @@ -53,7 +53,7 @@ public class MyTestType {}"; this should fail"; var compilationService = GetRoslynCompilationService(); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(fileContent), viewPath)); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(fileContent, viewPath)); var csharpDocument = new RazorCSharpDocument() { @@ -80,7 +80,7 @@ this should fail"; var content = "this should fail"; var compilationService = GetRoslynCompilationService(); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(fileContent), viewPath)); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(fileContent, viewPath)); var csharpDocument = new RazorCSharpDocument() { @@ -114,7 +114,7 @@ public class MyNonCustomDefinedClass {} var options = GetOptions(); options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE"); var compilationService = GetRoslynCompilationService(options: options); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(), viewPath)); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", viewPath)); var csharpDocument = new RazorCSharpDocument() { @@ -136,7 +136,7 @@ public class MyNonCustomDefinedClass {} var viewPath = "Views/Home/Index"; var generatedCodeFileName = "Generated Code"; var compilationService = GetRoslynCompilationService(); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent("view-content"), viewPath)); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("view-content", viewPath)); var assemblyName = "random-assembly-name"; var diagnostics = new[] @@ -220,7 +220,7 @@ public class MyNonCustomDefinedClass {} var options = GetOptions(c => usedCompilation = c); var compilationService = GetRoslynCompilationService(options: options); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(), "some-relative-path")); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path")); var csharpDocument = new RazorCSharpDocument() { @@ -244,7 +244,7 @@ public class MyNonCustomDefinedClass {} }); var content = "public class MyTestType {}"; var compilationService = GetRoslynCompilationService(options: options); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(), "some-relative-path.cshtml")); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path.cshtml")); var csharpDocument = new RazorCSharpDocument() { @@ -274,7 +274,7 @@ public class MyNonCustomDefinedClass {} var applicationPartManager = new ApplicationPartManager(); var compilationService = GetRoslynCompilationService(applicationPartManager, options); - var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(), "some-relative-path.cshtml")); + var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path.cshtml")); var csharpDocument = new RazorCSharpDocument() { @@ -340,19 +340,5 @@ public class MyNonCustomDefinedClass {} optionsAccessor, NullLoggerFactory.Instance); } - - private static MemoryStream CreateStreamContent(string content = "Hello, World!", Encoding encoding = null) - { - var stream = new MemoryStream(); - encoding = encoding ?? Encoding.UTF8; - using (var writer = new StreamWriter(stream, encoding, bufferSize: 1024, leaveOpen: true)) - { - writer.Write(content); - } - - stream.Seek(0L, SeekOrigin.Begin); - - return stream; - } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs index f5e4fa9b7d..f7cc60b7c4 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal Diagnostics = new List() }); - Assert.Equal(viewPath, document.Source.Filename); // Assert if source file name is the root relative path + Assert.Equal(viewPath, document.Source.FileName); // Assert if source file name is the root relative path }).Verifiable(); var razorService = new RazorCompilationService(