From 614e0bfb0c62d8bc70cd45bf519dffd24f9024e2 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Wed, 27 Mar 2024 09:00:31 -0500 Subject: [PATCH] Import external functions. --- DESCRIPTION | 2 +- NAMESPACE | 35 +++++++++++++++++++++++++++++++++++ R/as_bk_data.R | 28 ++++++++++++++-------------- R/beekeeper-package.R | 35 +++++++++++++++++++++++++++++++++++ R/generate.R | 30 +++++++++++++++--------------- R/generate_pkg_agent.R | 6 +++--- R/use_beekeeper.R | 16 ++++++++-------- R/utils.R | 14 +++++++------- 8 files changed, 118 insertions(+), 48 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7473bb8..e1217c8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,6 +29,7 @@ Imports: rprojroot, S7, snakecase, + stringr, styler, testthat, usethis, @@ -38,7 +39,6 @@ Suggests: covr, knitr, rmarkdown, - stringr, withr VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index a4e2a02..75a52a5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/as_bk_data.R b/R/as_bk_data.R index babba2b..bfbada9 100644 --- a/R/as_bk_data.R +++ b/R/as_bk_data.R @@ -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()) } @@ -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, @@ -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) @@ -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( @@ -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) { @@ -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, @@ -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()) diff --git a/R/beekeeper-package.R b/R/beekeeper-package.R index 7e53778..450d807 100644 --- a/R/beekeeper-package.R +++ b/R/beekeeper-package.R @@ -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 diff --git a/R/generate.R b/R/generate.R index 1b7213d..92eb0f5 100644 --- a/R/generate.R +++ b/R/generate.R @@ -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) ) } @@ -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) @@ -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 @@ -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)) diff --git a/R/generate_pkg_agent.R b/R/generate_pkg_agent.R index 6197ffe..c9bfd61 100644 --- a/R/generate_pkg_agent.R +++ b/R/generate_pkg_agent.R @@ -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() diff --git a/R/use_beekeeper.R b/R/use_beekeeper.R index 9f9683c..24c7115 100644 --- a/R/use_beekeeper.R +++ b/R/use_beekeeper.R @@ -25,20 +25,20 @@ 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 ) @@ -46,8 +46,8 @@ use_beekeeper <- function(x, } .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") ) diff --git a/R/utils.R b/R/utils.R index 7ffc685..42beea5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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) @@ -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." )) @@ -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") }