name: Release CI

on:
  workflow_dispatch:
  push:
    tags:
      - v**

env:
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: short

jobs:
  release:
    strategy:
      matrix:
        os: [windows-latest, ubuntu-20.04, macos-latest]
    runs-on: ${{ matrix.os }}
    if: startsWith(github.repository, 'zzzgydi')
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: install Rust stable
        uses: dtolnay/rust-toolchain@stable

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri

      - name: Install Node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          cache: "yarn"

      - name: Install Dependencies (ubuntu only)
        if: startsWith(matrix.os, 'ubuntu-')
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf openssl

      - name: Yarn install and check
        run: |
          yarn install --network-timeout 1000000 --frozen-lockfile
          yarn run check

      - name: Tauri build
        uses: tauri-apps/tauri-action@v0
        # enable cache even though failed
        # continue-on-error: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
        with:
          tagName: v__VERSION__
          releaseName: "Clash Verge v__VERSION__"
          releaseBody: "More new features are now supported."
          releaseDraft: false
          prerelease: true

      - name: Portable Bundle
        if: startsWith(matrix.os, 'windows-')
        # rebuild with env settings
        run: |
          yarn build
          yarn run portable
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          VITE_WIN_PORTABLE: 1

  release-update:
    needs: release
    runs-on: ubuntu-latest
    if: |
      startsWith(github.repository, 'zzzgydi') &&
      startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          cache: "yarn"

      - name: Yarn install
        run: yarn install --network-timeout 1000000 --frozen-lockfile

      - name: Release updater file
        run: yarn run updater
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
