Created
February 14, 2025 04:01
-
-
Save zhangr4/76895d3eb1c81e15d301f8bdcde73181 to your computer and use it in GitHub Desktop.
Generate Project Dependency of a CSharp project to dot file, which can be visualized by graphviz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$projects = Get-ChildItem -Recurse -Filter *.csproj # find all csharp project recursively from current working path | |
$dotFile = "dependencies.dot" # file name of generated dot file | |
$edges = @() # represent edges as an array of objects | |
foreach ($project in $projects) { | |
$projectXml = [xml](Get-Content $project.FullName) # parse .csproj as xml | |
$projectDir = Split-Path -Parent $project.FullName | |
$references = $projectXml.Project.ItemGroup.ProjectReference # get project references of $project | |
# generate graph | |
foreach ($reference in $references) { | |
$relativePath = $reference.Include | |
if ($relativePath) { | |
$targetProject = Resolve-Path -Path (Join-Path -Path $projectDir -ChildPath $relativePath) -ErrorAction SilentlyContinue | |
if ($targetProject) { | |
$edges += "`"$($project.BaseName)`" -> `"$([System.IO.Path]::GetFileNameWithoutExtension($targetProject))`";" | |
} | |
} | |
} | |
} | |
# write dotcontent and output to file | |
$dotContent = "digraph Dependencies { | |
rankdir = LR; // Left to Right layout | |
node [style = filled; fillcolor = lightgray; fontname = `"Arial`"; fontsize = 12;]; | |
edge [color = blue; penwidth = 1;];`n" + ($edges -join "`n") + "`n}" | |
$dotContent | Out-File $dotFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment