Skip to content

Instantly share code, notes, and snippets.

@zhangr4
Created February 14, 2025 04:01
Show Gist options
  • Save zhangr4/76895d3eb1c81e15d301f8bdcde73181 to your computer and use it in GitHub Desktop.
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
$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