Skip to content

Commit f5eb31e

Browse files
committed
add version arg to rdd_exract_code too
1 parent 6d02e7d commit f5eb31e

5 files changed

Lines changed: 55 additions & 34 deletions

File tree

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# rdocdump (development version)
1+
# rdocdump 0.1.1 (2025-08-21)
2+
3+
* Added `version` argument to `rdd_to_txt()` and `rdd_extract_code()` functions to specify the version of the package to download from CRAN for processing.
24

35
# rdocdump 0.1.0 (2025-06-15)
46

R/extract_code.R

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
#'
66
#' @param pkg A `character` string specifying the package. This can be:
77
#' \itemize{
8-
#' \item an installed package name,
9-
#' \item a full path to a package source directory,
10-
#' \item a full path to a package archive file (tar.gz), or
11-
#' \item a package name not installed (which will then be downloaded from CRAN).
8+
#' \item an installed package name,
9+
#' \item a full path to a package source directory,
10+
#' \item a full path to a package archive file (tar.gz), or
11+
#' \item a package name not installed (which will then be downloaded from CRAN).
1212
#' }
1313
#' @param file Optional. Save path for the output text file. If set, the function will return the path to the file instead of the combined text. Defaults to `NULL`.
1414
#' @param include_tests `logical`. If `TRUE`, for non-installed packages, the function will also include R source code from the `tests` directory. Defaults to `FALSE`.
1515
#' @param include_roxygen `logical`. If `TRUE`, roxygen2 documentation lines (lines starting with "#'") from R files will be included in the output. Defaults to `FALSE`.
16-
#' @param force_fetch `logical`. If `TRUE`, the package source will be fetched from CRAN even if the package is installed locally. Default is `FALSE`.
16+
#' @param force_fetch `logical`. If `TRUE`, the package source will be fetched from CRAN even if the package is installed locally. Default is `FALSE`, but when `version` is specified, it will be set to `TRUE`.
17+
#' @param version Optional. A `character` string specifying the package version to fetch from CRAN. If not provided, the latest version will be used.
1718
#' @param cache_path A `character` string specifying the directory to use as a cache. Defaults to the value of `getOption("rdocdump.cache_path")`.
1819
#'
1920
#' @inheritParams rdd_to_txt
@@ -33,26 +34,26 @@
3334
#' rdd_set_cache_path(paste0(tempdir(), "/rdocdump_cache"))
3435
#'
3536
#' local({
36-
#' code_with_roxygen <- rdd_extract_code(
37-
#' "ini",
38-
#' include_roxygen = TRUE,
39-
#' force_fetch = TRUE,
40-
#' repos = c("CRAN" = "https://cran.r-project.org")
37+
#' code_with_roxygen <- rdd_extract_code(
38+
#' "ini",
39+
#' include_roxygen = TRUE,
40+
#' force_fetch = TRUE,
41+
#' repos = c("CRAN" = "https://cran.r-project.org")
4142
#' )
42-
#' cat(substr(code_with_roxygen, 1, 1000))
43+
#' cat(substr(code_with_roxygen, 1, 1000))
4344
#'})
4445
#'
4546
#' # Extract R source code from a package source directory,
4647
#' # including test files but excluding roxygen2 docs.
4748
#' local({
48-
#' code_with_tests <- rdd_extract_code(
49-
#' "ini",
50-
#' include_roxygen = TRUE,
51-
#' include_tests = TRUE,
52-
#' force_fetch = TRUE,
53-
#' repos = c("CRAN" = "https://cran.r-project.org")
49+
#' code_with_tests <- rdd_extract_code(
50+
#' "ini",
51+
#' include_roxygen = TRUE,
52+
#' include_tests = TRUE,
53+
#' force_fetch = TRUE,
54+
#' repos = c("CRAN" = "https://cran.r-project.org")
5455
#' )
55-
#' cat(substr(code_with_tests, 1, 1000))
56+
#' cat(substr(code_with_tests, 1, 1000))
5657
#'})
5758
#' # clean cache directory
5859
#' unlink(getOption("rdocdump.cache_path"), recursive = TRUE, force = TRUE)
@@ -64,14 +65,17 @@ rdd_extract_code <- function(
6465
include_tests = FALSE,
6566
include_roxygen = FALSE,
6667
force_fetch = FALSE,
68+
version = NULL,
6769
cache_path = getOption("rdocdump.cache_path"),
6870
keep_files = "none",
6971
repos = getOption("rdocdump.repos", getOption("repos"))
7072
) {
73+
# Pass version to resolve_pkg_path and force fetching if a version is specified.
7174
pkg_info <- resolve_pkg_path(
7275
pkg,
7376
cache_path,
74-
force_fetch = force_fetch,
77+
force_fetch = force_fetch || !is.null(version),
78+
version = version,
7579
repos = repos
7680
)
7781

@@ -97,6 +101,7 @@ rdd_extract_code <- function(
97101
writeLines(combined_code, con = file)
98102
return(file)
99103
}
104+
100105
combined_code
101106
}
102107

