diff --git a/DESCRIPTION b/DESCRIPTION index baa3542..44108f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: fs, glue, httptest2, + memoise, nectar, purrr, rapid (>= 0.0.0.9003), diff --git a/NAMESPACE b/NAMESPACE index 9b0680f..1e0debf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,11 @@ # Generated by roxygen2: do not edit by hand export(generate_pkg) +export(read_api_abbr) export(read_api_definition) +export(read_api_title) export(read_config) +export(read_rapid_filename) export(use_beekeeper) if (getRversion() < "4.3.0") importFrom("S7", "@") importFrom(S7,class_any) diff --git a/R/aaa-shared_params.R b/R/aaa-shared_params.R index 7befacc..f4b5989 100644 --- a/R/aaa-shared_params.R +++ b/R/aaa-shared_params.R @@ -13,12 +13,11 @@ #' @param api_title (`character(1)`) The API title used in generated package #' files. #' @param base_path The root URL of the current project. -#' @param base_url (`character(1)`) The base URL used in generated test -#' helpers. +#' @param base_url (`character(1)`) The base URL used in generated test helpers. #' @param call (`environment`) The caller environment for error messages. #' @param config (`list`) Package-generation configuration data. -#' @param config_file (`character(1)` or `fs_path`) The path to a beekeeper yaml -#' config file. +#' @param config_filename (`character(1)` or `fs_path`) The path to a beekeeper +#' yaml config file (relative to the package root). #' @param data (`list`) Data passed to a template. #' @param dir (`character(1)`) The directory where a generated file should be #' written. @@ -51,8 +50,8 @@ #' operation identifier. #' @param pkg_dir (`character(1)` or `fs_path`) The directory containing package #' files. -#' @param rapid_file (`character(1)` or `fs_path`) The path to the R API -#' definition (rapid) file. +#' @param rapid_filename (`character(1)` or `fs_path`) The path to the R API +#' definition (rapid) file (relative to the package root). #' @param required (`logical`) Whether each parameter is required. #' @param security_arg_description (`character(1)`) The argument description. #' @param security_arg_name (`character(1)`) The argument name. diff --git a/R/generate_pkg.R b/R/generate_pkg.R index 3e50785..c95c37b 100644 --- a/R/generate_pkg.R +++ b/R/generate_pkg.R @@ -9,23 +9,21 @@ #' @returns (`character`, invisibly) Paths to files that were added or updated. #' @export generate_pkg <- function( - api_abbr = NULL, - api_definition = NULL, - api_title = NULL, - config_file = "_beekeeper.yml", + api_abbr = read_api_abbr(pkg_dir, config_filename), + api_definition = read_api_definition( + pkg_dir, + read_rapid_filename(pkg_dir, config_filename) + ), + api_title = read_api_title(pkg_dir, config_filename), + config_filename = "_beekeeper.yml", pkg_dir = "." ) { # TODO: Confirm that they use github & everything is committed. Error or warn # if not, letting them know that this can be destructive. Skip this check in # tests. .assert_is_pkg(pkg_dir) - if (purrr::some(list(api_abbr, api_definition, api_title), is.null)) { - config <- read_config(pkg_dir = pkg_dir, config_file = config_file) - } - api_abbr <- stbl::stabilize_character_scalar(api_abbr %||% config$api_abbr) - api_title <- stbl::stabilize_character_scalar(api_title %||% config$api_title) - api_definition <- api_definition %||% - read_api_definition(pkg_dir, config$rapid_file) + api_abbr <- stbl::stabilize_character_scalar(api_abbr) + api_title <- stbl::stabilize_character_scalar(api_title) config <- list(api_abbr = api_abbr, api_title = api_title) security_data <- .generate_security( api_abbr, diff --git a/R/read_config.R b/R/read_config.R index cd1874f..c63e895 100644 --- a/R/read_config.R +++ b/R/read_config.R @@ -11,7 +11,7 @@ #' - `api_abbr` (`character(1)`): An abbreviation for the API, used in function #' names and other identifiers. #' - `api_version` (`character(1)`): The version of the API. -#' - `rapid_file` (`character(1)`): The name of the file (relative to the +#' - `rapid_filename` (`character(1)`): The name of the file (relative to the #' package root) where the API definition is stored as an RDS file. By default, #' this is `_beekeeper_rapid.rds`, and is generated by [use_beekeeper()] based #' on an OpenAPI definition file. @@ -20,10 +20,12 @@ #' #' @inheritParams .shared-params #' @returns (`list`) Configuration information, with elements `api_title`, -#' `api_abbr`, `api_version`, `rapid_file`, and `updated_on`. +#' `api_abbr`, `api_version`, `rapid_filename`, and `updated_on`. #' @export -read_config <- function(pkg_dir = ".", config_file = "_beekeeper.yml") { - config <- yaml::read_yaml(fs::path(pkg_dir, config_file)) +#' @family config readers +read_config <- function(pkg_dir = ".", config_filename = "_beekeeper.yml") { + .assert_is_pkg(pkg_dir) + config <- yaml::read_yaml(fs::path(pkg_dir, config_filename)) return(.stabilize_config(config)) } @@ -39,7 +41,9 @@ read_config <- function(pkg_dir = ".", config_file = "_beekeeper.yml") { config$api_version, allow_null = TRUE ) - config$rapid_file <- stbl::stabilize_character_scalar(config$rapid_file) + config$rapid_filename <- stbl::stabilize_character_scalar( + config$rapid_filename + ) config$updated_on <- config$updated_on %&&% strptime( config$updated_on, @@ -49,6 +53,70 @@ read_config <- function(pkg_dir = ".", config_file = "_beekeeper.yml") { return(config) } +.read_config_field <- function(field, pkg_dir, config_filename) { + read_config(pkg_dir, config_filename)[[field]] +} + +#' Read rapid_filename config field +#' +#' Read the `rapid_filename` field from a beekeeper config file. +#' +#' @inheritParams .shared-params +#' +#' @returns (`character(1)`) The `rapid_filename` field from the beekeeper +#' config file. +#' @export +#' @family config readers +#' @examples +#' read_rapid_filename( +#' pkg_dir = fs::path_package("beekeeper"), +#' config_filename = "example_config.yml" +#' ) +read_rapid_filename <- function( + pkg_dir = ".", + config_filename = "_beekeeper.yml" +) { + .read_config_field("rapid_filename", pkg_dir, config_filename) +} + +#' Read api_abbr config field +#' +#' Read the `api_abbr` field from a beekeeper config file. +#' +#' @inheritParams .shared-params +#' +#' @returns (`character(1)`) The `api_abbr` field from the beekeeper +#' config file. +#' @export +#' @family config readers +#' @examples +#' read_api_abbr( +#' pkg_dir = fs::path_package("beekeeper"), +#' config_filename = "example_config.yml" +#' ) +read_api_abbr <- function(pkg_dir = ".", config_filename = "_beekeeper.yml") { + .read_config_field("api_abbr", pkg_dir, config_filename) +} + +#' Read api_title config field +#' +#' Read the `api_title` field from a beekeeper config file. +#' +#' @inheritParams .shared-params +#' +#' @returns (`character(1)`) The `api_title` field from the beekeeper +#' config file. +#' @export +#' @family config readers +#' @examples +#' read_api_title( +#' pkg_dir = fs::path_package("beekeeper"), +#' config_filename = "example_config.yml" +#' ) +read_api_title <- function(pkg_dir = ".", config_filename = "_beekeeper.yml") { + .read_config_field("api_title", pkg_dir, config_filename) +} + #' Read an API definition file #' #' Reads an RDS file (default name `_beekeeper_rapid.rds`) with the API @@ -58,9 +126,18 @@ read_config <- function(pkg_dir = ".", config_file = "_beekeeper.yml") { #' @inheritParams .shared-params #' @returns (`rapid::class_rapid`) The definition of the API. #' @export +#' @family config readers +#' @examples +#' api_definition <- read_api_definition( +#' pkg_dir = fs::path_package("beekeeper"), +#' rapid_filename = "example_beekeeper_rapid.rds" +#' ) +#' class(api_definition) +#' api_definition@info@origin@url read_api_definition <- function( pkg_dir = ".", - rapid_file = "_beekeeper_rapid.rds" + rapid_filename = read_rapid_filename(pkg_dir) ) { - readRDS(fs::path(pkg_dir, rapid_file)) + .assert_is_pkg(pkg_dir) + readRDS(fs::path(pkg_dir, rapid_filename)) } diff --git a/R/use_beekeeper.R b/R/use_beekeeper.R index efecfe9..04be554 100644 --- a/R/use_beekeeper.R +++ b/R/use_beekeeper.R @@ -13,25 +13,30 @@ #' #' @returns (`character(1)`, invisibly) The path to the configuration file. The #' config file is written as a side effect of this function. The rapid object -#' is also written, and the path to that file is saved in the config file. +#' is also written, and the path to that file (relative to `pkg_dir`) is saved +#' in the config file. #' @export use_beekeeper <- function( x, api_abbr, ..., - config_file = "_beekeeper.yml", - rapid_file = "_beekeeper_rapid.rds" + pkg_dir = ".", + config_filename = "_beekeeper.yml", + rapid_filename = "_beekeeper_rapid.rds" ) { + rlang::check_dots_empty() + .assert_is_pkg(pkg_dir) api_definition <- rapid::as_rapid(x) - rapid_file <- .write_rapid(api_definition, rapid_file) - config_file <- .write_config( + rapid_filename <- .write_rapid(api_definition, rapid_filename, pkg_dir) + config_filename <- .write_config( api_definition, api_abbr, - rapid_file, - config_file + rapid_filename, + config_filename, + pkg_dir ) - return(invisible(config_file)) + return(invisible(fs::path(pkg_dir, config_filename))) } #' Write the rapid definition file @@ -39,11 +44,11 @@ use_beekeeper <- function( #' @inheritParams .shared-params #' @returns (`character(1)`) The written file path. #' @keywords internal -.write_rapid <- function(api_definition, rapid_file) { - rapid_file <- stbl::stabilize_character_scalar(rapid_file) - saveRDS(api_definition, rapid_file) - usethis::use_build_ignore(rapid_file) - return(rapid_file) +.write_rapid <- function(api_definition, rapid_filename, pkg_dir) { + rapid_filename <- stbl::stabilize_character_scalar(rapid_filename) + saveRDS(api_definition, fs::path(pkg_dir, rapid_filename)) + usethis::with_project(pkg_dir, usethis::use_build_ignore(rapid_filename)) + return(rapid_filename) } #' Write the beekeeper config file @@ -51,19 +56,26 @@ use_beekeeper <- function( #' @inheritParams .shared-params #' @returns (`character(1)`) The written config file path. #' @keywords internal -.write_config <- function(api_definition, api_abbr, rapid_file, config_file) { - config_file <- stbl::stabilize_character_scalar(config_file) +.write_config <- function( + api_definition, + api_abbr, + rapid_filename, + config_filename, + pkg_dir +) { + config_filename <- stbl::stabilize_character_scalar(config_filename) update_time <- strptime(Sys.time(), format = "%Y-%m-%d %H:%M:%S", tz = "UTC") yaml::write_yaml( list( api_title = api_definition@info@title, api_abbr = stbl::stabilize_character_scalar(api_abbr), api_version = api_definition@info@version, - rapid_file = fs::path_rel(rapid_file, fs::path_dir(config_file)), + rapid_filename = rapid_filename, updated_on = as.character(update_time) ), - file = config_file + file = fs::path(pkg_dir, config_filename) ) - usethis::use_build_ignore(config_file) - return(config_file) + memoise::forget(read_config) + usethis::with_project(pkg_dir, usethis::use_build_ignore(config_filename)) + return(config_filename) } diff --git a/R/zzz.R b/R/zzz.R index 0b7eb0c..ecf8b07 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,5 +1,6 @@ -.onLoad <- function(...) { +.onLoad <- function(libname, pkgname) { S7::methods_register() # nocov + read_config <<- memoise::memoise(read_config) # nocov } # enable usage of @name in package code diff --git a/inst/example_beekeeper_rapid.rds b/inst/example_beekeeper_rapid.rds new file mode 100644 index 0000000..aedfdf2 Binary files /dev/null and b/inst/example_beekeeper_rapid.rds differ diff --git a/inst/example_config.yml b/inst/example_config.yml new file mode 100644 index 0000000..684c43b --- /dev/null +++ b/inst/example_config.yml @@ -0,0 +1,5 @@ +api_title: APIs.guru +api_abbr: guru +api_version: 2.2.0 +rapid_filename: example_beekeeper_rapid.rds +updated_on: 2026-05-12 07:57:02 diff --git a/man/dot-generate_paths.Rd b/man/dot-generate_paths.Rd index eb528a1..b434110 100644 --- a/man/dot-generate_paths.Rd +++ b/man/dot-generate_paths.Rd @@ -23,8 +23,7 @@ the API, for use in function names and environment variables.} \item{pagination_data}{(\code{list}) Pagination metadata used while generating path files.} -\item{base_url}{(\code{character(1)}) The base URL used in generated test -helpers.} +\item{base_url}{(\code{character(1)}) The base URL used in generated test helpers.} } \value{ (\code{character}) Generated file paths. diff --git a/man/dot-shared-params.Rd b/man/dot-shared-params.Rd index 7ff0906..19d92d9 100644 --- a/man/dot-shared-params.Rd +++ b/man/dot-shared-params.Rd @@ -20,15 +20,14 @@ files.} \item{base_path}{The root URL of the current project.} -\item{base_url}{(\code{character(1)}) The base URL used in generated test -helpers.} +\item{base_url}{(\code{character(1)}) The base URL used in generated test helpers.} \item{call}{(\code{environment}) The caller environment for error messages.} \item{config}{(\code{list}) Package-generation configuration data.} -\item{config_file}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper yaml -config file.} +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} \item{data}{(\code{list}) Data passed to a template.} @@ -86,8 +85,8 @@ operation identifier.} \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} -\item{rapid_file}{(\code{character(1)} or \code{fs_path}) The path to the R API -definition (rapid) file.} +\item{rapid_filename}{(\code{character(1)} or \code{fs_path}) The path to the R API +definition (rapid) file (relative to the package root).} \item{required}{(\code{logical}) Whether each parameter is required.} diff --git a/man/dot-write_config.Rd b/man/dot-write_config.Rd index cd3ec2a..e72f958 100644 --- a/man/dot-write_config.Rd +++ b/man/dot-write_config.Rd @@ -4,7 +4,13 @@ \alias{.write_config} \title{Write the beekeeper config file} \usage{ -.write_config(api_definition, api_abbr, rapid_file, config_file) +.write_config( + api_definition, + api_abbr, + rapid_filename, + config_filename, + pkg_dir +) } \arguments{ \item{api_definition}{(\code{rapid::class_rapid}) The API definition to generate @@ -13,11 +19,14 @@ package code from.} \item{api_abbr}{(\code{character(1)}) A short (about 2-5 letter) abbreviation for the API, for use in function names and environment variables.} -\item{rapid_file}{(\code{character(1)} or \code{fs_path}) The path to the R API -definition (rapid) file.} +\item{rapid_filename}{(\code{character(1)} or \code{fs_path}) The path to the R API +definition (rapid) file (relative to the package root).} -\item{config_file}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper yaml -config file.} +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} + +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} } \value{ (\code{character(1)}) The written config file path. diff --git a/man/dot-write_rapid.Rd b/man/dot-write_rapid.Rd index 45c7333..41bea7f 100644 --- a/man/dot-write_rapid.Rd +++ b/man/dot-write_rapid.Rd @@ -4,14 +4,17 @@ \alias{.write_rapid} \title{Write the rapid definition file} \usage{ -.write_rapid(api_definition, rapid_file) +.write_rapid(api_definition, rapid_filename, pkg_dir) } \arguments{ \item{api_definition}{(\code{rapid::class_rapid}) The API definition to generate package code from.} -\item{rapid_file}{(\code{character(1)} or \code{fs_path}) The path to the R API -definition (rapid) file.} +\item{rapid_filename}{(\code{character(1)} or \code{fs_path}) The path to the R API +definition (rapid) file (relative to the package root).} + +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} } \value{ (\code{character(1)}) The written file path. diff --git a/man/generate_pkg.Rd b/man/generate_pkg.Rd index 6e5607c..3aec639 100644 --- a/man/generate_pkg.Rd +++ b/man/generate_pkg.Rd @@ -5,10 +5,11 @@ \title{Use a beekeeper config file to generate code} \usage{ generate_pkg( - api_abbr = NULL, - api_definition = NULL, - api_title = NULL, - config_file = "_beekeeper.yml", + api_abbr = read_api_abbr(pkg_dir, config_filename), + api_definition = read_api_definition(pkg_dir, read_rapid_filename(pkg_dir, + config_filename)), + api_title = read_api_title(pkg_dir, config_filename), + config_filename = "_beekeeper.yml", pkg_dir = "." ) } @@ -22,8 +23,8 @@ package code from.} \item{api_title}{(\code{character(1)}) The API title used in generated package files.} -\item{config_file}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper yaml -config file.} +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} diff --git a/man/read_api_abbr.Rd b/man/read_api_abbr.Rd new file mode 100644 index 0000000..09774ca --- /dev/null +++ b/man/read_api_abbr.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_config.R +\name{read_api_abbr} +\alias{read_api_abbr} +\title{Read api_abbr config field} +\usage{ +read_api_abbr(pkg_dir = ".", config_filename = "_beekeeper.yml") +} +\arguments{ +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} + +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} +} +\value{ +(\code{character(1)}) The \code{api_abbr} field from the beekeeper +config file. +} +\description{ +Read the \code{api_abbr} field from a beekeeper config file. +} +\examples{ +read_api_abbr( + pkg_dir = fs::path_package("beekeeper"), + config_filename = "example_config.yml" +) +} +\seealso{ +Other config readers: +\code{\link[=read_api_definition]{read_api_definition()}}, +\code{\link[=read_api_title]{read_api_title()}}, +\code{\link[=read_config]{read_config()}}, +\code{\link[=read_rapid_filename]{read_rapid_filename()}} +} +\concept{config readers} diff --git a/man/read_api_definition.Rd b/man/read_api_definition.Rd index a043f2c..48dca93 100644 --- a/man/read_api_definition.Rd +++ b/man/read_api_definition.Rd @@ -4,14 +4,17 @@ \alias{read_api_definition} \title{Read an API definition file} \usage{ -read_api_definition(pkg_dir = ".", rapid_file = "_beekeeper_rapid.rds") +read_api_definition( + pkg_dir = ".", + rapid_filename = read_rapid_filename(pkg_dir) +) } \arguments{ \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} -\item{rapid_file}{(\code{character(1)} or \code{fs_path}) The path to the R API -definition (rapid) file.} +\item{rapid_filename}{(\code{character(1)} or \code{fs_path}) The path to the R API +definition (rapid) file (relative to the package root).} } \value{ (\code{rapid::class_rapid}) The definition of the API. @@ -21,3 +24,19 @@ Reads an RDS file (default name \verb{_beekeeper_rapid.rds}) with the API definition stored as a \code{\link[rapid:class_rapid]{rapid::class_rapid()}} object. This file is generated by \code{\link[=use_beekeeper]{use_beekeeper()}} based on an OpenAPI definition file. } +\examples{ +api_definition <- read_api_definition( + pkg_dir = fs::path_package("beekeeper"), + rapid_filename = "example_beekeeper_rapid.rds" +) +class(api_definition) +api_definition@info@origin@url +} +\seealso{ +Other config readers: +\code{\link[=read_api_abbr]{read_api_abbr()}}, +\code{\link[=read_api_title]{read_api_title()}}, +\code{\link[=read_config]{read_config()}}, +\code{\link[=read_rapid_filename]{read_rapid_filename()}} +} +\concept{config readers} diff --git a/man/read_api_title.Rd b/man/read_api_title.Rd new file mode 100644 index 0000000..9f1d6c2 --- /dev/null +++ b/man/read_api_title.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_config.R +\name{read_api_title} +\alias{read_api_title} +\title{Read api_title config field} +\usage{ +read_api_title(pkg_dir = ".", config_filename = "_beekeeper.yml") +} +\arguments{ +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} + +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} +} +\value{ +(\code{character(1)}) The \code{api_title} field from the beekeeper +config file. +} +\description{ +Read the \code{api_title} field from a beekeeper config file. +} +\examples{ +read_api_title( + pkg_dir = fs::path_package("beekeeper"), + config_filename = "example_config.yml" +) +} +\seealso{ +Other config readers: +\code{\link[=read_api_abbr]{read_api_abbr()}}, +\code{\link[=read_api_definition]{read_api_definition()}}, +\code{\link[=read_config]{read_config()}}, +\code{\link[=read_rapid_filename]{read_rapid_filename()}} +} +\concept{config readers} diff --git a/man/read_config.Rd b/man/read_config.Rd index 9dadade..664a7a9 100644 --- a/man/read_config.Rd +++ b/man/read_config.Rd @@ -4,18 +4,18 @@ \alias{read_config} \title{Read a beekeeper config file} \usage{ -read_config(pkg_dir = ".", config_file = "_beekeeper.yml") +read_config(pkg_dir = ".", config_filename = "_beekeeper.yml") } \arguments{ \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} -\item{config_file}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper yaml -config file.} +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} } \value{ (\code{list}) Configuration information, with elements \code{api_title}, -\code{api_abbr}, \code{api_version}, \code{rapid_file}, and \code{updated_on}. +\code{api_abbr}, \code{api_version}, \code{rapid_filename}, and \code{updated_on}. } \description{ Reads a YAML file (default name \verb{_beekeeper.yml}) with configuration @@ -30,7 +30,7 @@ and messages. \item \code{api_abbr} (\code{character(1)}): An abbreviation for the API, used in function names and other identifiers. \item \code{api_version} (\code{character(1)}): The version of the API. -\item \code{rapid_file} (\code{character(1)}): The name of the file (relative to the +\item \code{rapid_filename} (\code{character(1)}): The name of the file (relative to the package root) where the API definition is stored as an RDS file. By default, this is \verb{_beekeeper_rapid.rds}, and is generated by \code{\link[=use_beekeeper]{use_beekeeper()}} based on an OpenAPI definition file. @@ -38,3 +38,11 @@ on an OpenAPI definition file. last updated, in the format "YYYY-MM-DD HH:MM:SS" (UTC). } } +\seealso{ +Other config readers: +\code{\link[=read_api_abbr]{read_api_abbr()}}, +\code{\link[=read_api_definition]{read_api_definition()}}, +\code{\link[=read_api_title]{read_api_title()}}, +\code{\link[=read_rapid_filename]{read_rapid_filename()}} +} +\concept{config readers} diff --git a/man/read_rapid_filename.Rd b/man/read_rapid_filename.Rd new file mode 100644 index 0000000..94690ed --- /dev/null +++ b/man/read_rapid_filename.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_config.R +\name{read_rapid_filename} +\alias{read_rapid_filename} +\title{Read rapid_filename config field} +\usage{ +read_rapid_filename(pkg_dir = ".", config_filename = "_beekeeper.yml") +} +\arguments{ +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} + +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} +} +\value{ +(\code{character(1)}) The \code{rapid_filename} field from the beekeeper +config file. +} +\description{ +Read the \code{rapid_filename} field from a beekeeper config file. +} +\examples{ +read_rapid_filename( + pkg_dir = fs::path_package("beekeeper"), + config_filename = "example_config.yml" +) +} +\seealso{ +Other config readers: +\code{\link[=read_api_abbr]{read_api_abbr()}}, +\code{\link[=read_api_definition]{read_api_definition()}}, +\code{\link[=read_api_title]{read_api_title()}}, +\code{\link[=read_config]{read_config()}} +} +\concept{config readers} diff --git a/man/use_beekeeper.Rd b/man/use_beekeeper.Rd index 2a6b33c..75fd578 100644 --- a/man/use_beekeeper.Rd +++ b/man/use_beekeeper.Rd @@ -8,8 +8,9 @@ use_beekeeper( x, api_abbr, ..., - config_file = "_beekeeper.yml", - rapid_file = "_beekeeper_rapid.rds" + pkg_dir = ".", + config_filename = "_beekeeper.yml", + rapid_filename = "_beekeeper_rapid.rds" ) } \arguments{ @@ -23,16 +24,20 @@ the API, for use in function names and environment variables.} \item{...}{These dots are for future extensions and must be empty.} -\item{config_file}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper yaml -config file.} +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} -\item{rapid_file}{(\code{character(1)} or \code{fs_path}) The path to the R API -definition (rapid) file.} +\item{config_filename}{(\code{character(1)} or \code{fs_path}) The path to a beekeeper +yaml config file (relative to the package root).} + +\item{rapid_filename}{(\code{character(1)} or \code{fs_path}) The path to the R API +definition (rapid) file (relative to the package root).} } \value{ (\code{character(1)}, invisibly) The path to the configuration file. The config file is written as a side effect of this function. The rapid object -is also written, and the path to that file is saved in the config file. +is also written, and the path to that file (relative to \code{pkg_dir}) is saved +in the config file. } \description{ Create a configuration file for a package to use beekeeper. The configuration diff --git a/tests/testthat/_fixtures/000-create_fixtures.R b/tests/testthat/_fixtures/000-create_fixtures.R index 68475c9..7561dce 100644 --- a/tests/testthat/_fixtures/000-create_fixtures.R +++ b/tests/testthat/_fixtures/000-create_fixtures.R @@ -1,27 +1,16 @@ apid_url <- "https://api.apis.guru/v2/specs/apis.guru/2.2.0/openapi.yaml" api_abbr <- "guru" -rapid_write_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper_rapid.rds" -)) -config_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper.yml" -)) apid_url |> url() |> use_beekeeper( api_abbr = api_abbr, - config_file = config_path, - rapid_file = rapid_write_path + pkg_dir = test_path("_fixtures", api_abbr), + config_filename = "_beekeeper.yml", + rapid_filename = "_beekeeper_rapid.rds" ) apid_url <- "https://api.apis.guru/v2/specs/fec.gov/1.0/openapi.yaml" api_abbr <- "fec" -rapid_write_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper_rapid.rds" -)) -config_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper.yml" -)) fec_apid <- apid_url |> url() |> yaml::read_yaml() @@ -37,8 +26,9 @@ fec_rapid <- rapid::as_rapid(fec_apid) fec_rapid |> use_beekeeper( api_abbr = api_abbr, - config_file = config_path, - rapid_file = rapid_write_path + pkg_dir = test_path("_fixtures", api_abbr), + config_filename = "_beekeeper.yml", + rapid_filename = "_beekeeper_rapid.rds" ) fec_rapid@paths <- rapid::as_paths({ x <- fec_rapid@paths |> @@ -48,27 +38,16 @@ fec_rapid@paths <- rapid::as_paths({ x$tags <- NULL x }) -rapid_write_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/{api_abbr}_subset_rapid.rds" -)) -config_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/{api_abbr}_subset_beekeeper.yml" -)) fec_rapid |> use_beekeeper( api_abbr = api_abbr, - config_file = config_path, - rapid_file = rapid_write_path + pkg_dir = test_path("_fixtures", api_abbr), + config_filename = "fec_subset_beekeeper.yml", + rapid_filename = "fec_subset_rapid.rds" ) apid_url <- "https://api.apis.guru/v2/specs/trello.com/1.0/openapi.yaml" api_abbr <- "trello" -rapid_write_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper_rapid.rds" -)) -config_path <- test_path(glue::glue( - "_fixtures/{api_abbr}/_beekeeper.yml" -)) trello_rapid <- apid_url |> url() |> rapid::as_rapid() @@ -83,8 +62,9 @@ trello_rapid@paths <- rapid::as_paths({ trello_rapid |> use_beekeeper( api_abbr = api_abbr, - config_file = config_path, - rapid_file = rapid_write_path + pkg_dir = test_path("_fixtures", api_abbr), + config_filename = "_beekeeper.yml", + rapid_filename = "_beekeeper_rapid.rds" ) cli::cli_warn("Revert .Rbuildignore") diff --git a/tests/testthat/_fixtures/fec/_beekeeper.yml b/tests/testthat/_fixtures/fec/_beekeeper.yml index cbd7a2a..d90267f 100644 --- a/tests/testthat/_fixtures/fec/_beekeeper.yml +++ b/tests/testthat/_fixtures/fec/_beekeeper.yml @@ -1,5 +1,5 @@ api_title: OpenFEC api_abbr: fec api_version: '1.0' -rapid_file: _beekeeper_rapid.rds +rapid_filename: _beekeeper_rapid.rds updated_on: 2026-05-12 07:57:21 diff --git a/tests/testthat/_fixtures/fec/fec_subset_beekeeper.yml b/tests/testthat/_fixtures/fec/fec_subset_beekeeper.yml index 1f25328..448746c 100644 --- a/tests/testthat/_fixtures/fec/fec_subset_beekeeper.yml +++ b/tests/testthat/_fixtures/fec/fec_subset_beekeeper.yml @@ -1,5 +1,5 @@ api_title: OpenFEC api_abbr: fec api_version: '1.0' -rapid_file: fec_subset_rapid.rds +rapid_filename: fec_subset_rapid.rds updated_on: 2026-05-12 07:57:29 diff --git a/tests/testthat/_fixtures/guru/_beekeeper.yml b/tests/testthat/_fixtures/guru/_beekeeper.yml index 3c59cd4..35a1cf4 100644 --- a/tests/testthat/_fixtures/guru/_beekeeper.yml +++ b/tests/testthat/_fixtures/guru/_beekeeper.yml @@ -1,5 +1,5 @@ api_title: APIs.guru api_abbr: guru api_version: 2.2.0 -rapid_file: _beekeeper_rapid.rds +rapid_filename: _beekeeper_rapid.rds updated_on: 2026-05-12 07:57:02 diff --git a/tests/testthat/_fixtures/trello/_beekeeper.yml b/tests/testthat/_fixtures/trello/_beekeeper.yml index e7113dc..8441ca9 100644 --- a/tests/testthat/_fixtures/trello/_beekeeper.yml +++ b/tests/testthat/_fixtures/trello/_beekeeper.yml @@ -1,5 +1,5 @@ api_title: Trello api_abbr: trello api_version: '1.0' -rapid_file: _beekeeper_rapid.rds +rapid_filename: _beekeeper_rapid.rds updated_on: 2026-05-12 07:57:43 diff --git a/tests/testthat/_snaps/read_config.md b/tests/testthat/_snaps/read_config.md index 0a9e06d..be541e9 100644 --- a/tests/testthat/_snaps/read_config.md +++ b/tests/testthat/_snaps/read_config.md @@ -1,4 +1,4 @@ -# read_config() reads configs +# read_config() reads configs (#82) Code config @@ -12,7 +12,7 @@ $api_version [1] "2.2.0" - $rapid_file + $rapid_filename [1] "_beekeeper_rapid.rds" $updated_on diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 60bc6d2..5308453 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -35,8 +35,8 @@ scrub_updated <- function(input) { scrub_rapid_file_location <- function(input) { sub( - "rapid_file: .*$", - "rapid_file: RAPID_FILE_PATH", + "rapid_filename: .*$", + "rapid_filename: RAPID_FILE_PATH", input ) } @@ -104,15 +104,20 @@ guru_config <- read_config(pkg_dir = test_path("_fixtures", "guru")) guru_api_definition <- read_api_definition( pkg_dir = test_path("_fixtures", "guru") ) +memoise::forget(read_config) + trello_config <- read_config(pkg_dir = test_path("_fixtures", "trello")) trello_api_definition <- read_api_definition( pkg_dir = test_path("_fixtures", "trello") ) +memoise::forget(read_config) + fec_config <- read_config( pkg_dir = test_path("_fixtures", "fec"), - config_file = "fec_subset_beekeeper.yml" + config_filename = "fec_subset_beekeeper.yml" ) fec_api_definition <- read_api_definition( pkg_dir = test_path("_fixtures", "fec"), - rapid_file = "fec_subset_rapid.rds" + rapid_filename = "fec_subset_rapid.rds" ) +memoise::forget(read_config) diff --git a/tests/testthat/test-read_config.R b/tests/testthat/test-read_config.R index 7a5a738..84b890b 100644 --- a/tests/testthat/test-read_config.R +++ b/tests/testthat/test-read_config.R @@ -1,4 +1,5 @@ -test_that("read_config() reads configs", { +test_that("read_config() reads configs (#82)", { + withr::defer(memoise::forget(read_config)) config <- read_config(pkg_dir = test_path("_fixtures", "guru")) expect_s3_class(config$updated_on, c("POSIXlt", "POSIXt")) expect_snapshot({ @@ -6,9 +7,20 @@ test_that("read_config() reads configs", { }) }) -test_that("read_api_definition() reads api_definitions", { - api_definition <- read_api_definition( - pkg_dir = test_path("_fixtures", "guru") +test_that("read_api_definition() reads api definitions (#82)", { + withr::defer(memoise::forget(read_config)) + expect_s7_class( + read_api_definition(test_path("_fixtures", "guru")), + rapid::class_rapid ) - expect_s7_class(api_definition, rapid::class_rapid) +}) + +test_that("read_api_abbr() reads api abbrs (#99)", { + withr::defer(memoise::forget(read_config)) + expect_equal(read_api_abbr(test_path("_fixtures", "guru")), "guru") +}) + +test_that("read_api_title() reads api titles (#99)", { + withr::defer(memoise::forget(read_config)) + expect_equal(read_api_title(test_path("_fixtures", "guru")), "APIs.guru") }) diff --git a/tests/testthat/test-use_beekeeper.R b/tests/testthat/test-use_beekeeper.R index 0550003..cc77f6d 100644 --- a/tests/testthat/test-use_beekeeper.R +++ b/tests/testthat/test-use_beekeeper.R @@ -1,22 +1,29 @@ test_that("use_beekeeper writes a yml config (#10)", { + pkg_tempdir <- withr::local_tempdir() local_mocked_bindings( use_build_ignore = function(...) { invisible(TRUE) }, + with_project = function(...) { + invisible(NULL) + }, .package = "usethis" ) - config_path <- withr::local_tempfile(fileext = ".yml") - rapid_write_path <- withr::local_tempfile(fileext = ".rds") + local_mocked_bindings( + .assert_is_pkg = function(pkg_dir) { + expect_identical(pkg_dir, pkg_tempdir) + invisible(NULL) + } + ) test_result <- use_beekeeper( guru_api_definition, api_abbr = "guru", - config_file = config_path, - rapid_file = rapid_write_path + pkg_dir = pkg_tempdir ) - expect_identical(test_result, config_path) - reread_rapid <- readRDS(rapid_write_path) + expect_identical(test_result, fs::path(pkg_tempdir, "_beekeeper.yml")) + reread_rapid <- readRDS(fs::path(pkg_tempdir, "_beekeeper_rapid.rds")) expect_identical(guru_api_definition, reread_rapid) - test_result_file <- scrub_config(readLines(config_path)) + test_result_file <- scrub_config(readLines(test_result)) expected_result_file <- scrub_config( readLines(test_path("_fixtures", "guru", "_beekeeper.yml")) )