-
Notifications
You must be signed in to change notification settings - Fork 7
/
Update-BindingRedirect.ps1
120 lines (93 loc) · 4.32 KB
/
Update-BindingRedirect.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
<#
.SYNOPSIS
This will remove and re-add the binding redirects for each project in a solution. This can only
be executed from within the package manager console. Save all files before executing.
#>
function Update-BindingRedirect
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string]$SpecificProjectNameToTarget
)
$currentHost = Get-Host
Write-Verbose "Current host is '$($currentHost.Name)'"
if ($currentHost.Name -ine "Package Manager Host")
{
throw "This can only be invoked from the package manager console in VS"
}
if ($null -eq $dte)
{
throw "Unable to get a reference to the VS DTE"
}
if ($null -eq $dte.Solution -or (-Not (Test-Path $dte.Solution.FullName)))
{
throw "There must be a valid solution loaded"
}
Write-Verbose "Current solution path '$($dte.Solution.FullName)'"
$projectFiles = Get-ProjectFiles -SolutionFilePath $dte.Solution.FullName
$updatedCount = 0
foreach($projectFile in $projectFiles)
{
$projectFolder = Split-Path -Path $projectFile -Parent
$thisProjectName = [IO.Path]::GetFileNameWithoutExtension($projectFile)
if ([string]::IsNullOrWhiteSpace($SpecificProjectNameToTarget) -eq $false)
{
if ($thisProjectName -ieq $SpecificProjectNameToTarget)
{
Write-Verbose "A specific project name has been requested ($SpecificProjectNameToTarget) and this name ($thisProjectName) matches it"
}
else
{
Write-Verbose "A specific project name has been requested ($SpecificProjectNameToTarget) and this name ($thisProjectName)does not match it"
continue
}
}
$configFilePath = "$projectFolder\app.config"
if (-Not(Test-Path $configFilePath))
{
$configFilePath = "$projectFolder\Web.config"
if (-Not(Test-Path $configFilePath))
{
Write-Verbose "Project file '$projectFile' does not have a config file. Skipping"
continue
}
}
# AutoGenerateBindingRedirects
$autoGenerateBindingRedirect = Get-ProjectPropertyValue -ProjectFilePath $projectFile -PropertyName AutoGenerateBindingRedirects
Write-Verbose "Project file '$projectFile' has a config file at $configFilePath"
$configFileContent = [xml](Get-Content -Path $configFilePath)
Write-Verbose "Config file content loaded"
if (-Not ($configFileContent | Get-Member -Name "configuration"))
{
Write-Verbose "Config file '$configFilePath' does not have a configuration element. Skipping"
continue
}
if (-Not ($configFileContent.configuration | Get-Member -Name "runtime"))
{
Write-Verbose "Config file '$configFilePath' does not have a configuration.runtime element. Skipping"
continue
}
if (-Not ($configFileContent.configuration.runtime | Get-Member -Name "assemblyBinding"))
{
Write-Verbose "Config file '$configFilePath' does not have a configuration.runtime.assemblyBinding element. Skipping"
continue
}
Write-Verbose "Removing assemblyBinding node from configuration.runtime element"
$asmBindingNode = $configFileContent.configuration.runtime.assemblyBinding
$configFileContent.configuration.runtime.RemoveChild($asmBindingNode) | Out-Null
Write-Verbose "Saving changes to file"
$configFileContent.Save($configFilePath)
if ($null -ne $autoGenerateBindingRedirect -and $autoGenerateBindingRedirect -ieq "true")
{
Write-Verbose "Automatic generation of binding redirects is set for this project. Existing bindings `
will be removed but none will be generated to replace them"
}
Edit-TfVcFile -Path $configFilePath
Write-Verbose "Invoking adding of assembly binding redirect"
$projectName = [IO.Path]::GetFileNameWithoutExtension($projectFile)
Add-BindingRedirect -ProjectName $projectName
$updatedCount++
}
Write-Output "Completed, $updatedCount projects updated"
}