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

Scoping Fun #18

Open
RamblingCookieMonster opened this issue Apr 6, 2016 · 4 comments
Open

Scoping Fun #18

RamblingCookieMonster opened this issue Apr 6, 2016 · 4 comments

Comments

@RamblingCookieMonster
Copy link
Owner

RamblingCookieMonster commented Apr 6, 2016

A user might expect the following to work, and display Var is 2:

$var = 2

Deploy Example {
    By Task One {
        "Var is $var"
    }
    By noop Two {
        FromSource MyModule
        To C:\PSDeployTo
    }
}

Currently, this will display Var is, given fun with scoping.

An ugly workaround would be to use environment variables, or script scope. This might be an acceptable solution given that tools like psake, the build script, or the CI/CD solution may already populate global or environmental variables:

$script:var = 2

Deploy Example {
    By Task One {
        "Var is $script:var"
    }
    By noop Two {
        FromSource MyModule
        To C:\PSDeployTo
    }
}

These both successfully display Var is 2, but are not very friendly or intuitive to the user.

Potential solution: Add code that extracts script scope variables into the current scope. This would be needed in DeploymentType scripts, and in Task based DeploymentTypes (perhaps we could inject it).

Am I missing anything? The examples above are running from the dev branch.

@devblackops
Copy link
Sponsor Contributor

Perhaps something similar to how psake is doing it? https://github.com/psake/psake/blob/master/psake.psm1#L252
http://psake.readthedocs.org/en/latest/variable-referencing/

This looks like any variables defined in the properties function are added to the script scope. This is basically like your second example.

@mgreenegit
Copy link
Collaborator

What about:

Deploy Example {
  $var = 2
  By Task One {
    "Var is $var"
  }
  By noop Two {
    FromSource MyModule
      To "C:\PSDeploy$var"
  }
}

@mgreenegit
Copy link
Collaborator

To answer my own question, it would still need to be script scope. That does work though.

@DexterPOSH
Copy link
Contributor

Why not have a File preamble similar to what Pester has ?
All the variables (or the initialization code) in the preamble gets added to the script scope and is available inside.

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

4 participants