| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Initializes the Kestrun root directory for path resolution. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function sets the Kestrun root directory, which is used as a base for resolving relative paths in Kestrun a |
| | 6 | | It is typically called during the initialization phase of a Kestrun application. |
| | 7 | | This function should be called before any other Kestrun commands that rely on the root directory being set. |
| | 8 | | .PARAMETER Path |
| | 9 | | The path to the Kestrun root directory. |
| | 10 | | .PARAMETER PassThru |
| | 11 | | If specified, the cmdlet will return the absolute path to the Kestrun root directory after setting it. |
| | 12 | | .EXAMPLE |
| | 13 | | Initialize-KrRoot -Path "C:\Kestrun" |
| | 14 | | Sets the Kestrun root directory to "C:\Kestrun". |
| | 15 | | .EXAMPLE |
| | 16 | | Initialize-KrRoot -Path "~/Kestrun" |
| | 17 | | Sets the Kestrun root directory to the user's home directory. |
| | 18 | | .EXAMPLE |
| | 19 | | Initialize-KrRoot -Path "D:\Projects\Kestrun" |
| | 20 | | Sets the Kestrun root directory to "D:\Projects\Kestrun". |
| | 21 | | .EXAMPLE |
| | 22 | | Initialize-KrRoot -Path "C:\Kestrun" -PassThru |
| | 23 | | Returns the absolute path to the Kestrun root directory. |
| | 24 | | .OUTPUTS |
| | 25 | | [string] The absolute path to the Kestrun root directory. |
| | 26 | | .NOTES |
| | 27 | | This function is designed to be used in the context of a Kestrun server to ensure consistent path resolution. |
| | 28 | | #> |
| | 29 | | function Initialize-KrRoot { |
| | 30 | | [CmdletBinding()] |
| | 31 | | [KestrunRuntimeApi('Definition')] |
| | 32 | | [OutputType([string])] |
| | 33 | | param( |
| | 34 | | [Parameter(Mandatory, Position = 0)] |
| | 35 | | [string] $Path, |
| | 36 | | [switch] $PassThru |
| | 37 | | ) |
| | 38 | |
|
| | 39 | | # Expand ~ |
| 0 | 40 | | if ($Path -like '~*') { |
| 0 | 41 | | $Path = $Path -replace '^~', $HOME |
| | 42 | | } |
| | 43 | |
|
| | 44 | | # Resolve to absolute path |
| 0 | 45 | | $resolvedPath = Resolve-Path -Path $Path -ErrorAction Stop | |
| 0 | 46 | | Select-Object -First 1 -ExpandProperty Path |
| | 47 | |
|
| | 48 | | # Save for use in C# runtime |
| 0 | 49 | | [Kestrun.KestrunHostManager]::KestrunRoot = $resolvedPath |
| | 50 | |
|
| 0 | 51 | | if ($PassThru) { |
| | 52 | | # Return absolute path for chaining |
| 0 | 53 | | return [Kestrun.KestrunHostManager]::KestrunRoot |
| | 54 | | } |
| | 55 | | } |
| | 56 | |
|