-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apply-AssemblyVersionAndDefaults.ps1
70 lines (54 loc) · 2.5 KB
/
Apply-AssemblyVersionAndDefaults.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
param (
[Parameter(Mandatory = $true)] [string] $buildNumber,
[Parameter(Mandatory = $true)] [string] $solutionDirectory,
[Parameter(Mandatory = $true)] [string] $UKHOAssemblyCompany,
[Parameter(Mandatory = $true)] [string] $UKHOAssemblyCopyright,
[Parameter(Mandatory = $true)] [string] $UKHOAssemblyVersionPrefix,
[Parameter(Mandatory = $true)] [string] $UKHOAssemblyProduct
)
$buildNumberRegex = "(.+)_(\d{3,5}).(\d{1,2})"
$validBuildNumber = $buildNumber -match $buildNumberRegex
if ($validBuildNumber -eq $false) {
Write-Error "Build number passed in must be in the following format: (BuildDefinitionName)_(date:yy)(DayOfYear).(rev:.r)"
return
}
# Magic var $Matches comes from the above regex match statement: $buildNumber -match $buildNumberRegex
$buildRevisionNumber = $Matches.2 + "." + $Matches.3
$versionToApply = $UKHOAssemblyVersionPrefix + $buildRevisionNumber
$UKHOAssemblyCopyright = "$UKHOAssemblyCopyright $(Get-Date -Format "yyyy")"
$assemblyValues = @{
"Company" = $UKHOAssemblyCompany;
"Copyright" = $UKHOAssemblyCopyright;
"Description" = $UKHOAssemblyProduct;
"Product" = $UKHOAssemblyProduct;
"AssemblyVersion" = $versionToApply;
"FileVersion" = $versionToApply;
"Version" = $versionToApply;
}
function UpdateOrAddAttribute($xmlContent, $assemblyKey, $newValue, $namespace) {
$propertyGroup = $xmlContent.Project.PropertyGroup
if ($propertyGroup -is [array]) {
$propertyGroup = $propertyGroup[0]
}
$propertyGroupNode = $propertyGroup.$assemblyKey
if ($null -ne $propertyGroupNode) {
Write-Host "Assembly key $assemblyKey has been located in source file - updating with value: " + $newValue
$propertyGroup.$assemblyKey = $newValue
return $xmlContent
}
Write-Host "Assembly key $assemblyKey could not be located in source file - appending value " + $newValue
$newChild = $xmlContent.CreateElement($assemblyKey, $namespace)
$newChild.InnerText = $newValue
$propertyGroup.AppendChild($newChild)
return $propertyGroupNode
}
(Get-ChildItem -Path $solutionDirectory -File -Filter "*.csproj" -Recurse) | ForEach-Object {
$file = $_
Write-Host "Updating assembly file at path: $file"
[xml]$xmlContent = (Get-Content $file.FullName)
$assemblyValues.Keys | ForEach-Object {
$key = $_
UpdateOrAddAttribute $xmlContent $key $assemblyValues[$key] $xmlContent.DocumentElement.NamespaceURI
}
$xmlContent.Save($file.FullName)
}