name: "Actions to compute a matrix with all suggested packages"
outputs:
  matrix:
    description: "Generated matrix"
    value: ${{ steps.set-matrix.outputs.matrix }}

runs:
  using: "composite"
  steps:
    - id: set-matrix
      run: |
        get_deps <- function() {
          # Determine package dependencies
          if (!requireNamespace("desc", quietly = TRUE)) {
            install.packages("desc")
          }

          deps_df <- desc::desc_get_deps()
          deps_df_optional <- deps_df$package[deps_df$type %in% c("Suggests", "Enhances")]
          deps_df_hard <- deps_df$package[deps_df$type %in% c("Depends", "Imports", "LinkingTo")]

          packages <- sort(deps_df_optional)
          packages <- intersect(packages, rownames(available.packages()))

          # Too big to fail, or can't be avoided:
          off_limits <- c("testthat", "rmarkdown", "rcmdcheck", deps_df_hard)
          off_limits_dep <- unlist(tools::package_dependencies(off_limits, recursive = TRUE, which = "strong"))
          setdiff(packages, c(off_limits, off_limits_dep))
        }

        if (Sys.getenv("GITHUB_BASE_REF") != "") {
          print(Sys.getenv("GITHUB_BASE_REF"))
          has_diff <- (system("git diff ${{ github.event.pull_request.base.sha }}... | egrep '^[+][^+]' | grep -q ::") == 0)
          if (has_diff) {
            system("git diff ${{ github.event.pull_request.base.sha }}... | egrep '^[+][^+]' | grep -q ::")
            packages <- get_deps()
          } else {
            writeLines("No changes using :: found, not checking without suggested packages")
            packages <- character()
          }
        } else {
          packages <- get_deps()
        }

        if (length(packages) > 0) {
          json <- paste0(
            '{"package":[',
            paste0('"', packages, '"', collapse = ","),
            ']}'
          )
          writeLines(json)
          writeLines(paste0("matrix=", json), Sys.getenv("GITHUB_OUTPUT"))
        } else {
          writeLines("Package list empty!")
        }
      shell: Rscript {0}
