name: CI

on:
  push:
    branches:
      - '**'
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_suffix: ""
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            binary_suffix: ""
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_suffix: ".exe"
            vcpkg_triplet: x64-windows-static-md
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            binary_suffix: ".exe"
            vcpkg_triplet: arm64-windows-static-md

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          target: ${{ matrix.target }}

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}
        env:
          SQLX_OFFLINE: true

      - name: Rename binary (Unix)
        if: runner.os != 'Windows'
        run: mv target/${{ matrix.target }}/release/pixivdwn target/${{ matrix.target }}/release/pixivdwn-${{ matrix.target }}

      - name: Rename binary (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: mv "target/${{ matrix.target }}/release/pixivdwn.exe" "target/${{ matrix.target }}/release/pixivdwn-${{ matrix.target }}.exe"

      - name: Upload binary artifact
        uses: actions/upload-artifact@v7
        with:
          name: pixivdwn-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/pixivdwn-${{ matrix.target }}${{ matrix.binary_suffix }}
          archive: false

      - name: Upload to GitHub Release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: softprops/action-gh-release@c062e08bd532815e2082a85e87e3ef29c3e6d191
        with:
          files: target/${{ matrix.target }}/release/pixivdwn-${{ matrix.target }}${{ matrix.binary_suffix }}
