name: Release UI

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - "ui-v*" # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  build_on_windows:
    env:
      PUPPETEER_SKIP_DOWNLOAD: 'true'
      VITE_APP_GTAG_API_SECRET: ${{ secrets.VITE_APP_GTAG_API_SECRET }}
      VITE_APP_GTAG_MEASUREMENT_ID: ${{ secrets.VITE_APP_GTAG_MEASUREMENT_ID }}
    runs-on: windows-2022
    permissions: write-all

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Checkout source
      uses: actions/checkout@v2
    
    - name: Install pnpm
      run: |
        npm i pnpm@8.15.9 -g
         
    - name: Patch pnpm
      uses: actions/github-script@v3
      with:
        script: |
          // https://github.com/pnpm/pnpm/issues/5638
          const path = require('path')
          const fs = require('fs')
          const childProcess = require('child_process')
          const npmGlobalModuleFolder = childProcess.execSync('npm root -g').toString().trim()
          const pnpmCjsFilePath = path.join(npmGlobalModuleFolder, 'pnpm/bin/pnpm.cjs')
          let pnpmCjsFileContent = fs.readFileSync(pnpmCjsFilePath).toString().replace(/^(#!\/usr\/bin\/env node)/, '#!node')
          fs.writeFileSync(pnpmCjsFilePath, pnpmCjsFileContent)

    - name: Install dependencies
      run: |
        cd packages/ui
        pnpm i --force

    - name: Build
      run: |
        cd packages/ui
        pnpm run build:make-dist-for-current-platform

    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: binary-geekgeekrun-ui-windows-x64-${{ github.sha }}
        path: |
          ${{ github.workspace }}/packages/ui/dist/*.exe
        overwrite: true

  build_on_macos:
    strategy:
      matrix:
        platform:
          - os: macos-13 # for x64 build
            arch: x64
          - os: macos-14 # for arm64 build
            arch: arm64
    env:
      PUPPETEER_SKIP_DOWNLOAD: 'true'
      VITE_APP_GTAG_API_SECRET: ${{ secrets.VITE_APP_GTAG_API_SECRET }}
      VITE_APP_GTAG_MEASUREMENT_ID: ${{ secrets.VITE_APP_GTAG_MEASUREMENT_ID }}
    runs-on: ${{ matrix.platform.os }}
    permissions: write-all

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Checkout source
      uses: actions/checkout@v2
    
    - name: Install dependencies
      run: |
        npm i pnpm@8.15.9 -g
        cd packages/ui
        pnpm i --force

    - name: Build
      run: |
        cd packages/ui
        pnpm run build:make-dist-for-current-platform

    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: binary-geekgeekrun-ui-macos-${{ matrix.platform.arch }}-${{ github.sha }}
        path: |
          ${{ github.workspace }}/packages/ui/dist/*.dmg
        overwrite: true

  build_on_linux:
    env:
      PUPPETEER_SKIP_DOWNLOAD: 'true'
      VITE_APP_GTAG_API_SECRET: ${{ secrets.VITE_APP_GTAG_API_SECRET }}
      VITE_APP_GTAG_MEASUREMENT_ID: ${{ secrets.VITE_APP_GTAG_MEASUREMENT_ID }}
    runs-on: ubuntu-22.04
    permissions: write-all

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Checkout source
      uses: actions/checkout@v2
    
    - name: Install dependencies
      run: |
        npm i pnpm@8.15.9 -g
        cd packages/ui
        pnpm i --force

    - name: Build
      run: |
        cd packages/ui
        pnpm run build:make-dist-for-current-platform

    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: binary-geekgeekrun-ui-linux-x64-${{ github.sha }}
        path: |
          ${{ github.workspace }}/packages/ui/dist/*.deb
          ${{ github.workspace }}/packages/ui/dist/*.rpm
        overwrite: true

  release:
    runs-on: ubuntu-latest
    permissions: write-all
    needs: 
      - build_on_linux
      - build_on_macos
      - build_on_windows

    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          path: geekgeekrun-ui@${{ github.sha }}
          pattern: binary-geekgeekrun-ui-*
          merge-multiple: true

      - name: Display structure of downloaded files
        run: ls -llR geekgeekrun-ui@${{ github.sha }}

      - name: Create release
        uses: ncipollo/release-action@v1
        with:
          prerelease: true
          allowUpdates: true
          artifacts: geekgeekrun-ui@${{ github.sha }}/*
          tag: ${{ github.ref }}
          token: ${{ secrets.GITHUB_TOKEN }}
          body: TODO New Release.
