name: Build Binary

on:
  workflow_call:

jobs:
  build-frontend:
    name: Build frontend
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: web
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
      - name: Setup bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      - name: Install dependencies
        run: bun install --frozen-lockfile
      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-${{ hashFiles('docs/bun.lockb') }}
          restore-keys: |
            ${{ runner.os }}-bun-
      - name: Build Frontend
        run: bun run build
      - name: Upload Web Build Artifact
        uses: actions/upload-artifact@v4
        with:
          name: web-build
          path: web/build
  build:
    name: Build bili-sync-rs for ${{ matrix.platform.release_for }}
    needs: build-frontend
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        platform:
          - release_for: Linux-x86_64
            os: ubuntu-20.04
            target: x86_64-unknown-linux-musl
            bin: bili-sync-rs
            name: bili-sync-rs-Linux-x86_64-musl.tar.gz
          - release_for: Linux-aarch64
            os: ubuntu-20.04
            target: aarch64-unknown-linux-musl
            bin: bili-sync-rs
            name: bili-sync-rs-Linux-aarch64-musl.tar.gz
          - release_for: macOS-x86_64
            os: macOS-latest
            target: x86_64-apple-darwin
            bin: bili-sync-rs
            name: bili-sync-rs-Darwin-x86_64.tar.gz
          - release_for: macOS-aarch64
            os: macOS-latest
            target: aarch64-apple-darwin
            bin: bili-sync-rs
            name: bili-sync-rs-Darwin-aarch64.tar.gz
          - release_for: Windows-x86_64
            os: windows-latest
            target: x86_64-pc-windows-msvc
            bin: bili-sync-rs.exe
            name: bili-sync-rs-Windows-x86_64.zip
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
      - name: Download Web Build Artifact
        uses: actions/download-artifact@v4
        with:
          name: web-build
          path: web/build
      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2
      - name: Install musl-tools
        run: sudo apt-get update --yes && sudo apt-get install --yes musl-tools
        if: contains(matrix.platform.target, 'musl')
      - name: Build binary
        uses: houseabsolute/actions-rust-cross@v0
        with:
          command: build
          target: ${{ matrix.platform.target }}
          toolchain: stable
          args: "--locked --release"
          strip: true
      - name: Package as archive
        shell: bash
        run: |
          cd target/${{ matrix.platform.target }}/release
          if [[ "${{ matrix.platform.target }}" == "x86_64-pc-windows-msvc" ]]; then
            7z a ../../../${{ matrix.platform.name }} ${{ matrix.platform.bin }}
          else
            tar czvf ../../../${{ matrix.platform.name }} ${{ matrix.platform.bin }}
          fi
      - name: Upload release artifact
        uses: actions/upload-artifact@v4
        with:
          name: bili-sync-rs-${{ matrix.platform.release_for }}
          path: |
            ${{ github.workspace }}/${{ matrix.platform.name }}
