name: Artifacts
on:
  push:
    branches:
      - master
      - "3.4"
  pull_request:
    branches:
      - master
      - "3.4"
  release:
    types:
      - created

jobs:
  artifact-linux:
    name: Artifact on Linux
    runs-on: ubuntu-18.04
    container:
      # Older Ubuntu for older glibc
      image: phadej/ghc:8.6.5-xenial
    steps:
      - name: Set PATH
        run: |
          echo "$HOME/.cabal/bin" >> $GITHUB_PATH
      - name: Install newer Python3
        run: |
          add-apt-repository ppa:deadsnakes/ppa
          apt-get update
          apt-get install -y python3.8
      - name: Update Hackage index
        run: cabal v2-update
      - uses: actions/checkout@v2
      - name: Package project
        run: python3.8 release.py
      - uses: actions/upload-artifact@v2
        with:
          name: cabal-linux
          path: _build/artifacts/*

  artifact-macos:
    name: Artifact on macOS
    runs-on: macos-latest
    steps:
      - name: Install GHC
        run: |
          cd $(mktemp -d)
          curl -sLO "https://downloads.haskell.org/~ghc/8.6.5/ghc-8.6.5-x86_64-apple-darwin.tar.xz"
          tar -xJf ghc-*.tar.xz
          cd ghc-*
          ./configure --prefix=/opt/ghc/8.6.5
          sudo make install
      - name: Install Cabal
        run: |
          cd $(mktemp -d)
          curl -sLO https://downloads.haskell.org/~cabal/cabal-install-3.0.0.0/cabal-install-3.0.0.0-x86_64-apple-darwin17.7.0.tar.xz
          tar -xJf cabal-install-*.tar.xz
          sudo mkdir -p /opt/cabal/3.0/bin
          sudo cp cabal /opt/cabal/3.0/bin/cabal
          sudo chmod 755 /opt/cabal/3.0/bin/cabal
      - name: Set PATH
        run: |
          echo "/opt/ghc/8.6.5/bin" >> $GITHUB_PATH
          echo "/opt/cabal/3.0/bin" >> $GITHUB_PATH
          echo "$HOME/.cabal/bin" >> $GITHUB_PATH
      - name: Update Hackage index
        run: cabal v2-update
      - uses: actions/checkout@v2
      - name: Package project
        run: python3 release.py
      - uses: actions/upload-artifact@v2
        with:
          name: cabal-macos
          path: _build/artifacts/*

  artifact-windows:
    name: Artifact on Windows
    runs-on: windows-latest
    steps:
      - name: Install Cabal
        run: choco install -fy cabal --version 3.0.0.0
      - name: Install GHC
        run: |
          choco uninstall -y ghc
          choco install -y ghc --version 8.6.5
      - name: Set PATH
        run: |
          [Environment]::GetEnvironmentVariable("Path")
          Write-Output "C:\ProgramData\chocolatey\lib\cabal\tools\cabal-3.0.0.0" | Out-File $Env:GITHUB_PATH
          Write-Output "C:\ProgramData\chocolatey\lib\ghc\tools\ghc-8.6.5\bin" | Out-File $Env:GITHUB_PATH
      - name: Print versions
        run: |
          ghc --version
          cabal --version
          cabal user-config init -a "http-transport: plain-http" -a "store-dir: C:\SR" -f -v3
      - name: Update Hackage index
        run: cabal v2-update
      - uses: actions/checkout@v2
      - name: Package project
        run: python release.py
      - uses: actions/upload-artifact@v2
        with:
          name: cabal-macos
          path: _build/artifacts/*
