Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add RazorSourceDocument.Create(string template)
Browse files Browse the repository at this point in the history
Add document and fix casing of RazorSourceDocument.FileName
Fixes #1063
  • Loading branch information
pranavkm committed Mar 13, 2017
1 parent 1330b77 commit 7d43bfc
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void VisitChecksum(ChecksumIRNode node)
{
Context.Writer
.Write("#pragma checksum \"")
.Write(node.Filename)
.Write(node.FileName)
.Write("\" \"")
.Write(node.Guid)
.Write("\" \"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
var import = imports[j];

importsVisitor.Filename = import.Source.Filename;
importsVisitor.FileName = import.Source.FileName;
importsVisitor.VisitBlock(import.Root);
}
}

var visitor = new MainSourceVisitor(document, builder, namespaces)
{
Filename = syntaxTree.Source.Filename,
FileName = syntaxTree.Source.FileName,
};

visitor.VisitBlock(syntaxTree.Root);
Expand All @@ -79,7 +79,7 @@ public LoweringVisitor(DocumentIRNode document, RazorIRBuilder builder, HashSet<
_namespaces = namespaces;
}

public string Filename { get; set; }
public string FileName { get; set; }

public override void VisitImportSpan(AddImportChunkGenerator chunkGenerator, Span span)
{
Expand Down Expand Up @@ -162,7 +162,7 @@ public override void VisitTagHelperPrefixDirectiveSpan(TagHelperPrefixDirectiveC
}

var span = new SourceSpan(
node.Start.FilePath ?? Filename,
node.Start.FilePath ?? FileName,
node.Start.AbsoluteIndex,
node.Start.LineIndex,
node.Start.CharacterIndex,
Expand Down Expand Up @@ -317,7 +317,7 @@ public override void VisitTemplateBlock(TemplateBlockChunkGenerator chunkGenerat
var contentLength = templateNode.Children.Sum(child => child.Source?.Length ?? 0);

templateNode.Source = new SourceSpan(
sourceRangeStart.Value.FilePath ?? Filename,
sourceRangeStart.Value.FilePath ?? FileName,
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
Expand Down Expand Up @@ -353,7 +353,7 @@ public override void VisitExpressionBlock(ExpressionChunkGenerator chunkGenerato
var contentLength = expressionNode.Children.Sum(child => child.Source?.Length ?? 0);

expressionNode.Source = new SourceSpan(
sourceRangeStart.Value.FilePath ?? Filename,
sourceRangeStart.Value.FilePath ?? FileName,
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// 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.Diagnostics;
using System.Text;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;

namespace Microsoft.AspNetCore.Razor.Evolution
{
Expand All @@ -14,7 +11,7 @@ internal class DefaultRazorSourceDocument : RazorSourceDocument
private readonly string _content;
private readonly RazorSourceLineCollection _lines;

public DefaultRazorSourceDocument(string content, Encoding encoding, string filename)
public DefaultRazorSourceDocument(string content, Encoding encoding, string fileName)
{
if (content == null)
{
Expand All @@ -28,7 +25,7 @@ public DefaultRazorSourceDocument(string content, Encoding encoding, string file

_content = content;
Encoding = encoding;
Filename = filename;
FileName = fileName;

_lines = new DefaultRazorSourceLineCollection(this);
}
Expand All @@ -37,7 +34,7 @@ public DefaultRazorSourceDocument(string content, Encoding encoding, string file

public override Encoding Encoding { get; }

public override string Filename { get; }
public override string FileName { get; }

public override int Length => _content.Length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal override SourceLocation GetLocation(int position)
// We have an exact match for the start of a line.
Debug.Assert(_lineStarts[index] == position);

return new SourceLocation(_document.Filename, position, index, characterIndex: 0);
return new SourceLocation(_document.FileName, position, index, characterIndex: 0);
}


Expand All @@ -59,12 +59,12 @@ internal override SourceLocation GetLocation(int position)
if (index == -1)
{
// There's no preceding line, so it's based on the start of the string
return new SourceLocation(_document.Filename, position, 0, position);
return new SourceLocation(_document.FileName, position, 0, position);
}
else
{
var characterIndex = position - _lineStarts[index];
return new SourceLocation(_document.Filename, position, index, characterIndex);
return new SourceLocation(_document.FileName, position, index, characterIndex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FileSystemRazorProjectItem(string basePath, string path, FileInfo file)
public override bool Exists => File.Exists;

/// <inheritdoc />
public override string Filename => File.Name;
public override string FileName => File.Name;

/// <inheritdoc />
public override string PhysicalPath => File.FullName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ChecksumIRNode : RazorIRNode

public string Bytes { get; set; }

public string Filename { get; set; }
public string FileName { get; set; }

public string Guid { get; set; }

Expand All @@ -38,7 +38,7 @@ public static ChecksumIRNode Create(RazorSourceDocument sourceDocument)

var node = new ChecksumIRNode()
{
Filename = sourceDocument.Filename,
FileName = sourceDocument.FileName,
Guid = Sha1AlgorithmId
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class LargeTextRazorSourceDocument : RazorSourceDocument

private readonly int _length;

public LargeTextRazorSourceDocument(StreamReader reader, int chunkMaxLength, Encoding encoding, string filename)
public LargeTextRazorSourceDocument(StreamReader reader, int chunkMaxLength, Encoding encoding, string fileName)
{
if (reader == null)
{
Expand All @@ -32,7 +32,7 @@ public LargeTextRazorSourceDocument(StreamReader reader, int chunkMaxLength, Enc

_chunkMaxLength = chunkMaxLength;
Encoding = encoding;
Filename = filename;
FileName = fileName;

ReadChunks(reader, _chunkMaxLength, out _length, out _chunks);
_lines = new DefaultRazorSourceLineCollection(this);
Expand All @@ -51,7 +51,7 @@ public override char this[int position]

public override Encoding Encoding { get; }

public override string Filename { get; }
public override string FileName { get; }

public override int Length => _length;

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNetCore.Razor.Evolution/RazorProjectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ public virtual string Extension
{
get
{
var index = Filename.LastIndexOf('.');
var index = FileName.LastIndexOf('.');
if (index == -1)
{
return null;
}
else
{
return Filename.Substring(index);
return FileName.Substring(index);
}
}
}

/// <summary>
/// The name of the file including the extension.
/// </summary>
public virtual string Filename
public virtual string FileName
{
get
{
Expand Down
94 changes: 85 additions & 9 deletions src/Microsoft.AspNetCore.Razor.Evolution/RazorSourceDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,74 @@

namespace Microsoft.AspNetCore.Razor.Evolution
{
/// <summary>
/// The Razor template source.
/// </summary>
public abstract class RazorSourceDocument
{
private const int LargeObjectHeapLimitInChars = 40 * 1024; // 40K Unicode chars is 80KB which is less than the large object heap limit.
internal const int LargeObjectHeapLimitInChars = 40 * 1024; // 40K Unicode chars is 80KB which is less than the large object heap limit.

internal static readonly RazorSourceDocument[] EmptyArray = new RazorSourceDocument[0];

/// <summary>
/// Encoding of the file that the text was read from.
/// </summary>
public abstract Encoding Encoding { get; }

public abstract string Filename { get; }
/// <summary>
/// Path of the file the content was read from.
/// </summary>
public abstract string FileName { get; }

/// <summary>
/// Gets a character at given position.
/// </summary>
/// <param name="position">The position to get the character from.</param>
public abstract char this[int position] { get; }

/// <summary>
/// Gets the length of the text in characters.
/// </summary>
public abstract int Length { get; }

/// <summary>
/// Gets the <see cref="RazorSourceLineCollection"/>.
/// </summary>
public abstract RazorSourceLineCollection Lines { get; }

/// <summary>
/// Copies a range of characters from the <see cref="RazorSourceDocument"/> to the specified <paramref name="destination"/>.
/// </summary>
/// <param name="sourceIndex">The index of the first character in this instance to copy.</param>
/// <param name="destination">The destination buffer.</param>
/// <param name="destinationIndex">The index in destination at which the copy operation begins.</param>
/// <param name="count">The number of characters in this instance to copy to destination.</param>
public abstract void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

public static RazorSourceDocument ReadFrom(Stream stream, string filename)
/// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to read from.</param>
/// <param name="fileName">The file name of the template.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(Stream stream, string fileName)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

return ReadFromInternal(stream, filename, encoding: null);
return ReadFromInternal(stream, fileName, encoding: null);
}

public static RazorSourceDocument ReadFrom(Stream stream, string filename, Encoding encoding)
/// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to read from.</param>
/// <param name="fileName">The file name of the template.</param>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> to use to read the <paramref name="stream"/>.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(Stream stream, string fileName, Encoding encoding)
{
if (stream == null)
{
Expand All @@ -47,9 +86,14 @@ public static RazorSourceDocument ReadFrom(Stream stream, string filename, Encod
throw new ArgumentNullException(nameof(encoding));
}

return ReadFromInternal(stream, filename, encoding);
return ReadFromInternal(stream, fileName, encoding);
}

/// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="projectItem"/>.
/// </summary>
/// <param name="projectItem">The <see cref="RazorProjectItem"/> to read from.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem)
{
if (projectItem == null)
Expand All @@ -69,7 +113,39 @@ public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem)
}
}

private static RazorSourceDocument ReadFromInternal(Stream stream, string filename, Encoding encoding)
/// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">The template content.</param>
/// <param name="fileName">The file name of the <see cref="RazorSourceDocument"/>.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
/// <remarks>Uses <see cref="System.Text.Encoding.UTF8" /></remarks>
public static RazorSourceDocument Create(string content, string fileName)
=> Create(content, fileName, Encoding.UTF8);

/// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">The template content.</param>
/// <param name="fileName">The file name of the <see cref="RazorSourceDocument"/>.</param>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> of the file <paramref name="content"/> was read from.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument Create(string content, string fileName, Encoding encoding)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}

if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}

return new DefaultRazorSourceDocument(content, encoding, fileName);
}

private static RazorSourceDocument ReadFromInternal(Stream stream, string fileName, Encoding encoding)
{
var streamLength = (int)stream.Length;
var content = string.Empty;
Expand Down Expand Up @@ -109,14 +185,14 @@ private static RazorSourceDocument ReadFromInternal(Stream stream, string filena
reader,
LargeObjectHeapLimitInChars,
contentEncoding,
filename);
fileName);
}

content = reader.ReadToEnd();
}
}

return new DefaultRazorSourceDocument(content, contentEncoding, filename);
return new DefaultRazorSourceDocument(content, contentEncoding, fileName);
}
}
}
6 changes: 3 additions & 3 deletions src/RazorPageGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static IList<RazorPageGeneratorResult> MainCore(string rootNamespace, str
.SetBaseType("Microsoft.Extensions.RazorViews.BaseView")
.ConfigureClass((document, @class) =>
{
@class.Name = Path.GetFileNameWithoutExtension(document.Source.Filename);
@class.Name = Path.GetFileNameWithoutExtension(document.Source.FileName);
@class.AccessModifier = "internal";
});

Expand Down Expand Up @@ -76,7 +76,7 @@ public static IList<RazorPageGeneratorResult> MainCore(string rootNamespace, str

foreach (var item in cshtmlFiles)
{
Console.WriteLine(" Generating code file for view {0}...", item.Filename);
Console.WriteLine(" Generating code file for view {0}...", item.FileName);
results.Add(GenerateCodeFile(templateEngine, item));
Console.WriteLine(" Done!");
fileCount++;
Expand Down Expand Up @@ -118,7 +118,7 @@ public FileSystemRazorProjectItemWrapper(FileSystemRazorProjectItem item)
public override string Path => _source.Path;

// Mask the full name since we don't want a developer's local file paths to be commited.
public override string PhysicalPath => _source.Filename;
public override string PhysicalPath => _source.FileName;

public override bool Exists => _source.Exists;

Expand Down
Loading

0 comments on commit 7d43bfc

Please sign in to comment.