Added a console app to generate the Razor pages

- Replaces the TT files previously being used (in Katana)
- Modified the include file logic to work better with the new generated Razor output as the old syntax/logic didn't work well with the additional line pragmas, etc. being emitted
- Updated the middleware to work with new generated output (ExecuteAsync instead of Execute, etc.)
- Fixed a scoping issue in Error.cshtml
- gitignore sln.ide cache directory
- #3
This commit is contained in:
DamianEdwards 2014-05-01 15:45:27 -07:00
parent a78d5ac019
commit b4de33eeea
17 changed files with 4756 additions and 5514 deletions

3
.gitignore vendored
View File

@ -21,4 +21,5 @@ nuget.exe
*DS_Store
*.ncrunchsolution
*.*sdf
*.ipch
*.ipch
*.sln.ide

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30327.0
# Visual Studio 14
VisualStudioVersion = 14.0.21628.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{509A6F36-AD80-4A18-B5B1-717D38DFF29D}"
EndProject
@ -13,6 +13,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Diagnostic
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ErrorPageSample", "samples\ErrorPageSample\ErrorPageSample.kproj", "{589AC17F-9455-4764-8F82-FCD2AE58DA14}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PageGenerator", "src\PageGenerator\PageGenerator.kproj", "{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -51,6 +53,15 @@ Global
{589AC17F-9455-4764-8F82-FCD2AE58DA14}.Release|Mixed Platforms.Build.0 = Release|x86
{589AC17F-9455-4764-8F82-FCD2AE58DA14}.Release|x86.ActiveCfg = Release|x86
{589AC17F-9455-4764-8F82-FCD2AE58DA14}.Release|x86.Build.0 = Release|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Any CPU.ActiveCfg = Debug|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|x86.ActiveCfg = Debug|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|x86.Build.0 = Debug|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Any CPU.ActiveCfg = Release|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Mixed Platforms.ActiveCfg = Release|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Mixed Platforms.Build.0 = Release|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|x86.ActiveCfg = Release|x86
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -59,5 +70,6 @@ Global
{C5F59CBA-DF2D-4983-8CBB-11B6AF21B416} = {ACAA0157-A8C4-4152-93DE-90CCDF304087}
{68A1F0E1-ECCE-46D1-B20F-C43EE5B097DE} = {509A6F36-AD80-4A18-B5B1-717D38DFF29D}
{589AC17F-9455-4764-8F82-FCD2AE58DA14} = {ACAA0157-A8C4-4152-93DE-90CCDF304087}
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74} = {509A6F36-AD80-4A18-B5B1-717D38DFF29D}
EndGlobalSection
EndGlobal

View File

@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Diagnostics
{
try
{
DisplayException(context, ex);
await DisplayException(context, ex);
return;
}
catch (Exception)
@ -72,7 +72,7 @@ namespace Microsoft.AspNet.Diagnostics
}
// Assumes the response headers have not been sent. If they have, still attempt to write to the body.
private void DisplayException(HttpContext context, Exception ex)
private async Task DisplayException(HttpContext context, Exception ex)
{
var request = context.Request;
@ -103,7 +103,7 @@ namespace Microsoft.AspNet.Diagnostics
}*/
var errorPage = new ErrorPage() { Model = model };
errorPage.Execute(context);
await errorPage.ExecuteAsync(context);
}
private IEnumerable<ErrorDetails> GetErrorDetails(Exception ex, bool showSource)

View File

@ -20,13 +20,10 @@
<Content Include="Project.json" />
<Content Include="Resources.resx" />
<Content Include="Views\DiagnosticsPage.cshtml" />
<Content Include="Views\DiagnosticsPage.tt" />
<Content Include="Views\ErrorPage.cshtml" />
<Content Include="Views\ErrorPage.css" />
<Content Include="Views\ErrorPage.js" />
<Content Include="Views\ErrorPage.tt" />
<Content Include="Views\WelcomePage.cshtml" />
<Content Include="Views\WelcomePage.tt" />
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />

View File

