Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for creating custom Scoped Variables #1238

Merged
merged 6 commits into from
Feb 11, 2024
Merged

Conversation

Badgerati
Copy link
Owner

Description of the Change

Adds support for creating custom Scoped Variables (like $state: and $using:), and also for converting scriptblocks in an ad-hoc manner. Adds the following functions

  • Convert-PodeScopedVariables
  • Convert-PodeScopedVariable
  • Add-PodeScopedVariable
  • Remove-PodeScopedVariable
  • Test-PodeScopedVariable
  • Clear-PodeScopedVariables
  • Get-PodeScopedVariable
  • Use-PodeScopedVariables
  • Merge-PodeScriptblockArguments

Related Issue

Resolves #1207

Examples

  • Using Replace syntax:
Add-PodeScopedVariable -Name 'state' `
    -SetReplace "Set-PodeState -Name '{{name}}' -Value " `
    -GetReplace "Get-PodeState -Name '{{name}}'"
  • Using advanced scriptblock syntax:
Add-PodeScopedVariable -Name 'config' -ScriptBlock {
    param($ScriptBlock, $SessionState, $GetPattern, $SetPattern)
    # convert the scriptblock to a string, for searching
    $strScriptBlock = "$($ScriptBlock)"
    # the "get" template to be used, to convert "$config:Name" syntax to "(Get-PodeConfig).Name"
    $template = "(Get-PodeConfig).'{{name}}'"
    # loop through the scriptblock, replacing "$config:Name" syntax
    while ($strScriptBlock -imatch $GetPattern) {
        $getReplace = $template.Replace('{{name}}', $Matches['name'])
        $strScriptBlock = $strScriptBlock.Replace($Matches['full'], "($($getReplace))")
    }
    # convert string back to scriptblock, and return
    return [scriptblock]::Create($strScriptBlock)
}

@Badgerati Badgerati merged commit b2daf57 into develop Feb 11, 2024
8 checks passed
@Badgerati Badgerati deleted the Issue-1207 branch February 11, 2024 12:41
@Badgerati Badgerati mentioned this pull request Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inbuilt and Custom Scoped Variable Support
1 participant