Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Created September 6, 2024 04:43
Show Gist options
  • Save doggy8088/18ca8d450062d484d8ec8ad08cd7ad3f to your computer and use it in GitHub Desktop.
Save doggy8088/18ca8d450062d484d8ec8ad08cd7ad3f to your computer and use it in GitHub Desktop.
支援預設使用 PowerShell 執行環境的 GitHub Copilot Suggest Cmdlets,從 gh copilot alias 產生範本並加入預設 Prompt,請在下載後加入檔案到 $HOME\Documents\PowerShell\Scripts 目錄即可。
# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
param(
[ValidateSet('gh', 'git', 'shell')]
[Alias('t')]
[String]$Target = 'shell',
[Parameter(Position=0, ValueFromRemainingArguments)]
[string]$Prompt
)
begin {
# Create temporary file to store potential command user wants to execute when exiting
$executeCommandFile = New-TemporaryFile
# Store original value of GH_DEBUG environment variable
$envGhDebug = $Env:GH_DEBUG
}
process {
if ($PSBoundParameters['Debug']) {
$Env:GH_DEBUG = 'api'
}
gh copilot suggest -t $Target -s "$executeCommandFile" "I'm writing a PowerShell script on Windows. $Prompt"
}
end {
# Execute command contained within temporary file if it is not empty
if ($executeCommandFile.Length -gt 0) {
# Extract command to execute from temporary file
$executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()
# Insert command into PowerShell up/down arrow key history
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)
# Insert command into PowerShell history
$now = Get-Date
$executeCommandHistoryItem = [PSCustomObject]@{
CommandLine = $executeCommand
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
StartExecutionTime = $now
EndExecutionTime = $now.AddSeconds(1)
}
Add-History -InputObject $executeCommandHistoryItem
# Execute command
Write-Host "`n"
Invoke-Expression $executeCommand
}
}
clean {
# Clean up temporary file used to store potential command user wants to execute when exiting
Remove-Item -Path $executeCommandFile
# Restore GH_DEBUG environment variable to its original value
$Env:G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AW
# hE7uUKtxDXYQ3EKQyEmbAHZLuYPsYmVHJJ2wnPQisqekQ58fnXr7fk2mB6MAp0R7
# 3mbzpYupEdDN09UiWw==
# SIG # End signature block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment