From d0cbb1cc2f76aee99f9afedaad6b851c963dc4d3 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Tue, 5 Sep 2023 13:12:10 +0800 Subject: [PATCH] add support for vscode Signed-off-by: pengzhile --- README.md | 5 +++-- cocopilot.sh | 2 +- vscode.bat | 40 ++++++++++++++++++++++++++++++++++++++++ vscode.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 vscode.bat create mode 100644 vscode.sh diff --git a/README.md b/README.md index c23dc4a..88f5f04 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,6 @@ 3. `macOS/linux系统`执行`cocopilot.sh` 4. 看到`done. please restart your ide.`表示成功。 5. 重启你的IDE就好。 -6. 不支持`VSCode`,它都抛弃`macOS`了。 -7. 这是个小玩具,可能测试不充分,别找我。 +6. 这是个小玩具,可能测试不充分,别找我。 + +#### 对于`VSCode`,步骤基本相同,执行对应`vscode.sh`/`vscode.bat`,**无需执行**`cocopilot.sh`/`cocopilot.bat`。 diff --git a/cocopilot.sh b/cocopilot.sh index 2ce7ea9..a7ab7a9 100644 --- a/cocopilot.sh +++ b/cocopilot.sh @@ -10,7 +10,7 @@ fi COPILOT_DIR="$CONFIG_DIR/github-copilot" if [ ! -d "$COPILOT_DIR" ]; then - mkdir -p "${COPILOT_DIR}" + mkdir -p "$COPILOT_DIR" fi echo '{"github.com":{"user":"cocopilot","oauth_token":"ghu_ThisIsARealFreeCopilotKeyByCoCopilot","dev_override":{"copilot_token_url":"https://api.cocopilot.org/copilot_internal/v2/token"}}}' > "$COPILOT_DIR/hosts.json" diff --git a/vscode.bat b/vscode.bat new file mode 100644 index 0000000..9f2dcaa --- /dev/null +++ b/vscode.bat @@ -0,0 +1,40 @@ +@echo off +setlocal + +set extensions_dir=%userprofile%\.vscode\extensions +if not exist "%extensions_dir%" ( + echo ERROR: VSCode extensions directory not found! + pause + exit /b 1 +) + +for /f "tokens=*" %%a in ('dir /b /ad "%extensions_dir%" ^| findstr /r /c:"^github\.copilot-[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$"') do ( + set copilot_dir=%%a + goto :found +) + +echo ERROR: Copilot extension not found! +pause +exit /b 1 + +:found +set copilot_dir=%extensions_dir%\%copilot_dir% +set extension_file=%copilot_dir%\dist\extension.js +if not exist "%extension_file%" ( + echo ERROR: Copilot extension entry file not found! + pause + exit /b 1 +) + +echo please be patient... + +set tmp_file=%copilot_dir%\dist\extension.js.tmp +( + echo process.env.CODESPACES="true";process.env.GITHUB_TOKEN="ghu_ThisIsARealFreeCopilotKeyByCoCopilot";process.env.GITHUB_SERVER_URL="https://github.com";process.env.GITHUB_API_URL="https://api.cocopilot.org"; +) > "%tmp_file%" + +type "%extension_file%" >> "%tmp_file%" +move "%tmp_file%" "%extension_file%" > nul + +echo done. please restart your vscode. +pause diff --git a/vscode.sh b/vscode.sh new file mode 100644 index 0000000..2d9bc97 --- /dev/null +++ b/vscode.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -e + +EXTENSIONS_DIR="$HOME/.vscode/extensions" +if [ ! -d "$EXTENSIONS_DIR" ]; then + echo "ERROR: VSCode extensions directory not found!" + exit 1 +fi + +COPILOT_DIR=$(ls -lt "$EXTENSIONS_DIR" | grep '^d' | awk '{print $9}' | grep -E '^github\.copilot-\d+\.\d+\.\d+$' | head -n 1) +if [ -z "$COPILOT_DIR" ]; then + echo "ERROR: Copilot extension not found!" + exit 1 +fi + +COPILOT_DIR="$EXTENSIONS_DIR/$COPILOT_DIR" +EXTENSION_FILE="$COPILOT_DIR/dist/extension.js" +if [ ! -f "$EXTENSION_FILE" ]; then + echo "ERROR: Copilot extension entry file not found!" + exit 1 +fi + +TMP_FILE="$COPILOT_DIR/dist/extension.js.tmp" +echo 'process.env.CODESPACES="true";process.env.GITHUB_TOKEN="ghu_ThisIsARealFreeCopilotKeyByCoCopilot";process.env.GITHUB_SERVER_URL="https://github.com";process.env.GITHUB_API_URL="https://api.cocopilot.org";' > "$TMP_FILE" +cat "$EXTENSION_FILE" >> "$TMP_FILE" +mv "$TMP_FILE" "$EXTENSION_FILE" + +echo 'done. please restart your vscode.'