-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
614e0bf
commit 2ad30d6
Showing
23 changed files
with
266 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.generate_call <- function(config, api_definition, pkg_agent, security_data) { | ||
touched_files <- c( | ||
.generate_call_r(config, api_definition, pkg_agent, security_data), | ||
.generate_call_test(config$api_abbr) | ||
) | ||
return(touched_files) | ||
} | ||
|
||
.generate_call_r <- function(config, api_definition, pkg_agent, security_data) { | ||
.bk_use_template( | ||
template = "010-call.R", | ||
data = list( | ||
api_title = config$api_title, | ||
api_abbr = config$api_abbr, | ||
base_url = api_definition@servers@url, | ||
pkg_agent = pkg_agent, | ||
has_security = security_data$has_security, | ||
security_arg_helps = security_data$security_arg_helps, | ||
security_signature = security_data$security_signature, | ||
security_arg_list = security_data$security_arg_list | ||
) | ||
) | ||
} | ||
|
||
.generate_call_test <- function(api_abbr) { | ||
.bk_use_template( | ||
template = "test-010-call.R", | ||
dir = "tests/testthat", | ||
data = list(api_abbr = api_abbr) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#' Error if not in package | ||
#' | ||
#' @inheritParams .is_pkg | ||
#' | ||
#' @return `NULL`, invisibly. | ||
#' @keywords internal | ||
.assert_is_pkg <- function(base_path = usethis::proj_get()) { | ||
if (.is_pkg(base_path)) { | ||
return(invisible(NULL)) | ||
} | ||
cli::cli_abort(c( | ||
"Can't generate package files outside of a package.", | ||
x = "{.path {base_path}} is not inside a package." | ||
)) | ||
} | ||
|
||
#' Check whether we're in a package | ||
#' | ||
#' Inspired by usethis:::is_package. | ||
#' | ||
#' @param base_path The root URL of the current project. | ||
#' | ||
#' @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), | ||
error = function(cnd) NULL | ||
) | ||
!is.null(root_file) | ||
} | ||
|
||
.read_config <- function(config_file = "_beekeeper.yml") { | ||
config <- yaml::read_yaml(config_file) | ||
config <- .stabilize_config(config) | ||
return(config) | ||
} | ||
|
||
.stabilize_config <- function(config) { | ||
config$api_title <- stabilize_string(config$api_title) | ||
config$api_abbr <- stabilize_string(config$api_abbr) | ||
config$api_version <- stabilize_string(config$api_version) | ||
config$rapid_file <- stabilize_string(config$rapid_file) | ||
config$updated_on <- parse_date_time( | ||
config$updated_on, | ||
orders = c("ymd HMS", "ymd H", "ymd") | ||
) | ||
return(config) | ||
} | ||
|
||
.read_api_definition <- function(config_file, rapid_file) { | ||
readRDS( | ||
path(path_dir(config_file), rapid_file) | ||
) | ||
} | ||
|
||
.prepare_r <- function() { | ||
use_directory("R") | ||
use_testthat() | ||
quietly(use_httptest2)() | ||
use_package("nectar") | ||
use_package("beekeeper", type = "Suggests") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' Use a template in this package | ||
#' | ||
#' @param template The name of the template. | ||
#' @param dir The directory where the file should be created. | ||
#' @param data A list of variables to apply to the template. | ||
#' | ||
#' @return The path to the generated or updated file, invisibly. | ||
#' @keywords internal | ||
.bk_use_template <- function(template, | ||
dir = c("R", "tests/testthat"), | ||
data) { | ||
dir <- match.arg(dir) | ||
target <- proj_path(dir, template) | ||
save_as <- path_rel(target, proj_path()) | ||
|
||
use_template( | ||
template = template, | ||
save_as = save_as, | ||
data = data, | ||
package = "beekeeper" | ||
) | ||
capture.output({ | ||
style_file(target) | ||
}) | ||
|
||
return(invisible(target)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Use a beekeeper config file to generate code | ||
#' | ||
#' Creates or updates package files based on the information in a beekeeper | ||
#' config file (generated by [use_beekeeper()] or manually). The files enforce | ||
#' an opinionated framework for API packages. | ||
#' | ||
#' @param config_file The path to a beekeeper yaml file. | ||
#' @param pkg_agent A string to identify this package, for use in the | ||
#' `user_agent` argument of [nectar::call_api()]. | ||
#' | ||
#' @return A character vector of paths to files that were added or updated, | ||
#' invisibly. | ||
#' @export | ||
generate_pkg <- function(config_file = "_beekeeper.yml", | ||
pkg_agent = generate_pkg_agent(config_file)) { | ||
.assert_is_pkg() | ||
config <- .read_config(config_file) | ||
api_definition <- .read_api_definition(config_file, config$rapid_file) | ||
.prepare_r() | ||
touched_files <- .generate_pkg_impl(config, api_definition, pkg_agent) | ||
return(invisible(touched_files)) | ||
} | ||
|
||
.generate_pkg_impl <- function(config, api_definition, pkg_agent) { | ||
security_data <- .generate_security( | ||
config$api_abbr, | ||
api_definition@components@security_schemes | ||
) | ||
call_files <- .generate_call(config, api_definition, pkg_agent, security_data) | ||
touched_files <- c(call_files, security_data$security_file_path) | ||
return(invisible(touched_files)) | ||
} |
Oops, something went wrong.