Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Last active February 10, 2020 19:59
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thoemmi/a4e5566a53a4b4c72424c59fdbc4eba4 to your computer and use it in GitHub Desktop.
This Powershell scripts generates a nice workflow diagram from a TFS work item template definition file. See http://thomasfreudenberg.com/archive/2018/01/16/generating-workflow-diagram-for-witd/ for details
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, HelpMessage="Path to work item template definition file")]
[string]
$witdPath
)
# path to dot.exe; e.g. use "choco install graphviz"
$dotPath = "C:\ProgramData\chocolatey\bin\dot.exe"
$output = $witdPath + ".dot"
$outputPng = Join-Path $PSScriptRoot "$($witdPath).png"
# colors used for allowed users or groups. I don't expect more then 5 different users/groups
$colorStack = new-object system.collections.stack
$colorStack.Push("indigo")
$colorStack.Push("brown1")
$colorStack.Push("blue")
$colorStack.Push("forestgreen")
$colorStack.Push("red")
# this set will map users/groups to colors
$permissionToColor = @{}
"digraph Workflow {
graph [fontname = ""Calibri""]
node [fontname = ""Calibri""]
edge [fontname = ""Calibri""]
rankdir=LR
#splines=ortho" | Out-File -FilePath $output -Encoding ASCII
# get all edges/transitions
Select-Xml -Path $witdPath -XPath "//TRANSITIONS/TRANSITION" | % {
if ($_.node.for) {
$color = $permissionToColor[$_.node.for]
if (!$color) {
$color = $colorStack.Pop()
$permissionToColor[$_.node.for] = $color
}
} else {
$label = ""
$color = "black"
}
" ""$($_.node.from)"" -> ""$($_.node.to)"" [color=$color]" | Out-File -FilePath $output -Append -Encoding ASCII
}
# if any transition is restricted to certain users/groups, draw a legend
if ($permissionToColor.Count -gt 0) {
' subgraph legend {
node [shape=plaintext]
label = "Legend";
key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0">' | Out-File -FilePath $output -Append -Encoding ASCII
$i = 0
$permissionToColor.GetEnumerator() | % {
" <tr><td align=""right"" port=""i$i"">$($_.Name)</td></tr>" | Out-File -FilePath $output -Append -Encoding ASCII
$i++
}
' </table>>]
key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0">' | Out-File -FilePath $output -Append -Encoding ASCII
$i = 0
$permissionToColor.GetEnumerator() | % {
" <tr><td port=""i$i"">&nbsp;</td></tr>" | Out-File -FilePath $output -Append -Encoding ASCII
$i++
}
' </table>>]' | Out-File -FilePath $output -Append -Encoding ASCII
$i = 0
$permissionToColor.GetEnumerator() | % {
" key:i$($i):e -> key2:i$($i):w [color=$($_.Value)]" | Out-File -FilePath $output -Append -Encoding ASCII
$i++
}
' }' | Out-File -FilePath $output -Append -Encoding ASCII
}
"}" | Out-File -FilePath $output -Append -Encoding ASCII
# call dot.exe to generate the image
& $dotPath -Tpng $output -o $outputPng
# delete the DOT graph file
Remove-Item $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment