name: CI
on:
  pull_request:
    branches:
      - main
      - master
  push:
    branches:
      - main
      - master
  release:
    types:
      - created
jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - { os: macos-11, ghc: 9.0.1, cabal: 3.6.2.0 }
          - { os: ubuntu-20.04, ghc: 9.0.1, cabal: 3.6.2.0 }
          - { os: windows-2019, ghc: 9.0.1, cabal: 3.6.2.0, ext: .exe }
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - run: mkdir artifact
      - run: mkdir artifact/${{ matrix.os }}
      - id: setup-haskell
        uses: haskell/actions/setup@v1
        with:
          ghc-version: ${{ matrix.ghc }}
          cabal-version: ${{ matrix.cabal }}
      - run: cabal configure --enable-tests --flags pedantic --jobs
      - run: cabal freeze
      - run: cat cabal.project.freeze
      - uses: actions/cache@v2
        with:
          path: ${{ steps.setup-haskell.outputs.cabal-store }}
          key: ${{ matrix.os }}-${{ matrix.ghc }}-${{ matrix.cabal }}-${{ hashFiles('cabal.project.freeze') }}
          restore-keys: |
            ${{ matrix.os }}-${{ matrix.ghc }}-${{ matrix.cabal }}-
            ${{ matrix.os }}-${{ matrix.ghc }}-
      - run: cabal build
      - run: cabal install --installdir artifact/${{ matrix.os }} --install-method copy
      - run: cabal test --test-show-details direct
      - run: cabal check
      - run: cabal sdist --output-dir artifact/${{ matrix.os }}
      - uses: svenstaro/upx-action@v2
        with:
          file: artifact/${{ matrix.os }}/brittany${{ matrix.ext }}
      - uses: actions/upload-artifact@v2
        with:
          path: artifact
          name: brittany-${{ github.sha }}

  release:
    needs: build
    if: github.event_name == 'release'
    runs-on: ubuntu-20.04
    steps:

      - uses: actions/checkout@v2

      - uses: actions/download-artifact@v2
        with:
          name: brittany-${{ github.sha }}
          path: artifact

      - uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_content_type: application/octet-stream
          asset_name: brittany-${{ github.event.release.tag_name }}-ubuntu
          asset_path: artifact/ubuntu-20.04/brittany
          upload_url: ${{ github.event.release.upload_url }}

      - uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_content_type: application/octet-stream
          asset_name: brittany-${{ github.event.release.tag_name }}-macos
          asset_path: artifact/macos-11/brittany
          upload_url: ${{ github.event.release.upload_url }}

      - uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_content_type: application/octet-stream
          asset_name: brittany-${{ github.event.release.tag_name }}-windows.exe
          asset_path: artifact/windows-2019/brittany.exe
          upload_url: ${{ github.event.release.upload_url }}

      - uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_content_type: application/gzip
          asset_name: brittany-${{ github.event.release.tag_name }}.tar.gz
          asset_path: artifact/ubuntu-20.04/brittany-${{ github.event.release.tag_name }}.tar.gz
          upload_url: ${{ github.event.release.upload_url }}

      - run: cabal upload --publish --username '${{ secrets.HACKAGE_USERNAME }}' --password '${{ secrets.HACKAGE_PASSWORD }}' artifact/ubuntu-20.04/brittany-${{ github.event.release.tag_name }}.tar.gz
