name: "publish"
on:
  workflow_dispatch:

jobs:
  get-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      body: ${{ steps.version.outputs.body }}
    steps:
      - uses: actions/checkout@v4
      - id: version
        run: |
          echo "version=$(jq -r .version package.json)" >> $GITHUB_OUTPUT
          echo "body<<EOF" >> "$GITHUB_OUTPUT"
          cat RELEASE_NOTES.md >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

  publish-tauri:
    needs: get-version
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: "macos-latest"
            args: "--target aarch64-apple-darwin"
          - platform: "macos-latest"
            args: "--target x86_64-apple-darwin"
          - platform: "ubuntu-22.04"
            args: ""
          - platform: "windows-latest"
            args: ""

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v4

      - name: install dependencies (ubuntu only)
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf libudev-dev

      - name: setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
      - name: setup node
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
          cache: "pnpm"

      - name: install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

      - name: Rust cache
        uses: swatinem/rust-cache@v2
        with:
          workspaces: "./src-tauri -> target"

      - name: install frontend dependencies
        run: pnpm install
      - uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
        with:
          tagName: v__VERSION__
          releaseName: "Emerald v__VERSION__"
          releaseBody: ${{ needs.get-version.outputs.body }}
          releaseDraft: true
          prerelease: false
          uploadUpdaterJson: true
          args: ${{ matrix.args }}

  publish-flatpak:
    needs: get-version
    runs-on: ubuntu-latest
    permissions:
      contents: write
    container:
      image: bilelmoussaoui/flatpak-github-actions:freedesktop-23.08
      options: --privileged
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: sleep for 20 seconds
        run: sleep 20
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: emerald.flatpak
          manifest-path: flatpak/io.github.Emerald_Legacy_Launcher.Emerald_Legacy_Launcher.yml
          cache-key: flatpak-builder-${{ github.sha }}
      - uses: softprops/action-gh-release@v2
        with:
          body: ${{ needs.get-version.outputs.body }}
          tag_name: v${{ needs.get-version.outputs.version }}
          files: emerald.flatpak
          draft: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
