-
Notifications
You must be signed in to change notification settings - Fork 7
/
Update-ProjectProperty.ps1
112 lines (91 loc) · 3.71 KB
/
Update-ProjectProperty.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
<#
.SYNOPSIS
Updates or removes a project property from a proj file
.EXAMPLE
Update-ProjectProperty -ProjectFilePath C:\code\repo\src\Project1\Project1.csproj -PropertyName CodeAnalysisRuleSet -Remove -PendChange -Verbose
.EXAMPLE
Update-ProjectProperty -ProjectFilePath " C:\code\repo\src\Project1\Project1.csproj" -PropertyName CodeAnalysisRuleSet -Value "SecurityRules.ruleset" -PropertyGroupCondition "== 'CodeAnalysis|" -PendChange -Verbose
#>
function Update-ProjectProperty
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $ProjectFilePath,
[Parameter(Mandatory=$true)]
[string] $PropertyName,
[Parameter(Mandatory=$true, ParameterSetName='Remove')]
[switch] $Remove,
[Parameter(Mandatory=$true, ParameterSetName='Update')]
[string] $Value,
[Parameter(Mandatory=$false)]
[switch] $PendChange,
[Parameter(Mandatory=$false)]
[string] $PropertyGroupCondition
)
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("ErrorAction") -eq $false)
{
$Script:ErrorActionPreference = "Stop"
}
if ($PSCmdlet.ParameterSetName -eq "Update")
{
Write-Verbose "Updating project property $PropertyName for project '$ProjectFilePath' to be '$Value'"
}
else
{
Write-Verbose "Removing project property $PropertyName for project '$ProjectFilePath'"
}
$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
}
$changesMade = $false
foreach ($propertyGroup in $projFileContent.Project.PropertyGroup)
{
if ([string]::IsNullOrWhiteSpace($PropertyGroupCondition) -eq $false)
{
if ($propertyGroup.HasAttribute("Condition") -eq $false)
{
Write-Verbose "Property group condition '$PropertyGroupCondition' is specified, but this group has no condition"
continue
}
$condition = $propertyGroup.GetAttribute("Condition")
if ($condition.IndexOf($PropertyGroupCondition, [StringComparison]::OrdinalIgnoreCase) -lt 0)
{
Write-Verbose "Property group condition '$PropertyGroupCondition' is specified, but this group's condition ('$condition') doesnt match"
continue
}
}
if (-Not ($propertyGroup | Get-Member -Name $PropertyName ))
{
Write-Verbose "Property group does not have the property '$PropertyName'"
continue
}
if ($PSCmdlet.ParameterSetName -eq "Remove")
{
$propertyGroup.ChildNodes | Where-Object {$_.Name -eq $PropertyName} | ForEach-Object {[void]$_.ParentNode.RemoveChild($_)}
$changesMade = $true
}
else
{
$propertyGroup.ChildNodes | Where-Object {$_.Name -eq $PropertyName} | ForEach-Object {$_.InnerText = $Value}
$changesMade = $true
}
}
if ($changesMade -eq $false)
{
Write-Verbose "Project file '$projectFilePath' does not have any changes to make."
Write-Output "Project $ProjectFilePath was not updated"
return
}
Write-Verbose "Saving changes to project file"
$projFileContent.Save($ProjectFilePath)
Write-Output "Project $ProjectFilePath has been updated"
}