-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Show-BuildMermaid.ps1
167 lines (142 loc) · 3.96 KB
/
Show-BuildMermaid.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<#
.Synopsis
Shows Invoke-Build task graph using Mermaid.
Copyright (c) Roman Kuzmin
.Description
Requirements:
- Invoke-Build command is available for calls
- Internet connection for Mermaid, https://mermaid.js.org
The script calls Invoke-Build in order to get the tasks, generates the HTML
page with Mermaid graph code and scripts and shows the page in the browser.
Tasks without code are shown as ovals, conditional tasks as hexagons, other
tasks as boxes. Safe calls are shown with dotted arrows, regular calls with
solid arrows. Task synopses are shown at the bottom left corner on mouse
hovering over tasks. Job numbers are optionally shown on arrows.
.Parameter File
See: help Invoke-Build -Parameter File
.Parameter Output
Specifies the output HTML file.
The default is in the temp directory.
.Parameter Direction
Specifies the direction, Top-Bottom or Left-Right: TB, BT, LR, RL.
The default is LR.
.Parameter Directive
Specifies the directive text, see Mermaid manuals.
Example: %%{init: {"theme": "forest", "fontFamily": "monospace"}}%%
.Parameter Parameters
Build script parameters needed in special cases when they alter tasks.
.Parameter NoShow
Tells to create the output file without showing it.
Use Output in order to specify the file exactly.
.Parameter Number
Tells to show job numbers on arrows connecting tasks.
.Link
https://github.com/nightroman/Invoke-Build
#>
param(
[Parameter(Position=0)]
[string]$File
,
[Parameter(Position=1)]
[string]$Output
,
[ValidateSet('TB', 'BT', 'LR', 'RL')]
[string]$Direction = 'LR'
,
[string]$Directive
,
[hashtable]$Parameters
,
[switch]$NoShow
,
[switch]$Number
)
$ErrorActionPreference = 1
function Escape-Text($Text) {
$Text.Replace('"', '#quot;').Replace('<', '#lt;').Replace('>', '#gt;')
}
### resolve output
if ($Output) {
$Output = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Output)
}
else {
$path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($(if ($File) {$File} else {''}))
$name = [System.IO.Path]::GetFileNameWithoutExtension($path)
$hash = [System.IO.Path]::GetFileName([System.IO.Path]::GetDirectoryName($path))
$Output = [System.IO.Path]::GetTempPath() + "$name-$hash.html"
}
### get tasks
if (!$Parameters) {$Parameters = @{}}
$all = Invoke-Build ?? $File @Parameters
### for synopses
$docs = @{}
. Invoke-Build
### make text
$map = @{}
$text = @(
if ($Directive) {$Directive}
"graph $($Direction.ToUpper())"
$id = 0
foreach($it in $all.get_Values()) {
++$id
$name = $it.Name
$map[$name] = $id
$name = Escape-Text $name
$hasScript = foreach($job in $it.Jobs) {if ($job -is [scriptblock]) {1; break}}
if (!$hasScript) {
"$id([`"$name`"])"
}
elseif ($it.Inputs -or !(-9).Equals($it.If)) {
"$id{{`"$name`"}}"
}
else {
"$id[`"$name`"]"
}
if ($synopsis = Get-BuildSynopsis $it $docs) {
$synopsis = Escape-Text $synopsis
"click $id callback `"$synopsis`""
}
}
$id = 0
foreach($it in $all.get_Values()) {
++$id
$jobNumber = 0
foreach($job in $it.Jobs) {
++$jobNumber
if ($job -is [string]) {
$job, $safe = if ($job[0] -eq '?') {$job.Substring(1), 1} else {$job}
$id2 = $map[$job]
$arrow = if ($safe) {'-.->'} else {'-->'}
$text = if ($Number) {"|$jobNumber|"} else {''}
'{0} {1} {2} {3}' -f $id, $arrow, $text, $id2
}
}
}
)
### write HTML
@(
@"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>$([System.IO.Path]::GetFileNameWithoutExtension($Output)) tasks</title>
</head>
<body>
<pre class="mermaid">
"@
$text
@'
</pre>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
</body>
</html>
'@
) | Set-Content -LiteralPath $Output -Encoding UTF8
### show file
if (!$NoShow) {
Invoke-Item -LiteralPath $Output
}