-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathShow-SolutionDgml.ps1
254 lines (210 loc) · 7.09 KB
/
Show-SolutionDgml.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<#PSScriptInfo
.VERSION 1.1.1
.AUTHOR Roman Kuzmin
.COPYRIGHT (c) Roman Kuzmin
.TAGS DGML, solution, project, graph
.GUID 5a3b9854-4349-43e6-93f0-599e608ba81c
.LICENSEURI http://www.apache.org/licenses/LICENSE-2.0
.PROJECTURI https://github.com/nightroman/PowerShelf
#>
<#
.Synopsis
Generates and shows the solution project graph.
.Description
For the given solution, the script generates project graph with project
reference links defined in project files and build order links defined
in the solution. Then the generated DGML is opened by the associated
program, normally Visual Studio.
For viewing in Visual Studio ensure:
- Individual components \ Code tools \ DGML editor
.Parameter SolutionPath
Specifies the solution path. If it is omitted or empty then the *.sln
file in the current location is used, there must be one such file.
.Parameter Exclude
Specifies the projects to exclude. Wilcards are supported.
The patterns should match project names without extensions.
.Parameter JustProject
Tells to show just references defined in project files and ignore build
order dependencies in the solution.
.Parameter JustSolution
Tells to show just build order dependencies defined in the solution and
ignore references in project files.
.Link
https://github.com/nightroman/PowerShelf
#>
[CmdletBinding()]
param(
[string]$SolutionPath,
[string[]]$Exclude,
[switch]$JustProject,
[switch]$JustSolution
)
trap {$PSCmdlet.ThrowTerminatingError($_)}
Set-StrictMode -Version Latest
$ErrorActionPreference = 1
# resolve the solution path
if ($SolutionPath) {
$SolutionPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($SolutionPath)
if (![System.IO.File]::Exists($SolutionPath)) {throw "Missing file '$SolutionPath'."}
}
else {
$items = @(Get-Item *.sln)
if ($items.Count -eq 1) {
$SolutionPath = $items[0].FullName
}
elseif (!$items) {
throw 'Cannot find a solution file in the current location.'
}
else {
throw 'Too many solution files in the current location.'
}
}
# read the solution
$solutionText = [System.IO.File]::ReadAllText($SolutionPath)
$Output = "$env:TEMP\$([System.IO.Path]::GetFileNameWithoutExtension($SolutionPath)).dgml"
$RootPath = Split-Path $SolutionPath
$ccprojType = '{CC5FD16D-436D-48AD-A40C-5A424C6E3E79}'
$csprojType = '{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}'
$csprojTypeSdk = '{9A19103F-16F7-4668-BE54-9A1E7A4F7556}'
$folderType = '{2150E333-8FDC-42A3-9474-1A3956D46DE8}'
$fsprojType = '{F2A71F9B-5D33-465A-A702-920D77279786}'
$fsprojTypeSdk = '{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}'
$sfprojType = '{A07B5EB6-E848-4116-A8D0-A826331D98C6}'
$vcxprojType = '{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}'
function Get-ProjectCategory($Project) {
switch($Project.type) {
$ccprojType {return 'CloudService'}
$csprojType {return 'CSharp'}
$csprojTypeSdk {return 'CSharp.Sdk'}
$folderType {return 'Folder'}
$fsprojType {return 'FSharp'}
$fsprojTypeSdk {return 'FSharp.Sdk'}
$sfprojType {return 'ServiceFabric'}
$vcxprojType {return 'CPlusPlusCLI'}
default {return $_}
}
}
$reMatchProject = [regex]'(?msx)^\s* Project \b (.+?) \b ^\s* EndProject \s*$'
$reParseProject = [regex]@'
(?sx)
^\s* \(" ({[^}]+}) "\)
\s* = \s* " ([^"]+) " \s*,
\s* " ([^"]+) " \s*,
\s* " ({[^}]+}) "
\s* (.*)
'@
$reMatchProjectDependencies = [regex]@'
(?sx)
\b ProjectSection \s* \( \s* ProjectDependencies \s* \) \s* = \s* postProject \s*
(.*?)
EndProjectSection
'@
$reMatchProjectDependency = [regex]'(?mx) ^\s* ({[^}]+}) .*$'
$map = @{}
$projectMatches = $reMatchProject.Matches($solutionText)
foreach($match in $projectMatches) {
$text = $match.Groups[1].Value
# parse project text
if ($text -notmatch $reParseProject) {throw "Cannot parse project text: $text"}
$project = [PSCustomObject]@{
# project type GUID
type = $Matches[1]
# name, file base name
name = $Matches[2]
# path, normally relative
path = $Matches[3]
# GUID from the solution
id = $Matches[4]
# project text
body = $Matches[5]
}
# skip folders, for now
if ($project.type -eq $folderType) {continue}
# add project
$map.Add($project.id, $project)
}
$xml = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph GraphDirection="TopToBottom" Layout="Sugiyama">
<Styles>
<Style TargetType="Link">
<Condition Expression="HasCategory('SolutionLink')" />
<Setter Property="StrokeDashArray" Value="3 3" />
</Style>
</Styles>
</DirectedGraph>
'@
$doc = $xml.DocumentElement
$nodes = $doc.AppendChild($xml.CreateElement('Nodes'))
$links = $doc.AppendChild($xml.CreateElement('Links'))
$ns = @{x = 'http://schemas.microsoft.com/developer/msbuild/2003'}
function Test-Exclude($Name) {
foreach($_ in $Exclude) {
if ($Name -like $_) {return $true}
}
}
foreach($project in $map.Values) {
if (Test-Exclude ($project.name)) {continue}
$node = $nodes.AppendChild($xml.CreateElement('Node'))
$node.SetAttribute('Id', $project.name)
$node.SetAttribute('Path', $project.path)
$node.SetAttribute('Category', (Get-ProjectCategory $project))
### links from solution
if (!$JustProject -and $project.body -and $project.body -match $reMatchProjectDependencies) {
foreach($match in $reMatchProjectDependency.Matches($Matches[1])) {
$id = $match.Groups[1].Value
$project2 = $map[$id]
if (Test-Exclude ($project2.name)) {continue}
$link = $links.AppendChild($xml.CreateElement('Link'))
$link.SetAttribute('Source', $project.name)
$link.SetAttribute('Target', $project2.name)
$link.SetAttribute('Category', 'SolutionLink')
}
}
### links from project
if (!$JustSolution) {
# project full path
$projectPath = $project.path
if (![System.IO.Path]::IsPathRooted($projectPath)) {
$projectPath = Join-Path $RootPath $projectPath
}
# read project XML
$xml2 = [xml](Get-Content -LiteralPath $projectPath)
# query project references
if ($xml2.DocumentElement.GetAttribute('Sdk') -eq 'Microsoft.NET.Sdk') {
$references = $xml2 | Select-Xml //ProjectReference/@Include
}
else {
$references = $xml2 | Select-Xml //x:ProjectReference/@Include -Namespace $ns
}
# write project links
foreach ($reference in $references) {
$name2 = [System.IO.Path]::GetFileName($reference.Node.'#text')
$project2 = @(
foreach($_ in $map.Values) {
if ([System.IO.Path]::GetFileName($_.path) -eq $name2) {
$_
}
}
)
# When a project is removed from a solution its references are not
# removed from unloaded projects, so missing links are possible.
if ($project2.Count -eq 0) {
Write-Warning "Cannot find '$name2' referenced by '$projectPath' in the solution."
continue
}
# In theory, we may have several projects with the same name but
# with different extension of path. This is weird, fail for now.
if ($project2.Count -ge 2) {throw "Too many '$name2' in the solution."}
if (Test-Exclude ($project2[0].name)) {continue}
$link = $links.AppendChild($xml.CreateElement('Link'))
$link.SetAttribute('Source', $project.name)
$link.SetAttribute('Target', $project2[0].name)
$link.SetAttribute('Category', 'ProjectLink')
}
}
}
# finish, save, and open the graph
$doc.SetAttribute('xmlns', 'http://schemas.microsoft.com/vs/2009/dgml')
$xml.Save($Output)
Invoke-Item $Output