This commit is contained in:
Pranav K 2017-03-13 09:45:50 -07:00
parent cd9899363f
commit 7b53ba1f6b
13 changed files with 31 additions and 81 deletions

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
ClassDeclarationIRNode @class, ClassDeclarationIRNode @class,
RazorMethodDeclarationIRNode method) RazorMethodDeclarationIRNode method)
{ {
var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.Filename; var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FileName;
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method); base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
@class.Name = ClassName.GetClassNameFromPath(filePath); @class.Name = ClassName.GetClassNameFromPath(filePath);

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
ClassDeclarationIRNode @class, ClassDeclarationIRNode @class,
RazorMethodDeclarationIRNode method) RazorMethodDeclarationIRNode method)
{ {
var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.Filename; var filePath = codeDocument.GetRelativePath() ?? codeDocument.Source.FileName;
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method); base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page"; @class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page";

View File

@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
throw new ArgumentNullException(nameof(codeDocument)); throw new ArgumentNullException(nameof(codeDocument));
} }
_logger.GeneratedCodeToAssemblyCompilationStart(codeDocument.Source.Filename); _logger.GeneratedCodeToAssemblyCompilationStart(codeDocument.Source.FileName);
var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0; 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 assembly = LoadAssembly(assemblyStream, pdbStream);
var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested); var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested);
_logger.GeneratedCodeToAssemblyCompilationEnd(codeDocument.Source.Filename, startTimestamp); _logger.GeneratedCodeToAssemblyCompilationEnd(codeDocument.Source.FileName, startTimestamp);
return new CompilationResult(type); return new CompilationResult(type);
} }
@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
if (diagnostic.Location == Location.None) if (diagnostic.Location == Location.None)
{ {
return codeDocument.Source.Filename; return codeDocument.Source.FileName;
} }
return diagnostic.Location.GetMappedLineSpan().Path; return diagnostic.Location.GetMappedLineSpan().Path;
@ -196,7 +196,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
private static string GetContent(RazorCodeDocument codeDocument, string filePath) 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]; var chars = new char[codeDocument.Source.Length];
codeDocument.Source.CopyTo(0, chars, 0, chars.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++) for (var i = 0; i < codeDocument.Imports.Count; i++)
{ {
var import = codeDocument.Imports[i]; var import = codeDocument.Imports[i];
if (filePath == import.Filename) if (filePath == import.FileName)
{ {
var chars = new char[codeDocument.Source.Length]; var chars = new char[codeDocument.Source.Length];
codeDocument.Source.CopyTo(0, chars, 0, chars.Length); codeDocument.Source.CopyTo(0, chars, 0, chars.Length);

View File

@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
writer.Flush(); writer.Flush();
stream.Seek(0L, SeekOrigin.Begin); 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; } public RazorSourceDocument GlobalImports { get; }

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{ {
foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory)) foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory))
{ {
if (item.Filename.StartsWith("_")) if (item.FileName.StartsWith("_"))
{ {
// Pages like _PageImports should not be routable. // Pages like _PageImports should not be routable.
continue; continue;
@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
var routePrefix = item.PathWithoutExtension; var routePrefix = item.PathWithoutExtension;
model.Selectors.Add(CreateSelectorModel(routePrefix, template)); 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 parentDirectoryPath = item.Path;
var index = parentDirectoryPath.LastIndexOf('/'); var index = parentDirectoryPath.LastIndexOf('/');

View File

@ -161,15 +161,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
private RazorCodeDocument CreateDocument(string content) private RazorCodeDocument CreateDocument(string content)
{ {
using (var stream = new MemoryStream()) var source = RazorSourceDocument.Create(content, "test.cshtml");
{ return RazorCodeDocument.Create(source);
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);
}
} }
private ClassDeclarationIRNode FindClassNode(RazorIRNode node) private ClassDeclarationIRNode FindClassNode(RazorIRNode node)

View File

@ -156,15 +156,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
private RazorCodeDocument CreateDocument(string content) private RazorCodeDocument CreateDocument(string content)
{ {
using (var stream = new MemoryStream()) var source = RazorSourceDocument.Create(content, "test.cshtml");
{ return RazorCodeDocument.Create(source);
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);
}
} }
private ClassDeclarationIRNode FindClassNode(RazorIRNode node) private ClassDeclarationIRNode FindClassNode(RazorIRNode node)

View File

@ -167,15 +167,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
private RazorCodeDocument CreateDocument(string content) private RazorCodeDocument CreateDocument(string content)
{ {
using (var stream = new MemoryStream()) var source = RazorSourceDocument.Create(content, "test.cshtml");
{ return RazorCodeDocument.Create(source);
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);
}
} }
private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers) private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)

