-
Notifications
You must be signed in to change notification settings - Fork 7
/
Remove-ProjectConfiguration.ps1
121 lines (97 loc) · 4.26 KB
/
Remove-ProjectConfiguration.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
<#
.SYNOPSYS
Removes a specific configuration from a project.
.PARAMETER $ProjFilePath
The path to the project file
.PARAMETER $BuildConfiguration
Debug, Release, etc
.PARAMETER $PlatformTarget
x86, x64, AnyCPU
.PARAMETER $RemovedElementPath
If specified, the configuration elements that are removed are written to this path. It can be a file or folder. If folder, a file will be created with the removed config elements. If it is a file, the file will be appended.
.NOTE
Probably shouldnt run this while VS has the project open.
.NOTE
This would be part of an eventual "Normalize-ProjectConfiguration" that would update a solution and all projects to use the same set of build configs
#>
function Remove-ProjectConfiguration
{
[CmdletBinding()]
Param(
[string] $ProjectFilePath,
[string] $BuildConfiguration,
[string] $PlatformTarget,
[Parameter(Mandatory=$false)]
[string] $RemovedElementPath
)
Write-Verbose "Removing project configuration $BuildConfiguration/$PlatformTarget from '$ProjectFilePath'"
$configBackupPath = ""
if ([string]::IsNullOrWhiteSpace($RemovedElementPath) -eq $false)
{
$info = [System.IO.DirectoryInfo]::new($RemovedElementPath)
if ($info.Exists -and (Get-Item -Path $info).PSIsContainer)
{
$configBackupPath = Join-Path -Path $RemovedElementPath -ChildPath ("RemovedProjectConfigs$((Get-Date).ToFileTime()).txt")
Write-Verbose "Removed element backup path is a folder, using '$configBackupPath' to write the backup."
}
elseif ([io.path]::GetDirectoryName($RemovedElementPath) -ine $RemovedElementPath)
{
$configBackupPath = $RemovedElementPath
Write-Verbose "Removed element backup path is an existing file, using '$configBackupPath' to write the backup."
}
else
{
throw "Removed element backup path appears to be a folder, but the folder does not exist. Please create it before using it as a parameter"
}
}
Write-Verbose "Getting project file contents"
$projFileContent = [xml](Get-Content -Path $ProjectFilePath)
if (-Not ($projFileContent | Get-Member -Name Project))
{
Write-Warning "Project file '$projectFilePath' does not have a <project> element"
return
}
if (-Not ($projFileContent.Project | Get-Member -Name PropertyGroup))
{
Write-Warning "Project file '$projectFilePath' does not have any <propertygroup> elements"
return
}
$propertyGroupCondition = "'`$(Configuration)|`$(Platform)' == '$BuildConfiguration|$($PlatformTarget.Replace(" ",[string]::Empty))'"
$propertyGroupElementToRemove = $null
foreach ($propertyGroup in $projFileContent.Project.PropertyGroup)
{
if ($propertyGroup.HasAttribute("Condition") -eq $false)
{
Write-Verbose "Property group has no condition"
continue
}
$condition = $propertyGroup.GetAttribute("Condition")
if ($condition -ine $propertyGroupCondition)
{
Write-Verbose "Property group condition does not match, skipping"
continue
}
$propertyGroupElementToRemove = $propertyGroup
break
}
if ($null -eq $propertyGroupElementToRemove)
{
Write-Output "No property group element was found to remove from '$ProjFilePath' for '$propertyGroupCondition'"
return
}
if ([string]::IsNullOrWhiteSpace($configBackupPath) -eq $false)
{
Add-Content -Path $configBackupPath -Value $propertyGroupElementToRemove.OuterXml
Write-Output "Backup of removed element saved to '$configBackupPath'"
}
Write-Verbose "Removing node from parent"
$propertyGroupElementToRemove.ParentNode.RemoveChild($propertyGroupElementToRemove)
if ((Get-ItemProperty $ProjectFilePath -Name IsReadOnly).IsReadOnly -eq $true)
{
Write-Verbose "Removing readonly flag on file"
Set-ItemProperty $ProjectFilePath -Name IsReadOnly -Value $false
}
Write-Verbose "Saving file"
$projFileContent.Save($ProjectFilePath)
Write-Output "Removed project configuration $BuildConfiguration/$PlatformTarget from '$ProjectFilePath'"
}