name: Release Please

on:
  push:
    branches: [main] # updates/opens the release PR when commits land on main
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write
  id-token: write

concurrency:
  group: release-please
  cancel-in-progress: true

jobs:
  release-please:
    name: Release Please
    runs-on: ubuntu-latest
    outputs:
      release_created: ${{ steps.rp.outputs.release_created }}
    steps:
      - id: app-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.GH_STOAT_RELEASE_APP_ID }}
          private-key: ${{ secrets.GH_STOAT_RELEASE_APP_PRIVATE_KEY }}
      - id: rp
        uses: googleapis/release-please-action@v4
        with:
          token: ${{ steps.app-token.outputs.token }}
          config-file: release-please-config.json

  publish-release:
    name: Publish App
    needs: release-please
    if: needs.release-please.outputs.release_created == 'true'
    runs-on: ${{ matrix.os }}

    permissions:
      contents: write

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Checkout assets
        run: git -c submodule."assets".update=checkout submodule update --init assets

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install

      - name: Publish
        run: |
          pnpm run publish
        env:
          PLATFORM: ${{ matrix.os }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish macOS x64
        if: matrix.os == 'macos-latest'
        run: pnpm run publish --arch=x64
        env:
          PLATFORM: ${{ matrix.os }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish Linux arm64
        if: matrix.os == 'ubuntu-latest'
        run: pnpm run publish --arch=arm64
        env:
          PLATFORM: ${{ matrix.os }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
