Skip to content

Commit

Permalink
Import external functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek committed Mar 27, 2024
1 parent bf7e235 commit 614e0bf
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 48 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Imports:
rprojroot,
S7,
snakecase,
stringr,
styler,
testthat,
usethis,
Expand All @@ -38,7 +39,6 @@ Suggests:
covr,
knitr,
rmarkdown,
stringr,
withr
VignetteBuilder:
knitr
Expand Down
35 changes: 35 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,46 @@ export(generate_pkg)
export(generate_pkg_agent)
export(use_beekeeper)
if (getRversion() < "4.3.0") importFrom("S7", "@")
importFrom(S7,class_any)
importFrom(cli,cli_abort)
importFrom(cli,cli_warn)
importFrom(desc,desc)
importFrom(fs,is_dir)
importFrom(fs,path)
importFrom(fs,path_dir)
importFrom(fs,path_file)
importFrom(fs,path_rel)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(httptest2,use_httptest2)
importFrom(lubridate,now)
importFrom(lubridate,parse_date_time)
importFrom(nectar,call_api)
importFrom(nectar,stabilize_string)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map_chr)
importFrom(purrr,pmap)
importFrom(purrr,quietly)
importFrom(rapid,as_rapid)
importFrom(rapid,class_api_key_security_scheme)
importFrom(rapid,class_security_scheme_details)
importFrom(rapid,class_security_schemes)
importFrom(rlang,.data)
importFrom(rlang,set_names)
importFrom(rlang,try_fetch)
importFrom(rprojroot,find_package_root_file)
importFrom(snakecase,to_snake_case)
importFrom(stringr,str_remove)
importFrom(styler,style_file)
importFrom(testthat,test_that)
importFrom(usethis,proj_get)
importFrom(usethis,proj_path)
importFrom(usethis,use_build_ignore)
importFrom(usethis,use_directory)
importFrom(usethis,use_package)
importFrom(usethis,use_template)
importFrom(usethis,use_testthat)
importFrom(utils,capture.output)
importFrom(yaml,read_yaml)
importFrom(yaml,write_yaml)
28 changes: 14 additions & 14 deletions R/as_bk_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ as_bk_data <- S7::new_generic(
dispatch_args = "x"
)

S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
S7::method(as_bk_data, class_security_schemes) <- function(x) {
if (!length(x)) {
return(list())
}
Expand All @@ -22,7 +22,7 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
}

