feat: use gh cli if exists (#1017)

Signed-off-by: Hanchin Hsieh <me@yuchanns.xyz>
This commit is contained in:
Hanchin Hsieh 2025-01-01 20:51:52 +08:00 committed by GitHub
parent a0d05544af
commit c088ad7aff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 12 deletions

View File

@ -25,6 +25,19 @@ function Build-FromSource($feature) {
Remove-Item -Recurse -Force "target"
}
function Test-Command($cmdname) {
return $null -ne (Get-Command $cmdname -ErrorAction SilentlyContinue)
}
function Test-GHAuth {
try {
$null = gh api user
return $true
} catch {
return $false
}
}
function Download-Prebuilt($feature) {
$REPO_OWNER = "yetone"
$REPO_NAME = "avante.nvim"
@ -46,18 +59,23 @@ function Download-Prebuilt($feature) {
# Set the artifact name pattern
$ARTIFACT_NAME_PATTERN = "avante_lib-$PLATFORM-$ARCH-$LUA_VERSION"
# Get the artifact download URL
$LATEST_RELEASE = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest"
$ARTIFACT_URL = $LATEST_RELEASE.assets | Where-Object { $_.name -like "*$ARTIFACT_NAME_PATTERN*" } | Select-Object -ExpandProperty browser_download_url
$TempFile = Get-Item ([System.IO.Path]::GetTempFilename()) | Rename-Item -NewName { $_.Name + ".zip" } -PassThru
if ((Test-Command "gh") -and (Test-GHAuth)) {
gh release download --repo "$REPO_OWNER/$REPO_NAME" --pattern "*$ARTIFACT_NAME_PATTERN*" --output $TempFile --clobber
} else {
# Get the artifact download URL
$LATEST_RELEASE = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest"
$ARTIFACT_URL = $LATEST_RELEASE.assets | Where-Object { $_.name -like "*$ARTIFACT_NAME_PATTERN*" } | Select-Object -ExpandProperty browser_download_url
# Download and extract the artifact
Invoke-WebRequest -Uri $ARTIFACT_URL -OutFile $TempFile
}
# Create target directory if it doesn't exist
if (-not (Test-Path $TARGET_DIR)) {
New-Item -ItemType Directory -Path $TARGET_DIR | Out-Null
}
# Download and extract the artifact
$TempFile = Get-Item ([System.IO.Path]::GetTempFilename()) | Rename-Item -NewName { $_.Name + ".zip" } -PassThru
Invoke-WebRequest -Uri $ARTIFACT_URL -OutFile $TempFile
Expand-Archive -Path $TempFile -DestinationPath $TARGET_DIR -Force
Remove-Item $TempFile
}

View File

@ -50,11 +50,32 @@ LUA_VERSION="${LUA_VERSION:-luajit}"
# Set the artifact name pattern
ARTIFACT_NAME_PATTERN="avante_lib-$PLATFORM-$ARCH-$LUA_VERSION"
# Get the artifact download URL
ARTIFACT_URL=$(curl -s "https://api.github.com/repos/yetone/avante.nvim/releases/latest" | grep "browser_download_url" | cut -d '"' -f 4 | grep $ARTIFACT_NAME_PATTERN)
test_command() {
command -v "$1" >/dev/null 2>&1
}
set -x
test_gh_auth() {
if gh api user >/dev/null 2>&1; then
return 0
else
return 1
fi
}
mkdir -p "$TARGET_DIR"
if [ ! -d "$TARGET_DIR" ]; then
mkdir -p "$TARGET_DIR"
fi
if test_command "gh" && test_gh_auth; then
gh release download --repo "$REPO_OWNER/$REPO_NAME" --pattern "*$ARTIFACT_NAME_PATTERN*" --clobber --output - | tar -zxv -C "$TARGET_DIR"
else
# Get the artifact download URL
ARTIFACT_URL=$(curl -s "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest" | grep "browser_download_url" | cut -d '"' -f 4 | grep $ARTIFACT_NAME_PATTERN)
set -x
mkdir -p "$TARGET_DIR"
curl -L "$ARTIFACT_URL" | tar -zxv -C "$TARGET_DIR"
fi
curl -L "$ARTIFACT_URL" | tar -zxv -C "$TARGET_DIR"