on:
  push:
    branches:
      - main
      - master
  pull_request:
    branches:
      - main
      - 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: ubuntu-latest, r: 'release'}
          - {os: macOS-latest, r: 'release'}
          - {os: windows-latest, r: 'release'}

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Setup R
        uses: r-lib/actions/setup-r@master
        with:
          r-version: ${{ matrix.config.r }}

      - name: Install pandoc
        uses: r-lib/actions/setup-pandoc@v1

      - name: Install tinyTeX
        uses: r-lib/actions/setup-tinytex@v1

      - name: Install system dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt install -y curl libcurl4-doc libcurl4-openssl-dev

      - name: Install package dependencies
        run: |
          install.packages(c("remotes", "rcmdcheck", "covr", "sessioninfo", "BiocManager"))
          BiocManager::install(c("BiocCheck", "BiocGenerics", "BiocStyle", "S4Vectors", "SummarizedExperiment", "limma", "biotmleData"), ask = FALSE)
          remotes::install_deps(dependencies = TRUE)
        shell: Rscript {0}

      - name: R CMD Check package
        run: |
          options(crayon.enabled = TRUE)
          rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")
        shell: Rscript {0}

      - name: BiocCheck package
        run: |
          options(crayon.enabled = TRUE)
          BiocCheck::BiocCheck('.', `no-check-bioc-help`=TRUE, `no-check-formatting`=TRUE)
        shell: Rscript {0}

      - name: Upload code coverage
        if: runner.os == 'Linux'
        run: |
          covr::codecov(line_exclusions = list("R/utils.R", "R/plots.R"))
        shell: Rscript {0}
