name: Build

on:
  push:
    paths:
      - ".github/workflows/build.yml"
      - "Cargo.toml"
      - "src/**/*.rs"
  workflow_dispatch:

env:
  VERSION: 0.5.0

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: windows-11-arm
            target: aarch64-pc-windows-msvc
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: macos-15-intel
            target: x86_64-apple-darwin

    steps:
      - uses: actions/checkout@v6
      - name: Update Version
        shell: pwsh
        run: |
          $version = (Get-Content -Path Cargo.toml -Raw) -replace 'version = "0.1.0"', 'version = "${{ env.VERSION }}"'
          $version | Set-Content -Path Cargo.toml
      - name: Install Rust
        uses: christianhelle/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Rust cache
        uses: christianhelle/rust-cache@v2
      - name: Build
        run: cargo build --release --verbose
      - name: Run tests
        run: cargo test --verbose
      - name: Verify publish (dry-run)
        run: cargo publish --dry-run --allow-dirty
      - name: Build
        run: cargo build --release
      - name: Zip artifacts
        shell: pwsh
        run: |
          mv target/release/azdocli* ./
          Compress-Archive azdocli*, README.md, LICENSE ${{ matrix.target }}.zip
      - name: Upload artifacts
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.target }}
          path: ${{ matrix.target }}.zip
