Last active
November 8, 2017 18:18
-
-
Save dtbiedrzycki/64f9dc865e09754b7d52 to your computer and use it in GitHub Desktop.
Will create a plantUml diagram with the references in the Visual Studio solution file
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
<# | |
.SYNOPSIS | |
Creates a PNG diagram of a Visual Studio solution file's references | |
.DESCRIPTION | |
This script uses PlantUml (http://plantuml.com/) to dynamically generate | |
a diagram showing the dependencies of a Visual Studio solution. | |
Blue lines are distinctly colored to depict Project references (vs. dll references). | |
YOU MUST have the Get-VSSolutionReferences.ps1 to use this script! | |
.PARAMETER ImageName | |
[REQUIRED] The name of final image to be produced. | |
.PARAMETER SolutionFile | |
[REQUIRED] The file path to the solution file (.sln) you wish to create a diagram for. | |
.PARAMETER PlantUmlPath | |
[REQUIRED] The file path the to the plantuml.jar file. | |
.PARAMETER RequiredScriptPath | |
The file path to the Get-VSSolutionReferences.ps1 script; a required, dependent script. | |
You can acquire the script at the following link: https://gist.github.com/dtbiedrzycki/815e8c79bee9489658ad | |
Defaults to ".\Get-VSSolutionReferences.ps1". | |
.PARAMETER OutputDirectory | |
The directory to write the produced image to. | |
Defaults to "." | |
.PARAMETER ReferenceNameIncludes | |
An array of reference names to include. | |
Note that only projects with references to one of these items will be included. | |
Note that the names must match exactly. | |
.PARAMETER ReferenceNameLikeIncludes | |
An array of reference names to include using the $string -like syntax. | |
Note that only projects with references to one of these items will be included. | |
Note that "example*" will include all references with a name starting with "example" (search is case insensitive). | |
.PARAMETER ReferenceNameExcludes | |
An array of reference names to exclude. | |
Note that only projects with references that are NOT present in this list will be included. | |
Note that names must match exactly. | |
.PARAMETER ReferenceNameLikeExcludes | |
An array of reference names to exclude using the $string -like syntax. | |
Note that only projects with references that are NOT present in this list will be included. | |
Note that "example*" will exclude all references with a name starting with "example" (search is case insensitive). | |
.PARAMETER OpenOnCompletion | |
If set to $true, the script will open the image with the default image viewing application. | |
Default value is $false. | |
.PARAMETER CleanupOnCompletion | |
If set to $true, the scrip will clean up temporary .pu scripts used to generate the diagram. | |
Default value is $true. | |
.EXAMPLE | |
C:\PS> .\Write-VSSolutionReferenceImage.ps1 -PlantUmlPath .\plantuml.jar -OutputDirectory .\OutputDir -ImageName "MySolutionReferences" -SolutionFile C:\LotsORefs.sln -ReferenceNameLikeExcludes "System*", "*Microsoft*" | |
The following will create a diagram sans any Microsoft dlls | |
.EXAMPLE | |
C:\PS> .\Write-VSSolutionReferenceImage.ps1 -PlantUmlPath .\plantuml.jar -OutputDirectory .\OutputDir -ImageName "MySolutionReferences" -SolutionFile C:\LotsORefs.sln -ReferenceNameLikeIncludes "*Proj*" -ReferenceNameLikeExcludes "*badproj*" | |
The following will create a diagram that includes any projects with refences to a reference of name "*Proj*" but will exclude the subset that has a name of "*badproj*" | |
.NOTES | |
Author: David Thor Biedrzycki | |
Date: Dec 16, 2015 | |
#> | |
[CmdletBinding()] | |
Param ( | |
[parameter(Mandatory=$true)] | |
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })] | |
[string] | |
$PlantUmlPath, | |
[parameter(Mandatory=$true)] | |
[string] | |
$ImageName, | |
[parameter(Mandatory=$true)] | |
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })] | |
[string] | |
$SolutionFile, | |
[parameter(Mandatory=$false)] | |
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })] | |
[string] | |
$RequiredScriptPath = ".\Get-VSSolutionReferences.ps1", | |
[parameter(Mandatory=$false)] | |
[ValidateScript({ Test-Path -Path $_ -PathType Container })] | |
[string] | |
$OutputDirectory = ".\", | |
[parameter(Mandatory=$false)] | |
[string[]] | |
$ReferenceNameIncludes, | |
[parameter(Mandatory=$false)] | |
[string[]] | |
$ReferenceNameLikeIncludes, | |
[parameter(Mandatory=$false)] | |
[string[]] | |
$ReferenceNameExcludes, | |
[parameter(Mandatory=$false)] | |
[string[]] | |
$ReferenceNameLikeExcludes, | |
[parameter(Mandatory=$false)] | |
[bool] | |
$OpenOnCompletion = $false, | |
[parameter(Mandatory=$false)] | |
[bool] | |
$CleanupOnCompletion = $true | |
) | |
# get all references | |
Write-Host "=== Discovering all references..." | |
$refs = & $RequiredScriptPath $SolutionFile | |
# build .pu file | |
$FullFilePath = "$OutputDirectory$ImageName.pu" | |
"@startuml" | Out-File $FullFilePath -encoding utf8 | |
"title $ImageName" | Out-File $FullFilePath -Append -encoding utf8 | |
Write-Host "=== Filtering references..." | |
foreach($ref in $refs) { | |
$referencingProjectPath = $ref.ProjectPath | |
$referencingProjectName = [System.IO.Path]::GetFileNameWithoutExtension($referencingProjectPath) | |
$referencedName = $ref.Name | |
$addToFile = $true | |
if ($ReferenceNameIncludes.Count -gt 0) { | |
$addToFile = $ReferenceNameIncludes -contains $referencedName | |
} | |
if ($ReferenceNameExcludes.Count -gt 0) { | |
$addToFile = $addToFile -and $ReferenceNameExcludes -notcontains $referencedName | |
} | |
if ($ReferenceNameLikeIncludes.Count -gt 0) { | |
$inIncludesList = $false | |
foreach ($include in $ReferenceNameLikeIncludes) { | |
if ($referencedName -like $include) { | |
$inIncludesList = $true | |
} | |
} | |
$addToFile = $addToFile -and $inIncludesList | |
} | |
if ($ReferenceNameLikeExcludes.Count -gt 0) { | |
foreach ($exclude in $ReferenceNameLikeExcludes) { | |
if ($referencedName -like $exclude) { | |
$addToFile = $false | |
break | |
} | |
} | |
} | |
if ($addToFile -eq $true) { | |
$arrowColor = "" | |
if ($ref.ReferenceType -eq "Project") { | |
$arrowColor = "[#blue]" | |
} | |
"[$referencingProjectName] -$arrowColor-> [$referencedName]" | Out-File $FullFilePath -Append -encoding utf8 | |
} | |
} | |
"@enduml" | Out-File $FullFilePath -Append -encoding utf8 | |
# run plantUml | |
Write-Host "=== Creating image..." | |
$ImagePath = "$OutputDirectory$ImageName.png" | |
java -jar $PlantUmlPath -v -o $OutputDirectory $FullFilePath | |
Write-Host "=== Image created at $ImagePath" | |
# remove .pu file | |
if ($CleanupOnCompletion -eq $true) { | |
Write-Host "=== Deleting temporary files ($FullFilePath)..." | |
Remove-Item $FullFilePath | |
} | |
if ($OpenOnCompletion -eq $true) { | |
Write-Host "=== Opening image..." | |
start $ImagePath | |
} | |
Write-Host "=== SCRIPT COMPLETE!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment