name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build-and-release:
    name: 构建 NSIS 安装包并发布
    runs-on: windows-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v7
        with:
          node-version: '20'
          cache: 'npm'

      - run: npm ci

      - name: Tauri Build
        run: npx tauri build

      - name: 上传安装包到 Release
        run: |
          $installer = Get-ChildItem -Path "target\release\bundle\nsis\*.exe" | Select-Object -First 1
          if (gh release view $env:GITHUB_REF_NAME 2>$null) {
            gh release upload $env:GITHUB_REF_NAME "$installer" --clobber
          } else {
            gh release create $env:GITHUB_REF_NAME "$installer" --title "$env:GITHUB_REF_NAME" --generate-notes
          }
        env:
          GH_TOKEN: ${{ github.token }}
