-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathupdate-version.ps1
More file actions
162 lines (135 loc) · 5.17 KB
/
update-version.ps1
File metadata and controls
162 lines (135 loc) · 5.17 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
param(
[Parameter(Mandatory = $true)]
[string]$Version
)
function Abort([string]$msg) {
Write-Error $msg
exit 1
}
if ($Version -notmatch '^\d+\.\d+\.\d+$') {
Abort "Version must be major.minor.build (example: 6.0.4). You passed '$Version'."
}
$InformationalVersion = $Version
$AssemblyVersion = "$Version.0"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RelativeFiles = @(
"nuget\TypeGen.nuspec",
"nuget-dotnetcli\dotnet-typegen.nuspec",
"src\TypeGen\TypeGen.Cli\AssemblyInfo.cs",
"src\TypeGen\TypeGen.Core\AssemblyInfo.cs"
)
# Resolve absolute paths
$Files = foreach ($rel in $RelativeFiles) {
if ([System.IO.Path]::IsPathRooted($rel)) {
if (Test-Path $rel) { (Resolve-Path $rel).Path }
} else {
$abs = Join-Path $ScriptDir $rel
if (Test-Path $abs) { (Resolve-Path $abs).Path }
}
}
function Backup-File($Path) {
$stamp = (Get-Date).ToString("yyyyMMddHHmmss")
$bak = "$Path.$stamp.bak"
Copy-Item -Path $Path -Destination $bak -Force
Write-Host "Backup created: $bak"
}
function Update-Nuspec($Path, $NewVersion) {
if (-not (Test-Path $Path)) {
Write-Host "WARN: nuspec not found, skipping: $Path"
return
}
try {
# Read raw bytes and decode using BOM-aware reader
$bytes = [System.IO.File]::ReadAllBytes($Path)
# Let .NET detect BOM by using StreamReader with detectEncodingFromByteOrderMarks = $true
$ms = New-Object System.IO.MemoryStream(,$bytes)
$sr = New-Object System.IO.StreamReader($ms, $true)
$raw = $sr.ReadToEnd()
$sr.Close()
$ms.Close()
# Trim leading whitespace and any garbage before first '<'
$firstLt = $raw.IndexOf('<')
if ($firstLt -gt 0) {
$raw = $raw.Substring($firstLt)
}
# Remove leading BOM char if present (U+FEFF)
if ($raw.Length -gt 0 -and $raw[0] -eq [char]0xFEFF) {
$raw = $raw.Substring(1)
}
# Load cleaned XML string into XmlDocument
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.LoadXml($raw)
# Namespace support
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
if ($xml.DocumentElement -and $xml.DocumentElement.NamespaceURI) {
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
}
# Find <version>
$node = $xml.SelectSingleNode("/package/metadata/version", $ns)
if ($node -eq $null -and $ns.HasNamespace("d")) {
$node = $xml.SelectSingleNode("/d:package/d:metadata/d:version", $ns)
}
if ($node -eq $null) { $node = $xml.SelectSingleNode("//metadata/version", $ns) }
if ($node -eq $null) { $node = $xml.SelectSingleNode("//version", $ns) }
if ($node -eq $null) {
Write-Warning "No <version> element found in $Path; skipping."
return
}
$old = $node.InnerText
$node.InnerText = $NewVersion
# Write formatted XML (UTF8 without BOM)
$settings = New-Object System.Xml.XmlWriterSettings
$settings.Indent = $true
$settings.IndentChars = " "
$settings.NewLineChars = "`r`n"
$settings.NewLineHandling = "Replace"
$settings.OmitXmlDeclaration = $false
$settings.Encoding = New-Object System.Text.UTF8Encoding($false)
$writer = [System.Xml.XmlWriter]::Create($Path, $settings)
try { $xml.Save($writer) } finally { $writer.Close() }
Write-Host "Updated nuspec: $Path (version: $old -> $NewVersion)"
}
catch {
Write-Error ("Failed to update nuspec {0}: {1}" -f $Path, $($_.ToString()))
}
}
function Update-AssemblyInfo($Path, $AsmVer, $FileVer, $InfoVer) {
try {
$content = Get-Content -Path $Path -Raw
$orig = $content
$rules = @(
@{ P = 'AssemblyVersion'; V = $AsmVer }
@{ P = 'AssemblyFileVersion'; V = $FileVer }
@{ P = 'AssemblyInformationalVersion'; V = $InfoVer }
)
foreach ($r in $rules) {
$pattern = "(?m)^\s*\[assembly:\s*(?:System\.Reflection\.)?{0}\s*\(\s*""[^""]*""\s*\)\s*\]\s*$" -f $r.P
$replacement = '[assembly: {0}("{1}")]' -f $r.P, $r.V
if ($content -match $pattern) {
$content = [regex]::Replace($content, $pattern, $replacement)
} else {
$content = $content.TrimEnd() + "`r`n$replacement`r`n"
}
}
if ($content -ne $orig) {
Set-Content -Path $Path -Value $content -Encoding UTF8
Write-Host "Updated AssemblyInfo: $Path"
} else {
Write-Host "No changes: $Path"
}
}
catch {
Write-Error ("Failed to update AssemblyInfo {0}: {1}" -f $Path, $_)
}
}
# Main loop
foreach ($file in $Files) {
Backup-File $file
switch ([System.IO.Path]::GetExtension($file).ToLowerInvariant()) {
".nuspec" { Update-Nuspec $file $InformationalVersion }
".cs" { Update-AssemblyInfo $file $AssemblyVersion $AssemblyVersion $InformationalVersion }
}
}
Write-Host "Done. Updated to version $Version."
exit 0