name: release

on:
  push:
    branches:
      - main
    tags:
      - "v*"
    paths-ignore:
      - "README.md"
      - "LICENSE"
  workflow_dispatch:

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: windows-latest
            target: x86_64-pc-windows-msvc
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - run: rustup update stable
      - run: rustup default stable
      - run: rustup target add ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
      - run: |
          if [[ ${{ matrix.os }} = "windows-latest" ]]; then
            EXT=".exe"
          else
            EXT=""
          fi
          echo "EXT: $EXT"
          echo "ext=$EXT" >> $GITHUB_OUTPUT
        id: check
        shell: bash
      - run: |
          SRC="target/${{ matrix.target }}/release/jas${{ steps.check.outputs.ext }}"
          echo "SRC: $SRC"
          DST="target/release/jas-${{ matrix.target }}${{ steps.check.outputs.ext }}"
          echo "DST: $DST"
          cargo build --release --target ${{ matrix.target }}
          mv -v $SRC $DST
          echo "dst=$DST" >> $GITHUB_OUTPUT
        id: release
        shell: bash
      - uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          fail_on_unmatched_files: true
          files: ${{ steps.release.outputs.dst }}
          body: |
            Notable changes since the last release are documented in the [CHANGELOG.md](https://github.com/rikhuijzer/jas/blob/main/CHANGELOG.md) file.