name: build

on:
  push:
    branches:
      - master
      - stable
      - users
    tags:
      - "v*"
  pull_request:

jobs:
  prepare-release:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Clone project
        uses: actions/checkout@v2

      - name: Build changelog
        id: build_changelog
        uses: mikepenz/release-changelog-builder-action@v1
        with:
          configuration: .github/changelog_conf.json
          failOnError: true
          ignorePreReleases: true
          commitMode: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create release
        uses: softprops/action-gh-release@v1
        with:
          body: ${{ steps.build_changelog.outputs.changelog }}
          prerelease: true
          files: |
            LICENSE
          fail_on_unmatched_files: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build:
    name: build-${{ matrix.os }}
    if: always()
    needs: prepare-release
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            cache_path: ~/.cabal/store
            asset_name: simplex-chat-ubuntu-20_04-x86-64
          - os: ubuntu-18.04
            cache_path: ~/.cabal/store
            asset_name: simplex-chat-ubuntu-18_04-x86-64
          - os: macos-latest
            cache_path: ~/.cabal/store
            asset_name: simplex-chat-macos-x86-64
          - os: windows-latest
            cache_path: C:/cabal
            asset_name: simplex-chat-windows-x86-64
    steps:
      - name: Clone project
        uses: actions/checkout@v2

      - name: Setup Haskell
        uses: haskell/actions/setup@v1
        with:
          ghc-version: "8.10.7"
          cabal-version: "latest"

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ matrix.cache_path }}
            dist-newstyle
          key: ${{ matrix.os }}-${{ hashFiles('cabal.project', 'simplex-chat.cabal') }}

      # / Unix

      - name: Unix prepare cabal.project.local for Mac
        if: matrix.os == 'macos-latest'
        shell: bash
        run: |
          echo "ignore-project: False"                                      >> cabal.project.local
          echo "package direct-sqlcipher"                                   >> cabal.project.local
          echo "    extra-include-dirs: /usr/local/opt/openssl@1.1/include" >> cabal.project.local
          echo "    extra-lib-dirs: /usr/local/opt/openssl@1.1/lib"         >> cabal.project.local
          echo "    flags: +openssl"                                        >> cabal.project.local

      - name: Unix prepare cabal.project.local for Ubuntu
        if: matrix.os == 'ubuntu-20.04' || matrix.os == 'ubuntu-18.04'
        shell: bash
        run: |
          echo "ignore-project: False"    >> cabal.project.local
          echo "package direct-sqlcipher" >> cabal.project.local
          echo "    flags: +openssl"      >> cabal.project.local

      - name: Unix build
        id: unix_build
        if: matrix.os != 'windows-latest'
        shell: bash
        run: |
          cabal build --enable-tests
          echo "::set-output name=bin_path::$(cabal list-bin simplex-chat)"

      - name: Unix test
        if: matrix.os != 'windows-latest' && matrix.os != 'ubuntu-20.04'
        timeout-minutes: 20
        shell: bash
        run: cabal test --test-show-details=direct

      - name: Unix upload binary to release
        if: startsWith(github.ref, 'refs/tags/v') && matrix.os != 'windows-latest'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ steps.unix_build.outputs.bin_path }}
          asset_name: ${{ matrix.asset_name }}
          tag: ${{ github.ref }}

      # Unix /

      # / Windows

      # * In powershell multiline commands do not fail if individual commands fail - https://github.community/t/multiline-commands-on-windows-do-not-fail-if-individual-commands-fail/16753
      # * And GitHub Actions does not support parameterizing shell in a matrix job - https://github.community/t/using-matrix-to-specify-shell-is-it-possible/17065

      - name: Windows build
        id: windows_build
        if: matrix.os == 'windows-latest'
        shell: cmd
        run: |
          cabal build --enable-tests
          cabal list-bin simplex-chat > tmp_bin_path
          set /p bin_path= < tmp_bin_path
          echo ::set-output name=bin_path::%bin_path%

      - name: Windows upload binary to release
        if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'windows-latest'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ steps.windows_build.outputs.bin_path }}
          asset_name: ${{ matrix.asset_name }}
          tag: ${{ github.ref }}

      # Windows /
