NDepend and Cruise Control .Net

Well, it had to happen, I finally got around to posting my fix/hack/dirty for this problem.

1. The problem: NDepend html results are very pretty, but don't work when you integrate NDepend into CC.Net, CC.Net's own xslt log parsers mean that the pretty images created by NDepend (dependency diagrams, Zone diagram, etc) are not shown.

2. The fix!

Create new file called image.ashx (ashx = cut-down webpage without the tedious events wired up)

Stick this in there:

<%@ webhandler language="C#" class="ImageHandler" %>
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Caching;


public class ImageHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
try
{
string name = ctx.Request.QueryString["name"];
string project = ctx.Request.QueryString["project"];
string label = ctx.Request.QueryString["label"];
string imagePath = ctx.Request.QueryString["imagePath"];
string cacheKey = string.Format("{0}|{1}|{2}|{3}", name, project, label, imagePath);

if ( name != null && name.Length > 0)
{
Byte[] imageBytes = null;

// Check if the cache contains the image.
object cachedImageBytes = ctx.Cache.Get(cacheKey);

// Use cache if possible...
if ( cachedImageBytes != null )
{
imageBytes = cachedImageBytes as byte [];
}
else // Get the image from the project/build directory.
{
if ( !Path.IsPathRooted(imagePath) )
{
imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, imagePath);
}


try
{ // Get the image stream from the provided path.
using ( FileStream fs = new FileStream(Path.Combine(imagePath, name), FileMode.Open, FileAccess.Read) )
{
using(Image inputImage = Image.FromStream(fs))
{
using(Image outputImage = new Bitmap(inputImage))
{
using(MemoryStream stream = new MemoryStream())
{
outputImage.Save(stream, ImageFormat.Jpeg);
imageBytes = stream.GetBuffer();
}

ctx.Cache.Add(cacheKey, imageBytes, null,
DateTime.MaxValue, new TimeSpan(2, 0, 0),
CacheItemPriority.Normal, null);
}
}
}
}
catch ( Exception ex)
{
ctx.Response.Write(ex.Message);
}
}

ctx.Response.Cache.SetCacheability(HttpCacheability.Public);
ctx.Response.ContentType = "image/jpg";
ctx.Response.BufferOutput = false;
ctx.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
}
finally
{
ctx.Response.End();
}
}
}


Put image.ashx in the root directory of the CC.Net Web application (Program Files\CruiseControl.NET\webdashboard)

Then edit your ndepend.xsl and whenever there's a reference to an image, replace it with a reference to the .ashx file, passing in the project, imagePath, label and name as below:

\ccnet\image.ashx?project=&imagePath=&label=&name=AbstractnessVSInstability.png


et voila! Nice pictures in your combined report in CC.Net

Enjoy!

Comments

Popular posts from this blog

Non-cursor cursors

Spotify analogy doesn't work