name: Release

on:
  push:
    tags:
      - v*.*.*

permissions:
  contents: write

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

    strategy:
      matrix:
        os: [macos-latest, windows-latest]

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

      - 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: Install Dependencies
        run: yarn install

      - name: Build Linux
        if: matrix.os == 'ubuntu-latest'
        run: yarn build:linux
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}

      - name: Build Mac
        if: matrix.os == 'macos-latest'
        run: 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 }}
          GH_TOKEN: ${{ secrets.GH_TOKEN }}

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

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

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          draft: false
          files: |
            dist/*.exe
            dist/*.zip
            dist/*.dmg
            dist/*.AppImage
            dist/*.snap
            dist/*.deb
            dist/*.rpm
            dist/*.tar.gz
            dist/latest*.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
