Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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()`.
Expand Down
19 changes: 16 additions & 3 deletions R/document.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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") {
Expand All @@ -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
Expand Down
72 changes: 64 additions & 8 deletions R/rd.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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), "}"))
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}")
Expand Down
47 changes: 47 additions & 0 deletions inst/tinytest/test_prune.R
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions man/RD_MARKER.Rd
Original file line number Diff line number Diff line change
@@ -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}
8 changes: 7 additions & 1 deletion man/document.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
Expand All @@ -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{
Expand Down
24 changes: 24 additions & 0 deletions man/prune_stale_rd.Rd
Original file line number Diff line number Diff line change
@@ -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}
19 changes: 19 additions & 0 deletions man/tinyrox_owns_rd.Rd
Original file line number Diff line number Diff line change
@@ -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}
Loading