Just paste the code below in a file named
project-dependencies.js and read the comment at the top.
var g_solutionFile = "";
var g_includeFilter = [];
var g_excludeFilter = [];
var g_outputFormat = "png";
var g_keepDot = false;
var g_dotApp = "c:\\Program Files\\ATT\\Graphviz\\bin\\dot.exe";
var g_fso = null;
var g_shell = null;
var g_logLevel = "INFO";
String.prototype.trim = function()
{
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function Log(level, message)
{
if ((g_logLevel == "DEBUG") && (level == "TRACE"))
{
return;
}
if ((g_logLevel == "INFO") && ((level == "TRACE") || (level == "DEBUG")))
{
return;
}
if ((g_logLevel == "ERROR") && ((level == "TRACE") || (level == "DEBUG") || (level == "INFO")))
{
return;
}
WScript.Echo(level + ": " + message);
}
function Die(message)
{
Log("ERROR", message);
Log("INFO", "Aborting script.");
WScript.Quit(3);
}
function Exec(app, args)
{
try
{
var code = g_shell.Run("\"" + app + "\" " + args, 0, true);
if (code != 0)
{
Die("Failed to run '" + app + "'; " + code);
}
}
catch (e)
{
Die("Failed to run '" + app + "'; " + e.description);
}
}
function ParseCommandLine()
{
if ( WScript.Arguments.Named.Exists("solution") )
{
g_solutionFile = WScript.Arguments.Named.Item("solution");
}
if (g_solutionFile == null) Die("No solution file specified");
if (g_solutionFile.length <= 0) Die("No solution file specified");
if (!g_fso.FileExists(g_solutionFile)) Die("Solution file does not exist '" + g_solutionFile + "'");
if ( WScript.Arguments.Named.Exists("in") )
{
var filter = WScript.Arguments.Named.Item("in");
if (filter == null) Die("No exclude filter specified");
if (filter.length > 0) g_includeFilter = filter.split(";");
}
if ( WScript.Arguments.Named.Exists("out") )
{
var filter = WScript.Arguments.Named.Item("out");
if (filter == null) Die("No exclude filter specified");
if (filter.length > 0) g_excludeFilter = filter.split(";");
}
if ( WScript.Arguments.Named.Exists("format") )
{
g_outputFormat = WScript.Arguments.Named.Item("format");
if (g_outputFormat == null) Die("No output format specified");
g_outputFormat = g_outputFormat.trim();
if (g_outputFormat.length <= 0) Die("No output format specified");
}
if ( WScript.Arguments.Named.Exists("keep") )
{
g_keepDot = true;
}
if (WScript.Arguments.Named.Exists("dot") )
{
g_dotApp = WScript.Arguments.Named.Item("dot");
}
if (g_dotApp == null) Die("No dot application specified");
if (!g_fso.FileExists(g_dotApp)) Die("Application not found '" + g_dotApp + "'");
}
function PassesFilter(file)
{
if (g_includeFilter.length <= 0)
{
if (g_excludeFilter.length <= 0)
{
return true;
}
else
{
var ii;
for (ii = 0; ii < g_excludeFilter.length; ii++)
{
if (file.indexOf(g_excludeFilter[ii]) >= 0)
{
return false;
}
}
return true;
}
}
else
{
var ii;
for (ii = 0; ii < g_includeFilter.length; ii++)
{
if (file.indexOf(g_includeFilter[ii]) >= 0)
{
if (g_excludeFilter.length <= 0)
{
return true;
}
else
{
var jj;
for (jj = 0; jj < g_excludeFilter.length; jj++)
{
if (file.indexOf(g_excludeFilter[jj]) >= 0)
{
return false;
}
}
return true;
}
}
}
return false;
}
}
function Project(id, name, path)
{
this.id = id;
this.name = name;
this.path = path;
}
Project.prototype.toString = function ()
{
return this.name + " " + this.path + " (" + this.id + ")";
}
g_fso = new ActiveXObject("Scripting.FileSystemObject");
g_shell = new ActiveXObject("WScript.Shell");
ParseCommandLine();
var solutionName = g_fso.GetBaseName(g_solutionFile);
var outputFile = "project-dependencies-" + solutionName;
var dotFile = outputFile + ".dot";
outputFile = outputFile + "." + g_outputFormat;
Log("DEBUG", "Solution file : " + g_solutionFile);
Log("DEBUG", "Output file : " + outputFile);
Log("DEBUG", "Include filter : [" + g_includeFilter + "] (length " + g_includeFilter.length + ")");
Log("DEBUG", "Exclude filter : [" + g_excludeFilter + "] (length " + g_excludeFilter.length + ")");
var lineNr = 0;
var firstLine = true;
var projectsList = new ActiveXObject("Scripting.Dictionary");
var xStartProject = /^\s*Project\s*\(\s*\"\{[A-F0-9\-]{36}\}\"\s*\)\s*=\s*\"(\S+)\"\s*,\s*\"(.*\.(vcproj|csproj))\"\s*,\s*\"\{([A-F0-9\-]{36})\}\"\s*$/i;
var solutionHandle = g_fso.OpenTextFile(g_solutionFile, 1);
while (!solutionHandle.AtEndOfStream)
{
lineNr += 1;
var line = solutionHandle.ReadLine();
line = line.trim();
if (line.length <= 0)
{
// Skip empty lines
continue;
}
if (firstLine)
{
firstLine = false;
var xFirstLine = /Microsoft\s+Visual\s+Studio\s+Solution\s+File\s*,\s*Format\s+Version\s+\d+[,\.]\d+/i
if (!xFirstLine.test(line))
{
if (lineNr != 1)
{
Die("File is not a valid solution file (" + g_solutionFile + ")");
}
// First line might be UTF byte order marker (bom); try next line as well
firstLine = true;
}
continue;
}
if (xStartProject.test(line))
{
var items = line.match(xStartProject);
var id = items[4];
var project = new Project(id, items[1], items[2]);
projectsList.add(id, project);
Log("TRACE", project);
}
}
solutionHandle.Close();
if (projectsList.Count <= 1)
{
Die("Only " + projectsList.Count + " projects found in file '" + g_solutionFile + "'");
}
Log("INFO", "Solution '" + g_solutionFile + "' contains " + projectsList.Count + " projects");
// --- Look up project dependencies -------------------------------------------
Log("INFO", "Writing file '" + dotFile + "'");
var dotHandle = g_fso.CreateTextFile(dotFile, true);
dotHandle.WriteLine("
dotHandle.WriteLine("// Generated by " + "project-dependencies.js");
dotHandle.WriteLine("// Creation date " + Date());
dotHandle.WriteLine("// Solution file " + g_solutionFile);
dotHandle.WriteLine("// Filters in/out [" + g_includeFilter + "] / [" + g_excludeFilter + "]");
dotHandle.WriteLine("//");
dotHandle.WriteLine("digraph ProjectDependencyGraph {");
dotHandle.WriteLine("rankdir=LR;");
dotHandle.WriteLine("node [fontname=\"Arial\",fontsize=10,shape=box,fillcolor=\"#E3E4FA\",style=filled];");
dotHandle.WriteLine("edge [arrowhead=open,fontname=\"Arial\",fontsize=10];");
lineNr = 0;
projectId = "";
parsingDependencies = false;
solutionHandle = g_fso.OpenTextFile(g_solutionFile, 1);
while (!solutionHandle.AtEndOfStream)
{
lineNr += 1;
var line = solutionHandle.ReadLine();
line = line.trim();
if (line.length <= 0)
{
continue;
}
if (projectId.length <= 0)
{
if (xStartProject.test(line))
{
var items = line.match(xStartProject);
var id = items[4];
if (PassesFilter(items[2]))
{
projectId = id;
Log("TRACE", "Project " + items[1] + " passed filter (" + items [2] + ")");
}
else
{
Log("TRACE", "Skipping project " + items[1] + " (" + items [2] + ")");
}
}
}
else
{
var xEndProject = /^\s*EndProject\s*$/i;
if (xEndProject.test(line))
{
projectId = "";
continue;
}
}
if (projectId.length <= 0) continue;
var parentProject = projectsList(projectId);
if (!parsingDependencies)
{
var xStartDependencies = /^\s*ProjectSection\s*\(\s*ProjectDependencies\s*\)\s*=\s*postProject\s*$/i;
if (xStartDependencies.test(line))
{
Log("TRACE", "line " + lineNr + "; start parsing dependencies of project " + parentProject.name);
parsingDependencies = true;
}
continue;
}
else
{
var xEndDependencies = /^\s*EndProjectSection\s*$/i;
if (xEndDependencies.test(line))
{
Log("TRACE", "line " + lineNr + "; end parsing dependencies of project " + parentProject.name);
parsingDependencies = false;
continue;
}
}
if (!parsingDependencies) continue;
var xDependency = /^\s*\ {([A-F0-9\-]{36})\
}\s*=\s*\ {[A-F0-9\-]{36}\
}\s*$/i;
if (!xDependency.test(line)) Die("Failed to match dependency on line " + lineNr + "; \"" + line + "\"");
var id = line.match(xDependency)[1];
childProject = projectsList(id);
dotHandle.WriteLine("\"" + parentProject.name + "\" -> \"" + childProject.name + "\"");
}
solutionHandle.Close();
dotHandle.WriteLine("}");
dotHandle.Close();
Log("INFO", "Running dot...");
var arguments = "-T " + g_outputFormat + " -o \"" + outputFile + "\" \"" + dotFile + "\"";
Log("DEBUG", g_dotApp + " " + arguments);
Exec(g_dotApp, arguments);
if (!g_keepDot)
{
try
{
g_fso.DeleteFile(dotFile);
}
catch (e)
{
Log("WARNING", "Failed to delete file '" + dotFile + "'; " + e.description);
}
}
Log("INFO", "Done (" + outputFile + ")");