|
1 | | -if ($args.Length -eq 0) |
2 | | -{ |
3 | | - Write-Host "examples: |
4 | | -./update-version 2.4.9 # changes the current version to 2.4.9" |
5 | | - exit |
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [string]$Version |
| 4 | +) |
| 5 | + |
| 6 | +function Abort([string]$msg) { |
| 7 | + Write-Error $msg |
| 8 | + exit 1 |
6 | 9 | } |
7 | 10 |
|
8 | | -$versionRegex = "^\d+\.\d+\.\d+$" |
9 | | -$newVersion = $args[0] |
10 | | - |
11 | | -if (-not ($newVersion -match $versionRegex)) { |
12 | | - Write-Host "Wrong version format. Should be: $($versionRegex)" |
13 | | - exit |
14 | | -} |
15 | | - |
16 | | -$oldVersion = (Select-Xml //version "nuget\TypeGen.nuspec")[0].Node.InnerText |
17 | | -$oldVersionMajor = $oldVersion.Split(".")[0] |
18 | | -$oldVersionMinor = $oldVersion.Split(".")[1] |
19 | | - |
20 | | -$newVersionMajor = $newVersion.Split(".")[0] |
21 | | -$newVersionMinor = $newVersion.Split(".")[1] |
22 | | - |
23 | | -$assemblyOldVersion = "$($oldVersionMajor).$($oldVersionMinor).0.0" |
24 | | -$assemblyNewVersion = "$($newVersionMajor).$($newVersionMinor).0.0" |
25 | | - |
26 | | -# replace files' contents |
27 | | - |
28 | | -$nuspecPath = "nuget\TypeGen.nuspec" |
29 | | -if (Test-Path $nuspecPath) { |
30 | | - (Get-Content $nuspecPath) ` |
31 | | - -Replace "<version>$($oldVersion)</version>", "<version>$($newVersion)</version>" ` |
32 | | - | Set-Content $nuspecPath |
33 | | -} |
34 | | - |
35 | | -$dotNetCliNuspecPath = "nuget-dotnetcli\dotnet-typegen.nuspec" |
36 | | -if (Test-Path $dotNetCliNuspecPath) { |
37 | | - (Get-Content $dotNetCliNuspecPath) ` |
38 | | - -Replace "<version>$($oldVersion)</version>", "<version>$($newVersion)</version>" ` |
39 | | - | Set-Content $dotNetCliNuspecPath |
| 11 | +if ($Version -notmatch '^\d+\.\d+\.\d+$') { |
| 12 | + Abort "Version must be major.minor.build (example: 6.0.4). You passed '$Version'." |
40 | 13 | } |
41 | 14 |
|
42 | | -$appConfigPath = "src\TypeGen\TypeGen.Cli\ApplicationConfig.cs" |
43 | | -if (Test-Path $appConfigPath) { |
44 | | - (Get-Content $appConfigPath) ` |
45 | | - -Replace "Version => ""$($oldVersion)""", "Version => ""$($newVersion)""" ` |
46 | | - | Set-Content $appConfigPath |
| 15 | +$InformationalVersion = $Version |
| 16 | +$AssemblyVersion = "$Version.0" |
| 17 | + |
| 18 | +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 19 | + |
| 20 | +$RelativeFiles = @( |
| 21 | + "nuget\TypeGen.nuspec", |
| 22 | + "nuget-dotnetcli\dotnet-typegen.nuspec", |
| 23 | + "src\TypeGen\TypeGen.Cli\AssemblyInfo.cs", |
| 24 | + "src\TypeGen\TypeGen.Core\AssemblyInfo.cs" |
| 25 | +) |
| 26 | + |
| 27 | +# Resolve absolute paths |
| 28 | +$Files = foreach ($rel in $RelativeFiles) { |
| 29 | + if ([System.IO.Path]::IsPathRooted($rel)) { |
| 30 | + if (Test-Path $rel) { (Resolve-Path $rel).Path } |
| 31 | + } else { |
| 32 | + $abs = Join-Path $ScriptDir $rel |
| 33 | + if (Test-Path $abs) { (Resolve-Path $abs).Path } |
| 34 | + } |
47 | 35 | } |
48 | 36 |
|
49 | | -$nugetUpdatePath = "nuget-update.ps1" |
50 | | -if (Test-Path $nugetUpdatePath) { |
51 | | - (Get-Content $nugetUpdatePath) ` |
52 | | - -Replace "TypeGen.$($oldVersion)", "TypeGen.$($newVersion)" ` |
53 | | - -Replace "dotnet-typegen.$($oldVersion)", "dotnet-typegen.$($newVersion)" ` |
54 | | - | Set-Content $nugetUpdatePath |
| 37 | +function Backup-File($Path) { |
| 38 | + $stamp = (Get-Date).ToString("yyyyMMddHHmmss") |
| 39 | + $bak = "$Path.$stamp.bak" |
| 40 | + Copy-Item -Path $Path -Destination $bak -Force |
| 41 | + Write-Host "Backup created: $bak" |
55 | 42 | } |
56 | 43 |
|
57 | | -$applicationConfigPath = "src\TypeGen\TypeGen.Cli\ApplicationConfig.cs" |
58 | | -if (Test-Path $applicationConfigPath) { |
59 | | - (Get-Content $applicationConfigPath) ` |
60 | | - -Replace "Version = ""$($oldVersion)""", "Version = ""$($newVersion)""" ` |
61 | | - | Set-Content $applicationConfigPath |
| 44 | +function Update-Nuspec($Path, $NewVersion) { |
| 45 | + if (-not (Test-Path $Path)) { |
| 46 | + Write-Host "WARN: nuspec not found, skipping: $Path" |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + # Read raw bytes and decode using BOM-aware reader |
| 52 | + $bytes = [System.IO.File]::ReadAllBytes($Path) |
| 53 | + # Let .NET detect BOM by using StreamReader with detectEncodingFromByteOrderMarks = $true |
| 54 | + $ms = New-Object System.IO.MemoryStream(,$bytes) |
| 55 | + $sr = New-Object System.IO.StreamReader($ms, $true) |
| 56 | + $raw = $sr.ReadToEnd() |
| 57 | + $sr.Close() |
| 58 | + $ms.Close() |
| 59 | + |
| 60 | + # Trim leading whitespace and any garbage before first '<' |
| 61 | + $firstLt = $raw.IndexOf('<') |
| 62 | + if ($firstLt -gt 0) { |
| 63 | + $raw = $raw.Substring($firstLt) |
| 64 | + } |
| 65 | + |
| 66 | + # Remove leading BOM char if present (U+FEFF) |
| 67 | + if ($raw.Length -gt 0 -and $raw[0] -eq [char]0xFEFF) { |
| 68 | + $raw = $raw.Substring(1) |
| 69 | + } |
| 70 | + |
| 71 | + # Load cleaned XML string into XmlDocument |
| 72 | + $xml = New-Object System.Xml.XmlDocument |
| 73 | + $xml.PreserveWhitespace = $true |
| 74 | + $xml.LoadXml($raw) |
| 75 | + |
| 76 | + # Namespace support |
| 77 | + $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) |
| 78 | + if ($xml.DocumentElement -and $xml.DocumentElement.NamespaceURI) { |
| 79 | + $ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI) |
| 80 | + } |
| 81 | + |
| 82 | + # Find <version> |
| 83 | + $node = $xml.SelectSingleNode("/package/metadata/version", $ns) |
| 84 | + if ($node -eq $null -and $ns.HasNamespace("d")) { |
| 85 | + $node = $xml.SelectSingleNode("/d:package/d:metadata/d:version", $ns) |
| 86 | + } |
| 87 | + if ($node -eq $null) { $node = $xml.SelectSingleNode("//metadata/version", $ns) } |
| 88 | + if ($node -eq $null) { $node = $xml.SelectSingleNode("//version", $ns) } |
| 89 | + |
| 90 | + if ($node -eq $null) { |
| 91 | + Write-Warning "No <version> element found in $Path; skipping." |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + $old = $node.InnerText |
| 96 | + $node.InnerText = $NewVersion |
| 97 | + |
| 98 | + # Write formatted XML (UTF8 without BOM) |
| 99 | + $settings = New-Object System.Xml.XmlWriterSettings |
| 100 | + $settings.Indent = $true |
| 101 | + $settings.IndentChars = " " |
| 102 | + $settings.NewLineChars = "`r`n" |
| 103 | + $settings.NewLineHandling = "Replace" |
| 104 | + $settings.OmitXmlDeclaration = $false |
| 105 | + $settings.Encoding = New-Object System.Text.UTF8Encoding($false) |
| 106 | + |
| 107 | + $writer = [System.Xml.XmlWriter]::Create($Path, $settings) |
| 108 | + try { $xml.Save($writer) } finally { $writer.Close() } |
| 109 | + |
| 110 | + Write-Host "Updated nuspec: $Path (version: $old -> $NewVersion)" |
| 111 | + } |
| 112 | + catch { |
| 113 | + Write-Error ("Failed to update nuspec {0}: {1}" -f $Path, $($_.ToString())) |
| 114 | + } |
62 | 115 | } |
63 | 116 |
|
64 | | -$typeGenCliCsprojPath = "src\TypeGen\TypeGen.Cli\TypeGen.Cli.csproj" |
65 | | -if (Test-Path $typeGenCliCsprojPath) { |
66 | | - (Get-Content $typeGenCliCsprojPath) ` |
67 | | - -Replace "<AssemblyVersion>$($assemblyOldVersion)</AssemblyVersion>", "<AssemblyVersion>$($assemblyNewVersion)</AssemblyVersion>" ` |
68 | | - -Replace "<FileVersion>$($assemblyOldVersion)</FileVersion>", "<FileVersion>$($assemblyNewVersion)</FileVersion>" ` |
69 | | - -Replace "<Version>$($oldVersion)</Version>", "<Version>$($newVersion)</Version>" ` |
70 | | - | Set-Content $typeGenCliCsprojPath |
71 | | -} |
72 | 117 |
|
73 | | -$typeGenCoreCsprojPath = "src\TypeGen\TypeGen.Core\TypeGen.Core.csproj" |
74 | | -if (Test-Path $typeGenCoreCsprojPath) { |
75 | | - (Get-Content $typeGenCoreCsprojPath) ` |
76 | | - -Replace "<AssemblyVersion>$($assemblyOldVersion)</AssemblyVersion>", "<AssemblyVersion>$($assemblyNewVersion)</AssemblyVersion>" ` |
77 | | - -Replace "<FileVersion>$($assemblyOldVersion)</FileVersion>", "<FileVersion>$($assemblyNewVersion)</FileVersion>" ` |
78 | | - | Set-Content $typeGenCoreCsprojPath |
| 118 | +function Update-AssemblyInfo($Path, $AsmVer, $FileVer, $InfoVer) { |
| 119 | + try { |
| 120 | + $content = Get-Content -Path $Path -Raw |
| 121 | + $orig = $content |
| 122 | + |
| 123 | + $rules = @( |
| 124 | + @{ P = 'AssemblyVersion'; V = $AsmVer } |
| 125 | + @{ P = 'AssemblyFileVersion'; V = $FileVer } |
| 126 | + @{ P = 'AssemblyInformationalVersion'; V = $InfoVer } |
| 127 | + ) |
| 128 | + |
| 129 | + foreach ($r in $rules) { |
| 130 | + $pattern = "(?m)^\s*\[assembly:\s*(?:System\.Reflection\.)?{0}\s*\(\s*""[^""]*""\s*\)\s*\]\s*$" -f $r.P |
| 131 | + $replacement = '[assembly: {0}("{1}")]' -f $r.P, $r.V |
| 132 | + |
| 133 | + if ($content -match $pattern) { |
| 134 | + $content = [regex]::Replace($content, $pattern, $replacement) |
| 135 | + } else { |
| 136 | + $content = $content.TrimEnd() + "`r`n$replacement`r`n" |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if ($content -ne $orig) { |
| 141 | + Set-Content -Path $Path -Value $content -Encoding UTF8 |
| 142 | + Write-Host "Updated AssemblyInfo: $Path" |
| 143 | + } else { |
| 144 | + Write-Host "No changes: $Path" |
| 145 | + } |
| 146 | + } |
| 147 | + catch { |
| 148 | + Write-Error ("Failed to update AssemblyInfo {0}: {1}" -f $Path, $_) |
| 149 | + } |
79 | 150 | } |
80 | 151 |
|
81 | | -# remove old NuGet package |
82 | | - |
83 | | -$oldNupkgPath = "nuget\TypeGen.$($oldVersion).nupkg" |
84 | | -if (Test-Path $oldNupkgPath) { |
85 | | - rm $oldNupkgPath |
| 152 | +# Main loop |
| 153 | +foreach ($file in $Files) { |
| 154 | + Backup-File $file |
| 155 | + switch ([System.IO.Path]::GetExtension($file).ToLowerInvariant()) { |
| 156 | + ".nuspec" { Update-Nuspec $file $InformationalVersion } |
| 157 | + ".cs" { Update-AssemblyInfo $file $AssemblyVersion $AssemblyVersion $InformationalVersion } |
| 158 | + } |
86 | 159 | } |
87 | 160 |
|
88 | | -$oldDotNetCliNupkgPath = "nuget-dotnetcli\dotnet-typegen.$($oldVersion).nupkg" |
89 | | -if (Test-Path $oldDotNetCliNupkgPath) { |
90 | | - rm $oldDotNetCliNupkgPath |
91 | | -} |
| 161 | +Write-Host "Done. Updated to version $Version." |
| 162 | +exit 0 |
0 commit comments