Skip to content

Commit 31f7c64

Browse files
committed
Try using separate powershell script to help determine how to build
1 parent dd36e34 commit 31f7c64

File tree

2 files changed

+95
-70
lines changed

2 files changed

+95
-70
lines changed

.github/workflows/build.ps1

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# build.ps1
2+
param(
3+
[string]$SolutionOrProjectPath
4+
)
5+
6+
function Test-IsSdkProject {
7+
param([string]$csprojPath)
8+
try {
9+
[xml]$xml = Get-Content -LiteralPath $csprojPath
10+
} catch { return $false }
11+
# SDK-style has <Project Sdk="..."> on the root element
12+
return $xml.Project -and $xml.Project.Sdk
13+
}
14+
15+
function Get-SolutionProjects {
16+
param([string]$slnPath)
17+
$root = Split-Path -Parent $slnPath
18+
Select-String -Path $slnPath -Pattern 'Project\(".*"\)\s*=\s*".*?",\s*"(.*?)",\s*".*?"' |
19+
ForEach-Object {
20+
$rel = $_.Matches[0].Groups[1].Value
21+
if ($rel -and $rel.EndsWith(".csproj", [StringComparison]::OrdinalIgnoreCase)) {
22+
Join-Path $root $rel
23+
}
24+
} | Where-Object { Test-Path $_ }
25+
}
26+
27+
function Classify-Build {
28+
param([string]$path)
29+
30+
if ($path.EndsWith(".sln")) {
31+
$projects = Get-SolutionProjects $path
32+
if (-not $projects) { return "none" }
33+
34+
$hasPackagesConfig = $projects | ForEach-Object {
35+
$pkg = Join-Path (Split-Path -Parent $_) "packages.config"
36+
Test-Path $pkg
37+
} | Where-Object { $_ } | Measure-Object | Select-Object -ExpandProperty Count
38+
39+
if ($hasPackagesConfig -gt 0) { return "legacy" }
40+
41+
$allSdk = $projects | ForEach-Object { Test-IsSdkProject $_ } |
42+
Where-Object { -not $_ } | Measure-Object | Select-Object -ExpandProperty Count
43+
return ($allSdk -eq 0) ? "sdk" : "legacy"
44+
}
45+
elseif ($path.EndsWith(".csproj")) {
46+
$pkg = Join-Path (Split-Path -Parent $path) "packages.config"
47+
if (Test-Path $pkg) { return "legacy" }
48+
return (Test-IsSdkProject $path) ? "sdk" : "legacy"
49+
}
50+
else {
51+
return "none"
52+
}
53+
}
54+
55+
$path = Resolve-Path $SolutionOrProjectPath
56+
$kind = Classify-Build $path
57+
Write-Host "Detected build kind: $kind"
58+
59+
switch ($kind) {
60+
"sdk" {
61+
Write-Host "Using dotnet restore/build"
62+
dotnet restore "$path"
63+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
64+
dotnet build "$path" --configuration Release --no-restore --verbosity minimal
65+
exit $LASTEXITCODE
66+
}
67+
"legacy" {
68+
Write-Host "Using NuGet + MSBuild (/restore)"
69+
nuget restore "$path" -NonInteractive
70+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
71+
msbuild "$path" /restore /m /p:Configuration=Release /p:Platform="Any CPU" /verbosity:minimal /nologo
72+
exit $LASTEXITCODE
73+
}
74+
default {
75+
Write-Host "No solution or project found or unrecognized format."
76+
exit 0
77+
}
78+
}

.github/workflows/schema-generation.yml

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -190,83 +190,30 @@ jobs:
190190
echo "order=$orderString" >> $env:GITHUB_OUTPUT
191191
shell: pwsh
192192

