Hi,
I was wondering whether it is possible to use PowerShell scripts that would offer certain interactivity, like assume user text input, or provide a list of values to choose from (preferably using the VSCode's InputBox and QuickPick). With that, there will be a possibility to make these tasks more dynamic and adapt to the project on the go.
For example, I'm working on a project that has 60+ Pester Tests already. As the project grows, I'd like to be able to quickly run only the tests that are related to the code that I'm working on (and not wait for unrelated tests to finish). And to be able to do that, I'd like to have a task (since, I'm not so good with TypeScript yet to write an extension, I was thinking about writing a couple of PowerShell cmdlets instead) that once invoked, would provide me with a list of tests and tags that are available within the project's pester tests (dynamically discovered by going through the Syntax Tree of the tests) and allow me to chose the ones I'd like to run. And once the list is obtained, run Pester task similarly to how it is currently configured to run.
This is just one of the scenarios that I'm thinking of right now, but I'm sure this could open up a whole lot of others.
Thanks!
The PowerShell extension does expose an API that can be called from PowerShell. That API is currently aimed more at manipulating files but it can show messages e.g.:
$psEditor.Window.ShowInformationMessage()
The way you typically use $psEditor is to create a Microsoft.VSCode_profile.ps1 in your $Home\Documents\WindowsPowerShell dir and put commands like this in there:
Register-EditorCommand `
-Name "Helpers.LinesOfScript" `
-DisplayName "Count Lines of Script" `
-ScriptBlock {
param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context)
$loc = 0
$inBlockComment = $false
$lines = $context.CurrentFile.GetTextLines()
foreach ($line in $lines) {
if ($line -match '#>(.*?)') {
$inBlockComment = $false
if ($matches[1]) {
$loc++
}
continue
}
if ($line -match '^(.*?)<#') {
$inBlockComment = $true
if ($matches[1]) {
$loc++
}
continue
}
if ($line -match '^(\s*#.*?|\s*)$') { continue }
$loc++
}
$psEditor.Window.SetStatusBarMessage("Lines of script: $loc")
}
Now reload VSCode, bring up the Command Palette (F1) and type "Show additional", select PowerShell: Show additional commands, now the above command show up Count lines of script. Select that command and then look in the status bar.
You can explore the $psEditor object model in VSCode within a .ps1 file by typing $psEditor. and exploring via IntelliSense. AFAICT, there is no quick pick support yet. You can explore what is available via the $context parameter by looking at $context IntelliSense in the Register-EditorCommand scriptblock. But here is what's currently available:

I could see registering one editor command (say TestCoverageInitialize) that runs Invoke-Pester on each individual test file and stashes the filenames of all source scripts that it tests in a hashtable. Then another editor command (say RunImpactedTests) that would execute test files that test the $context.CurrentFile.
I'll be adding quick pick support to $psEditor this week!
Most helpful comment
I'll be adding quick pick support to
$psEditorthis week!