name: Create Release

on:
  push:
    tags:
      - 'v*'

jobs:
  draft_release:
    name: Create Release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}

    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            Changes in this Release
            - First Change
            - Second Change
          draft: true
          prerelease: false

  release-mac:
    name: Create Release for macOS
    needs: draft_release
    runs-on: ${{ matrix.os }}
    env:
      MACOSX_DEPLOYMENT_TARGET: 10.13
    strategy:
      matrix:
        os:
          - macOS-10.15
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - uses: haskell/actions/setup@v1.2
        with:
          ghc-version: 8.10.4
          cabal-version: 3.4.0.0

      - name: create ~/.local/bin
        run: mkdir -p "$HOME/.local/bin"
        shell: bash

      - name: Add ~/.local/bin to PATH
        run: echo "$HOME/.local/bin" >> $GITHUB_PATH
        shell: bash

      - name: Update cabal cache
        run: cabal update
        shell: bash

      - name: Install cabal dependencies
        run: cabal build --only-dependencies
        shell: bash

      - name: Build
        run: cabal build -w ghc-${GHC_VERSION} --constraint="zlib +bundled-c-zlib" --constraint="lzma +static" -ftui
        shell: bash

      - name: Install
        run: cp "$(cabal list-bin exe:ghcup)" ~/.local/bin/ghcup
        shell: bash

      - name: Strip
        run: strip ~/.local/bin/ghcup
        shell: bash

      - name: Run tests
        run: cabal test --constraint="zlib +bundled-c-zlib" --constraint="lzma +static" all
        shell: bash

      - name: Install git
        run: brew install git

      - name: set HOME
        run: echo "HOME=$HOME" >> $GITHUB_ENV
        shell: bash

      - name: Set ASSET_PATH
        run: echo "ASSET_PATH=$HOME/.local/bin/ghcup" >> $GITHUB_ENV
        shell: bash

      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.draft_release.outputs.upload_url }}
          asset_path: ${{ env.ASSET_PATH }}
          asset_name: ghcup-${{ matrix.os }}
          asset_content_type: application/octet-stream

      - if: always()
        uses: actions/upload-artifact@v2
        with:
          name: plan.json
          path: ./dist-newstyle/cache/plan.json