View File

@ -195,12 +195,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
{ {
filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml"); filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml");
var bytes = Encoding.UTF8.GetBytes(content); var source = RazorSourceDocument.Create(content, filePath);
using (var stream = new MemoryStream(bytes)) return RazorCodeDocument.Create(source);
{
var source = RazorSourceDocument.ReadFrom(stream, filePath);
return RazorCodeDocument.Create(source);
}
} }
private static RazorEngine CreateEngine() => RazorEngine.Create(); private static RazorEngine CreateEngine() => RazorEngine.Create();

View File

@ -215,12 +215,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
{ {
filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml"); filePath = filePath ?? Path.Combine(Directory.GetCurrentDirectory(), "Test.cshtml");
var bytes = Encoding.UTF8.GetBytes(content); var source = RazorSourceDocument.Create(content, filePath);
using (var stream = new MemoryStream(bytes)) return RazorCodeDocument.Create(source);
{
var source = RazorSourceDocument.ReadFrom(stream, filePath);
return RazorCodeDocument.Create(source);
}
} }
private static RazorEngine CreateEngine() private static RazorEngine CreateEngine()

View File

@ -319,15 +319,8 @@ public class __Generated__TagCloudViewComponentTagHelper : Microsoft.AspNetCore.
private RazorCodeDocument CreateDocument(string content) private RazorCodeDocument CreateDocument(string content)
{ {
using (var stream = new MemoryStream()) var source = RazorSourceDocument.Create(content, "test.cshtml");
{ return RazorCodeDocument.Create(source);
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);
}
} }
private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers) private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)

View File

@ -28,7 +28,7 @@ public class MyTestType {}";
var compilationService = GetRoslynCompilationService(); 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() var csharpDocument = new RazorCSharpDocument()
{ {
@ -53,7 +53,7 @@ public class MyTestType {}";
this should fail"; this should fail";
var compilationService = GetRoslynCompilationService(); var compilationService = GetRoslynCompilationService();
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(fileContent), viewPath)); var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(fileContent, viewPath));
var csharpDocument = new RazorCSharpDocument() var csharpDocument = new RazorCSharpDocument()
{ {
@ -80,7 +80,7 @@ this should fail";
var content = "this should fail"; var content = "this should fail";
var compilationService = GetRoslynCompilationService(); var compilationService = GetRoslynCompilationService();
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.ReadFrom(CreateStreamContent(fileContent), viewPath)); var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(fileContent, viewPath));
var csharpDocument = new RazorCSharpDocument() var csharpDocument = new RazorCSharpDocument()
{ {
@ -114,7 +114,7 @@ public class MyNonCustomDefinedClass {}
var options = GetOptions(); var options = GetOptions();
options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE"); options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");
var compilationService = GetRoslynCompilationService(options: options); 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() var csharpDocument = new RazorCSharpDocument()
{ {
@ -136,7 +136,7 @@ public class MyNonCustomDefinedClass {}
var viewPath = "Views/Home/Index"; var viewPath = "Views/Home/Index";
var generatedCodeFileName = "Generated Code"; var generatedCodeFileName = "Generated Code";
var compilationService = GetRoslynCompilationService(); 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 assemblyName = "random-assembly-name";
var diagnostics = new[] var diagnostics = new[]
@ -220,7 +220,7 @@ public class MyNonCustomDefinedClass {}
var options = GetOptions(c => usedCompilation = c); var options = GetOptions(c => usedCompilation = c);
var compilationService = GetRoslynCompilationService(options: options); 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() var csharpDocument = new RazorCSharpDocument()
{ {
@ -244,7 +244,7 @@ public class MyNonCustomDefinedClass {}
}); });
var content = "public class MyTestType {}"; var content = "public class MyTestType {}";
var compilationService = GetRoslynCompilationService(options: options); 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() var csharpDocument = new RazorCSharpDocument()
{ {
@ -274,7 +274,7 @@ public class MyNonCustomDefinedClass {}
var applicationPartManager = new ApplicationPartManager(); var applicationPartManager = new ApplicationPartManager();
var compilationService = GetRoslynCompilationService(applicationPartManager, options); 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() var csharpDocument = new RazorCSharpDocument()
{ {
@ -340,19 +340,5 @@ public class MyNonCustomDefinedClass {}
optionsAccessor, optionsAccessor,
NullLoggerFactory.Instance); 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;
}
} }
} }

View File

@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
Diagnostics = new List<RazorDiagnostic>() Diagnostics = new List<RazorDiagnostic>()
}); });
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(); }).Verifiable();
var razorService = new RazorCompilationService( var razorService = new RazorCompilationService(