| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Defines or updates a global variable accessible across Kestrun scripts. |
| | 4 | |
|
| | 5 | | .DESCRIPTION |
| | 6 | | Stores a value in the Kestrun global variable table. Variables may be marked |
| | 7 | | as read-only to prevent accidental modification. |
| | 8 | | If the variable already exists, its value is updated. If it does not exist, |
| | 9 | | it is created. |
| | 10 | | .PARAMETER Name |
| | 11 | | Name of the variable to create or update. |
| | 12 | | .PARAMETER Value |
| | 13 | | Value to assign to the variable. |
| | 14 | | .PARAMETER AllowsValueType |
| | 15 | | If specified, allows the variable to hold value types (e.g., int, bool). |
| | 16 | | .EXAMPLE |
| | 17 | | Set-KrSharedState -Name "MyVariable" -Value "Hello, World!" |
| | 18 | | This creates a global variable "MyVariable" with the value "Hello, World!". |
| | 19 | | .EXAMPLE |
| | 20 | | Set-KrSharedState -Name "MyNamespace.MyVariable" -Value @{item=42} |
| | 21 | | This creates a global variable "MyNamespace.MyVariable" with the value @{item=42}. |
| | 22 | | .NOTES |
| | 23 | | This function is part of the Kestrun.SharedState module and is used to define or update global variables. |
| | 24 | | #> |
| | 25 | | function Set-KrSharedState { |
| | 26 | | [KestrunRuntimeApi('Definition')] |
| | 27 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 28 | | [CmdletBinding()] |
| | 29 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 30 | | param( |
| | 31 | | [Parameter(Mandatory)] |
| | 32 | | [string]$Name, |
| | 33 | |
|
| | 34 | | [Parameter(Mandatory)] |
| | 35 | | [object]$Value, |
| | 36 | |
|
| | 37 | | [Parameter()] |
| | 38 | | [switch]$AllowsValueType |
| | 39 | | ) |
| | 40 | | process { |
| | 41 | | # Define or update the variable; throws if it was already read-only |
| 1 | 42 | | $null = [Kestrun.SharedState.SharedStateStore]::Set( |
| | 43 | | $Name, |
| | 44 | | $Value, |
| | 45 | | $AllowsValueType.IsPresent # Allow value types if specified |
| | 46 | | ) |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|