@@ -108,7 +113,9 @@ rdd_extract_code <- function(
108113
extract_code_installed <- function(pkg_name) {
109114
# Load the namespace of the installed package.
110115
ns <- asNamespace(pkg_name)
116+
111117
obj_names <- ls(ns, all.names = TRUE)
118+
112119
code_strings <- lapply(obj_names, function(nm) {
113120
obj <- get(nm, envir = ns)
114121
if (is.function(obj)) {
@@ -120,8 +127,10 @@ extract_code_installed <- function(pkg_name) {
120127
NULL
121128
}
122129
})
130+
123131
# Combine all function source codes.
124132
combined_code <- paste(unlist(code_strings), collapse = "\n\n")
133+
125134
return(combined_code)
126135
}
127136

@@ -138,17 +147,20 @@ extract_code_source <- function(
138147
include_roxygen = FALSE
139148
) {
140149
code_text <- ""
150+
141151
# Read all .R files in the R directory.
142152
r_dir <- file.path(pkg_path, "R")
143153
if (dir.exists(r_dir)) {
144154
r_files <- list.files(r_dir, pattern = "\\.[rR]$", full.names = TRUE)
145155
for (rf in r_files) {
146156
header <- paste0(strrep("-", 80), "\nFile: ", basename(rf), "\n")
147157
file_content <- readLines(rf, warn = FALSE)
158+
148159
# Exclude roxygen2 documentation if not requested.
149160
if (!include_roxygen) {
150161
file_content <- file_content[!grepl("^#'", file_content)]
151162
}
163+
152164
code_text <- paste(
153165
code_text,
154166
header,
@@ -170,6 +182,7 @@ extract_code_source <- function(
170182
pattern = "\\.[rR]$",
171183
full.names = TRUE
172184
)
185+
173186
for (tf in test_files) {
174187
header <- paste0(strrep("-", 80), "\nTest File: ", basename(tf), "\n")
175188
file_content <- readLines(tf, warn = FALSE)

R/to_txt.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ rdd_to_txt <- function(
131131
include_tests = FALSE,
132132
include_roxygen = FALSE,
133133
force_fetch = force_fetch || !is.null(version),
134+
version = version,
134135
cache_path = cache_path,
135136
keep_files = "both" # make sure the files are not deleted prematurely, as rdd_to_txt will take care of that later
136137
)

man/rdd_extract_code.Rd

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-to_txt.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ test_that("rdd_to_txt sets force_fetch=TRUE internally whenever 'version' is pro
341341
include_tests,
342342
include_roxygen,
343343
force_fetch,
344+
version,
344345
cache_path,
345346
keep_files
346347
) {
@@ -405,6 +406,7 @@ test_that("rdd_to_txt passes through force_fetch unchanged when 'version' is NUL
405406
include_roxygen,
406407
force_fetch,
407408
cache_path,
409+
version,
408410
keep_files
409411
) {
410412
calls$rdd_force_fetch <- force_fetch

0 commit comments

Comments
 (0)