-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetVersion.ps1
49 lines (39 loc) · 1.23 KB
/
SetVersion.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
# SetVersion.ps1
# Powershell helper script for CI scripts to update .csproj files.
#
# Usage:
# SetVersion -Path <CSPROJ_PATH> [<PROPERTY>=<VALUE>]...
#
# Example:
# SetVersion -Path MyProject/MyProject.csproj Version=$(git describe --tags --abbrev=0)
param(
[Parameter(Mandatory=$true,HelpMessage="Path to the target .csproj file.")][String]$Path="",
[Parameter(ValueFromRemainingArguments=$true)][String[]]$PropertySetters
)
# Resolve Path
$Path = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($(Get-Location), $Path))
"Reading '$Path'"
# Read XML file
[xml]$CONTENT = Get-Content -Path "$Path"
# doing this prevents failure when there are multiple PropertyGroup nodes:
$TARGET_NODE = $CONTENT.SelectSingleNode("//Project/PropertyGroup")
foreach ($SETTER in $PropertySetters)
{
$v = $SETTER -split "=",2
$previousValue = $TARGET_NODE."$($v[0])"
if ($v[1] -eq $previousValue)
{
"$($v[0]) was already set to $previousValue"
continue
}
# Set the property
$TARGET_NODE."$($v[0])" = $v[1]
if ($previousValue.Length -eq 0) {
"Set $($v[0]) to $($v[1])"
}
else {
"Set $($v[0]) to $($v[1]) (Was $previousValue)"
}
}
$CONTENT.Save("$Path")
"Saved '$Path'"