Skip to content

Commit 58fbf3c

Browse files
committed
ci: Fix build, maybe.
1 parent be3681f commit 58fbf3c

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,60 +113,70 @@ jobs:
113113
id: set_version
114114
shell: pwsh
115115
run: |
116+
[int]$revVal = 0
117+
$ok = [int]::TryParse($env:BRANCH_BUILD, [ref]$revVal)
118+
if (-not $ok -or $revVal -le 0) {
119+
$revVal = [int]$env:GITHUB_RUN_NUMBER
120+
}
121+
$env:BRANCH_BUILD = "$revVal"
116122
function replaceNumericVersion($name, $fileContent) {
117-
$pattern = '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d+)\.(\d+)\.(\d+)(\.(\d+))?"\)'
123+
# allow multi-digit segments; match the whole attribute
124+
$pattern = '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?"\)\s*$'
118125
$re = [regex]::new($pattern)
119-
foreach ($content in $fileContent) {
120-
$m = $re.Match($content)
126+
foreach ($line in $fileContent) {
127+
$m = $re.Match($line)
121128
if ($m.Success) {
122129
$major = [int]$m.Groups[1].Value
123130
$minor = [int]$m.Groups[2].Value
124131
$patch = [int]$m.Groups[3].Value
125-
return $fileContent -replace $pattern, "[assembly: $name(""$major.$minor.$patch.$env:BRANCH_BUILD"")]".TrimEnd("]")
132+
$new = "[assembly: $name(""$major.$minor.$patch.$($env:BRANCH_BUILD)"")]"
133+
return $fileContent -replace $pattern, [System.Text.RegularExpressions.Regex]::Escape($new).Replace('\[','[').Replace('\]',']')
126134
}
127135
}
128136
}
129137
130138
function replaceFullVersion($name, $fileContent) {
131-
$pattern = '[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d)\.(\d)\.(\d)(-([\w\W]+))?"\)'
139+
# use \d+ (not just \d) and capture optional -tag
140+
$pattern = '[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d+)\.(\d+)\.(\d+)(?:-([\w\W]+))?"\)\s*$'
132141
$re = [regex]::new($pattern)
133-
foreach ($content in $fileContent) {
134-
$m = $re.Match($content)
142+
foreach ($line in $fileContent) {
143+
$m = $re.Match($line)
135144
if ($m.Success) {
136145
$major = [int]$m.Groups[1].Value
137146
$minor = [int]$m.Groups[2].Value
138147
$patch = [int]$m.Groups[3].Value
139-
$tag = $m.Groups[5].Value
148+
$tag = $m.Groups[4].Value
149+
140150
$branch = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
141151
if ($branch -eq "master" -or $branch -eq "main") {
142-
if ($tag) { $version = "$major.$minor.$patch-$tag-r$env:BRANCH_BUILD" }
143-
else { $version = "$major.$minor.$patch-r$env:BRANCH_BUILD" }
152+
$version = if ($tag) { "$major.$minor.$patch-$tag-r$($env:BRANCH_BUILD)" } else { "$major.$minor.$patch-r$($env:BRANCH_BUILD)" }
144153
} else {
145-
$version = "$major.$minor.$patch-$($branch.ToUpper())-r$env:BRANCH_BUILD"
154+
$version = "$major.$minor.$patch-$($branch.ToUpper())-r$($env:BRANCH_BUILD)"
146155
}
156+
147157
"version=$version" >> $env:GITHUB_OUTPUT
148-
return $fileContent -replace $pattern, "[assembly: $name(""$version"")]".TrimEnd("]")
158+
$new = "[assembly: $name(""$version"")]"
159+
return $fileContent -replace $pattern, [System.Text.RegularExpressions.Regex]::Escape($new).Replace('\[','[').Replace('\]',']')
149160
}
150161
}
151162
}
152163
153164
function replaceAny($name, $replaceWith, $fileContent) {
154-
$pattern = '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("[^"]*"\)'
165+
$pattern = '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("[^"]*"\)\s*$'
155166
$re = [regex]::new($pattern)
156-
foreach ($content in $fileContent) {
157-
$m = $re.Match($content)
158-
if ($m.Success) {
159-
return $fileContent -replace $pattern, "[assembly: $name(""$replaceWith"")]".TrimEnd("]")
167+
foreach ($line in $fileContent) {
168+
if ($re.IsMatch($line)) {
169+
$new = "[assembly: $name(""$replaceWith"")]"
170+
return $fileContent -replace $pattern, [System.Text.RegularExpressions.Regex]::Escape($new).Replace('\[','[').Replace('\]',']')
160171
}
161172
}
162173
}
163-
164174
165175
$assemblyFile = "VersionAssemblyInfo.cs"
166-
$fileContent = Get-Content $assemblyFile
167-
$fileContent = replaceNumericVersion 'AssemblyVersion' $fileContent
168-
$fileContent = replaceNumericVersion 'AssemblyFileVersion' $fileContent
169-
$fileContent = replaceFullVersion 'AssemblyInformationalVersion' $fileContent
176+
$fileContent = Get-Content $assemblyFile
177+
$fileContent = replaceNumericVersion 'AssemblyVersion' $fileContent
178+
$fileContent = replaceNumericVersion 'AssemblyFileVersion' $fileContent
179+
$fileContent = replaceFullVersion 'AssemblyInformationalVersion' $fileContent
170180
if (-not $env:GITHUB_REF.StartsWith("refs/tags/")) {
171181
$fileContent = replaceAny 'AssemblyConfiguration' $env:GITHUB_SHA.Substring(0, 7) $fileContent
172182
}

0 commit comments

Comments
 (0)