forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleanupBuild.ps1
127 lines (110 loc) · 5.79 KB
/
CleanupBuild.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
# ----------------------------------------------------------------------------------
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
.SYNOPSIS
Clean up unrelated files before nuget publish
.PARAMETER GenerateDocumentationFile
Decide whether keeps XML files
#>
[CmdletBinding()]
Param
(
[Parameter()]
[string]$BuildConfig ="Debug",
[Parameter()]
[string]$GenerateDocumentationFile ="false"
)
$output = Join-Path (Get-Item $PSScriptRoot).Parent.FullName "artifacts\$BuildConfig"
Write-Verbose "The output folder is set to $output"
$resourceManagerPath = $output
$outputPaths = @($output)
$resourcesFolders = @("de", "es", "fr", "it", "ja", "ko", "ru", "zh-Hans", "zh-Hant", "cs", "pl", "pt-BR", "tr")
$keepItems = @("*.dll-Help.xml", "Scaffold.xml", "RoleSettings.xml", "WebRole.xml", "WorkerRole.xml")
$removeItems = @("*.lastcodeanalysissucceeded", "*.dll.config", "*.pdb")
if($GenerateDocumentationFile -eq "false"){
Write-Verbose "Removing *.xml files from $output"
$removeItems += "*.xml"
}
$webdependencies = @("Microsoft.Web.Hosting.dll", "Microsoft.Web.Delegation.dll", "Microsoft.Web.Administration.dll", "Microsoft.Web.Deployment.Tracing.dll")
foreach ($path in $outputPaths)
{
Write-Verbose "Removing generated NuGet folders from $path"
Get-ChildItem -Include $resourcesFolders -Recurse -Force -Path $path | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Verbose "Removing autogenerated XML help files, code analysis, config files, and symbols."
Get-ChildItem -Include $removeItems -Exclude $keepItems -Recurse -Path $path | Remove-Item -Force -Recurse
Get-ChildItem -Recurse -Path $path -Include *.dll-Help.psd1 | Remove-Item -Force
Write-Verbose "Removing markdown help files and folders"
Get-ChildItem -Recurse -Path $path -Include *.md | Remove-Item -Force -Confirm:$false
Get-ChildItem -Directory -Include help -Recurse -Path $path | Remove-Item -Force -Recurse -Confirm:$false -ErrorAction "Ignore"
Write-Verbose "Removing unneeded web deployment dependencies"
Get-ChildItem -Include $webdependencies -Recurse -Path $path | Remove-Item -Force
}
$resourceManagerPaths = @($resourceManagerPath)
foreach($RMPath in $resourceManagerPaths)
{
$resourceManagerFolders = Get-ChildItem -Path $RMPath -Directory
foreach ($RMFolder in $resourceManagerFolders)
{
$psd1 = Get-ChildItem -Path $RMFolder.FullName -Filter "$($RMFolder.Name).psd1"
if ($null -eq $psd1)
{
Write-Host "Could not find .psd1 file in folder $RMFolder"
continue
}
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $psd1.DirectoryName -FileName $psd1.Name
$acceptedDlls = @(
# netcoreapp, can't be in RequiredAssemblies, but we need to pack it
"Microsoft.Azure.PowerShell.AuthenticationAssemblyLoadContext.dll",
# customized AutoMapper
"Microsoft.Azure.PowerShell.AutoMapper.dll"
)
# NestedModule Assemblies may have a folder path, just getting the dll name alone
foreach($cmdAssembly in $ModuleMetadata.NestedModules)
{
# if the nested module is script module, we need to keep the dll behind the script module
if ($cmdAssembly.EndsWith(".psm1")) {
if (!$cmdAssembly.Contains("/") -and !$cmdAssembly.Contains("\")) {
$acceptedDlls += "Microsoft.Azure.PowerShell.Cmdlets." + $cmdAssembly.Split(".")[-2] + ".dll"
}
continue
}
if($cmdAssembly.Contains("/")) {
$acceptedDlls += $cmdAssembly.Split("/")[-1]
} else {
$acceptedDlls += $cmdAssembly.Split("\")[-1]
}
}
# RequiredAssmeblies may have a folder path, just getting the dll name alone
foreach($assembly in $ModuleMetadata.RequiredAssemblies)
{
if($assembly.Contains("/")) {
$acceptedDlls += $assembly.Split("/")[-1]
} else {
$acceptedDlls += $assembly.Split("\")[-1]
}
}
Write-Host "Removing redundant dlls in $($RMFolder.Name)"
$removedDlls = Get-ChildItem -Path $RMFolder.FullName -Filter "*.dll" -Recurse | where { $acceptedDlls -notcontains $_.Name -and !$_.FullName.Contains("Assemblies") }
# do not remove lib dlls (for example Az.Accounts/lib/netcoreapp2.1/Azure.Core.dll)
$libPattern = [System.IO.Path]::DirectorySeparatorChar + "lib" + [System.IO.Path]::DirectorySeparatorChar;
$removedDlls = $removedDlls | Where-Object { -not $_.FullName.Contains($libPattern) }
$removedDlls | % { Write-Host "Removing $($_.Name)"; Remove-Item $_.FullName -Force }
Write-Host "Removing scripts and psd1 in $($RMFolder.FullName)"
$exludedPsd1 = @(
"PsSwaggerUtility*.psd1",
"Az.KeyVault.Extension.psd1"
)
$removedPsd1 = Get-ChildItem -Path "$($RMFolder.FullName)" -Include "*.psd1" -Exclude $exludedPsd1 -Recurse | where { $_.FullName -ne "$($RMFolder.FullName)$([IO.Path]::DirectorySeparatorChar)$($RMFolder.Name).psd1" }
$removedPsd1 | % { Write-Host "Removing $($_.FullName)"; Remove-Item $_.FullName -Force }
}
}