| | 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 |
| 0 | 20 | | $realDotnetPath = Join-Path -Path (Split-Path -Parent $dotnetPath) -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 |
| 1 | 32 | | foreach ($verDir in $versionDirs) { |
| 1 | 33 | | $assemblies = @() |
| | 34 | |
|
| 2 | 35 | | Get-ChildItem -Path $verDir.FullName -Filter 'Microsoft.*.dll' | ForEach-Object { |
| 1 | 36 | | if ($assemblies -notcontains $_.Name) { |
| 1 | 37 | | $assemblies += $_.Name |
| | 38 | | } |
| | 39 | | } |
| 1 | 40 | | $allFound = $true |
| 1 | 41 | | foreach ($asm in $assemblies) { |
| 1 | 42 | | $asmPath = Join-Path -Path $verDir.FullName -ChildPath $asm |
| 2 | 43 | | if (-not (Test-Path -Path $asmPath)) { |
| 0 | 44 | | Write-Verbose "Assembly $asm not found in $($verDir.FullName)" |
| 0 | 45 | | $allFound = $false |
| | 46 | | break |
| | 47 | | } |
| | 48 | | } |
| 1 | 49 | | if ($allFound) { |
| 1 | 50 | | $result = $true |
| 1 | 51 | | foreach ($asm in $assemblies) { |
| 1 | 52 | | $asmPath = Join-Path -Path $verDir.FullName -ChildPath $asm |
| 2 | 53 | | $result = $result -and (Assert-KrAssemblyLoaded -AssemblyPath $asmPath) |
| | 54 | | } |
| 2 | 55 | | Write-Verbose "Loaded ASP.NET Core assemblies from $($verDir.FullName)" |
| 1 | 56 | | return $result |
| | 57 | | } |
| 0 | 58 | | return $false |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | Write-Error "Could not find ASP.NET Core assemblies for version $Version in $baseDir." |
| 0 | 62 | | Write-Warning "Please download the Runtime $Version from https://dotnet.microsoft.com/en-us/download/dotnet/$version |
| | 63 | |
|
| 0 | 64 | | throw "Microsoft.AspNetCore.App version $Version not found in $baseDir." |
| | 65 | | } |
| | 66 | |
|