Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
fs,
glue,
httptest2,
memoise,
nectar,
purrr,
rapid (>= 0.0.0.9003),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 5 additions & 6 deletions R/aaa-shared_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 9 additions & 11 deletions R/generate_pkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
Comment thread
jonthegeek marked this conversation as resolved.
Resolved
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,
Expand Down
89 changes: 82 additions & 7 deletions R/read_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,10 +20,11 @@
#'
#' @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") {
config <- yaml::read_yaml(fs::path(pkg_dir, config_filename))
return(.stabilize_config(config))
}

Expand All @@ -39,7 +40,9 @@ read_config <- function(pkg_dir = ".", config_file = "_beekeeper.yml") {
config$api_version,
allow_null = TRUE
)
Comment thread
jonthegeek marked this conversation as resolved.
Resolved
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,
Expand All @@ -49,6 +52,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
Expand All @@ -58,9 +125,17 @@ 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))
readRDS(fs::path(pkg_dir, rapid_filename))
}
49 changes: 30 additions & 19 deletions R/use_beekeeper.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,68 @@
#'
#' @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 = ".",
Comment thread
jonthegeek marked this conversation as resolved.
Resolved
config_filename = "_beekeeper.yml",
rapid_filename = "_beekeeper_rapid.rds"
) {
.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
#'
#' @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::use_build_ignore(rapid_filename)
return(rapid_filename)
Comment thread
jonthegeek marked this conversation as resolved.
Resolved
}

#' Write the beekeeper config file
#'
#' @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::use_build_ignore(config_filename)
return(config_filename)
}
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.onLoad <- function(...) {
.onLoad <- function(libname, pkgname) {
S7::methods_register() # nocov
read_config <<- memoise::memoise(read_config) # nocov
}

# enable usage of <S7_object>@name in package code
Expand Down
Binary file added inst/example_beekeeper_rapid.rds
Binary file not shown.
5 changes: 5 additions & 0 deletions inst/example_config.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions man/dot-generate_paths.Rd

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

11 changes: 5 additions & 6 deletions man/dot-shared-params.Rd

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

19 changes: 14 additions & 5 deletions man/dot-write_config.Rd

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

Loading
Loading