on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

name: R-CMD-check

jobs:
  R-CMD-check:
    runs-on: ${{ matrix.config.os }}

    name: ${{ matrix.config.os }} (${{ matrix.config.r }})

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: macOS-latest,   r: 'release'}
          - {os: windows-latest, r: 'release'}
          - {os: ubuntu-latest,   r: 'devel', http-user-agent: 'release'}
          - {os: ubuntu-latest,   r: 'release'}
          - {os: ubuntu-latest,   r: 'oldrel-1'}

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      # don't treat missing suggested packages as error:
      _R_CHECK_FORCE_SUGGESTS_: false
      RSPM: ${{ matrix.config.rspm }}
      GITHUB_PAT: ${{ secrets.GH_TOKEN }}

    steps:
      - uses: actions/checkout@v2

      - uses: r-lib/actions/setup-r@v2
        with:
          r-version: ${{ matrix.config.r }}

      - uses: r-lib/actions/setup-pandoc@v2

      - name: Install tinytex
        run: |
          install.packages('tinytex')
          tinytex::install_tinytex()
        shell: Rscript {0}
      
      - name: Install XQuartz on MacOS
        if: runner.os == 'macOS'
        run: brew install --cask xquartz

      - name: Install freetype on MacOS
        if: runner.os == 'macOS'
        run: |
          brew install freetype
          brew link --overwrite freetype
          
      - name: Install cairo on MacOS for xml2
        if: runner.os == 'macOS'
        run: |
          brew install cairo
          
      - name: Install XML2 on MacOS
        if: runner.os == 'macOS'
        run: |
          brew install libxml2
          brew link --force libxml2
          xml2-config --cflags
          
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          extra-packages: any::rcmdcheck
          needs: check

      - uses: r-lib/actions/check-r-package@v2
        with:
          upload-snapshots: true

      - name: Upload check results
        if: failure()
        uses: actions/upload-artifact@master
        with:
          name: ${{ runner.os }}-r${{ matrix.config.r }}-results
          path: check
