name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: kaslc-aarch64-apple-darwin.tar.gz
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: kaslc-x86_64-apple-darwin.tar.gz
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: kaslc-x86_64-unknown-linux-gnu.tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: kaslc-x86_64-pc-windows-msvc.zip
    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout repository and submodules
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Add target
        run: rustup target add ${{ matrix.target }}

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Archive (Unix)
        if: matrix.os != 'windows-latest'
        run: tar -czf ${{ matrix.artifact }} -C target/${{ matrix.target }}/release kaslc

      - name: Archive (Windows)
        if: matrix.os == 'windows-latest'
        run: Compress-Archive -Path target/${{ matrix.target }}/release/kaslc.exe -DestinationPath ${{ matrix.artifact }}

      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}

  release:
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - run: |
          gh release create ${{ github.ref_name }} \
          kaslc-aarch64-apple-darwin.tar.gz \
          kaslc-x86_64-apple-darwin.tar.gz \
          kaslc-x86_64-unknown-linux-gnu.tar.gz \
          kaslc-x86_64-pc-windows-msvc.zip \
          --generate-notes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
