Skip to content

Passing declared variables into New-Flancy -WebSchema #43

Open

Description

@dotps1

This may not be possible but it would be nice if variables could be passed in the -WebSchema parameter of New-Flancy:

$thing = "SomeText"
New-Flancy -WebSchema {
    Get "/" {
        $thing
    }
}

The only reason I did this was because things like $PSScriptRoot don't seem to exist in the scope of those cmd blocks.

Activity

  1. toenuff commented on Aug 9, 2016

    @toenuff
    Owner

    I think the only way to achieve this would be to build a dynamic script block. For example:

    $thing
    $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
      $thing
    "@)
    
    New-Flancy -WebSchema {
      Get "/" $scriptblock
    }

    This brought up a really old memory: https://powertoe.wordpress.com/2010/02/10/dynamic-code-in-powershell/

  2. toenuff commented on Aug 9, 2016

    @toenuff
    Owner

    It might be interesting to use a scriptblock property with some sort of variable list property that we interpret and inject vars from the current session, but then again, doing it the PS way with NewScriptBlock() is probably the better way to do this.

  3. dotps1 commented on Aug 9, 2016

    @dotps1
    Author

    I tried this:

    $text = "Hello world"
    $sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
        $text
    "@)
    
    New-Flancy -url http://localhost:8002 -webschema @(
        Get "/" {
            $sb
        }
    )

    and when I go to the url, the page is blank.

  4. dotps1 commented on Aug 9, 2016

    @dotps1
    Author

    tired it like this as well:

    $text = "Hello world"
    $sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
        $text
    "@)
    
    New-Flancy -url http://localhost:8002 -webschema @(
        Get "/" $sb
    )

    still blank.

  5. toenuff commented on Aug 10, 2016

    @toenuff
    Owner

    That's probably because Hello world isn't a function or anything. If you want to return it, you'd need to do this

    $sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
        "$text"
    "@)

    You can always test the code locally too:
    & $sb

    See if that works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions