on: ['push']

jobs:
  build:
    name: Build on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest]
        ghc: ["9.4.8"]

    steps:
    - run: echo "$HOME/.local/bin" >> $GITHUB_PATH

    - if: matrix.os == 'macOS-latest'
      run: 'brew install pkg-config'

    - run: 'echo version=${GITHUB_REF#refs/tags/} >>$GITHUB_OUTPUT'
      id: get_version

    - uses: actions/checkout@v4

    - uses: haskell-actions/setup@v2
      id: setup
      with:
        ghc-version: ${{ matrix.ghc }}

    - uses: actions/cache@v3
      with:
        path: ${{ steps.setup.outputs.cabal-store }}
        key: ${{ runner.os }}-${{ matrix.ghc }}-v9-${{ hashFiles('patat.cabal') }}
        restore-keys: |
          ${{ runner.os }}-${{ matrix.ghc }}-v9-

    - run: make build
      id: build

    - run: make test

    - if: startsWith(github.ref, 'refs/tags')
      run: make artifact
      env:
        PATAT_TAG: ${{ steps.get_version.outputs.version }}

    - uses: actions/upload-artifact@v4
      if: startsWith(github.ref, 'refs/tags')
      with:
        path: artifacts/*
        name: artifacts-${{ runner.os }}-${{ matrix.ghc }}

  release:
    name: Release
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags')

    steps:
    - uses: actions/download-artifact@v4
      with:
        pattern: artifacts-*

    - run: ls -R
    - run: 'sha256sum artifacts-*/patat-*'

    - uses: softprops/action-gh-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        files: 'artifacts-*/patat-*'
