name: Size Comparison

on:
  pull_request:
    branches: [master]
    paths:
      - .github/workflows/size-cmp.yml
      - "ci/**"
      - "packages/**"
      - "examples/**"
      - "Cargo.toml"

jobs:
  size-cmp:
    name: Collect ${{ matrix.target }} Size
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: ["master", "pr"]

    steps:
      - name: Checkout master
        uses: actions/checkout@v4
        if: ${{ matrix.target == 'master' }}
        with:
          repository: "yewstack/yew"
          ref: master

      - name: Checkout pull request
        uses: actions/checkout@v4
        if: ${{ matrix.target == 'pr' }}

      - name: Write Optimisation Flags
        run: |
          echo 'share-generics = true' >> .cargo/config.toml
          echo 'build-std = ["std", "panic_abort"]' >> .cargo/config.toml
          echo 'build-std-features = ["panic_immediate_abort"]' >> .cargo/config.toml

      - name: Setup toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: nightly
          components: rust-src
          targets: wasm32-unknown-unknown

      - name: Restore Rust cache for master
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: size-compare

      - name: Setup Trunk
        uses: jetli/trunk-action@v0.5.0
        with:
          version: "latest"

      - name: Build examples
        run: find ./*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0
        working-directory: examples
        env:
          RUSTUP_TOOLCHAIN: nightly
          RUSTFLAGS: --cfg nightly_yew

      - name: Collect size information
        run: python3 ci/collect_sizes.py
        env:
          ISSUE_NUMBER: ${{ github.event.number }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: size-cmp-${{ matrix.target }}-info
          include-hidden-files: true
          path: ".SIZE_CMP_INFO"
          retention-days: 1
