Free dependency graph generation using PowerShell and yUML

I don't like the idea of paying for (or installing) a tool to just show me some simple relationships between my Visual Studio projects, but it's something that is pretty useful when trying to tidy up a huge legacy codebase.

I whipped together a quick PowerShell script that spits out project references in a format ready for pasting into yuml.me to draw a chart:

function Get-ProjectReferences
{
    param(
        [Parameter(Mandatory)]
        [string]$rootFolder,
        [string[]]$excludeProjectsContaining
    )
    dir $rootFolder -Filter *.csproj -Recurse |
        # Exclude any files matching our rules
        where { $excludeProjectsContaining -notlike "*$($_.BaseName)*" } |
        Select-References
}
function Select-References
{
    param(
        [Parameter(ValueFromPipeline, Mandatory)]
        [System.IO.FileInfo]$project,
        [string[]]$excludeProjectsContaining
    )
    process
    {
        $projectName = $_.BaseName
        [xml]$projectXml = Get-Content $_.FullName
        $ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }
        $projectXml |
            # Find the references xml nodes
            Select-Xml '//defaultNamespace:ProjectReference/defaultNamespace:Name' -Namespace $ns |
            # Get the node values
            foreach { $_.node.InnerText } |
            # Exclude any references pointing to projects that match our rules
            where { $excludeProjectsContaining -notlike "*$_*" } |
            # Output in yuml.me format
            foreach { "[" + $projectName + "] -> [" + $_ + "]" }
    }
}
$excludedProjects = "Test1", "Test2"
Get-ProjectReferences "C:\Users\DanTup\Documents\MyProject" -excludeProjectsContaining $excludedProjects | Out-File "C:\Users\DanTup\Documents\MyProject\References.txt"

The output will look something like this:

 

If you likey, then why not Subscribe to my RSS feed or follow @DanTup on Twitter? :)