Skip to content

Commit

Permalink
configurable globals filename. closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Jul 30, 2023
1 parent 32dc0c2 commit 93f6bcd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ S3method(roclet_process,roclet_global)
S3method(roxy_tag_parse,roxy_tag_autoglobal)
S3method(roxy_tag_parse,roxy_tag_global)
export(global_roclet)
export(options_get_filename)
export(options_get_unique)
export(options_set_filename)
export(options_set_unique)
export(use_roxyglobals)
importFrom(roxygen2,roclet)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# roxyglobals (development version)

- (optionally) write unique globals (#6)
- configurable globals filename (#10)
- preserve existing roclets (#9)
- (optionally) write unique globals (#6)

# roxyglobals 0.2.2

Expand Down
8 changes: 5 additions & 3 deletions R/global_roclet.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ roclet_process.roclet_global <- function(x, blocks, env, base_path) {
#' @importFrom roxygen2 roclet_output
#' @export
roclet_output.roclet_global <- function(x, results, base_path, ...) {
stringi::stri_write_lines(results, globals_path(base_path))
stringi::stri_write_lines(results, globals_filename(base_path))
invisible(NULL)
}

#' @importFrom roxygen2 roclet_clean
#' @export
roclet_clean.roclet_global <- function(x, base_path) {
unlink(globals_path(base_path), force = TRUE)
unlink(globals_filename(base_path), force = TRUE)
}

#' @importFrom roxygen2 roxy_tag_parse
Expand Down Expand Up @@ -92,4 +92,6 @@ generated_by <- function() {
paste0("# Generated by ", utils::packageName(), ": do not edit by hand\n")
}

globals_path <- function(base_path) file.path(base_path, "R", "globals.R")
globals_filename <- function(base_path) {
file.path(base_path, "R", options_get_filename(base_path))
}
29 changes: 26 additions & 3 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NULL


#' @export
#' @return The option value
#' @describeIn options get unique
options_get_unique <- function(file = ".") {
text <- desc::desc_get_field(
Expand All @@ -21,12 +22,34 @@ options_get_unique <- function(file = ".") {
}

#' @export
#' @param unique Whether to emit unique globals
#' @param value The new option value
#' @describeIn options set unique
options_set_unique <- function(unique, file = ".") {
options_set_unique <- function(value, file = ".") {
desc::desc_set(
options_key("unique"),
isTRUE(unique),
isTRUE(value),
file = file
)
}

#' @export
#' @describeIn options get filename
options_get_filename <- function(file = ".") {
desc::desc_get_field(
options_key("filename"),
"globals.R",
file = file
)
}

#' @export
#' @describeIn options set filename
options_set_filename <- function(value, file = ".") {
stopifnot(is_r_file(value))

desc::desc_set(
options_key("filename"),
basename(value[1]),
file = file
)
}
Expand Down
3 changes: 3 additions & 0 deletions R/use_roxyglobals.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use_roxyglobals <- function() {
paste0(utils::packageName(), "::", substitute(global_roclet))
))

# use global_roclet
options_set_roxygen(options)

# ensure roxyglobals options
options_set_filename(options_get_filename())
options_set_unique(options_get_unique())
}
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ first <- function(x) x[[1]]
quote_str <- function(x, q = "\"") paste0(q, x, q)
paste_line <- function(...) paste0(c(...), collapse = "\n")
indent <- function(..., size = 2) paste0(strrep(" ", size), ...)

is_r_file <- function(filename) {
ext <- toupper(tools::file_ext(trimws(filename)))
ext == "R" %??% FALSE
}
24 changes: 22 additions & 2 deletions man/options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93f6bcd

Please sign in to comment.