|
4 | 4 | [string] $Configuration = 'Release'
|
5 | 5 | )
|
6 | 6 |
|
7 |
| -$msBuild = "msbuild" |
8 |
| -try |
9 |
| -{ |
10 |
| - & $msBuild /version |
11 |
| - Write-Host "Likely on Linux/macOS." |
12 |
| - $solution = "SharpSnmpLib.Samples.slnx" |
13 |
| - |
14 |
| - & dotnet restore $solution -c $Configuration |
15 |
| - & dotnet clean $solution -c $Configuration |
16 |
| - & dotnet build $solution -c $Configuration |
17 |
| -} |
18 |
| -catch |
19 |
| -{ |
20 |
| - Write-Host "MSBuild doesn't exist. Use VSSetup instead." |
| 7 | +# Improved OS detection |
| 8 | +$isWindowsOS = ($PSVersionTable.PSVersion.Major -ge 6 -and $PSVersionTable.Platform -eq 'Win32NT') -or ($PSVersionTable.PSVersion.Major -lt 6 -and $env:OS -match 'Windows') |
| 9 | +$isMacOSX = $PSVersionTable.PSVersion.Major -ge 6 -and $PSVersionTable.OS -match 'Darwin' |
| 10 | +$isLinuxOS = $PSVersionTable.PSVersion.Major -ge 6 -and $PSVersionTable.OS -match 'Linux' |
21 | 11 |
|
22 |
| - Install-Module VSSetup -Scope CurrentUser -Force |
23 |
| - Update-Module VSSetup |
24 |
| - $instance = Get-VSSetupInstance -All -Prerelease |
| 12 | +if ($isWindowsOS) { |
| 13 | + Write-Host "Detected Windows OS." |
| 14 | + |
| 15 | + Install-Module VSSetup -Scope CurrentUser -Force -ErrorAction SilentlyContinue |
| 16 | + Update-Module VSSetup -ErrorAction SilentlyContinue |
| 17 | + $instance = Get-VSSetupInstance -All -Prerelease -ErrorAction SilentlyContinue |
25 | 18 | $installDir = $instance.installationPath
|
26 |
| - Write-Host "Found VS in " + $installDir |
27 |
| - $msBuild = $installDir + '\MSBuild\Current\Bin\MSBuild.exe' |
28 |
| - if (![System.IO.File]::Exists($msBuild)) |
29 |
| - { |
30 |
| - $msBuild = $installDir + '\MSBuild\15.0\Bin\MSBuild.exe' |
31 |
| - if (![System.IO.File]::Exists($msBuild)) |
32 |
| - { |
33 |
| - Write-Host "MSBuild doesn't exist on Windows. Exit." |
34 |
| - exit 1 |
35 |
| - } |
36 |
| - else |
37 |
| - { |
38 |
| - Write-Host "Likely on Windows with VS2017." |
| 19 | + |
| 20 | + if ($installDir) { |
| 21 | + Write-Host "Found VS in " + $installDir |
| 22 | + $msBuild = Join-Path -Path $installDir -ChildPath 'MSBuild\Current\Bin\MSBuild.exe' |
| 23 | + |
| 24 | + if (![System.IO.File]::Exists($msBuild)) { |
| 25 | + $msBuild = Join-Path -Path $installDir -ChildPath 'MSBuild\15.0\Bin\MSBuild.exe' |
| 26 | + |
| 27 | + if (![System.IO.File]::Exists($msBuild)) { |
| 28 | + Write-Host "MSBuild doesn't exist on Windows. Trying dotnet command..." |
| 29 | + $solution = "SharpSnmpLib.Samples.Windows.slnx" |
| 30 | + |
| 31 | + & dotnet restore $solution -c $Configuration |
| 32 | + & dotnet clean $solution -c $Configuration |
| 33 | + & dotnet build $solution -c $Configuration |
| 34 | + } else { |
| 35 | + Write-Host "Using MSBuild from VS2017." |
| 36 | + $solution = "SharpSnmpLib.Samples.Windows.slnx" |
| 37 | + |
| 38 | + & $msBuild $solution /p:Configuration=$Configuration /t:restore |
| 39 | + & $msBuild $solution /p:Configuration=$Configuration /t:clean |
| 40 | + & $msBuild $solution /p:Configuration=$Configuration |
| 41 | + } |
| 42 | + } else { |
| 43 | + Write-Host "Using MSBuild from VS2019 or newer." |
| 44 | + $solution = "SharpSnmpLib.Samples.Windows.slnx" |
| 45 | + |
| 46 | + & $msBuild $solution /p:Configuration=$Configuration /t:restore |
| 47 | + & $msBuild $solution /p:Configuration=$Configuration /t:clean |
| 48 | + & $msBuild $solution /p:Configuration=$Configuration |
39 | 49 | }
|
| 50 | + } else { |
| 51 | + Write-Host "Visual Studio not found. Using dotnet command..." |
| 52 | + $solution = "SharpSnmpLib.Samples.Windows.slnx" |
| 53 | + |
| 54 | + & dotnet restore $solution -c $Configuration |
| 55 | + & dotnet clean $solution -c $Configuration |
| 56 | + & dotnet build $solution -c $Configuration |
40 | 57 | }
|
41 |
| - else |
42 |
| - { |
43 |
| - Write-Host "Likely on Windows with VS2019." |
| 58 | +} else { |
| 59 | + # On macOS or Linux |
| 60 | + if ($isMacOSX) { |
| 61 | + Write-Host "Detected macOS." |
| 62 | + } elseif ($isLinuxOS) { |
| 63 | + Write-Host "Detected Linux." |
| 64 | + } else { |
| 65 | + Write-Host "Detected non-Windows OS (unknown type)." |
44 | 66 | }
|
45 |
| - |
46 |
| - Write-Host "MSBuild found. Compile the projects." |
47 |
| - |
48 |
| - $solution = "SharpSnmpLib.Samples.Windows.slnx" |
49 |
| - |
50 |
| - & $msBuild $solution /p:Configuration=$Configuration /t:restore |
51 |
| - & $msBuild $solution /p:Configuration=$Configuration /t:clean |
52 |
| - & $msBuild $solution /p:Configuration=$Configuration |
| 67 | + |
| 68 | + $solution = "SharpSnmpLib.Samples.slnx" |
| 69 | + Write-Host "Using cross-platform solution: $solution" |
| 70 | + |
| 71 | + & dotnet restore $solution -c $Configuration |
| 72 | + & dotnet clean $solution -c $Configuration |
| 73 | + & dotnet build $solution -c $Configuration |
53 | 74 | }
|
54 | 75 |
|
55 | 76 | if ($LASTEXITCODE -ne 0)
|
|
0 commit comments