为 Github Copilot CLI 创建别名(ghcs、ghce 无法识别)

问题描述 投票:0回答:1

我在为 github copilot 设置别名时遇到问题。

gh copilot suggest
gh copilot explain
工作正常,我想将它们减少为
ghcs
ghce

当我运行

ghcs "create new dir"
时,这是我收到的错误,

ghcs : The term 'ghcs' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ ghcs "new dir"
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (ghcs:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

背景 我在 Windows 上使用 VSC 和 Powershell,也刚刚下载了 Microsoft 的 Powershell 扩展,但没有帮助。

在 github 的手册页上,他们说它应该像在终端中运行一样简单

gh copilot alias

这在我的 powershell 文件夹中创建了两个文件: New Files in the folder

PS。 我还尝试通过

New-Item -Force $PROFILE
创建一个新的 powershell 配置文件并复制此内容(这是 GH 在其手册中也建议的内容)
$GH_COPILOT_PROFILE = Join-Path -Path $(Split-Path -Path $PROFILE -Parent) -ChildPath "gh-copilot.ps1" gh copilot alias -- pwsh | Out-File ( New-Item -Path $GH_COPILOT_PROFILE -Force ) echo ". $GH_COPILOT_PROFILE" >> $PROFILE
,但仍然没有运气。

提前致谢! :)

windows powershell github command-line-interface alias
1个回答
0
投票

tl;博士

  • 运行说明中详细介绍的别名安装命令一次,以便以编程方式更新您的PowerShell

    $PROFILE
    文件 - 不要将这些指令本身添加到后者。

  • 在撰写本文时,生成的“别名”(实际上是包装器

    function
    s)仅适用于PowerShell(核心)7+。请参阅下文了解 Windows PowerShell 解决方案。


目前的问题:

在撰写本文时,GitHub CLI

Copilot 扩展
gh存在两个问题:

  • gh copilot alias -- pwsh
    生成仅适用于 PowerShell (Core) 7+ 的 PowerShell 代码;您的症状表明您是从 Windows PowerShell 运行的。

      但是,可以轻松修复该代码以在 Windows PowerShell 中运行。
    • 请参阅
    • GitHub 问题 #32189

  • 设置别名的说明

    令人困惑:

      显示的代码是
    • 设置代码

      ,意味着运行一次,以便以编程方式将“别名”(实际上是函数)添加到受支持 shell 的配置文件/初始化文件中,而指令,例如“将以下内容添加到您的 PowerShell 配置文件中:”,错误地建议将安装代码本身添加到配置文件/初始化文件中。

      请参阅
    • GitHub 问题 #32187
  • 安装
Windows PowerShell 兼容
别名(函数):
希望在 Copilot 扩展更新后,对以下解决方法的需求将会消失。

如果您对底层

gh copilot
    命令的功能只不过是
  • 短名称 感到满意,您可以按如下方式更新您的 $PROFILE
    $GH_COPILOT_PROFILE = Join-Path (Split-Path $PROFILE) gh-copilot.ps1
    @'
    function ghce { gh copilot explain @args }
    function ghcs { gh copilot suggest @args }
    '@ >> (New-Item -FORCE $GH_COPILOT_PROFILE)
    ". $GH_COPILOT_PROFILE" >> $PROFILE
    
如果您想使用 
gh copilot alias -- pwsh
    生成的更复杂的包装函数的 Windows PowerShell 兼容版本(基于撰写本文时的当前版本):
  • $GH_COPILOT_PROFILE = Join-Path (Split-Path $PROFILE) gh-copilot.ps1
    @'
    function ghcs {
        # 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" $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 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:GH_DEBUG = $envGhDebug
        }
    }
    
    function ghce {
        # 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(
            [Parameter(Position=0, ValueFromRemainingArguments)]
            [string[]]$Prompt
        )
        begin {
            # Store original value of GH_DEBUG environment variable
            $envGhDebug = $Env:GH_DEBUG
        }
        process {
            if ($PSBoundParameters['Debug']) {
                $Env:GH_DEBUG = 'api'
            }
    
            gh copilot explain $Prompt
    
            # Restore GH_DEBUG environment variable to its original value
            $Env:GH_DEBUG = $envGhDebug
        }
    }
    '@ >> (New-Item -FORCE $GH_COPILOT_PROFILE)
    ". $GH_COPILOT_PROFILE" >> $PROFILE
    
包装函数添加了对调试的支持,并且
ghcs
能够执行建议的命令并将其添加到 PowerShell 的命令历史记录中。

请注意,仅对原始代码应用了最小的更改集;简而言之:
clean

块的内容(仅 PowerShell 7+ 支持)已移至

end

 / 
process
 块中。
配置文件加载的一般注意事项:


请注意,安装
    Visual Studio Code
  • PowerShell 扩展(它带有自己的专用 shell - PIC(PowerShell 集成控制台)) - 会更改 automatic $PROFILE 变量 指向的文件路径,因为 PIC 使用单独的

    profile
    文件。 此外,可以在 PIC 中

    禁用
      配置文件加载,因此请确保已启用它:打开“设置”视图 (
    • Ctrl+,) 并导航至 PowerShell: Enable Profile Loading;搜索 profile load
       应该足以找到它。
      
      
    因此,从 PIC 安装别名将
  • 不会
  • 使它们在常规 PowerShell 控制台/Windows 终端会话中可用,也不会在 Visual Studio Code 集成终端中的常规(非 PIC)PowerShell 会话中可用。

    要使所有这些环境
      共享
    • 定义,您有两个选择:

      要么:目标
        $PROFILE.CurrentUserAllHosts
      • 而不仅仅是

        $PROFILE

        ;前者指向的路径由当前用户运行的
        所有
         PowerShell 环境加载,无论托管它们的应用程序是什么。

        或者:从常规(非 PIC)会话修改
      • $PROFILE
      • ,然后将以下内容添加到特定于 PIC 的

        $PROFILE

         文件(使用 
        psedit $PROFILE
         打开它进行编辑),这只需加载 
        .CurrentUserCurrentHost
        基于控制台(基于终端)的 PowerShell 会话使用的配置文件:
        . ($PROFILE -replace '\.VSCode_', '.PowerShell_')
        

        
        
    修改配置文件后,您必须启动新的 PowerShell 会话或显式重新加载它,例如与
  • . $PROFILE
  • ,使用

    .

    点采购运算符

© www.soinside.com 2019 - 2024. All rights reserved.