Skip to content

Commit 50f0992

Browse files
committed
#142 converted to powershell
1 parent 58e52fa commit 50f0992

1 file changed

Lines changed: 102 additions & 90 deletions

File tree

Scripts/update-versions.ps1

Lines changed: 102 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,102 @@
1-
@echo off
2-
setlocal
3-
set return=goto :eof
4-
set checkError=if errorlevel 1 goto :error
5-
6-
cd /d "%~dp0"
7-
8-
echo Getting the version number from version.txt
9-
call :getNewVersion newVersion
10-
%checkError%
11-
12-
echo New Version: %newVersion%
13-
14-
set newVersionCommas=%newVersion:.=,%
15-
16-
rem Non-optimal regexes: useful characters for OR, lookbehind and " are troublesome in batch scripting
17-
set versionNumberRegex=\d+(?:\.\d+){2,3}
18-
set assemblyVersionRegex=(Assembly(?:File)?Version(?:Attribute)?\(.)%versionNumberRegex%
19-
set assemblyVersionReplacement=${1}%newVersion%
20-
21-
set csprojAssemblyVersionRegex=(\u003CAssemblyVersion\u003E)%versionNumberRegex%(\u003C/AssemblyVersion\u003E)
22-
set csprojFileVersionRegex=(\u003CFileVersion\u003E)%versionNumberRegex%(\u003C/FileVersion\u003E)
23-
set csprojProductVersionRegex=(\u003CVersion\u003E)%versionNumberRegex%(\u003C/Version\u003E)
24-
set csprojVersionReplacement=${1}%newVersion%${2}
25-
26-
set wixVersionRegex=(.\?define\s+VERSION\s*=\s*.)%versionNumberRegex%(.\s*\?.)
27-
set wixVersionReplacement=${1}%newVersion%${2}
28-
29-
set txd=Trixter.XDream
30-
31-
echo Updating API
32-
call :updatecsproj "..\%txd%.API\%txd%.API.csproj"
33-
%checkError%
34-
35-
echo Updating Diagnostics
36-
call :updatecsproj "..\%txd%.Diagnostics\%txd%.Diagnostics.csproj"
37-
%checkError%
38-
39-
echo Updating Test Controller
40-
call :updatecsproj "..\%txd%.TestController\%txd%.TestController.csproj"
41-
%checkError%
42-
43-
echo Updating installer project
44-
call :regexReplace "..\%txd%.Installer\Version.wxi" "%wixVersionRegex%" "%wixVersionReplacement%" utf8
45-
%checkError%
46-
47-
echo Done.
48-
49-
%return%
50-
51-
:updatecsproj
52-
53-
rem %1 is the file path of the .csproj file
54-
55-
call :regexReplace "%~1" "%csprojAssemblyVersionRegex%" "%csprojVersionReplacement%" utf8
56-
%checkError%
57-
58-
call :regexReplace "%~1" "%csprojFileVersionRegex%" "%csprojVersionReplacement%" utf8
59-
%checkError%
60-
61-
call :regexReplace "%~1" "%csprojProductVersionRegex%" "%csprojVersionReplacement%" utf8
62-
%checkError%
63-
64-
%return%
65-
66-
:getNewVersion
67-
rem sets the environment variable specified by the first argument to the last value found in the version.txt file
68-
for /f "tokens=*" %%f in ('type %~dp0\version.txt') do set %1=%%f
69-
if "%newVersion%"=="" exit /b 1
70-
%return%
71-
72-
73-
:regexReplace
74-
rem %1 input file path
75-
rem %2 search pattern
76-
rem %3 replacement pattern
77-
rem %4 output encoding
78-
set tempFile=%temp%\bhcvbhcvlhb.tmp
79-
powershell (Get-Content -path "%~1" -Raw -Encoding utf8) -replace '%~2','%~3' ^| out-file "%tempFile%" -encoding %~4 -NoNewline
80-
if errorlevel 1 exit /b 1
81-
82-
copy "%tempFile%" "%~1" > nul
83-
if errorlevel 1 exit /b 1
84-
85-
%return%
86-
87-
:error
88-
echo ERROR
89-
exit /b 1
90-
1+
<#
2+
.SYNOPSIS
3+
Update project and installer version numbers from version.txt.
4+
5+
.DESCRIPTION
6+
Replaces product/version fields in .csproj files and the VERSION define in the installer
7+
Version.wxi. Intended to run from the repository Scripts folder (the script sets location
8+
to its own folder automatically).
9+
10+
.USAGE
11+
.\update-versions.ps1 # reads version from version.txt
12+
.\update-versions.ps1 -NewVersion 1.6.0
13+
#>
14+
15+
param(
16+
[string]$NewVersion
17+
)
18+
19+
Set-StrictMode -Version Latest
20+
21+
try {
22+
# ensure script runs from its directory (matches batch behavior)
23+
Set-Location -Path $PSScriptRoot
24+
25+
if (-not $NewVersion) {
26+
$versionFile = Join-Path $PSScriptRoot 'version.txt'
27+
if (-not (Test-Path $versionFile)) {
28+
Write-Error "version.txt not found at $versionFile"
29+
exit 1
30+
}
31+
32+
$raw = Get-Content -Path $versionFile -Raw -ErrorAction Stop
33+
$NewVersion = $raw.Trim()
34+
}
35+
36+
if ([string]::IsNullOrWhiteSpace($NewVersion)) {
37+
Write-Error 'No version found or provided.'
38+
exit 1
39+
}
40+
41+
Write-Host "New Version: $NewVersion"
42+
43+
# Regex patterns (uses .NET regex)
44+
$versionNumberRegex = '\d+(?:\.\d+){2,3}'
45+
$csprojProductVersionRegex = "(<Version>)$versionNumberRegex(</Version>)"
46+
$csprojVersionReplacement = '${1}' + $NewVersion + '${2}'
47+
48+
$wixVersionRegex = '(<\?define\s+VERSION\s*=\s*")' + $versionNumberRegex + '("\s*\?>)'
49+
$wixVersionReplacement = '${1}' + $NewVersion + '${2}'
50+
51+
$txd = 'Trixter.XDream'
52+
53+
function Replace-InFile {
54+
param(
55+
[Parameter(Mandatory=$true)][string]$Path,
56+
[Parameter(Mandatory=$true)][string]$Pattern,
57+
[Parameter(Mandatory=$true)][string]$Replacement
58+
)
59+
60+
if (-not (Test-Path $Path)) {
61+
Write-Error "File not found: $Path"
62+
return $false
63+
}
64+
65+
# Read/Write using UTF8 to match original script behavior
66+
$content = Get-Content -Path $Path -Raw -Encoding UTF8
67+
$newContent = [regex]::Replace($content, $Pattern, $Replacement)
68+
69+
if ($newContent -ne $content) {
70+
Set-Content -Path $Path -Value $newContent -Encoding UTF8
71+
Write-Host "Updated: $Path"
72+
}
73+
else {
74+
Write-Host "No matching pattern found in: $Path"
75+
}
76+
77+
return $true
78+
}
79+
80+
Write-Host 'Updating API'
81+
$apiProj = Join-Path $PSScriptRoot "..\$txd.API\$txd.API.csproj"
82+
if (-not (Replace-InFile -Path $apiProj -Pattern $csprojProductVersionRegex -Replacement $csprojVersionReplacement)) { exit 1 }
83+
84+
Write-Host 'Updating Diagnostics'
85+
$diagProj = Join-Path $PSScriptRoot "..\$txd.Diagnostics\$txd.Diagnostics.csproj"
86+
if (-not (Replace-InFile -Path $diagProj -Pattern $csprojProductVersionRegex -Replacement $csprojVersionReplacement)) { exit 1 }
87+
88+
Write-Host 'Updating Test Controller'
89+
$tcProj = Join-Path $PSScriptRoot "..\$txd.TestController\$txd.TestController.csproj"
90+
if (-not (Replace-InFile -Path $tcProj -Pattern $csprojProductVersionRegex -Replacement $csprojVersionReplacement)) { exit 1 }
91+
92+
Write-Host 'Updating installer project'
93+
$wxi = Join-Path $PSScriptRoot "..\$txd.Installer\Version.wxi"
94+
if (-not (Replace-InFile -Path $wxi -Pattern $wixVersionRegex -Replacement $wixVersionReplacement)) { exit 1 }
95+
96+
Write-Host 'Done.'
97+
exit 0
98+
}
99+
catch {
100+
Write-Error $_.Exception.Message
101+
exit 1
102+
}

0 commit comments

Comments
 (0)