forked from wixtoolset/Home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatepackage.ps1
91 lines (80 loc) · 3.83 KB
/
updatepackage.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
param([string]$TargetFolder, [string]$PackageName, [string]$NewVersion)
function updateGlobalJson {
param($targetFile, [string]$packageName, [string]$newVersion)
#Write-Host 'global.json' $targetFile
$changed = $false
$fileContents = [System.IO.File]::ReadAllText($targetFile)
$matches = [System.Text.RegularExpressions.Regex]::Matches($fileContents, "['""]$packageName['""]: *['""]([^'""]*)['""]")
$matches | ForEach-Object {
#$previousVersion = $_.Groups[1]
#Write-Host $previousVersion
$fileContents = $fileContents.Replace($_.Value, """$packageName"": ""$newVersion""")
$changed = $true
}
if ($changed) {
[System.IO.File]::WriteAllText($targetFile, $fileContents)
}
}
function updatePackagesConfig {
param($targetFile, [string]$packageName, [string]$newVersion)
#Write-Host 'packages' $targetFile
$xml = New-Object XML
$xml.Load($targetFile)
$query = "/packages/package[@id='" + $packageName + "']";
$packageNode = $xml.SelectSingleNode($query);
if ($packageNode) {
$previousVersion = $packageNode.Attributes['version'].Value
#Write-Host $previousVersion
if ($previousVersion -ne $newVersion) {
$packageNode.Attributes['version'].Value = $newVersion
$xml.Save($targetFile)
}
}
}
function updateProject {
param($targetFile, [string]$packageName, [string]$newVersion)
#Write-Host 'project' $targetFile
$changed = $false
$fileContents = [System.IO.File]::ReadAllText($targetFile) # project files should be small right?
$oldStyleMatches = [System.Text.RegularExpressions.Regex]::Matches($fileContents, '[\\">]packages\\' + $packageName + '\.([0-9][^\\]*)\\')
$oldStyleMatches | ForEach-Object {
$previousVersion = $_.Groups[1]
#Write-Host $previousVersion
$fileContents = $fileContents.Replace($packageName + '.' + $previousVersion, $packageName + '.' + $newVersion)
$changed = $true
}
# hmmm, don't want to use XML parsing for the whole document because it might make annoying formatting changes throughout the file
# let's assume PackageReference elements don't span multiple lines
$packageReferenceMatches = [System.Text.RegularExpressions.Regex]::Matches($fileContents, '<PackageReference.*' + $packageName + '.*>')
$packageReferenceMatches | ForEach-Object {
$xml = New-Object XML
$xml.LoadXml($_.Value)
$query = "//PackageReference[@Include='" + $packageName + "']";
$packageReferenceNode = $xml.SelectSingleNode($query);
if ($packageReferenceNode) {
$previousVersion = $packageReferenceNode.Attributes['Version'].Value
#Write-Host $previousVersion
if ($previousVersion -ne $newVersion) {
$packageReferenceNode.Attributes['Version'].Value = $newVersion
$fileContents = $fileContents.Replace($_.Value, $packageReferenceNode.OuterXml)
$changed = $true
}
}
}
if ($changed) {
[System.IO.File]::WriteAllText($targetFile, $fileContents)
}
}
function updateNugetPackageVersion {
param([string]$targetFolder, [string]$packageName, [string]$newVersion)
Get-ChildItem -Path $targetFolder -Include global.json -Recurse | ForEach-Object {
updateGlobalJson -targetFile $_ -packageName $packageName -newVersion $newVersion
}
Get-ChildItem -Path $targetFolder -Include packages.config -Recurse | ForEach-Object {
updatePackagesConfig -targetFile $_ -packageName $packageName -newVersion $newVersion
}
Get-ChildItem -Path $targetFolder -Include *.*proj -Recurse | ForEach-Object {
updateProject -targetFile $_ -packageName $packageName -newVersion $newVersion
}
}
updateNugetPackageVersion -targetFolder $TargetFolder -packageName $PackageName -newVersion $NewVersion