Skip to content

Commit

Permalink
feat: use gh cli if exists
Browse files Browse the repository at this point in the history
Signed-off-by: Hanchin Hsieh <[email protected]>
  • Loading branch information
yuchanns committed Jan 1, 2025
1 parent 9abbec4 commit 567f47a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
32 changes: 25 additions & 7 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
31 changes: 26 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 567f47a

Please sign in to comment.