Skip to content

Add working directory option to Exec function#135

Closed
DaveSenn wants to merge 2 commits intopsake:masterfrom
DaveSenn:master
Closed

Add working directory option to Exec function#135
DaveSenn wants to merge 2 commits intopsake:masterfrom
DaveSenn:master

Conversation

@DaveSenn
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

@DaveSenn up

DaveSenn pushed a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn pushed a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn pushed a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
DaveSenn pushed a commit to DaveSenn/psake that referenced this pull request Jan 8, 2017
@DaveSenn
Copy link
Copy Markdown
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
Copy Markdown
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.

6 participants