question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run script within push and pop location

See original GitHub issue

Summary of the new feature / enhancement

As a user, I want to be able to execute code in a different directory so that I can quickly run a command and return to my current state.

Having a single command for this removes the need for wrapping these commands in pushd and popd

I am unaware of this functionality existing elsewhere, although the only place I could think of was Invoke-Command

Proposed technical implementation details (optional)

This can be achieved with the following script:

function Use-Location {
  [CmdletBinding()] param (
    [Parameter(Mandatory)] [string] $Location,
    [Parameter(Mandatory)] [scriptblock] $ScriptBlock
  )
  Push-Location -Path $Location
  Invoke-Command -ScriptBlock $ScriptBlock
  Pop-Location
}

Example usage:

"repo1/my-project", "repo2/my-other-project" | %{
  Use-Location $_ {dotnet build}
}

Issue Analytics

  • State:open
  • Created 3 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
aguzevcommented, Jul 5, 2023

This improvement could cover the risk of unpaired Push-Location calls in the invoked script block, mentioned by @fflaten above.

An alternative implementation.

Invoke-Command -WorkingDirectory '… a path …" -ScriptBlock { … }
"repo1/my-project", "repo2/my-other-project" | %{
  Invoke-Command -WorkingDirectory $_ {dotnet build}
}
1reaction
MattMScommented, Jul 12, 2023

Thank you for the feedback about the missing Pop-Location issue, and especially for the multiple suggested solutions, that is helpful.

My request was prompted after writing a loop for building and publishing packages:

Get-ChildItem src | % {
  Push-Location $_
  dotnet build
  Push-Location bin/Release
  dotnet nuget push *.nupkg
  Pop-Location
  Pop-Location
}

I thought it would be cleaner if I could do the following instead:

Get-ChildItem src | % {
  Use-Location $_ {dotnet build}
  Use-Location $_/bin/Release {dotnet nuget push *.nupkg}
}

As this is intended to be committed in a repo, it would not work having this function only defined in my profile.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Push-Location and Pop-Location command in ...
First, it saves the current location to the top of the stack, and second, it browse the path specified. If there is no...
Read more >
Push-Location (Microsoft.PowerShell.Management)
You can use the Pop-Location cmdlet to get locations from the location stack. ... This command pushes the current location onto the default...
Read more >
How to automatically call Pop-Location at end of scope
Short answer: No. My take on Push-Location and Pop-Location is that you should generally avoid them, and adapt your script to use the...
Read more >
Pop-Location (Microsoft.PowerShell.Management)
The Pop-Location cmdlet changes the current location to the location most recently pushed onto the stack by using the Push-Location cmdlet.
Read more >
push/pop current directory? - linux
Excerpt from the link pushd Saves the current directory on the top of the directory stack and then cd to dir. With no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found