name: Release

on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag (e.g. v1.0.0)'
        required: true
        default: 'v0.9.18'
  push:
    tags:
      - v*.*.*

permissions:
  contents: write

jobs:
  release:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
      fail-fast: false

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4

      - name: Get release tag
        id: get-tag
        shell: bash
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "tag=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
          else
            echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          fi

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 20

      - name: Install corepack
        run: corepack enable && corepack prepare yarn@4.3.1 --activate

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT

      - name: Cache yarn dependencies
        uses: actions/cache@v3
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install Dependencies
        run: yarn install

      - name: Build Linux
        if: matrix.os == 'ubuntu-latest'
        run: |
          yarn build:npm linux
          yarn build:linux

        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}

      - name: Build Mac
        if: matrix.os == 'macos-latest'
        run: |
          yarn build:npm mac
          yarn build:mac
        env:
          CSC_LINK: ${{ secrets.CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
          APPLE_ID: ${{ vars.APPLE_ID }}
          APPLE_APP_SPECIFIC_PASSWORD: ${{ vars.APPLE_APP_SPECIFIC_PASSWORD }}
          APPLE_TEAM_ID: ${{ vars.APPLE_TEAM_ID }}
          RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Windows
        if: matrix.os == 'windows-latest'
        run: yarn build:win
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}

      - name: Replace spaces in filenames
        run: node scripts/replace-spaces.js

      - name: Release
        uses: ncipollo/release-action@v1
        with:
          draft: true
          allowUpdates: true
          makeLatest: false
          tag: ${{ steps.get-tag.outputs.tag }}
          artifacts: 'dist/*.exe,dist/*.zip,dist/*.dmg,dist/*.AppImage,dist/*.snap,dist/*.deb,dist/*.rpm,dist/*.tar.gz,dist/latest*.yml,dist/*.blockmap'
          token: ${{ secrets.GITHUB_TOKEN }}
