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

Add working directory option to Exec function #135

Closed
wants to merge 2 commits into from
Closed

Add working directory option to Exec function #135

wants to merge 2 commits into from

Conversation

DaveSenn
Copy link
Contributor

I think it would be very useful to have an option to set the working directory when executing script blocks (For example if you want to run git in the root of your repository and not in the directory containing the script).
Because of this I added a workingDirectory option to the Exec function.
The option is optional. If specified the current path gets saved using Push-Location and the current location gets set to the specified working directory. After the script block is executed the location will be set back to the previous location.

@rvdginste
Copy link
Contributor

Because I use this pattern a lot in my scripts, I want to suggest to put the "Pop-Location" in a finally block. The end result would then be something like the following:

Push-Location
try
{
  # ....
}
finally
{
  Pop-Location
}

If an exception is thrown in the body of the 'try' clause, then the script will still end up in the location it started from...

@gep13
Copy link
Member

gep13 commented Dec 8, 2015

@DaveSenn what were your thoughts about the suggestion from @rvdginste? Did you want to incorporate it into your PR? Thanks!

@alexandear
Copy link
Contributor

@DaveSenn up

DaveSenn added a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn added a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn added a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn added a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
@DaveSenn
Copy link
Contributor Author

DaveSenn commented Jan 8, 2017

@gep13 I like the suggestion of rvdginste. It's a good idea to move the Pop-Location statement to a finally block. With my solution, the user could end up in a wrong directory, if some script/exe fails.
Please see #200

@devblackops
Copy link
Member

devblackops commented Nov 7, 2017

Closing this out as the better solution using the finally block is in PR #200

@devblackops devblackops closed this Nov 7, 2017
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.

None yet

5 participants