这是我最终在 PowerShell 中完成的 StampVer 的替代方案,它在构建之前修改 .rc 文件。我对用足够的空间预填充版本字符串的 StampVer 约束感到不满意。
#################################################################
#
# Patch all of the given *.rc files and set
# the version strings for DLLs and OCX controls.
#
#################################################################
# Hand parse the arguments so we can separate them with spaces.
$files = @()
$previousArg = "__"
foreach ($arg in $args)
{
if ($previousArg -eq "-version")
{
$version = $arg
}
elseif ($previousArg -eq "__")
{
}
else
{
$files += $arg
}
$previousArg = $arg
}
Function PatchRCFiles([string]$version, [string[]]$files)
{
# check the version number
if ( $version -match "[0-9]+.[0-9]+.[0-9]+.[0-9]+" )
{
echo "Patching all .rc files to version $version"
# convert the version number to .rc format
$rc_version = $version -replace "\.", ","
$rc_version_spaced = $version -replace "\.", ", "
# patch the files we found
ForEach ($file In $files)
{
echo "Processing $file..."
$content = (Get-Content $file)
$content |
Foreach-Object {
$_ -replace "^\s*FILEVERSION\s*[0-9]+,[0-9]+,[0-9]+,[0-9]+$", " FILEVERSION $rc_version" `
-replace "^\s*PRODUCTVERSION\s*[0-9]+,[0-9]+,[0-9]+,[0-9]+$", " PRODUCTVERSION $rc_version" `
-replace "(^\s*VALUE\s*`"FileVersion`",\s*)`"[0-9]+,\s*[0-9]+,\s*[0-9]+,\s*[0-9]+`"$", "`$1`"$rc_version_spaced`"" `
-replace "(^\s*VALUE\s*`"ProductVersion`",\s*)`"[0-9]+,\s*[0-9]+,\s*[0-9]+,\s*[0-9]+`"$", "`$1`"$rc_version_spaced`""
} |
Set-Content $file
}
}
else
{
echo "The version must have four numbers separated by periods, e.g. 5.4.2.123"
}
}
PatchRCFiles $version $files
TeamCity 中的配置如下所示:
只需为脚本提供您想要调整的 .rc 文件列表即可。此步骤必须在主要构建步骤之前运行。