Skip to content

Commit

Permalink
make install-r-deps more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb committed Feb 8, 2025
1 parent 8cd2627 commit dae117f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
83 changes: 66 additions & 17 deletions .ci/install-r-deps.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
args <- commandArgs(trailingOnly = TRUE)
# install R dependencies, using only base R
#
# Supported arguments:
#
# --all Install all the 'Depends', 'Imports', 'LinkingTo', and 'Suggests' dependencies.
# (automatically implies --build --test).
#
# --build install the packages needed to build
#
# --exclude=<pkg1,pkg2,...> Comma-delimited list of packages to NOT install.
#
# --include=<pkg1,pkg2,...> Comma-delimited list of additional packages to install.
# These will always be installed, unless also used in "--exclude".
#
# --test install packages needed to run tests
#


# [description] Parse command line arguments into an R list.
# Returns a list where keys are arguments and values
# are either TRUE (for flags) or a vector of values passed via a
# comma-delimited list.
.parse_args <- function(args) {
out <- list(
"--all" = FALSE
, "--build" = FALSE
, "--exclude" = character(0L)
, "--include" = character(0L)
, "--test" = FALSE
)
for (arg in args) {
print(sprintf("arg: '%s'", arg))
parsed_arg <- unlist(strsplit(arg, "=", fixed = TRUE))
arg_name <- parsed_arg[[1L]]
if (!(arg_name %in% names(out))) {
stop(sprintf("Unrecognized argument: '%s'", arg_name))
}
if (length(parsed_arg) == 2L) {
# lists, like "--include=roxygen2,testthat"
values <- unlist(strsplit(parsed_arg[[2L]], ",", fixed = TRUE))
out[[arg_name]] <- values
} else {
# flags, like "--build"
out[[arg]] <- TRUE
}
}
return(out)
}

args <- .parse_args(
commandArgs(trailingOnly = TRUE)
)

# which dependencies to install
ALL_DEPS <- "--all" %in% args
BUILD_DEPS <- ALL_DEPS || ("--build" %in% args)
ROXYGEN_DEPS <- ALL_DEPS || ("--roxygen" %in% args)
TEST_DEPS <- ALL_DEPS || ("--test" %in% args)
ALL_DEPS <- isTRUE(args[["--all"]])
BUILD_DEPS <- ALL_DEPS || isTRUE(args[["--build"]])
TEST_DEPS <- ALL_DEPS || isTRUE(args[["--test"]])

# force downloading of binary packages on macOS
COMPILE_FROM_SOURCE <- "both"
Expand Down Expand Up @@ -40,13 +90,6 @@ if (isTRUE(BUILD_DEPS)) {
)
}

if (isTRUE(ROXYGEN_DEPS)) {
deps_to_install <- c(
deps_to_install
, "roxygen"
)
}

if (isTRUE(TEST_DEPS)) {
deps_to_install <- c(
deps_to_install
Expand All @@ -55,11 +98,17 @@ if (isTRUE(TEST_DEPS)) {
)
}

# in some builds, {Matrix} is pre-installed to pin to an old version,
# so we don't want to overwrite that
if (requireNamespace("Matrix")) {
deps_to_install <- setdiff(deps_to_install, "Matrix")
}
# add packages passed through '--include'
deps_to_install <- unique(c(
deps_to_install
, args[["--include"]]
))

# remove packages passed through '--exclude'
deps_to_install <- setdiff(
x = deps_to_install
, args[["--exclude"]]
)

msg <- sprintf(
"[install-r-deps] installing R packages: %s\n"
Expand Down
2 changes: 1 addition & 1 deletion .ci/test-r-package-windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Remove-Item "$env:RTOOLS_MINGW_BIN/cmake.exe" -Force -ErrorAction Ignore
Write-Output "Done installing CMake"

Write-Output "Installing dependencies"
Rscript.exe --vanilla ".ci/install-r-deps.R" --build --test ; Assert-Output $?
Rscript.exe --vanilla ".ci/install-r-deps.R" --build --include=processx --test ; Assert-Output $?

Write-Output "Building R-package"

Expand Down
2 changes: 1 addition & 1 deletion .ci/test-r-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Rscript --vanilla -e "install.packages('lattice', repos = '${CRAN_MIRROR}', lib
Rscript --vanilla -e "install.packages('https://cran.r-project.org/src/contrib/Archive/Matrix/Matrix_1.6-5.tar.gz', repos = NULL, lib = '${R_LIB_PATH}')"

# Manually install dependencies to avoid a CI-time dependency on devtools (for devtools::install_deps())
Rscript --vanilla ./.ci/install-r-deps.R --build --test || exit 1
Rscript --vanilla ./.ci/install-r-deps.R --build --test --exclude=Matrix || exit 1

cd "${BUILD_DIRECTORY}"
PKG_TARBALL="lightgbm_$(head -1 VERSION.txt).tar.gz"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/r_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ jobs:
- name: Install packages and run tests
shell: bash
run: |
Rscript ./.ci/install-r-deps.R --build --test
Rscript ./.ci/install-r-deps.R --build --test --exclude=testthat
sh build-cran-package.sh
# 'rchk' isn't run through 'R CMD check', use the approach documented at
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Install packages
shell: bash
run: |
Rscript ./.ci/install-r-deps.R --build --roxygen
Rscript ./.ci/install-r-deps.R --build --include=roxygen
sh build-cran-package.sh || exit 1
R CMD INSTALL --with-keep.source lightgbm_*.tar.gz || exit 1
- name: Test documentation
Expand Down
4 changes: 1 addition & 3 deletions .vsts-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,14 @@ jobs:
- script: |
git clean -d -f -x
displayName: 'Clean source directory'
# yamllint disable rule:line-length
- script: |
LGB_VER=$(head -n 1 VERSION.txt | sed "s/rc/-/g")
R_LIB_PATH=~/Rlib
export R_LIBS=${R_LIB_PATH}
mkdir -p ${R_LIB_PATH}
RDscript .ci/install-r-deps.R --build --test || exit 1
RDscript .ci/install-r-deps.R --build --include=RhpcBLASctl || exit 1
sh build-cran-package.sh --r-executable=RD || exit 1
mv lightgbm_${LGB_VER}.tar.gz $(Build.ArtifactStagingDirectory)/lightgbm-${LGB_VER}-r-cran.tar.gz
# yamllint enable rule:line-length
displayName: 'Build CRAN R-package'
- task: PublishBuildArtifacts@1
condition: succeeded()
Expand Down

0 comments on commit dae117f

Please sign in to comment.