| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Loads required ASP.NET Core assemblies for PowerShell usage. |
| | | 4 | | .PARAMETER Version |
| | | 5 | | The .NET version to target (e.g. net8, net9, net10). |
| | | 6 | | #> |
| | | 7 | | function Add-KrAspNetCoreType { |
| | | 8 | | [CmdletBinding()] |
| | | 9 | | [OutputType([bool])] |
| | | 10 | | param ( |
| | | 11 | | [Parameter()] |
| | | 12 | | [ValidateSet('net8.0', 'net9.0', 'net10.0')] |
| | | 13 | | [string]$Version = 'net8.0' |
| | | 14 | | ) |
| | 1 | 15 | | $versionNumber = $Version -replace 'net(\d+).*', '$1' |
| | 2 | 16 | | $dotnetPath = (Get-Command -Name 'dotnet' -ErrorAction Stop).Source |
| | 2 | 17 | | $realDotnetPath = (Get-Item $dotnetPath).Target |
| | 2 | 18 | | if (-not $realDotnetPath) { $realDotnetPath = $dotnetPath }elseif ($realDotnetPath -notmatch '^/') { |
| | | 19 | | # If the target is a relative path, resolve it from the parent of $dotnetPath (e.g. symlink in same folder) |
| | 0 | 20 | | $realDotnetPath = Join-Path -Path (Split-Path -Path $dotnetPath -Parent) -ChildPath $realDotnetPath |
| | 0 | 21 | | $realDotnetPath = [System.IO.Path]::GetFullPath($realDotnetPath) |
| | | 22 | | } |
| | 1 | 23 | | $dotnetDir = Split-Path -Path $realDotnetPath -Parent |
| | 1 | 24 | | if (-not $dotnetDir) { |
| | 0 | 25 | | throw 'Could not determine the path to the dotnet executable.' |
| | | 26 | | } |
| | 1 | 27 | | $baseDir = Join-Path -Path $dotnetDir -ChildPath 'shared\Microsoft.AspNetCore.App' |
| | 2 | 28 | | if (-not (Test-Path -Path $baseDir -PathType Container)) { |
| | 0 | 29 | | throw "ASP.NET Core shared framework not found at $baseDir." |
| | | 30 | | } |
| | 5 | 31 | | $versionDirs = Get-ChildItem -Path $baseDir -Directory | Where-Object { $_.Name -like "$($versionNumber).*" } | Sort |
| | | 32 | | |
| | | 33 | | # Try each version directory until we find one with all required assemblies |
| | 2 | 34 | | $verDir = $versionDirs | Select-Object -First 1 |
| | 1 | 35 | | if (-not $verDir) { |
| | 0 | 36 | | Write-Error "Could not find ASP.NET Core assemblies for version $Version in $baseDir." |
| | 0 | 37 | | Write-Warning "Please download the Runtime $Version from https://dotnet.microsoft.com/en-us/download/dotnet/$ver |
| | | 38 | | |
| | 0 | 39 | | throw "Microsoft.AspNetCore.App version $Version not found in $baseDir." |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | # Collect required assemblies |
| | 1 | 43 | | $assemblies = @() |
| | | 44 | | |
| | 1 | 45 | | Get-ChildItem -Path $verDir.FullName -Filter '*.dll' | |
| | 1 | 46 | | Where-Object { |
| | 1 | 47 | | $_.Name -like 'Microsoft.*.dll' -or |
| | | 48 | | $_.Name -eq 'System.IO.Pipelines.dll' |
| | 1 | 49 | | } | ForEach-Object { |
| | 1 | 50 | | if ($assemblies -notcontains $_.Name) { |
| | 1 | 51 | | $assemblies += $_.Name |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | 2 | 55 | | Write-Verbose "Collected assemblies: $($assemblies -join ', ')" |
| | | 56 | | |
| | | 57 | | # Check if all required assemblies are present |
| | 1 | 58 | | $allFound = $true |
| | 1 | 59 | | foreach ($asm in $assemblies) { |
| | 1 | 60 | | $asmPath = Join-Path -Path $verDir.FullName -ChildPath $asm |
| | 2 | 61 | | if (-not (Test-Path -Path $asmPath)) { |
| | 0 | 62 | | Write-Verbose "Assembly $asm not found in $($verDir.FullName)" |
| | 0 | 63 | | $allFound = $false |
| | | 64 | | break |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | if (-not $allFound) { return $false } |
| | | 69 | | |
| | | 70 | | # Load all assemblies |
| | 1 | 71 | | $result = $true |
| | 1 | 72 | | foreach ($asm in $assemblies) { |
| | 1 | 73 | | $asmPath = Join-Path -Path $verDir.FullName -ChildPath $asm |
| | 2 | 74 | | $result = $result -and (Assert-KrAssemblyLoaded -AssemblyPath $asmPath) |
| | | 75 | | } |
| | | 76 | | |
| | 2 | 77 | | Write-Verbose "Loaded ASP.NET Core assemblies from $($verDir.FullName)" |
| | 1 | 78 | | return $result |
| | | 79 | | } |
| | | 80 | | |