From 6e6ba9aaaa6dd246338d4340bb2ef94856fc705c Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 24 Jun 2026 07:03:17 -0500 Subject: [PATCH 1/2] Prune stale tinyrox-generated Rd files during document() (#22) After regenerating, document() removes man/*.Rd pages it no longer produces (renamed or deleted topics), but only files tinyrox owns: line 1 must be the marker, now factored into a shared RD_MARKER constant so the generator and the ownership check cannot drift. Hand-written Rd, and Rd from other tools, are never touched. New prune_rd argument (default TRUE) gates it; prune_rd = FALSE keeps the old behaviour. The returned list gains a pruned element. --- R/document.R | 19 ++++++++-- R/rd.R | 72 +++++++++++++++++++++++++++++++++----- inst/tinytest/test_prune.R | 47 +++++++++++++++++++++++++ man/RD_MARKER.Rd | 17 +++++++++ man/document.Rd | 8 ++++- man/prune_stale_rd.Rd | 24 +++++++++++++ man/tinyrox_owns_rd.Rd | 19 ++++++++++ 7 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 inst/tinytest/test_prune.R create mode 100644 man/RD_MARKER.Rd create mode 100644 man/prune_stale_rd.Rd create mode 100644 man/tinyrox_owns_rd.Rd diff --git a/R/document.R b/R/document.R index 6c1882f..3e9930d 100644 --- a/R/document.R +++ b/R/document.R @@ -13,8 +13,13 @@ #' @param cran_check Run CRAN compliance checks (code issues and missing #' examples) and undocumented-parameter warnings. Default TRUE. #' @param silent Operate less verbose without messages. Default FALSE. +#' @param prune_rd Remove stale Rd files for topics no longer documented +#' (e.g. after a rename or deletion)? Only files tinyrox generated are +#' removed; hand-written Rd is never touched. Default TRUE. Set FALSE to +#' leave existing Rd in place. #' @return Invisibly returns a list with: #' - rd_files: character vector of generated Rd file paths +#' - pruned: character vector of stale Rd file paths removed #' - namespace: path to NAMESPACE file (or NULL if mode="none") #' #' @export @@ -42,7 +47,7 @@ #' } document <- function(path = ".", namespace = c("overwrite", "append", "none"), - cran_check = TRUE, silent = FALSE) { + cran_check = TRUE, silent = FALSE, prune_rd = TRUE) { namespace <- match.arg(namespace) # Validate path @@ -66,7 +71,8 @@ document <- function(path = ".", if (!silent) { message("No documentation blocks found.") } - return(invisible(list(rd_files = character(), namespace = NULL))) + return(invisible(list(rd_files = character(), pruned = character(), + namespace = NULL))) } if (!silent) { @@ -82,6 +88,13 @@ document <- function(path = ".", message("Generated ", length(rd_files), " Rd file(s).") } + # Prune stale Rd left behind by renamed or deleted topics (tinyrox-owned + # files only; hand-written Rd is never removed). + pruned <- character() + if (prune_rd) { + pruned <- prune_stale_rd(path, rd_files, silent) + } + # Generate NAMESPACE ns_file <- NULL if (namespace != "none") { @@ -99,7 +112,7 @@ document <- function(path = ".", message("Leaving DESCRIPTION alone as one should.") } - invisible(list(rd_files = rd_files, namespace = ns_file)) + invisible(list(rd_files = rd_files, pruned = pruned, namespace = ns_file)) } #' Clean Generated Files diff --git a/R/rd.R b/R/rd.R index 78cd7f3..77ad9c2 100644 --- a/R/rd.R +++ b/R/rd.R @@ -1,3 +1,11 @@ +#' Header Marker for tinyrox-Generated Rd Files +#' +#' The first line of every Rd file tinyrox writes. It both warns against hand +#' editing and lets tinyrox recognise files it owns (see tinyrox_owns_rd()), +#' so document() can prune only its own stale pages. +#' @keywords internal +RD_MARKER <- "% tinyrox says don't edit this manually, but it can't stop you!" + #' Render User-Defined @section Blocks to Rd #' #' Emits one Rd section macro per parsed @section tag. Content is @@ -31,8 +39,7 @@ generate_rd <- function(tags, formals = NULL, source_file = NULL, lines <- character() # Header comment - distinctively tinyrox - lines <- c(lines, - "% tinyrox says don't edit this manually, but it can't stop you!") + lines <- c(lines, RD_MARKER) # Required sections lines <- c(lines, paste0("\\name{", escape_rd(tags$name), "}")) @@ -163,8 +170,7 @@ generate_data_rd <- function(tags, source_file = NULL, format_string = NULL) { lines <- character() # Header comment - distinctively tinyrox - lines <- c(lines, - "% tinyrox says don't edit this manually, but it can't stop you!") + lines <- c(lines, RD_MARKER) # docType for data lines <- c(lines, "\\docType{data}") @@ -499,6 +505,58 @@ write_rd <- function(content, name, path = ".") { filepath } +#' Identify a tinyrox-Generated Rd File +#' +#' Returns TRUE only if the file's first line is exactly RD_MARKER. This is +#' deliberately conservative: hand-written Rd, or Rd written by other tools, +#' never matches, so prune_stale_rd() cannot delete them. +#' +#' @param file Path to an Rd file. +#' @return TRUE if tinyrox generated the file, FALSE otherwise. +#' @keywords internal +tinyrox_owns_rd <- function(file) { + con <- file(file, "r") + on.exit(close(con)) + line1 <- readLines(con, n = 1, warn = FALSE) + length(line1) == 1L && identical(line1, RD_MARKER) +} + +#' Prune Stale tinyrox-Generated Rd Files +#' +#' Removes man/*.Rd files that the current document() run did not +#' (re)generate, but only those tinyrox can prove it owns via +#' tinyrox_owns_rd(). Hand-written Rd, and any Rd lacking the marker, are +#' left untouched. +#' +#' @param path Package root directory. +#' @param generated Character vector of Rd file paths produced this run. +#' @param silent Suppress the removal message? Default FALSE. +#' @return Character vector of removed file paths (invisibly). +#' @keywords internal +prune_stale_rd <- function(path, generated, silent = FALSE) { + man_dir <- file.path(path, "man") + if (!dir.exists(man_dir)) { + return(invisible(character())) + } + existing <- list.files(man_dir, pattern = "\\.Rd$", full.names = TRUE) + # Compare by basename so path-spelling differences do not matter. + stale <- existing[!(basename(existing) %in% basename(generated))] + + removed <- character() + for (f in stale) { + if (tinyrox_owns_rd(f)) { + file.remove(f) + removed <- c(removed, f) + } + } + + if (length(removed) > 0 && !silent) { + message("Pruned ", length(removed), " stale Rd file(s): ", + paste(basename(removed), collapse = ", ")) + } + invisible(removed) +} + #' Generate Rd Content for Grouped Blocks (Multiple @rdname Entries) #' #' Merges multiple documentation blocks that share an @rdname topic @@ -517,8 +575,7 @@ generate_rd_grouped <- function(topic, entries, all_tags, lines <- character() # Header - lines <- c(lines, - "% tinyrox says don't edit this manually, but it can't stop you!") + lines <- c(lines, RD_MARKER) # Find primary block: whose tags$name matches the topic name primary_idx <- NULL @@ -890,8 +947,7 @@ generate_package_rd <- function(tags, pkg_name, source_file) { lines <- character() # Header - distinctively tinyrox - lines <- c(lines, - "% tinyrox says don't edit this manually, but it can't stop you!") + lines <- c(lines, RD_MARKER) # docType lines <- c(lines, "\\docType{package}") diff --git a/inst/tinytest/test_prune.R b/inst/tinytest/test_prune.R new file mode 100644 index 0000000..8383517 --- /dev/null +++ b/inst/tinytest/test_prune.R @@ -0,0 +1,47 @@ +# Tests for stale-Rd pruning during document() (issue #22) + +pkg <- file.path(tempdir(), "prunepkg") +if (dir.exists(pkg)) unlink(pkg, recursive = TRUE) +dir.create(file.path(pkg, "R"), recursive = TRUE) +writeLines(c("Package: prunepkg", "Title: Test", "Version: 0.1.0"), + file.path(pkg, "DESCRIPTION")) +writeLines(c("#' Foo", "#' @param x A number.", "#' @return x.", "#' @export", + "foo <- function(x) x"), + file.path(pkg, "R", "fns.R")) + +# First run generates man/foo.Rd +tinyrox::document(pkg, namespace = "none", cran_check = FALSE, silent = TRUE) +expect_true(file.exists(file.path(pkg, "man", "foo.Rd"))) + +# Ownership check: tinyrox recognises its own header, not hand-written Rd +expect_true(tinyrox:::tinyrox_owns_rd(file.path(pkg, "man", "foo.Rd"))) +writeLines(c("\\name{handwritten}", "\\alias{handwritten}", "\\title{Hand}"), + file.path(pkg, "man", "handwritten.Rd")) +expect_false(tinyrox:::tinyrox_owns_rd(file.path(pkg, "man", "handwritten.Rd"))) + +# A second stale tinyrox-owned page (the copy carries the marker on line 1) +file.copy(file.path(pkg, "man", "foo.Rd"), file.path(pkg, "man", "old_topic.Rd")) + +# Rename foo -> baz, so foo.Rd and old_topic.Rd become stale +writeLines(c("#' Baz", "#' @param x A number.", "#' @return x.", "#' @export", + "baz <- function(x) x"), + file.path(pkg, "R", "fns.R")) + +# prune_rd = FALSE preserves the old behaviour: nothing removed +res_keep <- tinyrox::document(pkg, namespace = "none", cran_check = FALSE, + silent = TRUE, prune_rd = FALSE) +expect_equal(length(res_keep$pruned), 0L) +expect_true(file.exists(file.path(pkg, "man", "foo.Rd"))) # stale, kept +expect_true(file.exists(file.path(pkg, "man", "old_topic.Rd"))) # stale, kept +expect_true(file.exists(file.path(pkg, "man", "baz.Rd"))) # new + +# prune_rd = TRUE removes only stale tinyrox-owned files +res <- tinyrox::document(pkg, namespace = "none", cran_check = FALSE, + silent = TRUE, prune_rd = TRUE) +expect_true(file.exists(file.path(pkg, "man", "baz.Rd"))) # current -> kept +expect_true(file.exists(file.path(pkg, "man", "handwritten.Rd"))) # no marker -> kept +expect_false(file.exists(file.path(pkg, "man", "foo.Rd"))) # stale tinyrox -> gone +expect_false(file.exists(file.path(pkg, "man", "old_topic.Rd"))) # stale tinyrox -> gone +expect_equal(sort(basename(res$pruned)), c("foo.Rd", "old_topic.Rd")) + +unlink(pkg, recursive = TRUE) diff --git a/man/RD_MARKER.Rd b/man/RD_MARKER.Rd new file mode 100644 index 0000000..053a47c --- /dev/null +++ b/man/RD_MARKER.Rd @@ -0,0 +1,17 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\docType{data} +\name{RD_MARKER} +\alias{RD_MARKER} +\title{Header Marker for tinyrox-Generated Rd Files} +\format{ +An object of class \code{character} of length 1. +} +\usage{ +RD_MARKER +} +\description{ +The first line of every Rd file tinyrox writes. It both warns against hand +editing and lets tinyrox recognise files it owns (see tinyrox_owns_rd()), +so document() can prune only its own stale pages. +} +\keyword{internal} diff --git a/man/document.Rd b/man/document.Rd index db762df..9ee0e4c 100644 --- a/man/document.Rd +++ b/man/document.Rd @@ -4,7 +4,7 @@ \title{Generate Documentation for an R Package} \usage{ document(path = ".", namespace = c("overwrite", "append", "none"), - cran_check = TRUE, silent = FALSE) + cran_check = TRUE, silent = FALSE, prune_rd = TRUE) } \arguments{ \item{path}{Path to package root directory. Default is current directory.} @@ -20,10 +20,16 @@ document(path = ".", namespace = c("overwrite", "append", "none"), examples) and undocumented-parameter warnings. Default TRUE.} \item{silent}{Operate less verbose without messages. Default FALSE.} + +\item{prune_rd}{Remove stale Rd files for topics no longer documented +(e.g. after a rename or deletion)? Only files tinyrox generated are +removed; hand-written Rd is never touched. Default TRUE. Set FALSE to +leave existing Rd in place.} } \value{ Invisibly returns a list with: - rd_files: character vector of generated Rd file paths + - pruned: character vector of stale Rd file paths removed - namespace: path to NAMESPACE file (or NULL if mode="none") } \description{ diff --git a/man/prune_stale_rd.Rd b/man/prune_stale_rd.Rd new file mode 100644 index 0000000..813ac55 --- /dev/null +++ b/man/prune_stale_rd.Rd @@ -0,0 +1,24 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{prune_stale_rd} +\alias{prune_stale_rd} +\title{Prune Stale tinyrox-Generated Rd Files} +\usage{ +prune_stale_rd(path, generated, silent = FALSE) +} +\arguments{ +\item{path}{Package root directory.} + +\item{generated}{Character vector of Rd file paths produced this run.} + +\item{silent}{Suppress the removal message? Default FALSE.} +} +\value{ +Character vector of removed file paths (invisibly). +} +\description{ +Removes man/*.Rd files that the current document() run did not +(re)generate, but only those tinyrox can prove it owns via +tinyrox_owns_rd(). Hand-written Rd, and any Rd lacking the marker, are +left untouched. +} +\keyword{internal} diff --git a/man/tinyrox_owns_rd.Rd b/man/tinyrox_owns_rd.Rd new file mode 100644 index 0000000..8fb1583 --- /dev/null +++ b/man/tinyrox_owns_rd.Rd @@ -0,0 +1,19 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{tinyrox_owns_rd} +\alias{tinyrox_owns_rd} +\title{Identify a tinyrox-Generated Rd File} +\usage{ +tinyrox_owns_rd(file) +} +\arguments{ +\item{file}{Path to an Rd file.} +} +\value{ +TRUE if tinyrox generated the file, FALSE otherwise. +} +\description{ +Returns TRUE only if the file's first line is exactly RD_MARKER. This is +deliberately conservative: hand-written Rd, or Rd written by other tools, +never matches, so prune_stale_rd() cannot delete them. +} +\keyword{internal} From 0abf1c5df4d1a2ada5f116136e985c0e531ba881 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 24 Jun 2026 07:03:17 -0500 Subject: [PATCH 2/2] Bump version to 0.3.3.7 --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 73a77a2..f851d83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tinyrox Title: Minimal R Documentation Generator -Version: 0.3.3.6 +Version: 0.3.3.7 Authors@R: person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), comment = c(ORCID = "0009-0005-4248-604X")) diff --git a/NEWS.md b/NEWS.md index c9a996a..3168129 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# tinyrox 0.3.3.7 + +* `document()` now prunes stale Rd files for topics that were renamed or removed (#22). After regenerating, it deletes `man/*.Rd` pages the current run did not produce, but only files tinyrox owns (first line is the tinyrox marker); hand-written Rd and Rd from other tools are never touched. Pass `prune_rd = FALSE` for the previous keep-everything behaviour. The returned list gains a `pruned` element. + # tinyrox 0.3.3.6 * Accept `@returns` as a plural alias of `@return` (#24). roxygen2 supports both spellings, so an unlisted `@returns` no longer aborts `document()`.