name: Release App

on:
  push:
    tags:
      - 'v*.*.*'
    paths-ignore:
      - 'README.md'
      - 'docs/**'
      - '.vscode'
  pull_request:
    branches:
      - master
    paths-ignore:
      - 'docs/**'
      - 'README.md'
      - '.vscode'

permissions:
  actions: write
  contents: write
  security-events: write

concurrency:
  # The concurrency group contains the workflow name and the branch name for
  # pull requests or the commit hash for any other events.
  group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
  cancel-in-progress: true

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      RELEASE_UPLOAD_ID: ${{ steps.create_release.outputs.id }}

    steps:
      - uses: actions/checkout@v2
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:10}"
          echo "version=${GITHUB_REF:10}" >> $GITHUB_ENV
      - name: Create Release
        id: create_release
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          draft: true
          tag_name: '${{ env.version }}'
          release_name: 'AFFiNE ${{ env.version }}'
          body: 'See the assets to download this version and install.'

  build-tauri:
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        platform: [macos-latest, ubuntu-latest, windows-latest]

    runs-on: ${{ matrix.platform }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - name: install dependencies (ubuntu only)
        if: matrix.platform == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
      - name: install Rust stable
        uses: dtolnay/rust-toolchain@stable
      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: latest
      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
      - name: Get npm cache directory
        uses: actions/cache@v3
        id: cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Get rust cache
        uses: Swatinem/rust-cache@v2
        id: rust-cache
        with:
          workspaces: |
            apps/desktop/src-tauri

      - name: Install dependencies
        run: pnpm i -r

      - name: Make App Binary
        run: pnpm build:app
        working-directory: apps/desktop
        env:
          CI: true
          CI_PULL_REQUEST: ${{ github.event_name == 'pull_request' }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload to release on git tag Or output artifact path on nightly
        uses: tauri-apps/tauri-action@dev
        id: tauri-action
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          releaseId: ${{ needs.create-release.outputs.RELEASE_UPLOAD_ID }}
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: tauri-client-app-artifact
          path: |
            ${{ fromJSON(steps.tauri-action.outputs.artifactPaths)[0] }}
            ${{ fromJSON(steps.tauri-action.outputs.artifactPaths)[1] }}
            ${{ fromJSON(steps.tauri-action.outputs.artifactPaths)[2] }}
