13,199,311 members (64,739 online)
alternative version

#### Stats

32.1K views
32 bookmarked
Posted 17 Aug 2007

# Visualizing Project Dependencies Automatically

, 4 Sep 2007
 Rate this:
Have a large code tree? Wondering which projects refer to which other ones? Manually run this console app, schedule it to run nightly or after each build.

## Introduction

Developer 1: Hey I want to make some changes to CommonLibrary1. Is that going to affect the 15 other projects people are working on?

Developer 2: I dunno, ask John.

Developer 1: He told me to ask you.

Sound familiar? I wanted a quick easy, foolproof way to visualize project dependencies without depending on human intervention, i.e. some poor sap typing stuff into a Word document which would probably be wrong before he saved the document.

This project attempts to solve that by taking a directory location and an output JPG location as parameters. It then scans the directories getting all the *.csproj files and determines their relationships. It then uses the graphviz package to visualize these dependencies quickly and easily with fancy schmancy arrows and circles.

## Background

I thought for sure that someone would have done this already but I could only find something similar for C++ apps. Even if there was an existing thing that did this, chances are it wouldn't work for the code structure I like to use. Different companies like to do their references and what not differently, so it would be nice to own the code and modify it according to personal preference... hence this project was born.

I like to put in post build events for code that will copy the output DLLs to a directory called References. That way, whether it is a debug or release compile, the referencing projects all point to the same place and everyone has consistent references. This project assumes that structure so if you have something different then you will need to make code changes. Don't worry, I have included 3 sample projects and full unit tests so you can figure out how it's supposed to work first, then change it to suit your needs. You'll need Nunit to run the tests. It's free, get it.

## Using the Code

In the DependencyTracker/Launch directory, there is a *.bat file which will generate two JPG files: one for the library code projects only and one that generates a JPG for all projects. By changing the search directory and output JPG locations, you can customize it to your scenario and then schedule it to run on whatever schedule works best. Output to a network drive which happens to be on a Web server and you have instant up to date documentation which is easily accessible.

Most of the magic happens in Project.cs when it is trying to find the references:

/// <span class="code-SummaryComment"><summary></span>
/// Load project file from disk and get list of project paths
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><returns></returns></span>
private string[] GetReferenceProjectPaths()
{
XmlDocument project = new XmlDocument();
XmlNamespaceManager namespaceManager =
new XmlNamespaceManager(project.NameTable);
"ns",
"http://schemas.microsoft.com/developer/msbuild/2003");

Nothing earth shattering, just parsing the *.csproj file which is XML and looking for the references. The only complication is that the reference is stored as a relative file path, but by changing the current directory to where the project is and then just browsing to that path, we can get to the referenced DLL pretty easy. Then we just have to figure out where the source project is given the compiled DLL's location. As mentioned previously, I like to keep a consistent "References" folder so it isn't too difficult to find the referenced project file from there. If you have a different structure, you will need to make changes here.

After all of this is loaded in memory, we then call out to a command line program to launch the graphviz image generation based on a file that is output below. We must recursively parse all the various projects and dependencies that have been loaded and take care not to process the same one twice so we don't get phantom arrows on the graph. The basic format of the graphviz program is to take a *.dot file which has lines like "a -> b" which means a points to b.

/// <span class="code-SummaryComment"><summary></span>
/// This will output all dependencies since that is what we care about.
/// Will need some additional work if we want to display
/// straggler projects with no dependencies also.
/// <span class="code-SummaryComment"></summary></span>
private void AppendProjectLinks(StringBuilder sb, ProjectList projects)
{
//loop through every project and output its dependencies to the
//file in the format
//parent -> child
//order doesn't matter for dot files
foreach (Project project in projects)
foreach (Project reference in project.ReferencedProjects)
{
//if not processed output link and add to processed list
project.Name + "-" + reference.Name)
)
{
sb.Append(
"\"" + project.Name + "\" -> \"" +
reference.Name + "\"" + Environment.NewLine
);
}

}
}

## Points of Interest

All the tests use relative paths so it shouldn't matter where you unzip the files to, they should work as long as you don't move any of the folders around in the zip file.

## History

• 17-August-2007 - Initial version
• 04-September-2007 - Changed dot.exe launch method to use registry key

## About the Author

 United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

## Comments and Discussions

 First Prev Next
 Nice Job 377988621-May-08 6:25 3779886 21-May-08 6:25
 Fixed infinite loop problem Doug in NH12-Sep-07 2:55 Doug in NH 12-Sep-07 2:55
 Re: Fixed infinite loop problem Paul B.12-Sep-07 4:04 Paul B. 12-Sep-07 4:04
 To add support for VB Projects... Doug in NH12-Sep-07 2:47 Doug in NH 12-Sep-07 2:47
 Too much hard coded stuff Vagif Abilov4-Sep-07 2:13 Vagif Abilov 4-Sep-07 2:13
 Well, this is not type of code you distribute: //call out to graphviz to create JPG file System.Diagnostics.Process.Start( @"C:\Program Files\ATT\Graphviz\bin\dot.exe", "-Tjpg \"" + DOTFile + "\" \"-o" + OutputJPGFilePath + "\"" ); Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.
 Re: Too much hard coded stuff Paul B.4-Sep-07 3:59 Paul B. 4-Sep-07 3:59
 Bug on non english OS Lukas Ith20-Aug-07 0:59 Lukas Ith 20-Aug-07 0:59
 Re: Bug on non english OS Paul B.20-Aug-07 3:26 Paul B. 20-Aug-07 3:26
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Oct-17 9:21 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.