name: CI

on:
  push:
  pull_request:

defaults:
  run:
    shell: pwsh

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2.4.0
      with:
        persist-credentials: false
    - uses: actions/setup-python@v2
      with:
        python-version: '3.10'
        architecture: 'x64'
    - name: Restore
      run: |
        python -m pip install -r src/requirements.txt
    - name: Build
      env:
        PYTHONUTF8: 1
      run: |
        ./build.ps1
    - name: Build documents
      env:
        PYTHONUTF8: 1
      run: |
        ./build.ps1 docs
    - name: Upload artifacts
      uses: actions/upload-artifact@v2
      with:
        name: dist
        path: ./dist
    - name: Upload docs artifacts
      uses: actions/upload-artifact@v2
      with:
        name: apidocs
        path: ./docs/gen/ref/_build
  integration-test:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        python: ["3.10"]
        experimental: [false]
    continue-on-error: ${{ matrix.experimental }}
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2.3.4
      with:
        persist-credentials: false
    - uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python }}
        architecture: 'x64'
    - name: Restore
      run: |
        python -m pip install -r src/requirements.txt
    - name: Download artifacts
      uses: actions/download-artifact@v2
      with:
        name: dist
        path: ./dist
    - name: Unit Test
      env:
        PYTHONUTF8: 1
      run: |
        ./build.ps1 unittest
    - name: Upload artifacts
      uses: actions/upload-artifact@v2
      with:
        name: test-cov-${{ matrix.os }}
        path: ./test/htmlcov
    - name: Integration Test
      env:
        PYTHONUTF8: 1
      run: |
        ./build.ps1 integrationtest
  deploy-docs:
    needs: integration-test
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2.3.4
      with:
        persist-credentials: false
    - name: Download cov artifacts
      uses: actions/download-artifact@v2
      with:
        name: test-cov-ubuntu-latest
        path: ./docs/cov
    - name: Remove gitignore
      run: |
        rm ./docs/cov/.gitignore
    - name: Download apidocs artifacts
      uses: actions/download-artifact@v2
      with:
        name: apidocs
        path: ./docs/api
    - name: Deploy GitHub Pages
      uses: JamesIves/github-pages-deploy-action@v4.2.3
      with:
        token: ${{ secrets.PA_TOKEN }}
        branch: gh-pages
        folder: ./docs