193-
- name: Build repositories in dependency order
193+
- name: Build repositories in dependency order (clean detection)
194+
shell: pwsh
194195
run: |
195196
$buildOrder = "${{ steps.build-order.outputs.order }}" -split ","
196197
foreach ($repo in $buildOrder) {
197198
$repo = $repo.Trim()
198-
if ($repo -ne "") {
199-
Write-Host "Building $repo..."
200-
Push-Location ".repos/$repo"
201-
202-
# Find solution files
203-
$solutionFiles = Get-ChildItem -Recurse -Filter "*.sln" | Where-Object { $_.Directory.Name -notmatch "(Test|Tests|Example|Examples)" }
204-
205-
if ($solutionFiles.Count -gt 0) {
206-
$solutionFile = $solutionFiles[0].FullName
207-
Write-Host "Building solution: $solutionFile"
208-
209-
# Try dotnet first
210-
Write-Host "Restoring packages with dotnet..."
211-
dotnet restore "$solutionFile"
212-
if ($LASTEXITCODE -eq 0) {
213-
Write-Host "Building with dotnet..."
214-
dotnet build "$solutionFile" --configuration Release --verbosity minimal --no-restore
215-
if ($LASTEXITCODE -ne 0) {
216-
Write-Host "Dotnet build failed, trying MSBuild fallback..."
217-
nuget restore "$solutionFile"
218-
msbuild "$solutionFile" /p:Configuration=Release /p:Platform="Any CPU" /verbosity:minimal
219-
if ($LASTEXITCODE -ne 0) {
220-
Write-Host "MSBuild also failed for $repo, continuing with next repository..."
221-
}
222-
}
223-
} else {
224-
Write-Host "Dotnet restore failed, trying NuGet + MSBuild fallback..."
225-
nuget restore "$solutionFile"
226-
msbuild "$solutionFile" /p:Configuration=Release /p:Platform="Any CPU" /verbosity:minimal
227-
if ($LASTEXITCODE -ne 0) {
228-
Write-Host "MSBuild failed for $repo, continuing with next repository..."
229-
}
230-
}
231-
} else {
232-
# Try to find project files if no solution
233-
$projectFiles = Get-ChildItem -Recurse -Filter "*.csproj" | Where-Object { $_.Directory.Name -notmatch "(Test|Tests|Example|Examples)" }
234-
if ($projectFiles.Count -gt 0) {
235-
foreach ($projectFile in $projectFiles) {
236-
Write-Host "Building project: $($projectFile.FullName)"
237-
238-
# Try dotnet first
239-
dotnet restore "$($projectFile.FullName)"
240-
if ($LASTEXITCODE -eq 0) {
241-
dotnet build "$($projectFile.FullName)" --configuration Release --verbosity minimal --no-restore
242-
if ($LASTEXITCODE -ne 0) {
243-
Write-Host "Dotnet build failed for project $($projectFile.Name), trying MSBuild..."
244-
nuget restore "$($projectFile.FullName)"
245-
msbuild "$($projectFile.FullName)" /p:Configuration=Release /p:Platform="Any CPU" /verbosity:minimal
246-
if ($LASTEXITCODE -ne 0) {
247-
Write-Host "MSBuild also failed for project $($projectFile.Name), continuing..."
248-
break
249-
}
250-
}
251-
} else {
252-
Write-Host "Dotnet restore failed for project $($projectFile.Name), trying NuGet + MSBuild..."
253-
nuget restore "$($projectFile.FullName)"
254-
msbuild "$($projectFile.FullName)" /p:Configuration=Release /p:Platform="Any CPU" /verbosity:minimal
255-
if ($LASTEXITCODE -ne 0) {
256-
Write-Host "MSBuild failed for project $($projectFile.Name), continuing..."
257-
break
258-
}
259-
}
260-
}
261-
} else {
262-
Write-Host "No solution or project files found in $repo"
263-
}
199+
if (-not $repo) { continue }
200+
Push-Location ".repos/$repo"
201+
Write-Host "Building $repo..."
202+
203+
# Prefer a solution if present; otherwise build each csproj
204+
$solutions = Get-ChildItem -Recurse -Filter *.sln | Where-Object { $_.Directory.Name -notmatch '(Test|Tests|Example|Examples)' }
205+
if ($solutions) {
206+
# Build the first solution (or loop them if you have multiple)
207+
$sln = $solutions[0].FullName
208+
../../.github/workflows/build.ps1 -SolutionOrProjectPath $sln
209+
} else {
210+
$projects = Get-ChildItem -Recurse -Filter *.csproj | Where-Object { $_.Directory.Name -notmatch '(Test|Tests|Example|Examples)' }
211+
foreach ($proj in $projects) {
212+
../../.github/workflows/build.ps1 -SolutionOrProjectPath $proj.FullName
264213
}
265-
266-
Pop-Location
267214
}
215+
Pop-Location
268216
}
269-
shell: powershell
270217
271218
- name: Build Generation solution
272219
run: |

0 commit comments

Comments
 (0)