Created
March 27, 2011 03:41
-
-
Save ScottWeinstein/888889 to your computer and use it in GitHub Desktop.
CoffeeScriptHandler for ASP.Net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Web; | |
using System.Diagnostics; | |
using System.Web.Caching; | |
using System.IO; | |
namespace CoffeeScriptHandler | |
{ | |
public class CoffeeScriptHandler : IHttpHandler | |
{ | |
private const string NODE_BIN = @"c:\node\bin\"; | |
public bool IsReusable | |
{ | |
get { return true; } | |
} | |
public void ProcessRequest(HttpContext context) | |
{ | |
ProcessRequest(new HttpContextWrapper(context)); | |
} | |
public static void ProcessRequest(HttpContextWrapper context) | |
{ | |
string path = context.Request.PhysicalPath; | |
DateTime lastWrite = File.GetLastWriteTime(path); | |
Cache cache = context.Cache; | |
var cachedItem = ((Tuple<DateTime, string>)cache[path]); | |
if (cachedItem == null || cachedItem.Item1 < lastWrite) | |
{ | |
ProcessStartInfo psi = new ProcessStartInfo(NODE_BIN + "node.exe", "coffee -cs"); | |
psi.UseShellExecute = false; | |
psi.RedirectStandardOutput = true; | |
psi.RedirectStandardInput = true; | |
psi.WorkingDirectory = NODE_BIN; | |
Process process = Process.Start(psi); | |
process.StandardInput.Write(File.ReadAllText(path)); | |
process.StandardInput.Close(); | |
process.WaitForExit(); | |
string result = process.StandardOutput.ReadToEnd(); | |
cache[path] = cachedItem = Tuple.Create<DateTime, string>(lastWrite, result); | |
} | |
context.Response.ContentType = "application/javascript"; | |
context.Response.Write(cachedItem.Item2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment