Skip to content

Commit df7b89e

Browse files
Split script out and try to take care over messing with ending newlines
1 parent dc3f903 commit df7b89e

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
param()
2+
3+
$ErrorActionPreference = 'Stop'
4+
5+
Write-Host "Creating global.json to enforce .NET 8 for MSBuild"
6+
$globalJson = '{"sdk":{"version":"8.0.0","rollForward":"latestFeature"}}'
7+
[System.IO.File]::WriteAllText('global.json', $globalJson, [System.Text.Encoding]::UTF8)
8+
9+
Write-Host "Searching for project files under Tests/TestData..."
10+
$projFiles = Get-ChildItem -Path Tests/TestData -Recurse -Include *.csproj,*.vbproj,*.fsproj -File -ErrorAction SilentlyContinue
11+
12+
if (-not $projFiles) {
13+
Write-Host "No project files found under Tests/TestData"
14+
exit 0
15+
}
16+
17+
$changed = $false
18+
foreach ($f in $projFiles) {
19+
$path = $f.FullName
20+
Write-Host "Processing: $path"
21+
22+
# Use StreamReader to detect encoding and preserve it when writing back
23+
$sr = [System.IO.StreamReader]::new($path, $true)
24+
try {
25+
$content = $sr.ReadToEnd()
26+
$encoding = $sr.CurrentEncoding
27+
} finally {
28+
$sr.Close()
29+
}
30+
31+
# Replace net10.0 and net10.0-windows with net8.0 / net8.0-windows
32+
$updated = [System.Text.RegularExpressions.Regex]::Replace($content, '<TargetFramework>net10\.0(-windows)?</TargetFramework>', '<TargetFramework>net8.0$1</TargetFramework>')
33+
34+
if ($updated -ne $content) {
35+
Write-Host "Updating TargetFramework in: $path"
36+
# Write back preserving detected encoding and internal newlines
37+
[System.IO.File]::WriteAllText($path, $updated, $encoding)
38+
$changed = $true
39+
}
40+
}
41+
42+
if ($changed) {
43+
Write-Host "Changes detected — committing to local repo so working tree is clean for tests"
44+
git config user.name "github-actions[bot]"
45+
if ($env:GITHUB_ACTOR) {
46+
git config user.email "$($env:GITHUB_ACTOR)@users.noreply.github.com"
47+
} else {
48+
git config user.email "actions@github.com"
49+
}
50+
git add -A
51+
git commit -m "CI: Update Tests/TestData TargetFramework -> net8.0 for .NET 8 run" || Write-Host "No commit created (maybe no staged changes)"
52+
Write-Host "Committed changes locally."
53+
} else {
54+
Write-Host "No TargetFramework updates required."
55+
}
56+
57+
Write-Host "Done."

.github/workflows/dotnet-tests.yml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,7 @@ jobs:
3535
- name: Create global.json to enforce .NET 8 for MSBuild and update Tests/TestData projects
3636
if: inputs.dotnet-version == '8.0.x'
3737
shell: pwsh
38-
run: |
39-
# ensure .NET 8 SDK is used for MSBuild operations
40-
'{"sdk":{"version":"8.0.0","rollForward":"latestFeature"}}' | Out-File -FilePath global.json -Encoding utf8
41-
Write-Host "Updating <TargetFramework>net10.0...</TargetFramework> entries to net8.0 under Tests/TestData"
42-
43-
# Replace net10.0 and net10.0-windows with net8.0 / net8.0-windows respectively
44-
$projFiles = Get-ChildItem -Path Tests/TestData -Recurse -Include *.csproj,*.vbproj,*.fsproj -ErrorAction SilentlyContinue
45-
foreach ($f in $projFiles) {
46-
$path = $f.FullName
47-
$content = Get-Content -Path $path -Raw -ErrorAction Stop
48-
$updated = $content -replace '<TargetFramework>net10.0(-windows)?</TargetFramework>', '<TargetFramework>net8.0$1</TargetFramework>'
49-
if ($updated -ne $content) {
50-
Write-Host "Updating: $path"
51-
$updated | Set-Content -Path $path -Encoding utf8
52-
}
53-
}
54-
55-
# If any files were changed, commit them so the working tree is clean for subsequent test steps.
56-
$status = git status --porcelain
57-
if ($status) {
58-
git config user.name "github-actions[bot]"
59-
git config user.email "${{ github.actor }}@users.noreply.github.com"
60-
git add -A
61-
git commit -m "CI: Update Tests/TestData TargetFramework -> net8.0 for .NET 8 run" || Write-Host "git commit returned non-zero (possibly no staged changes)"
62-
Write-Host "Committed changes to local repo (not pushed)."
63-
} else {
64-
Write-Host "No project files needed updating."
65-
}
38+
run: ./.github/scripts/update-testdata-targetframework.ps1
6639

6740
- name: Log MSBuild version
6841
run: msbuild -version

0 commit comments

Comments
 (0)