
on:
  push:
    branches: master
  pull_request:
    branches: master
name: check

env:
  GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
  R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
  WARNINGS_ARE_ERRORS: 1
  _R_CHECK_CRAN_INCOMING_REMOTE_: false
  _R_CHECK_TESTS_NLINES_: 0  # disable limiting of output lines
  _R_CHECK_CRAN_INCOMING_: false
  _R_CHECK_FORCE_SUGGESTS_: false

jobs:
  check:
    defaults:
      run:
        shell: bash
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false  # continue runs even when a parallel run failed
      matrix:
        # run on ubuntu, macos, windows. 20.04 is more recent than ubuntu-latest, currently.
        os: [macos-latest, windows-latest, ubuntu-latest]
        action: [check]
        r: [release]
        include:
          # additionally run r-devel, but only on ubuntu, and an old R version on an old ubuntu
          - os: ubuntu-latest
            action: check
            r: devel
          - os: ubuntu-20.04
            action: check
            r: '4.1'
          - os: ubuntu-latest
            action: coverage
            r: release
          - os: ubuntu-latest
            action: notensorflow
            r: release

    steps:
      - uses: actions/checkout@v4
      - uses: r-lib/actions/setup-r@v2
        id: install-r
        with:
          r-version: ${{ matrix.r }}
      - if: runner.os == 'Linux'
        name: apt-get update
        run: sudo apt-get update
      - name: Install curl for Ubuntu  
        # necessary for pkgdown & covr, so we do that on ubuntu-latest / R release only
        if: ${{ matrix.r == 'release' && matrix.os == 'ubuntu-latest' }}
        run: sudo apt-get install libcurl4-openssl-dev libharfbuzz-dev libfribidi-dev
      - uses: r-lib/actions/setup-pandoc@v2
      - uses: r-lib/actions/setup-tinytex@v2-branch
      - name: Create Cache Key
        shell: Rscript {0}
        run: |
          if (!require("remotes", quietly = TRUE)) install.packages("remotes")
          writeLines(capture.output(print(remotes::dev_package_deps())), ".github/deps.txt")
      - uses: actions/cache@v4
        with:
          path: ${{ env.R_LIBS_USER }}
          key: ${{ matrix.os }}-${{ steps.install-r.outputs.installed-r-version }}-${{ hashFiles('.github/deps.txt') }}
      - shell: Rscript {0}
        name: install necessary build env packages
        run: for (req in c("remotes", "roxygen2", "covr", "pkgdown")) if (!require(req, quietly = TRUE)) install.packages(req)
      - if: runner.os == 'Linux'
        name: install system requirements
        run: |
          while read -r depinst ; do
            echo "> $depinst"
            eval sudo $depinst
          done < <( Rscript -e "writeLines(remotes::system_requirements('ubuntu', '$( . /etc/os-release ; echo $VERSION_ID )'))")
      - shell: Rscript {0}
        name: install package dependencies
        run: | 
          remotes::install_deps(dependencies = TRUE)
      - name: Install Miniconda
        shell: Rscript {0}
        run: |
          reticulate::install_miniconda()
      - name: Mac OS Miniconda Setup
        run: |
          echo "options(reticulate.conda_binary = reticulate:::miniconda_conda())" >> .Rprofile
      - if: matrix.action != 'notensorflow'
        name: Install TF
        shell: Rscript {0}
        run: |
          reticulate::conda_create('r-reticulate', packages = c('python==3.10'))
          keras::install_keras(envname = 'r-reticulate')
      - name: Session Info
        shell: Rscript {0}
        run: |
          options(width = 200)
          installed.packages()[, c("Package", "Version", "Built")]
          sessionInfo()
          tensorflow::tf_config()
          tensorflow::tf_version()
          reticulate::py_config()
      - name: Document
        shell: Rscript {0}
        run: |
          roxygen2::roxygenise()
          system("attic/clean_man.sh")
      - if: runner.os == 'windows'
        name: Install tinytex packages
        shell: Rscript {0}
        run: |
          library(tinytex)
          tlmgr_version()
          tlmgr_update()
          tlmgr_install('collection-latexrecommended')
          tlmgr_install('collection-fontsrecommended')
          tlmgr(c('list', '--only-installed'))
      - name: Build
        if: ${{ matrix.action != 'coverage' }}
        working-directory: ..
        run: 'R CMD build */'
      - name: Check
        if: ${{ matrix.action != 'coverage' }}
        working-directory: ..
        run: 'R CMD check --as-cran --run-donttest *.tar.gz'
      - name: Coverage
        if: ${{ matrix.action == 'coverage' }}
        shell: Rscript {0}
        run: covr::codecov(quiet = FALSE, type = "tests")
      - name: install
        if: ${{ matrix.action != 'coverage' && matrix.os == 'ubuntu-20.04' && matrix.r == 'release' }}
        working-directory: ..
        run: 'R CMD INSTALL *.tar.gz'
      - name: Build Docs
        if: ${{ matrix.action != 'coverage' && matrix.os == 'ubuntu-20.04' && matrix.r == 'release' }}
        shell: Rscript {0}
        run: |
          pkgdown::build_site()
          roxygen2::roxygenise()
          tools::buildVignettes(dir = ".")
      - name: Deploy Docs
        if: ${{ matrix.action != 'coverage' && matrix.os == 'ubuntu-20.04' && matrix.r == 'release' && github.ref == 'refs/heads/master' }}
        run: |
          git config --local user.name "github-actions[bot]"
          git config --local user.email "mb706@users.noreply.github.com"
          git checkout --orphan gh-pages
          git --work-tree=docs add --all
          git --work-tree=docs commit -m "gh-pages"
          git push origin HEAD:gh-pages --force
      - name: Install Log
        if: ${{ failure() }}
        working-directory: ..
        run: 'cat *.Rcheck/00install.out'
      - name: Check Log
        if: ${{ failure() }}
        working-directory: ..
        run: 'cat *.Rcheck/00check.log'
      - name: Latex Log
        if: ${{ failure() }}
        working-directory: ..
        run: 'cat *.Rcheck/Rdlatex.log'

