diff --git a/Build.ps1 b/Build.ps1 index c9db75f..9ea78f6 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -89,22 +89,37 @@ function Main { Write-Host "Building for $Version..." Build-FromSource $Version } else { - $latestTag = git tag --sort=-creatordate | Select-Object -First 1 - $latestTagTime = [int](git log -1 $latestTag --format=%at 2>&1 | Where-Object { $_ -match '^\d+$' }) - - $currentBuildTime = if ($buildFiles = Get-ChildItem -Path "build/avante_html2md*" -ErrorAction SilentlyContinue) { - [long](($buildFiles | ForEach-Object { $_.LastWriteTime } | - Measure-Object -Maximum).Maximum.Subtract([datetime]'1970-01-01').TotalSeconds) + $latestTag = git describe --tags --abbrev=0 2>&1 | Where-Object { $_ -ne $null } + $builtTag = if (Test-Path "build/.tag") { + Get-Content "build/.tag" } else { - $latestTagTime + $null } - if ($latestTagTime -lt $currentBuildTime) { - Write-Host "Local build is up to date. No download needed." - return + function Save-Tag($tag) { + $tag | Set-Content "build/.tag" + } + + if ($latestTag -eq $builtTag -and $latestTag) { + Write-Host "Local build is up to date. No download needed." + } elseif ($latestTag -ne $builtTag -and $latestTag) { + if (Test-Command "gh" -and Test-GHAuth) { + gh release download $latestTag --repo "github.com/$REPO_OWNER/$REPO_NAME" --pattern "*$ARTIFACT_NAME_PATTERN*" --clobber --output - | tar -zxvf - -C $TARGET_DIR + Save-Tag $latestTag + } else { + $ARTIFACT_URL = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$latestTag" | Select-String -Pattern "browser_download_url" | ForEach-Object { $_.Matches.Groups[1].Value } | Where-Object { $_ -match $ARTIFACT_NAME_PATTERN } + + Invoke-WebRequest -Uri $ARTIFACT_URL -OutFile "$TempFile" + Expand-Archive -Path "$TempFile" -DestinationPath "$TARGET_DIR" -Force + Remove-Item "$TempFile" + Save-Tag $latestTag + } + } else { + cargo build --release --features=$Version + Get-ChildItem -Path "target/release/avante_*.dll" | ForEach-Object { + Copy-Item $_.FullName "build/$($_.Name)" + } } - Write-Host "Downloading for $Version..." - Download-Prebuilt $Version } Write-Host "Completed!" } diff --git a/build.sh b/build.sh index 6e5c032..c537a2a 100644 --- a/build.sh +++ b/build.sh @@ -51,36 +51,48 @@ LUA_VERSION="${LUA_VERSION:-luajit}" ARTIFACT_NAME_PATTERN="avante_lib-$PLATFORM-$ARCH-$LUA_VERSION" test_command() { - command -v "$1" >/dev/null 2>&1 + command -v "$1" >/dev/null 2>&1 } test_gh_auth() { - if gh api user >/dev/null 2>&1; then - return 0 - else - return 1 - fi + if gh api user >/dev/null 2>&1; then + return 0 + else + return 1 + fi } if [ ! -d "$TARGET_DIR" ]; then - mkdir -p "$TARGET_DIR" + mkdir -p "$TARGET_DIR" fi -latest_tag_time=$(git tag --sort=-creatordate | xargs git log -1 --format=%at) -current_build_time=$(stat -c %Y build/avante_html2md* 2>/dev/null || echo "$latest_tag_time") -if [[ "$latest_tag_time" -ge "$current_build_time" ]]; then +latest_tag="$(git describe --tags --abbrev=0 || true)" # will be empty in clone repos +built_tag="$(cat build/.tag 2>/dev/null || true)" + +save_tag() { + echo "$latest_tag" >build/.tag +} + +if [[ "$latest_tag" = "$built_tag" && -n "$latest_tag" ]]; then + echo "Local build is up to date. No download needed." +elif [[ "$latest_tag" != "$built_tag" && -n "$latest_tag" ]]; then if test_command "gh" && test_gh_auth; then - gh release download --repo "github.com/$REPO_OWNER/$REPO_NAME" --pattern "*$ARTIFACT_NAME_PATTERN*" --clobber --output - | tar -zxv -C "$TARGET_DIR" + gh release download "$latest_tag" --repo "github.com/$REPO_OWNER/$REPO_NAME" --pattern "*$ARTIFACT_NAME_PATTERN*" --clobber --output - | tar -zxv -C "$TARGET_DIR" + save_tag 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) + ARTIFACT_URL=$(curl -s "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$latest_tag" | 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" + save_tag fi else - echo "Local build is up to date. No download needed." + cargo build --release --features=$LUA_VERSION + for f in target/release/lib*.so; do + cp "$f" "build/$(echo $f | sed 's#.*/lib##')" + done fi