Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Created March 29, 2013 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyond-code-github/5274142 to your computer and use it in GitHub Desktop.
Save beyond-code-github/5274142 to your computer and use it in GitHub Desktop.
Phantom Express is a console application that will start IIS Express for a given folder and port, then run Phantom JS as writing the results to the console. Once phantom JS is finished, IIS express is closed.
namespace PhantomExpress
{
using System;
using System.Diagnostics;
using System.Threading.Tasks;
internal class Program
{
private static int Main(string[] args)
{
string iisExpressPath = args[0];
string websitePath = args[1];
string websitePort = args[2];
string phantomJSPath = args[3];
string jsControllerPath = args[4];
var startIisTask = StartIis(iisExpressPath, websitePath, websitePort);
Process iisexpress;
try
{
iisexpress = startIisTask.Result;
}
catch (Exception)
{
Console.Write("An error occurred while starting IIS express");
#if DEBUG
Console.ReadKey();
#endif
return -1;
}
if (iisexpress != null)
{
var phantomJS = new Process();
phantomJS.StartInfo.FileName = phantomJSPath;
phantomJS.StartInfo.Arguments = jsControllerPath;
phantomJS.StartInfo.UseShellExecute = false;
phantomJS.StartInfo.RedirectStandardOutput = true;
var tcs = new TaskCompletionSource<bool>();
phantomJS.Start();
Task.Run(
() =>
{
string str;
while ((str = phantomJS.StandardOutput.ReadLine()) != null)
{
if (str == "Unable to load the address!")
{
tcs.SetException(new ArgumentException(str));
return;
}
Console.WriteLine(str);
}
tcs.SetResult(true);
});
try
{
var complete = tcs.Task.Result;
}
catch (Exception)
{
#if DEBUG
Console.ReadKey();
#endif
phantomJS.Kill();
iisexpress.Kill();
return -1;
}
#if DEBUG
Console.ReadKey();
#endif
iisexpress.Kill();
return 0;
}
Console.Write("An error occurred while starting IIS express");
#if DEBUG
Console.ReadKey();
#endif
return -1;
}
private static Task<Process> StartIis(string iisExpressPath, string websitePath, string websitePort)
{
var tcs = new TaskCompletionSource<Process>();
var iisexpress = new Process();
iisexpress.StartInfo.FileName = iisExpressPath;
iisexpress.StartInfo.Arguments = string.Format("/path:{0} /port:{1}", websitePath, websitePort);
iisexpress.StartInfo.RedirectStandardOutput = true;
iisexpress.StartInfo.UseShellExecute = false;
iisexpress.EnableRaisingEvents = true;
// Implicit capture is ok here as we are capturing an object we need later.
iisexpress.Exited += IisexpressOnExited(tcs);
iisexpress.Start();
Task.Run(
() =>
{
string str;
while ((str = iisexpress.StandardOutput.ReadLine()) != null)
{
if (str.Contains("IIS Express is running"))
{
iisexpress.Exited -= IisexpressOnExited(tcs);
tcs.SetResult(iisexpress);
}
}
});
return tcs.Task;
}
private static EventHandler IisexpressOnExited(TaskCompletionSource<Process> tcs)
{
return (sender, args) => tcs.TrySetCanceled();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment