# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]
  workflow_dispatch:

name: R-CMD-check

jobs:
  R-CMD-check:
    runs-on: ${{ matrix.config.os }}
    defaults:
      run:
        shell: bash

    name: "${{ matrix.config.os }} (R: ${{ matrix.config.r }}, rust: ${{ matrix.config.rust }})"

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: 'macos-latest',     r: 'release', rust: 'stable'}
          - {os: 'windows-latest',   r: 'release', rust: 'stable'}
          - {os: 'ubuntu-latest',    r: 'release', rust: 'stable'}
          - {os: 'ubuntu-latest',    r: 'devel',   rust: 'stable', http-user-agent: 'release'}
          - {os: 'ubuntu-latest',    r: 'release', rust: 'nightly'}
          - {os: 'ubuntu-24.04-arm', r: 'release', rust: 'stable', rspm: 'false'}

    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@nightly
        if: matrix.config.rust == 'nightly'

      - uses: r-lib/actions/setup-r@v2
        with:
          r-version: ${{ matrix.config.r }}
          http-user-agent: ${{ matrix.config.http-user-agent }}
          use-public-rspm: ${{ matrix.config.rspm || 'true' }}

      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          extra-packages: |
            any::rcmdcheck
            github::yutannihilation/savvy-helper-R-package
          needs: check
          working-directory: R-package
  

      - name: Run cargo test
        run: |
          cargo test --manifest-path=./savvy-macro/Cargo.toml
          cargo test --manifest-path=./savvy-bindgen/Cargo.toml
          cargo test --manifest-path=./savvy-cli/Cargo.toml

          # run `savvy-cli test` to test savvy itself
          cargo r-test --features complex

          # run `savvy-cli test` on R-package to test `savvy-cli test`
          cargo r-test ./R-package/src/rust/
        env:
          PROFILE_OVERRIDE: dev

      - name: Tweak
        run: |
          cat - <<EOF > ./R-package/src/rust/Cargo.toml
          [package]
          name = "simple-savvy"
          version = "0.1.0"
          edition = "2021"

          [lib]
          crate-type = ["staticlib"]

          [dependencies]
          savvy = { path = "../savvy", features = ["complex", "altrep", "logger"] }
          savvy-ffi = { path = "../savvy/savvy-ffi" }

          [workspace.package]
          version = "0.1.0"
          edition = "2021"
          authors = ["Hiroaki Yutani"]
          license = "MIT"
          repository = "https://github.com/yutannihilation/savvy/"
          EOF

          mkdir ./R-package/src/savvy
          mv src/ Cargo.toml build.rs ./R-package/src/savvy/
          mv savvy-macro/ ./R-package/src/savvy/
          mv savvy-bindgen/ ./R-package/src/savvy/
          mv savvy-ffi/ ./R-package/src/savvy/

      - uses: r-lib/actions/check-r-package@v2
        with:
          args: 'c("--no-manual")'  # no --as-cran
          upload-snapshots: true
          working-directory: R-package
        env:
          MAKEFLAGS: -j2   # cf. https://github.com/yutannihilation/savvy/issues/355#issuecomment-2740005471
          PROFILE_OVERRIDE: dev
