Skip to content

Passing declared variables into New-Flancy -WebSchema #43

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

Open
dotps1 opened this issue Aug 4, 2016 · 5 comments
Open

Passing declared variables into New-Flancy -WebSchema #43

dotps1 opened this issue Aug 4, 2016 · 5 comments

Comments

@dotps1
Copy link

dotps1 commented Aug 4, 2016

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.

@toenuff
Copy link
Owner

toenuff commented Aug 9, 2016

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/

@toenuff
Copy link
Owner

toenuff commented Aug 9, 2016

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.

@dotps1
Copy link
Author

dotps1 commented Aug 9, 2016

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.

@dotps1
Copy link
Author

dotps1 commented Aug 9, 2016

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.

@toenuff
Copy link
Owner

toenuff commented Aug 10, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants