## Read more about GitHub actions the features of this GitHub Actions workflow
## at https://lcolladotor.github.io/biocthis/articles/biocthis.html#use_bioc_github_action
##
## For more details, check the biocthis developer notes vignette at
## https://lcolladotor.github.io/biocthis/articles/biocthis_dev_notes.html
##
## You can add this workflow to other packages using:
## > biocthis::use_bioc_github_action()
##
## Using GitHub Actions exposes you to many details about how R packages are
## compiled and installed in several operating system.s
### If you need help, please follow the steps listed at
## https://github.com/r-lib/actions#where-to-find-help
##
## If you found an issue specific to biocthis's GHA workflow, please report it
## with the information that will make it easier for others to help you.
## Thank you!

## Acronyms:
## * GHA: GitHub Action
## * OS: operating system

on:
  push:
  pull_request:

name: R-CMD-check-bioc

## These environment variables control whether to run GHA code later on that is
## specific to testthat, covr, and pkgdown.
##
## If you need to clear the cache of packages, update the number inside
## cache-version as discussed at https://github.com/r-lib/actions/issues/86.
## Note that you can always run a GHA test without the cache by using the word
## "/nocache" in the commit message.
env:
  has_testthat: 'true'
  run_covr: 'true'
  run_pkgdown: 'true'
  has_RUnit: 'false'
  has_BiocCheck: 'false'
  cache-version: 'cache-v1'
  # Avoid application of UCRT
  # https://developer.r-project.org/WindowsBuilds/winutf8/ucrt3/howto.html
  _R_INSTALL_TIME_PATCHES: '=no'

jobs:
  build-check:
    runs-on: ${{ matrix.config.os }}
    name: ${{ matrix.config.os }} (${{ matrix.config.r }})
    container: ${{ matrix.config.cont }}
    ## Environment variables unique to this job.

    strategy:
      fail-fast: false
      matrix:
        config:
#          - { os: ubuntu-latest, r: 'devel', bioc: '3.15', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
#          - { os: macOS-latest, r: 'devel', bioc: '3.15'}
          - { os: windows-latest, r: 'devel', bioc: '3.15'}
    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      RSPM: ${{ matrix.config.rspm }}
      NOT_CRAN: true
      TZ: UTC
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:

      ## Set the R library to the directory matching the
      ## R packages cache step further below when running on Docker (Linux).
      - name: Set R Library home on Linux
        if: runner.os == 'Linux'
        run: |
          mkdir /__w/_temp/Library
          echo ".libPaths('/__w/_temp/Library')" > ~/.Rprofile

      ## Most of these steps are the same as the ones in
      ## https://github.com/r-lib/actions/blob/master/examples/check-standard.yaml
      ## If they update their steps, we will also need to update ours.
      - name: Checkout Repository
        uses: actions/checkout@v2

      ## R is already included in the Bioconductor docker images
      - name: Setup R from r-lib
        if: runner.os != 'Linux'
        uses: r-lib/actions/setup-r@master
        with:
          r-version: ${{ matrix.config.r }}

      ## pandoc is already included in the Bioconductor docker images
      - name: Setup pandoc from r-lib
        if: runner.os != 'Linux'
        uses: r-lib/actions/setup-pandoc@master

      - name: Query dependencies
        run: |
          install.packages('remotes')
          saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
        shell: Rscript {0}

      - name: Cache R packages
        if: "!contains(github.event.head_commit.message, '/nocache') && runner.os != 'Linux'"
        uses: actions/cache@v2
        with:
          path: ${{ env.R_LIBS_USER }}
          key: ${{ env.cache-version }}-${{ runner.os }}-biocversion-devel-r-devel-${{ hashFiles('.github/depends.Rds') }}
          restore-keys: ${{ env.cache-version }}-${{ runner.os }}-biocversion-devel-r-devel-

      - name: Cache R packages on Linux
        if: "!contains(github.event.head_commit.message, '/nocache') && runner.os == 'Linux' "
        uses: actions/cache@v2
        with:
          path: /home/runner/work/_temp/Library
          key: ${{ env.cache-version }}-${{ runner.os }}-biocversion-devel-r-devel-${{ hashFiles('.github/depends.Rds') }}
          restore-keys: ${{ env.cache-version }}-${{ runner.os }}-biocversion-devel-r-devel-

      - name: Install Linux system dependencies
        if: runner.os == 'Linux'
        run: |
          sysreqs=$(Rscript -e 'cat("apt-get update -y && apt-get install -y", paste(gsub("apt-get install -y ", "", remotes::system_requirements("ubuntu", "20.04")), collapse = " "))')
          echo $sysreqs
          sudo -s eval "$sysreqs"

      - name: Install macOS system dependencies
        if: matrix.config.os == 'macOS-latest'
        run: |
          ## Enable installing XML from source if needed
          brew install libxml2
          echo "XML_CONFIG=/usr/local/opt/libxml2/bin/xml2-config" >> $GITHUB_ENV

          ## Required to install magick as noted at
          ## https://github.com/r-lib/usethis/commit/f1f1e0d10c1ebc75fd4c18fa7e2de4551fd9978f#diff-9bfee71065492f63457918efcd912cf2
          brew install imagemagick@6

          ## For textshaping, required by ragg, and required by pkgdown
          brew install harfbuzz fribidi

          ## For installing usethis's dependency gert
          brew install libgit2

          ## required for ncdf4
          ## brew install netcdf ## Does not work as it is compiled with gcc
          ## Use pre-compiled libraries from https://mac.r-project.org/libs-4/
          curl -O https://mac.r-project.org/libs-4/netcdf-4.7.4-darwin.17-x86_64.tar.gz
          tar fvxzm netcdf-4.7.4-darwin.17-x86_64.tar.gz -C /
          rm netcdf-4.7.4-darwin.17-x86_64.tar.gz
          curl -O https://mac.r-project.org/libs-4/hdf5-1.12.0-darwin.17-x86_64.tar.gz
          tar fvxzm hdf5-1.12.0-darwin.17-x86_64.tar.gz -C /
          rm hdf5-1.12.0-darwin.17-x86_64.tar.gz
          curl -O https://mac.r-project.org/libs-4/szip-2.1.1-darwin.17-x86_64.tar.gz
          tar fvxzm szip-2.1.1-darwin.17-x86_64.tar.gz -C /
          rm szip-2.1.1-darwin.17-x86_64.tar.gz

      - name: Install Windows system dependencies
        if: runner.os == 'Windows'
        run: |
          ## Edit below if you have any Windows system dependencies
        shell: Rscript {0}

      - name: Install BiocManager
        run: |
          message(paste('****', Sys.time(), 'installing BiocManager ****'))
          remotes::install_cran("BiocManager")
        shell: Rscript {0}

      - name: Set BiocVersion
        run: |
          BiocManager::install(version = "${{ matrix.config.bioc }}", ask = FALSE)
        shell: Rscript {0}

      - name: Install dependencies pass 1
        run: |
          ## Try installing the package dependencies in steps. First the local
          ## dependencies, then any remaining dependencies to avoid the
          ## issues described at
          ## https://stat.ethz.ch/pipermail/bioc-devel/2020-April/016675.html
          ## https://github.com/r-lib/remotes/issues/296
          ## Ideally, all dependencies should get installed in the first pass.

          ## Pass #1 at installing dependencies
          message(paste('****', Sys.time(), 'pass number 1 at installing dependencies: local dependencies ****'))
          remotes::install_local(dependencies = TRUE, repos =
          BiocManager::repositories(), build_vignettes = FALSE, upgrade = TRUE)

          BiocManager::install(c("rmarkdown", "BiocStyle"))
        continue-on-error: true
        shell: Rscript {0}

      - name: Install dependencies pass 2
        run: |
          ## Pass #2 at installing dependencies
          message(paste('****', Sys.time(), 'pass number 2 at installing dependencies: any remaining dependencies ****'))
          remotes::install_local(dependencies = TRUE, repos = BiocManager::repositories(), build_vignettes = FALSE, upgrade = TRUE)

          ## For running the checks
          message(paste('****', Sys.time(), 'installing rcmdcheck and BiocCheck ****'))
          remotes::install_cran("rcmdcheck")
          BiocManager::install(c("BiocCheck", "DBI"))
        shell: Rscript {0}

      - name: Install BiocGenerics
        if:  env.has_RUnit == 'true'
        run: |
          ## Install BiocGenerics
          BiocManager::install("BiocGenerics")
        shell: Rscript {0}

      - name: Install covr
        if: github.ref == 'refs/heads/master' && env.run_covr == 'true' && runner.os == 'Linux'
        run: |
          remotes::install_cran("covr")
        shell: Rscript {0}

      - name: Install pkgdown
        if: github.ref == 'refs/heads/master' && env.run_pkgdown == 'true' && runner.os == 'Linux'
        run: |
          remotes::install_github("r-lib/pkgdown")
        shell: Rscript {0}

      - name: Session info
        run: |
          options(width = 100)
          pkgs <- installed.packages()[, "Package"]
          sessioninfo::session_info(pkgs, include_base = TRUE)
        shell: Rscript {0}

      - name: Run CMD check
        env:
          _R_CHECK_CRAN_INCOMING_: false
        run: |
          rcmdcheck::rcmdcheck(
              args = c("--no-build-vignettes", "--no-manual", "--timings"),
              build_args = c("--no-manual", "--no-resave-data"),
              error_on = "warning",
              check_dir = "check"
          )
        shell: Rscript {0}

      ## Might need an to add this to the if:  && runner.os == 'Linux'
      - name: Reveal testthat details
        if:  env.has_testthat == 'true'
        run: find . -name testthat.Rout -exec cat '{}' ';'

      - name: Run RUnit tests
        if:  env.has_RUnit == 'true'
        run: |
          BiocGenerics:::testPackage()
        shell: Rscript {0}

      - name: Run BiocCheck
        if:  env.has_BiocCheck == 'true'
        run: |
          BiocCheck::BiocCheck(
              dir('check', 'tar.gz$', full.names = TRUE),
              `quit-with-status` = TRUE,
              `no-check-R-ver` = TRUE,
              `no-check-bioc-help` = TRUE
          )
        shell: Rscript {0}

      - name: Test coverage
        if: github.ref == 'refs/heads/master' && env.run_covr == 'true' && runner.os == 'Linux'
        run: |
          covr::codecov()
        shell: Rscript {0}

      - name: Install package
        if: github.ref == 'refs/heads/master' && env.run_pkgdown == 'true' && runner.os == 'Linux'
        run: R CMD INSTALL .

      - name: Deploy package
        if: github.ref == 'refs/heads/master' && env.run_pkgdown == 'true' && runner.os == 'Linux'
        run: |
          git config --local user.email "actions@github.com"
          git config --local user.name "GitHub Actions"
          Rscript -e "pkgdown::deploy_to_branch(new_process = FALSE)"
        shell: bash {0}
        ## Note that you need to run pkgdown::deploy_to_branch(new_process = FALSE)
        ## at least one locally before this will work. This creates the gh-pages
        ## branch (erasing anything you haven't version controlled!) and
        ## makes the git history recognizable by pkgdown.

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