Skip to content

Commit

Permalink
scan_deps: use .Rproj files
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Dec 15, 2024
1 parent 956effc commit 6424fad
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 16 deletions.
47 changes: 31 additions & 16 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,10 @@

scan_deps <- function(path = ".") {
path <- tryCatch(find_project_root(path), error = function(...) path)
paths <- dir(path, pattern = scan_deps_pattern(), recursive = TRUE)
if (file.exists(file.path(path, "DESCRIPTION"))) {
paths <- c(paths, "DESCRIPTION")
}
if (file.exists(file.path(path, "NAMESPACE"))) {
paths <- c(paths, "NAMESPACE")
}
if (file.exists(file.path(path, "_pkgdown.yml"))) {
paths <- c(paths, "_pkgdown.yml")
}
if (file.exists(file.path(path, "renv.lock"))) {
paths <- c(paths, "renv.lock")
}
if (file.exists(file.path(path, "rsconnect"))) {
paths <- c(paths, "rsconnect")
}
paths <- c(
dir(path, pattern = scan_deps_pattern(), recursive = TRUE),
dir(path, pattern = scan_deps_pattern_root(), recursive = FALSE)
)
full_paths <- normalizePath(file.path(path, paths))
deps_list <- lapply(full_paths, scan_path_deps)
deps <- do.call("rbind", c(list(scan_deps_df()), deps_list))
Expand All @@ -118,13 +106,26 @@ scan_deps_pattern <- function() {
"[.]rmarkdown",
"[.]Rmarkdown",
"[.]qmd$",
"[.]Rproj$",
"^_bookdown[.]yml$",
"^_quarto[.]yml$",
NULL
)
paste0("(", paste0(collapse = "|", ptrns), ")")
}

scan_deps_pattern_root <- function() {
ptrns <- c(
"^DESCRIPTION$",
"^NAMESPACE$",
"^_pkgdown.yml$",
"^renv[.]lock$",
"^rsconnect$",
NULL
)
paste0("(", paste0(collapse = "|", ptrns), ")")
}

scan_deps_file_type_use_basename <- function() {
# for these we don't use the extension but the basename
# to decide which parser to use
Expand Down Expand Up @@ -285,6 +286,7 @@ scan_path_deps_do <- function(code, path) {
"_quarto.yml" = scan_path_deps_do_quarto(code, path),
"renv.lock" = scan_path_deps_do_renv_lock(code, path),
"rsconnect" = scan_path_deps_do_rsconnect(code, path),
".rproj" = scan_path_deps_do_rproj(code, path),
stop("Cannot parse ", ext, " file for dependencies, internal error")
)
}
Expand Down Expand Up @@ -1040,3 +1042,16 @@ scan_path_deps_do_rsconnect <- function(code, path) {
}

# -------------------------------------------------------------------------

scan_path_deps_do_rproj <- function(code, path) {
con <- if (is.raw(code)) rawConnection(code) else textConnection(code)
on.exit(close(con), add = TRUE)
if ("yes" %in% tolower(read.dcf(con, fields = "PackageUseDevtools"))) {
scan_deps_df(
path = path,
package = c("devtools", "roxygen2"),
type = "dev",
code = "PackageUseDevtools: Yes"
)
}
}
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/scan-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,14 @@
3 renv.lock renv renv * prod <NA> 1 1 1
4 rsconnect rsconnect rsconnect * prod <NA> 1 1 1

# .Rproj file

Code
scan_deps(project)[]
Output
# A data frame: 2 x 9
path ref package version type code start_row start_column start_byte
<chr> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
1 pkgdepends.Rproj devtools devtools * prod PackageUseDevtools: Yes 1 1 1
2 pkgdepends.Rproj roxygen2 roxygen2 * prod PackageUseDevtools: Yes 1 1 1

20 changes: 20 additions & 0 deletions tests/testthat/fixtures/scan/project-2/pkgdepends.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: No
PackageInstallArgs: --no-multiarch --with-keep.source
2 changes: 2 additions & 0 deletions tests/testthat/fixtures/scan/project-3/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: testpackage
Version: 1.0.0
20 changes: 20 additions & 0 deletions tests/testthat/fixtures/scan/project-3/pkgdepends.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
8 changes: 8 additions & 0 deletions tests/testthat/test-scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,11 @@ test_that("scan_path_deps_do_{bookdown,pkgdown,quarto,renv_lock,rsconnect}", {
scan_deps(project)[]
})
})

test_that(".Rproj file", {
local_reproducible_output(width = 500)
project <- test_path("fixtures/scan/project-3")
expect_snapshot({
scan_deps(project)[]
})
})

0 comments on commit 6424fad

Please sign in to comment.