-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start writing config pluck helpers #2514
Changes from all commits
524a095
6fe6d72
367dc85
31a6484
2670deb
ab42654
6d24a30
973c583
569783e
fc4360f
6c29b52
e05bc0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,126 @@ | ||
check_yaml_has <- function(missing, where, pkg, call = caller_env()) { | ||
if (length(missing) == 0) { | ||
return() | ||
} | ||
config_pluck <- function(pkg, path, default = NULL) { | ||
check_string(path, allow_empty = FALSE, .internal = TRUE) | ||
|
||
missing_components <- lapply(missing, function(x) c(where, x)) | ||
msg_flds <- purrr::map_chr(missing_components, paste, collapse = ".") | ||
where <- strsplit(path, ".", fixed = TRUE)[[1]] | ||
purrr::pluck(pkg$meta, !!!where, .default = default) | ||
} | ||
|
||
config_abort( | ||
pkg, | ||
"Can't find {cli::qty(missing)} component{?s} {.field {msg_flds}}.", | ||
call = call | ||
config_pluck_character <- function(pkg, | ||
path, | ||
default = character(), | ||
call = caller_env()) { | ||
x <- config_pluck(pkg, path, default) | ||
config_check_character( | ||
x, | ||
error_path = path, | ||
error_pkg = pkg, | ||
error_call = call | ||
) | ||
} | ||
|
||
yaml_character <- function(pkg, where) { | ||
x <- purrr::pluck(pkg$meta, !!!where) | ||
config_pluck_string <- function(pkg, | ||
path, | ||
default = "", | ||
call = caller_env()) { | ||
x <- config_pluck(pkg, path, default) | ||
config_check_string( | ||
x, | ||
error_path = path, | ||
error_pkg = pkg, | ||
error_call = call | ||
) | ||
} | ||
|
||
# checks --------------------------------------------------------------------- | ||
|
||
if (identical(x, list()) || is.null(x)) { | ||
config_check_character <- function(x, | ||
error_pkg, | ||
error_path, | ||
error_call = caller_env()) { | ||
if (is.character(x)) { | ||
x | ||
} else if (identical(x, list())) { | ||
character() | ||
} else if (is.character(x)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know 😄 But I think it must be some yaml special case where we want to handle an empty list. |
||
} else { | ||
config_abort_type( | ||
must_be = "a character vector", | ||
not = x, | ||
error_pkg = error_pkg, | ||
error_path = error_path, | ||
error_call = error_call | ||
) | ||
} | ||
} | ||
|
||
config_check_string <- function(x, | ||
error_pkg, | ||
error_path, | ||
error_call = caller_env()) { | ||
|
||
if (is_string(x)) { | ||
x | ||
} else { | ||
path <- paste0(where, collapse = ".") | ||
config_abort_type( | ||
must_be = "a string", | ||
not = x, | ||
error_pkg = error_pkg, | ||
error_path = error_path, | ||
error_call = error_call | ||
) | ||
} | ||
} | ||
|
||
config_abort_type <- function(must_be, not, error_pkg, error_path, error_call) { | ||
not_str <- obj_type_friendly(not) | ||
config_abort( | ||
error_pkg, | ||
"{.field {error_path}} must be {must_be}, not {not_str}.", | ||
call = error_call | ||
) | ||
|
||
} | ||
|
||
config_check_list <- function(x, | ||
has_names = NULL, | ||
error_pkg, | ||
error_path, | ||
error_call = caller_env()) { | ||
if (is_list(x)) { | ||
if (!is.null(has_names) && !all(has_name(x, has_names))) { | ||
missing <- setdiff(has_names, names(x)) | ||
config_abort( | ||
error_pkg, | ||
c( | ||
"{.field {error_path}} must have {cli::qty(has_names)} component{?s} {.str {has_names}}.", | ||
"{length(missing)} missing component{?s}: {.str {missing}}." | ||
), | ||
call = error_call | ||
) | ||
} else { | ||
x | ||
} | ||
} else { | ||
not <- obj_type_friendly(x) | ||
config_abort( | ||
pkg, | ||
"{.field {path}} must be a character vector.", | ||
call = caller_env() | ||
error_pkg, | ||
"{.field {error_path}} must be a list, not {not}.", | ||
call = error_call | ||
) | ||
} | ||
} | ||
|
||
# generic error --------------------------------------------------------------- | ||
|
||
config_abort <- function(pkg, | ||
message, | ||
..., | ||
call = caller_env(), | ||
.envir = caller_env()) { | ||
|
||
edit <- cli::format_inline("Edit {config_path(pkg)} to fix the problem.") | ||
|
||
cli::cli_abort( | ||
c( | ||
message, | ||
i = "Edit {config_path(pkg)} to fix the problem." | ||
), | ||
c(message, i = edit), | ||
..., | ||
call = call, | ||
.envir = .envir | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ init_site <- function(pkg = ".") { | |
|
||
copy_assets <- function(pkg = ".") { | ||
pkg <- as_pkgdown(pkg) | ||
template <- purrr::pluck(pkg$meta, "template", .default = list()) | ||
template <- config_pluck(pkg, "template") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default to |
||
|
||
# pkgdown assets | ||
if (!identical(template$default_assets, FALSE)) { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -56,7 +56,7 @@ bs_theme <- function(pkg = ".") { | |||||
bs_theme_rules <- function(pkg) { | ||||||
paths <- path_pkgdown("BS5", "assets", "pkgdown.scss") | ||||||
|
||||||
theme <- purrr::pluck(pkg, "meta", "template", "theme", .default = "arrow-light") | ||||||
theme <- config_pluck_string(pkg, "template.theme", default = "arrow-light") | ||||||
theme_path <- path_pkgdown("highlight-styles", paste0(theme, ".scss")) | ||||||
if (!file_exists(theme_path)) { | ||||||
cli::cli_abort(c( | ||||||
|
@@ -66,8 +66,8 @@ bs_theme_rules <- function(pkg) { | |||||
} | ||||||
paths <- c(paths, theme_path) | ||||||
|
||||||
package <- purrr::pluck(pkg, "meta", "template", "package") | ||||||
if (!is.null(package)) { | ||||||
package <- config_pluck_string(pkg, "template.package") | ||||||
if (package != "") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I find that style hard to read |
||||||
package_extra <- path_package_pkgdown("extra.scss", package, pkg$bs_version) | ||||||
if (file_exists(package_extra)) { | ||||||
paths <- c(paths, package_extra) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be called
config_pluck_chr()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and the string one,
config_pluck_str()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these are more matches to rlang's
check_
helpers than purrr's, because they're about selecting single values (that are sometimes vectors).