@ -5,6 +5,7 @@ using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Diagnostics.Views
{
@ -37,20 +38,20 @@ namespace Microsoft.AspNet.Diagnostics.Views
/// Execute an individual request
/// </summary>
/// <param name="context"></param>
public void Execute(HttpContext context)
public async Task ExecuteAsync(HttpContext context)
{
Context = context;
Request = Context.Request;
Response = Context.Response;
Output = new StreamWriter(Response.Body);
Execute();
await ExecuteAsync();
Output.Dispose();
}
/// <summary>
/// Execute an individual request
/// </summary>
public abstract void Execute();
public abstract Task ExecuteAsync();
/// <summary>
/// Write the given value directly to the output

View File

@ -1,38 +1,29 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18213
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Microsoft.AspNet.Diagnostics.Views
{
#line 1 "DiagnosticsPage.cshtml"
using System
namespace Microsoft.AspNet.Diagnostics.Views {
#line 1 "DiagnosticsPage.cshtml"
using System;
#line default
#line hidden
#line 2 "DiagnosticsPage.cshtml"
using System.Globalization;
#line default
#line hidden
public class DiagnosticsPage : Microsoft.AspNet.Diagnostics.Views.BaseView {
#line default
#line hidden
public DiagnosticsPage() {
;
#line 2 "DiagnosticsPage.cshtml"
using System.Globalization
#line default
#line hidden
;
using System.Threading.Tasks;
public class DiagnosticsPage : Microsoft.AspNet.Diagnostics.Views.BaseView
{
#line hidden
public DiagnosticsPage()
{
}
public override void Execute() {
#line 3 "DiagnosticsPage.cshtml"
public override async Task ExecuteAsync()
{
#line 3 "DiagnosticsPage.cshtml"
Response.ContentType = "text/html";
string error = Request.Query.Get("error");
@ -41,93 +32,70 @@ namespace Microsoft.AspNet.Diagnostics.Views {
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "User requested error '{0}'", error));
}
#line default
#line hidden
WriteLiteral("\r\n<!DOCTYPE html>\r\n\r\n<html");
#line default
#line hidden
WriteLiteral(" lang=\"en\"");
WriteLiteral("\r\n<!DOCTYPE html>\r\n\r\n<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head" +
">\r\n <meta charset=\"utf-8\" />\r\n <title>");
Write(
#line 16 "DiagnosticsPage.cshtml"
Resources.DiagnosticsPageHtml_Title
WriteLiteral(" xmlns=\"http://www.w3.org/1999/xhtml\"");
#line default
#line hidden
);
WriteLiteral(">\r\n<head>\r\n <meta");
WriteLiteral("</title>\r\n</head>\r\n<body>\r\n <div class=\"main\">\r\n <h1>");
Write(
#line 20 "DiagnosticsPage.cshtml"
Resources.DiagnosticsPageHtml_Title
WriteLiteral(" charset=\"utf-8\"");
#line default
#line hidden
);
WriteLiteral(" />\r\n <title>");
WriteLiteral("</h1>\r\n <p>");
Write(
#line 21 "DiagnosticsPage.cshtml"
Resources.DiagnosticsPageHtml_Information
#line 16 "DiagnosticsPage.cshtml"
Write(Resources.DiagnosticsPageHtml_Title);
#line default
#line hidden
);
#line default
#line hidden
WriteLiteral("</title>\r\n</head>\r\n<body>\r\n <div");
WriteLiteral("</p>\r\n </div>\r\n <div class=\"errors\">\r\n <h2>");
Write(
#line 24 "DiagnosticsPage.cshtml"
Resources.DiagnosticsPageHtml_TestErrorSection
WriteLiteral(" class=\"main\"");
#line default
#line hidden
);
WriteLiteral(">\r\n <h1>");
WriteLiteral("</h2>\r\n <p><a");
WriteAttribute("href", Tuple.Create(" href=\"", 767), Tuple.Create("\"", 858),
Tuple.Create(Tuple.Create("", 774), Tuple.Create<System.Object, System.Int32>(
#line 25 "DiagnosticsPage.cshtml"
Request.PathBase
#line 20 "DiagnosticsPage.cshtml"
Write(Resources.DiagnosticsPageHtml_Title);
#line default
#line hidden
, 774), false),
Tuple.Create(Tuple.Create("", 791), Tuple.Create<System.Object, System.Int32>(
#line 25 "DiagnosticsPage.cshtml"
Request.Path
#line default
#line hidden
WriteLiteral("</h1>\r\n <p>");
#line 21 "DiagnosticsPage.cshtml"
Write(Resources.DiagnosticsPageHtml_Information);
#line default
#line hidden
WriteLiteral("</p>\r\n </div>\r\n <div");
WriteLiteral(" class=\"errors\"");
WriteLiteral(">\r\n <h2>");
#line 24 "DiagnosticsPage.cshtml"
Write(Resources.DiagnosticsPageHtml_TestErrorSection);
#line default
#line hidden
WriteLiteral("</h2>\r\n <p><a");
WriteAttribute("href", Tuple.Create(" href=\"", 767), Tuple.Create("\"", 858)
#line 25 "DiagnosticsPage.cshtml"
, Tuple.Create(Tuple.Create("", 774), Tuple.Create<System.Object, System.Int32>(Request.PathBase
#line default
#line hidden
, 774), false)
#line 25 "DiagnosticsPage.cshtml"
, Tuple.Create(Tuple.Create("", 791), Tuple.Create<System.Object, System.Int32>(Request.Path
#line default
#line hidden
, 791), false)
, Tuple.Create(Tuple.Create("", 804), Tuple.Create("?error=", 804), true)
#line 25 "DiagnosticsPage.cshtml"
, Tuple.Create(Tuple.Create("", 811), Tuple.Create<System.Object, System.Int32>(Resources.DiagnosticsPageHtml_TestErrorMessage
#line default
#line hidden
, 811), false)
);
WriteLiteral(">throw InvalidOperationException</a></p>\r\n </div>\r\n</body>\r\n</html>\r\n");
#line default
#line hidden
, 791), false), Tuple.Create(Tuple.Create("", 804), Tuple.Create("?error=", 804), true),
Tuple.Create(Tuple.Create("", 811), Tuple.Create<System.Object, System.Int32>(
#line 25 "DiagnosticsPage.cshtml"
Resources.DiagnosticsPageHtml_TestErrorMessage
#line default
#line hidden
, 811), false));
WriteLiteral(">throw InvalidOperationException</a></p>\r\n </div>\r\n</body>\r\n</html>\r\n");
}
}
}

View File

@ -1,26 +0,0 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web.Razor" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Web.Razor" #>
<#@ import namespace="System.Web.Razor.Text" #>
<#@ output extension=".cs" #>
<#
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = "Microsoft.Owin.Diagnostics.Views.BaseView";
var engine = new RazorTemplateEngine(host);
var path = Host.ResolvePath("DiagnosticsPage.cshtml");
var code = engine.GenerateCode(
new System.IO.StreamReader(path),
"DiagnosticsPage",
"Microsoft.Owin.Diagnostics.Views",
@"DiagnosticsPage.cshtml");
var provider = new Microsoft.CSharp.CSharpCodeProvider();
var writer = new System.IO.StringWriter();
provider.GenerateCodeFromCompileUnit(code.GeneratedCode, writer, null);
#>
<#=writer.ToString()#>

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@
/// <summary>
///
/// </summary>
public Views.ErrorPageModel Model { get; set; }
public ErrorPageModel Model { get; set; }
}
@{
Response.StatusCode = 500;
@ -17,22 +17,23 @@
string location = string.Empty;
}
<!DOCTYPE html>
<html lang="@System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName" xmlns="http://www.w3.org/1999/xhtml">
<html lang="@CultureInfo.CurrentUICulture.TwoLetterISOLanguageName" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>@Resources.ErrorPageHtml_Title</title>
<style>
@(@"[[ErrorPage.css]]")
<%$ include: ErrorPage.css %>
</style>
</head>
<body>
Hello
<h1>@Resources.ErrorPageHtml_UnhandledException</h1>
@if (Model.Options.ShowExceptionDetails)
{
foreach (var errorDetail in Model.ErrorDetails)
{
<h2 class="titleerror">@errorDetail.Error.GetType().Name: @errorDetail.Error.Message</h2>
{
@{
StackFrame firstFrame = null;
firstFrame = errorDetail.StackFrames.FirstOrDefault();
if (firstFrame != null)
@ -43,7 +44,7 @@
{
location = errorDetail.Error.TargetSite.DeclaringType.FullName + "." + errorDetail.Error.TargetSite.Name;
}*/
}
}
if (!string.IsNullOrEmpty(location) && firstFrame != null && !string.IsNullOrEmpty(firstFrame.File))
{
<p class="location">@location in <code title="@firstFrame.File">@System.IO.Path.GetFileName(firstFrame.File)</code>, line @firstFrame.Line</p>
@ -272,10 +273,10 @@
*/
}
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<script type="text/javascript">
<script>
//<!--
@(@"[[ErrorPage.js]]")
//-->
<%$ include: ErrorPage.js %>
//-->
</script>
</body>
</html>

View File

@ -1,5 +1,4 @@

(function ($) {
(function (window, undefined) {
$('.collapsable').hide();
$('.page').hide();
$('#stackpage').show();
@ -33,4 +32,4 @@
}
});
})(jQuery);
})(window);

View File

@ -1,51 +0,0 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web.Razor" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Web.Razor" #>
<#@ import namespace="System.Web.Razor.Text" #>
<#@ output extension=".cs" #>
<#
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = "Microsoft.Owin.Diagnostics.Views.BaseView";
var engine = new RazorTemplateEngine(host);
var path = Host.ResolvePath("ErrorPage.cshtml");
var code = engine.GenerateCode(
new System.IO.StreamReader(path),
"ErrorPage",
"Microsoft.Owin.Diagnostics.Views",
@"ErrorPage.cshtml");
var provider = new Microsoft.CSharp.CSharpCodeProvider();
var writer = new System.IO.StringWriter();
provider.GenerateCodeFromCompileUnit(code.GeneratedCode, writer, null);
var source = writer.ToString();
var startIndex = 0;
while(startIndex < source.Length)
{
var startMatch = @"Write(@""[[";
var startCode = @"WriteLiteral(@""";
var endMatch = @"]]"");";
var endCode = @""");";
startIndex = source.IndexOf(startMatch, startIndex);
if (startIndex == -1)
{
break;
}
var endIndex = source.IndexOf(endMatch, startIndex);
if (endIndex == -1)
{
break;
}
var fileName = source.Substring(startIndex + startMatch.Length, endIndex - (startIndex + startMatch.Length));
var replacement = File.ReadAllText(Host.ResolvePath(fileName)).Replace("\"", "\"\"");
source = source.Substring(0, startIndex) + startCode + replacement + endCode + source.Substring(endIndex + endMatch.Length);
startIndex = startIndex + startCode.Length + replacement.Length + endCode.Length;
}
#>
<#=source#>

File diff suppressed because it is too large Load Diff

View File

@ -1,50 +0,0 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web.Razor" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Web.Razor" #>
<#@ import namespace="System.Web.Razor.Text" #>
<#@ output extension=".cs" #>
<#
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = "Microsoft.Owin.Diagnostics.Views.BaseView";
var engine = new RazorTemplateEngine(host);
var path = Host.ResolvePath("WelcomePage.cshtml");
var code = engine.GenerateCode(
new System.IO.StreamReader(path),
"WelcomePage",
"Microsoft.Owin.Diagnostics.Views",
"WelcomePage.cshtml");
var provider = new Microsoft.CSharp.CSharpCodeProvider();
var writer = new System.IO.StringWriter();
provider.GenerateCodeFromCompileUnit(code.GeneratedCode, writer, null);
var source = writer.ToString();
var startIndex = 0;
while(startIndex < source.Length)
{
var startMatch = @"Write(@""[[";
var startCode = @"WriteLiteral(@""";
var endMatch = @"]]"");";
var endCode = @""");";
startIndex = source.IndexOf(startMatch, startIndex);
if (startIndex == -1)
{
break;
}
var endIndex = source.IndexOf(endMatch, startIndex);
if (endIndex == -1)
{
break;
}
var fileName = source.Substring(startIndex + startMatch.Length, endIndex - (startIndex + startMatch.Length));
var replacement = File.ReadAllText(Host.ResolvePath(fileName)).Replace("\"", "\"\"");
source = source.Substring(0, startIndex) + startCode + replacement + endCode + source.Substring(endIndex + endMatch.Length);
startIndex = startIndex + startCode.Length + replacement.Length + endCode.Length;
}
#>
<#=source#>

View File

@ -48,8 +48,7 @@ namespace Microsoft.AspNet.Diagnostics
{
// Dynamically generated for LOC.
var welcomePage = new WelcomePage();
welcomePage.Execute(context);
return Task.FromResult(0);
return welcomePage.ExecuteAsync(context);
}
return _next(context);

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\ProjectK\Microsoft.Web.ProjectK.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>4d4a785a-ecb9-4916-a88f-0fd306ee3b74</ProjectGuid>
<OutputType>Console</OutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Project.json" />
</ItemGroup>
<Import Project="$(VSToolsPath)\ProjectK\Microsoft.Web.ProjectK.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Razor;
using Microsoft.Net.Runtime;
namespace PageGenerator
{
public class Program
{
private readonly ILibraryManager _libraryManager;
public Program(ILibraryManager libraryManager)
{
_libraryManager = libraryManager;
}
public void Main(string[] args)
{
var diagnosticsLibInfo = _libraryManager.GetLibraryInformation("Microsoft.AspNet.Diagnostics");
var viewBasePath = Path.Combine(Path.GetDirectoryName(diagnosticsLibInfo.Path), "Views");
Console.WriteLine("Generating code files for views in {0}", viewBasePath);
Console.WriteLine();
var cshtmlFiles = GetCshtmlFiles(viewBasePath);
var fileCount = 0;
foreach (var fileName in cshtmlFiles)
{
Console.WriteLine(" Generating code file for view {0}...", Path.GetFileName(fileName));
GenerateCodeFile(fileName);
Console.WriteLine(" Done!");
fileCount++;
}
Console.WriteLine();
Console.WriteLine("{0} files successfully generated.", fileCount);
Console.WriteLine();
Console.Write("Press enter to close application...");
Console.ReadLine();
}
private static IEnumerable<string> GetCshtmlFiles(string path)
{
if (!Directory.Exists(path))
{
throw new ArgumentException("path");
}
return Directory.EnumerateFiles(path, "*.cshtml");
}
private static void GenerateCodeFile(string cshtmlFilePath)
{
var basePath = Path.GetDirectoryName(cshtmlFilePath);
var fileName = Path.GetFileName(cshtmlFilePath);
var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
var codeLang = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(codeLang);
host.DefaultBaseClass = "Microsoft.AspNet.Diagnostics.Views.BaseView";
var engine = new RazorTemplateEngine(host);
using (var fileStream = File.OpenText(cshtmlFilePath))
{
var code = engine.GenerateCode(
input: fileStream,
className: fileNameNoExtension,
rootNamespace: "Microsoft.AspNet.Diagnostics.Views",
sourceFileName: fileName);
var source = code.GeneratedCode;
var startIndex = 0;
while (startIndex < source.Length)
{
var startMatch = @"<%$ include: ";
var endMatch = @" %>";
startIndex = source.IndexOf(startMatch, startIndex);
if (startIndex == -1)
{
break;
}
var endIndex = source.IndexOf(endMatch, startIndex);
if (endIndex == -1)
{
break;
}
var includeFileName = source.Substring(startIndex + startMatch.Length, endIndex - (startIndex + startMatch.Length));
includeFileName = SanitizeFileName(includeFileName);
Console.WriteLine(" Inlining file {0}", includeFileName);
var replacement = File.ReadAllText(Path.Combine(basePath, includeFileName)).Replace("\"", "\\\"").Replace("\r\n", "\\r\\n");
source = source.Substring(0, startIndex) + replacement + source.Substring(endIndex + endMatch.Length);
startIndex = startIndex + replacement.Length;
}
File.WriteAllText(Path.Combine(basePath, string.Format("{0}.cs", fileNameNoExtension)), source);
}
}
private static string SanitizeFileName(string fileName)
{
// The Razor generated code sometimes splits strings across multiple lines
// which can hit the include file name, so we need to strip out the non-filename chars.
//ErrorPage.j" +
//"s
var invalidChars = new List<char>(Path.GetInvalidFileNameChars());
invalidChars.Add('+');
invalidChars.Add(' ');
return string.Join(string.Empty, fileName.Where(c => !invalidChars.Contains(c)).ToArray());
}
}
}

View File

@ -0,0 +1,20 @@
{
"dependencies": {
"Microsoft.AspNet.Diagnostics": "",
"Microsoft.Net.Runtime.Interfaces": "0.1.0-alpha-*",
"Microsoft.AspNet.Mvc": "0.1.0-alpha-*"
},
"configurations" : {
"net45" : {
"dependencies": {
"System.Runtime": ""
}
},
"k10" : {
"dependencies": {
"System.Console": "4.0.0.0"
}
}
}
}