.security_schemes_collect <- function(x) {
purrr::pmap(
pmap(
list(
x@name,
x@details,
Expand All @@ -35,7 +35,7 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
.security_scheme_rotate <- function(name, details, description) {
security_scheme <- c(
list(
name = snakecase::to_snake_case(name),
name = to_snake_case(name),
description = description
),
as_bk_data(details)
Expand Down Expand Up @@ -79,7 +79,7 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
}

.security_args_compile <- function(security_schemes) {
security_args <- sort(unique(purrr::map_chr(security_schemes, "arg_name")))
security_args <- sort(unique(map_chr(security_schemes, "arg_name")))
return(list(
security_arg_names = security_args,
security_arg_list = .collapse_comma(
Expand All @@ -93,13 +93,13 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
}

.security_arg_help_generate <- function(security_schemes, security_args) {
security_arg_description <- rlang::set_names(
purrr::map_chr(security_schemes, "description"),
purrr::map_chr(security_schemes, "arg_name")
security_arg_description <- set_names(
map_chr(security_schemes, "description"),
map_chr(security_schemes, "arg_name")
)
security_arg_description <- unname(security_arg_description[security_args])
return(
purrr::map2(
map2(
security_arg_description,
security_args,
function(arg_description, arg_name) {
Expand All @@ -109,17 +109,17 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
)
}

S7::method(as_bk_data, rapid::class_security_scheme_details) <- function(x) {
purrr::map(x, as_bk_data)
S7::method(as_bk_data, class_security_scheme_details) <- function(x) {
map(x, as_bk_data)
}

S7::method(as_bk_data, rapid::class_api_key_security_scheme) <- function(x) {
S7::method(as_bk_data, class_api_key_security_scheme) <- function(x) {
if (length(x)) {
return(
list(
parameter_name = x@parameter_name,
arg_name = stringr::str_remove(
snakecase::to_snake_case(x@parameter_name),
to_snake_case(x@parameter_name),
"^x_"
),
location = x@location,
Expand All @@ -131,8 +131,8 @@ S7::method(as_bk_data, rapid::class_api_key_security_scheme) <- function(x) {
return(list())
}

S7::method(as_bk_data, S7::class_any) <- function(x) {
cli::cli_warn(
S7::method(as_bk_data, class_any) <- function(x) {
cli_warn(
"No method for as_bk_data() for class {.cls {class(x)}}."
)
return(list())
Expand Down
35 changes: 35 additions & 0 deletions R/beekeeper-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,48 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom cli cli_abort
#' @importFrom cli cli_warn
#' @importFrom desc desc
#' @importFrom fs is_dir
#' @importFrom fs path
#' @importFrom fs path_dir
#' @importFrom fs path_file
#' @importFrom fs path_rel
#' @importFrom glue glue
#' @importFrom glue glue_collapse
#' @importFrom httptest2 use_httptest2
#' @importFrom lubridate now
#' @importFrom lubridate parse_date_time
#' @importFrom nectar call_api
#' @importFrom nectar stabilize_string
#' @importFrom purrr map
#' @importFrom purrr map_chr
#' @importFrom purrr map2
#' @importFrom purrr pmap
#' @importFrom purrr quietly
#' @importFrom rapid as_rapid
#' @importFrom rapid class_api_key_security_scheme
#' @importFrom rapid class_security_scheme_details
#' @importFrom rapid class_security_schemes
#' @importFrom rlang .data
#' @importFrom rlang set_names
#' @importFrom rlang try_fetch
#' @importFrom rprojroot find_package_root_file
#' @importFrom S7 class_any
#' @importFrom snakecase to_snake_case
#' @importFrom stringr str_remove
#' @importFrom styler style_file
#' @importFrom testthat test_that
#' @importFrom usethis proj_get
#' @importFrom usethis proj_path
#' @importFrom usethis use_build_ignore
#' @importFrom usethis use_directory
#' @importFrom usethis use_package
#' @importFrom usethis use_template
#' @importFrom usethis use_testthat
#' @importFrom utils capture.output
#' @importFrom yaml read_yaml
#' @importFrom yaml write_yaml
## usethis namespace: end
NULL
30 changes: 15 additions & 15 deletions R/generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ generate_pkg <- function(config_file = "_beekeeper.yml",

.read_api_definition <- function(config_file, rapid_file) {
readRDS(
fs::path(fs::path_dir(config_file), rapid_file)
path(path_dir(config_file), rapid_file)
)
}

Expand All @@ -34,10 +34,10 @@ generate_pkg <- function(config_file = "_beekeeper.yml",
)

data <- list(
api_title = nectar::stabilize_string(config$api_title),
api_abbr = nectar::stabilize_string(config$api_abbr),
base_url = nectar::stabilize_string(api_definition@servers@url),
pkg_agent = nectar::stabilize_string(pkg_agent)
api_title = stabilize_string(config$api_title),
api_abbr = stabilize_string(config$api_abbr),
base_url = stabilize_string(api_definition@servers@url),
pkg_agent = stabilize_string(pkg_agent)
)
data <- c(data, security_data)

Expand All @@ -56,11 +56,11 @@ generate_pkg <- function(config_file = "_beekeeper.yml",
}

.prepare_r <- function() {
usethis::use_directory("R")
usethis::use_testthat()
purrr::quietly(httptest2::use_httptest2)()
usethis::use_package("nectar")
usethis::use_package("beekeeper", type = "Suggests")
use_directory("R")
use_testthat()
quietly(use_httptest2)()
use_package("nectar")
use_package("beekeeper", type = "Suggests")
}

#' Use a template in this package
Expand All @@ -75,17 +75,17 @@ generate_pkg <- function(config_file = "_beekeeper.yml",
dir = c("R", "tests/testthat"),
data) {
dir <- match.arg(dir)
target <- usethis::proj_path(dir, template)
save_as <- fs::path_rel(target, usethis::proj_path())
target <- proj_path(dir, template)
save_as <- path_rel(target, proj_path())

usethis::use_template(
use_template(
template = template,
save_as = save_as,
data = data,
package = "beekeeper"
)
utils::capture.output({
styler::style_file(target)
capture.output({
style_file(target)
})

return(invisible(target))
Expand Down
6 changes: 3 additions & 3 deletions R/generate_pkg_agent.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#'
#' @export
generate_pkg_agent <- function(path = ".") {
if (!fs::is_dir(path) && fs::path_file(path) != "DESCRIPTION") {
path <- fs::path_dir(path) # nocov
if (!is_dir(path) && path_file(path) != "DESCRIPTION") {
path <- path_dir(path) # nocov
}
pkg_desc <- desc::desc(file = path)
pkg_desc <- desc(file = path)
pkg_name <- pkg_desc$get_field("Package")
pkg_url_glue <- ""
pkg_url <- pkg_desc$get_urls()
Expand Down
16 changes: 8 additions & 8 deletions R/use_beekeeper.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ use_beekeeper <- function(x,
config_file = "_beekeeper.yml",
rapid_file = "_beekeeper_rapid.rds") {
x <- as_rapid(x)
api_abbr <- nectar::stabilize_string(api_abbr)
config_file <- nectar::stabilize_string(config_file)
rapid_file <- nectar::stabilize_string(rapid_file)
api_abbr <- stabilize_string(api_abbr)
config_file <- stabilize_string(config_file)
rapid_file <- stabilize_string(rapid_file)
saveRDS(x, rapid_file)

use_build_ignore(c(config_file, rapid_file))

yaml::write_yaml(
write_yaml(
list(
api_title = x@info@title,
api_abbr = api_abbr,
api_version = x@info@version,
rapid_file = fs::path_rel(rapid_file, fs::path_dir(config_file)),
updated_on = as.character(lubridate::now(tzone = "UTC"))
rapid_file = path_rel(rapid_file, path_dir(config_file)),
updated_on = as.character(now(tzone = "UTC"))
),
file = config_file
)
return(invisible(config_file))
}

.read_config <- function(config_file = "_beekeeper.yml") {
config <- yaml::read_yaml(config_file)
config$updated_on <- lubridate::parse_date_time(
config <- read_yaml(config_file)
config$updated_on <- parse_date_time(
config$updated_on,
orders = c("ymd HMS", "ymd H", "ymd")
)
Expand Down
14 changes: 7 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#'
#' @return `TRUE` if the project is a package, `FALSE` if not.
#' @keywords internal
.is_pkg <- function(base_path = usethis::proj_get()) {
root_file <- rlang::try_fetch(
rprojroot::find_package_root_file(path = base_path),
.is_pkg <- function(base_path = proj_get()) {
root_file <- try_fetch(
find_package_root_file(path = base_path),
error = function(cnd) NULL
)
!is.null(root_file)
Expand All @@ -20,11 +20,11 @@
#'
#' @return `NULL`, invisibly.
#' @keywords internal
.assert_is_pkg <- function(base_path = usethis::proj_get()) {
.assert_is_pkg <- function(base_path = proj_get()) {
if (.is_pkg(base_path)) {
return(invisible(NULL))
}
cli::cli_abort(c(
cli_abort(c(
"Can't generate package files outside of a package.",
x = "{.path {base_path}} is not inside a package."
))
Expand All @@ -39,9 +39,9 @@
}

.collapse_comma <- function(x) {
glue::glue_collapse(x, sep = ", ")
glue_collapse(x, sep = ", ")
}

.collapse_comma_newline <- function(x) {
glue::glue_collapse(x, sep = ",\n")
glue_collapse(x, sep = ",\n")
}

0 comments on commit 614e0bf

Please sign in to comment.