Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
February 9, 2023

Live PowerShell Tool Making Part 3

We’re back once more to continue building a Crescendo module around the GitHub command line tool gh.exe. You’ve seen how I approach building a PowerShell and the questions and decisions that come up. Now that I’ve built a few more Crescendo commands, I realize I can begin copying and pasting. Some of the commands I have in mind, I think, will be very similar. Plus, I have my VSCode Crescendo command snippet.

As I was reviewing my build.ps1 script, I realized it was going to grow out of control quickly. I decided it made more sense to separate Crescendo command definitions into separate files and call them from the build script. I created a file for each definition.

Param([string]$cli)

$cmd = New-CrescendoCommand -Verb Get -Noun ghLabel -OriginalName $cli
$cmd.Platform = "Windows"
$cmd.DefaultParameterSetName = "default"
$cmd.OriginalCommandElements = @("label","list","--json", "name,description,color")
$cmd.Description = "Use this command to retrieve a list of labels defined for current repository."
$cmd.OriginalText = $(gh label list --help | Out-String)
$cmd.usage = New-UsageInfo -usage "Get issue labels"
$example = @{
    Command         = 'Get-ghLabel'
    Description     = 'Get all issue labels for the current GitHub repository.'
    OriginalCommand = 'gh label list'
}
$cmd.Examples += New-ExampleInfo @example
$handler = New-OutputHandler
$handler.ParameterSetName = "default"
$handler.StreamOutput = $true
$handler.handler = '$input | ConvertFrom-JSON | Convert-LabelJSON'
$cmd.OutputHandlers = $handler
#write the command object to the pipeline
$cmd

I came up with a name convention of cr_<command name>.ps1. The file above is cr_Get-ghLabel.ps1 and is in the same folder as build.ps1. The build script now looks like this, with placeholder comments for the remaining commands.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.