forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateFilterMappings.ps1
317 lines (279 loc) · 9.72 KB
/
CreateFilterMappings.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<#
Returns an ordered hashtable with the following paths having empty mappings:
- All files at the root of the repository
- All folders at the root of the repository (except "src")
- All files in the "src" folder
#>
function Initialize-Mappings
{
param
(
[Parameter(Mandatory = $false)]
[string[]]$PathsToIgnore,
[Parameter(Mandatory = $false)]
[Hashtable]$CustomMappings
)
$Mappings = [ordered]@{}
Get-ChildItem -Path $Script:RootPath -File | ForEach-Object { $Mappings[$_.Name] = @() }
Get-ChildItem -Path $Script:RootPath -Directory | Where-Object { $_.Name -ne "src" } | ForEach-Object { $Mappings[$_.Name] = @() }
Get-ChildItem -Path $Script:SrcPath -File | ForEach-Object { $Mappings["src/$_.Name"] = @() }
if ($CustomMappings -ne $null)
{
$CustomMappings.GetEnumerator() | ForEach-Object { $Mappings[$_.Name] = $_.Value }
}
if ($null -ne $PathsToIgnore)
{
foreach ($Path in $PathsToIgnore)
{
$Mappings[$Path] = $null
$Mappings.Remove($Path)
}
}
return $Mappings
}
<#
Converts a hashtable into a compressed JSON and formats it for display.
#>
function Format-Json
{
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[hashtable]$InputObject
)
$Tab = " "
return $InputObject | ConvertTo-Json -Depth 4 -Compress | ForEach-Object { $_.Replace("{", "{`n$Tab").Replace("],", "],`n$Tab").Replace(":[", ":[`n$Tab$Tab").Replace("`",", "`",`n$Tab$Tab").Replace("`"]", "`"`n$Tab]").Replace("]}", "]`n}") }
}
<#
Turns a file path into a normalized key (strips out everything before "src").
#>
function Create-Key
{
param
(
[Parameter(Mandatory = $true)]
[string]$FilePath
)
$Key = ""
$TempFilePath = $FilePath
while ($true)
{
$TempItem = Get-Item -Path $TempFilePath
$Name = $TempItem.Name
$Key = $Name + "/" + $Key
if ($Name -eq "src")
{
break
}
if ($null -ne $TempItem.Parent)
{
$TempFilePath = $TempItem.Parent.FullName
}
else
{
$TempFilePath = $TempItem.Directory.FullName
}
}
return $Key
}
function Create-ProjectToFullPathMappings
{
$Mappings = [ordered]@{}
foreach ($ServiceFolder in $Script:ServiceFolders)
{
$CsprojFiles = Get-ChildItem -Path $ServiceFolder -Filter "*.csproj" -Recurse
foreach ($CsprojFile in $CsprojFiles)
{
$Mappings[$CsprojFile.BaseName] = $CsprojFile.FullName
}
}
return $Mappings
}
<#
Creates a mapping from a solution file to the projects it references. For example:
{
"C:\\azure-powershell\\src\\Aks\\Aks.sln":[
"Commands.Aks",
"Commands.Resources.Rest",
"Commands.Resources"
],
"C:\\azure-powershell\\src\\AnalysisServices\\AnalysisServices.sln":[
"Commands.AnalysisServices",
"Commands.AnalysisServices.Dataplane"
],
...
}
#>
function Create-SolutionToProjectMappings
{
$Mappings = [ordered]@{}
foreach ($ServiceFolder in $Script:ServiceFolders)
{
$SolutionFiles = Get-ChildItem -Path $ServiceFolder.FullName -Filter "*.sln"
foreach ($SolutionFile in $SolutionFiles)
{
$Mappings = Add-ProjectDependencies -Mappings $Mappings -SolutionPath $SolutionFile.FullName
}
}
return $Mappings
}
<#
Parses a solution file to find the projects it is composed of (excluding common projects).
#>
function Add-ProjectDependencies
{
param
(
[Parameter(Mandatory = $true)]
[hashtable]$Mappings,
[Parameter(Mandatory = $true)]
[string]$SolutionPath
)
$CommonProjectsToIgnore = @("ScenarioTest.ResourceManager", "TestFx", "Tests" )
$ProjectDependencies = @()
$Content = Get-Content -Path $SolutionPath
$Content | Select-String -Pattern "`"[a-zA-Z0-9.]*`"" | ForEach-Object { $_.Matches[0].Value.Trim('"') } | Where-Object { $CommonProjectsToIgnore -notcontains $_ } | ForEach-Object { $ProjectDependencies += $_ }
$Mappings[$SolutionPath] = $ProjectDependencies
return $Mappings
}
<#
Creates a mapping from a project to its parent solution. For example:
{
"Commands.Aks":[
"C:\\azure-powershell\\src\\Aks\\Aks.sln"
],
"Commands.AnalysisServices":[
"C:\\azure-powershell\\src\\AnalysisServices\\AnalysisServices.sln"
],
"Commands.AnalysisServices.Dataplane":[
"C:\\azure-powershell\\src\\AnalysisServices\\AnalysisServices.sln"
],
...
}
#>
function Create-ProjectToSolutionMappings
{
$Mappings = [ordered]@{}
foreach ($ServiceFolder in $Script:ServiceFolders)
{
$Mappings = Add-SolutionReference -Mappings $Mappings -ServiceFolderPath $ServiceFolder.FullName
}
return $Mappings
}
<#
Map a project to the solution file it should be build with (e.g., Commands.Compute --> src/Compute/Compute.sln)
#>
function Add-SolutionReference
{
param
(
[Parameter(Mandatory = $true)]
[hashtable]$Mappings,
[Parameter(Mandatory = $true)]
[string]$ServiceFolderPath
)
.($PSScriptRoot + "\PreloadToolDll.ps1")
$CsprojFiles = Get-ChildItem -Path $ServiceFolderPath -Filter "*.csproj" -Recurse | Where-Object { (-not [Tools.Common.Utilities.ModuleFilter]::IsAzureStackModule($_.FullName)) -and $_.FullName -notlike "*.Test*" }
foreach ($CsprojFile in $CsprojFiles)
{
$Key = $CsprojFile.BaseName
$Mappings[$Key] = @()
$Script:SolutionToProjectMappings.Keys | Where-Object { $Script:SolutionToProjectMappings[$_] -contains $Key } | ForEach-Object { $Mappings[$Key] += $_ }
}
return $Mappings
}
<#
Creates the ModuleMappings.json file used during the build to filter StaticAnalysis and help generation by module.
#>
function Create-ModuleMappings
{
$PathsToIgnore = @("tools")
$CustomMappings = @{}
$Script:ModuleMappings = Initialize-Mappings -PathsToIgnore $PathsToIgnore -CustomMappings $CustomMappings
foreach ($ServiceFolder in $Script:ServiceFolders)
{
$Key = "src/$($ServiceFolder.Name)/"
$ModuleManifestFiles = Get-ChildItem -Path $ServiceFolder.FullName -Filter "*.psd1" -Recurse | Where-Object { $_.FullName -notlike "*.Test*" -and `
$_.FullName -notlike "*Release*" -and `
$_.FullName -notlike "*Debug*" -and `
$_.Name -like "Az.*" }
if ($null -ne $ModuleManifestFiles)
{
$Value = @()
$ModuleManifestFiles | ForEach-Object { $Value += $_.BaseName }
$Script:ModuleMappings[$Key] = $Value
}
}
}
<#
Creates the CsprojMappings.json file used during the build to filter the build step by project.
#>
function Create-CsprojMappings
{
$PathsToIgnore = @("tools")
$CustomMappings = @{}
$Script:CsprojMappings = Initialize-Mappings -PathsToIgnore $PathsToIgnore -CustomMappings $CustomMappings
foreach ($ServiceFolder in $Script:ServiceFolders)
{
Add-CsprojMappings -ServiceFolderPath $ServiceFolder.FullName
}
}
<#
Maps a normalized path to the projects to be built based on the service folder provided.
#>
function Get-ModuleFromPath
{
param
(
[Parameter(Mandatory = $true)]
[string]$FilePath
)
return $FilePath.Replace('/', '\').Split('\src\')[1].Split('\')[0]
}
function Add-CsprojMappings
{
param
(
[Parameter(Mandatory = $true)]
[string]$ServiceFolderPath
)
$Key = Create-Key -FilePath $ServiceFolderPath
$CsprojFiles = Get-ChildItem -Path $ServiceFolderPath -Filter "*.csproj" -Recurse
if ($null -ne $CsprojFiles)
{
$Values = New-Object System.Collections.Generic.HashSet[string]
foreach ($CsprojFile in $CsprojFiles)
{
$Project = Get-ModuleFromPath $CsprojFile.FullName
foreach ($ProjectName in $Script:ProjectToSolutionMappings.Keys)
{
foreach ($Solution in $Script:ProjectToSolutionMappings[$ProjectName])
{
$ProjectNameFromSolution = Get-ModuleFromPath $Solution
if ($ProjectNameFromSolution -eq $Project)
{
foreach ($ReferencedProject in $Script:SolutionToProjectMappings[$Solution])
{
$TempValue = $Script:ProjectToFullPathMappings[$ReferencedProject]
if (-not [string]::IsNullOrEmpty($TempValue))
{
$Values.Add($TempValue) | Out-Null
}
}
}
}
}
}
$Script:CsprojMappings[$Key] = $Values
}
}
$Script:RootPath = (Get-Item -Path $PSScriptRoot).Parent.FullName
$Script:SrcPath = Join-Path -Path $Script:RootPath -ChildPath "src"
$Script:ServiceFolders = Get-ChildItem -Path $Script:SrcPath -Directory
$Script:ProjectToFullPathMappings = Create-ProjectToFullPathMappings
$Script:SolutionToProjectMappings = Create-SolutionToProjectMappings
$Script:ProjectToSolutionMappings = Create-ProjectToSolutionMappings
# Create-ModuleMappings
Create-CsprojMappings
# $Script:ModuleMappings | Format-Json | Set-Content -Path (Join-Path -Path $Script:RootPath -ChildPath "ModuleMappings.json")
$Script:CsprojMappings | Format-Json | Set-Content -Path (Join-Path -Path $Script:RootPath -ChildPath "CsprojMappings.json")