-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build-PlantUML.ps1
127 lines (114 loc) · 4.46 KB
/
Build-PlantUML.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
<#
.SYNOPSIS
Send PlantUML scripts to the PlantUML server and download the built diagrams.
.PARAMETER Directory
The directory of PlantUML scripts. It will also be used as the diagram directory. The default is the current directory.
.PARAMETER ScriptExtension
The file extension of PlantUML scripts. The default is `puml`.
.PARAMETER Recurse
Whether to recursively enumerate PlantUML scripts in subdirectories.
.PARAMETER File
The name of a PlantUML script.
.PARAMETER DiagramExtension
The file extension of built diagrams, supporting `png` and `svg`. The default is `png`.
.EXAMPLE
PS> .\Build-PlantUML.ps1
The script uploads all `.puml` files in the current directory to the PlantUML server, and downloads `.png` diagrams to the source directory.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -Recurse
The script uploads all `.puml` files in the current directory and its subdirectories to the PlantUML server, and downloads `.png` diagrams to the source directories.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -ScriptExtension 'txt'
The script uploads all `.txt` files in the current directory to the PlantUML server, and downloads `.png` diagrams to the source directory.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -DiagramExtension 'svg'
The script uploads all `.puml` files in the current directory to the PlantUML server, and downloads `.svg` diagrams to the source directory.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -Directory 'docs'
The script uploads all `.puml` files in the `docs` directory to the PlantUML server, and downloads `.png` diagrams to the source directory.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -File 'hello.puml'
The script uploads the `hello.puml` file to the PlantUML server, and downloads the `hello.png` diagram to the source directory.
.EXAMPLE
PS> .\Build-PlantUML.ps1 -File 'hello.puml' -DiagramExtension 'svg'
The script uploads the `hello.puml` file to the PlantUML server, and downloads the `hello.svg` diagram to the source directory.
.LINK
https://plantuml.com/en/text-encoding
#>
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'Directory')]
[ValidateNotNullOrEmpty()]
[string]$Directory = '.',
[Parameter(ParameterSetName = 'Directory')]
[ValidateNotNullOrEmpty()]
[string]$ScriptExtension = 'puml',
[Parameter(ParameterSetName = 'Directory')]
[switch]$Recurse,
[Parameter(ParameterSetName = 'File', Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$File,
[ValidateSet('png', 'svg')]
[string]$DiagramExtension = 'png'
)
<#
.SYNOPSIS
Format a PlantUML script to a hexadecimal byte string.
#>
function Format-Script {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Content
)
$bytes = $Content | Format-Hex
return [System.BitConverter]::ToString($bytes.Bytes) -replace '-'
}
<#
.SYNOPSIS
Send a PlantUML script to the PlantUML server and download the diagram.
#>
function Build-Script {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Content,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DiagramPath
)
New-Variable -Name 'server' -Value 'https://www.plantuml.com/plantuml' -Option Constant
$encoded = Format-Script -Content $Content
$url = "$server/$DiagramExtension/~h$encoded"
Invoke-WebRequest -Uri $url -OutFile $DiagramPath
}
<#
.SYNOPSIS
Read a PlantUML script file and build the diagram.
#>
function Build-File {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$Path,
[ValidateSet('png', 'svg')]
[string]$DiagramExtension = 'png'
)
try {
$script = Get-Content -Path $Path | Out-String
if (![string]::IsNullOrWhiteSpace($script)) {
$diagram = Join-Path $Path.Directory "$($Path.BaseName).$DiagramExtension"
Build-Script -Content $script -DiagramPath $diagram
Write-Verbose "'$Path' has been built to '$diagram'."
}
} catch {
Write-Host $_ -ForegroundColor Red
}
}
if ($File) {
Build-File -Path $File -DiagramExtension $DiagramExtension
} else {
foreach ($file in Get-ChildItem -Recurse:$Recurse -Path (Join-Path $Directory '*') -Include "*.$ScriptExtension" ) {
Build-File -Path $file -DiagramExtension $DiagramExtension
}
}