name: auto CI

on:
  push:
    branches: [ master, release]
  pull_request:
    branches: [ master, release ]
  # schedule:
  # - cron: "0 2 * * 1-5"

jobs:
  build:
    strategy:
      matrix:
        config:
          # - { os: windows-latest, suffix: 'zip' }
          - { os: ubuntu-latest, suffix: 'tar.gz', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
        r-version: ['4.1.1']
    runs-on: ${{ matrix.config.os }}
    name: ${{ matrix.config.os }} (${{ matrix.r-version }})
    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      RSPM: ${{ matrix.config.rspm }}
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes
      # R_LIBS_USER:  

    steps:
      - uses: actions/checkout@v3

      - name: Set up R ${{ matrix.r-version }}
        uses: r-lib/actions/setup-r@v2
        with:
          r-version: ${{ matrix.r-version }}
          use-public-rspm: true
          # http-user-agent: ${{ matrix.config.http-user-agent }}

      # - uses: r-lib/actions/setup-pandoc@v1

      - name: Set RENV_PATHS_ROOT
        shell: bash
        run: |
          echo "RENV_PATHS_ROOT=${{ runner.temp }}/renv" >> $GITHUB_ENV

      - name: Install and activate renv
        run: |
          install.packages("renv")
          renv::activate()
        shell: Rscript {0}

      - name: Get R and OS version
        id: get-version
        run: |
          cat("##[set-output name=os-version;]", sessionInfo()$running, "\n", sep = "")
          cat("##[set-output name=r-version;]", R.Version()$version.string, sep = "")
        shell: Rscript {0}
        
      - name: Restore Renv package cache
        uses: actions/cache@v3
        with:
          path: ${{ env.RENV_PATHS_ROOT }}
          key: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{ inputs.cache-version }}-${{ hashFiles('renv.lock') }}
          restore-keys: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{inputs.cache-version }}-
          
      - name: Install Linux build tools
        if: startsWith(runner.os, 'Linux')
        run: |
          sudo apt update
          sudo apt upgrade
          sudo apt install build-essential libcurl4-openssl-dev libbz2-dev liblzma-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev libxml2-dev libssl-dev libgtk-3-dev libzmq3-dev

      - name: Install dependencies
        run: |
          install.packages(c("devtools", "remotes"))
          saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
          writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
        shell: Rscript {0}

      # - name: Check
      #   run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")
      #   shell: Rscript {0}

      # - name: Build src pkg
      #   run: |
      #     devtools::build()
      #   shell: Rscript {0}

      - name: Build bin pkg
        run: |
          devtools::build(binary = TRUE)
        shell: Rscript {0}

      - name: List all file
        run: |
          cd ..
          tree
          
      # - name: Upload Release Asset
      #   uses: svenstaro/upload-release-action@v2
      #   with:
      #     repo_token: ${{ secrets.GITHUB_TOKEN }}
      #     file: '../*.${{ matrix.config.suffix }}'
      #     file_glob: true
      #     tag: bioc_${{ env.bioc_ver}}
      #     overwrite: true
      #     body: "This is my release text"

      # - name: Release
      #   uses: ncipollo/release-action@v1
      #   with:
      #     tag: ${{ env.APP_VERSION }}
      #     name: ${{ env.APP_VERSION }}
      #     token: ${{ secrets.GITHUB_TOKEN }}
      #     artifacts: '**.*.tar.gz,**.*.zip'