Skip to content
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

feat: Use {cli} for console printing #104

Merged
merged 6 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ BugReports: https://github.com/posit-dev/r-shinylive/issues
Imports:
archive,
brio,
cli,
fs,
gh,
glue,
Expand All @@ -38,7 +39,8 @@ Imports:
renv,
rlang,
tools,
whisker
whisker,
withr
Suggests:
httpuv (>= 1.6.12),
pkgcache,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* `template_params` takes a list of parameters to be interpolated into the template. The default template include `title` (the title for the page with the exported app), `include_in_head` (HTML added to the `<head>` of the page), and `include_before_body` (HTML added just after `<body>`) and `include_after_body` (HTML added just after `</body>`).
* `template_dir` is the directory containing the template files. The default is the `export_template` directory of the shinylive assets being used for the export. Use `assets_info()` to locate installed shinylive assets where you can find the default template files.

* shinylive now uses `{cli}` for console printing. Console output can be suppressed via the global R option by calling `options(shinylive.quiet = TRUE)`. (#104)

* `export()` and `assets_info()` gain a `quiet` argument. In `export()`, `quiet` replaces the now-deprecated `verbose` option, which continues to work with a warning. (#104)

# shinylive 0.1.1

* Bump shinylive assets dependency to 0.2.3. (#38)
Expand Down
20 changes: 12 additions & 8 deletions R/app_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ read_app_files <- function(

cur_basename <- basename(cur_path)
if (cur_basename == "shinylive.js") {
message(
"Warning: Found shinylive.js in source directory '", curdur, "'.",
" Are you including a shinylive distribution in your app?"
)
cli::cli_warn(c(
"Warning: Found {.path shinylive.js} in source directory {.path {curdur}}.",
i = "Are you including a shinylive distribution in your app?"
))
}

# Get file content
Expand Down Expand Up @@ -157,9 +157,10 @@ write_app_json <- function(
destdir,
template_dir,
template_params = list(),
verbose = is_interactive()
quiet = getOption("shinylive.quiet", FALSE)
) {
verbose_print <- if (isTRUE(verbose)) message else list
local_quiet(quiet)

stopifnot(inherits(app_info, APP_INFO_CLASS))
# stopifnot(fs::dir_exists(destdir))
stopifnot(fs::dir_exists(template_dir))
Expand Down Expand Up @@ -205,12 +206,15 @@ write_app_json <- function(

app_json_output_file <- fs::path(app_destdir, "app.json")

verbose_print("Writing ", app_json_output_file, appendLF = FALSE)
cli_progress_step("Writing {.path {app_json_output_file}}")
jsonlite::write_json(
app_info$files,
path = app_json_output_file,
auto_unbox = TRUE,
pretty = FALSE
)
verbose_print(": ", fs::file_info(app_json_output_file)$size[1], " bytes")
cli_progress_done()
cli_alert_info("Wrote {.path {app_json_output_file}} ({fs::file_info(app_json_output_file)$size[1]} bytes)")

invisible(app_json_output_file)
}
86 changes: 46 additions & 40 deletions R/assets.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ assets_download <- function(
add = TRUE
)

message("Downloading shinylive assets v", version, "...")
cli_progress_step("Downloading shinylive assets {.field v{version}}")
req <- httr2::request(url)
req <- httr2::req_progress(req)
httr2::req_perform(req, path = tmp_targz)
message("") # Newline after progress bar

message("Unzipping to ", dir, "/")

cli_progress_step("Unzipping shinylive assets to {.path {dir}}")
fs::dir_create(dir)
archive::archive_extract(tmp_targz, dir)

invisible()

cli_progress_done()
invisible(dir)
}


Expand Down Expand Up @@ -108,20 +108,23 @@ install_local_helper <- function(
stopifnot(fs::dir_exists(assets_repo_dir))
repo_build_dir <- fs::path(assets_repo_dir, "build")
if (!fs::dir_exists(repo_build_dir)) {
stop("Assets repo build dir does not exist (`", repo_build_dir, "`).\nHave you called `make all` yet?")
cli::cli_abort(c(
"Assets repo build dir does not exist ({.path {repo_build_dir}}).",
i = "Have you called {.code make all} yet?"
))
}
target_dir <- assets_dir_impl(dir = dir, version = version)

unlink_path(target_dir)
install_fn(repo_build_dir, target_dir)

if (version != assets_version()) {
message(
"Warning: You are installing a local copy of shinylive assets that is not the same as the version used by the shinylive R package.",
"\nWarning: Unexpected behavior may occur!",
"\n\nNew assets version: ", version,
"\nSupported assets version: ", assets_version()
)
cli::cli_warn(c(
"You are installing a local copy of shinylive assets that is not the same as the version used by the shinylive R package.",
"Unexpected behavior may occur!",
x = "New assets version: {version}",
i = "Supported assets version: {assets_version()}"
))
}
}

Expand Down Expand Up @@ -204,13 +207,13 @@ assets_ensure <- function(
) {
stopifnot(length(list(...)) == 0)
if (!fs::dir_exists(dir)) {
message("Creating assets cache directory ", dir)
cli_alert_info("Creating assets cache directory ", dir)
fs::dir_create(dir)
}

assets_path <- assets_dir(version, dir = dir)
if (!fs::dir_exists(assets_path)) {
message("`", assets_path, "` assets directory does not exist.")
cli_alert_warning("{.path {assets_path}} assets directory does not exist.")
assets_download(url = url, version = version, dir = dir)
}

Expand Down Expand Up @@ -246,7 +249,7 @@ assets_cleanup <- function(
character(1)
)
if (assets_version() %in% versions) {
message("Keeping version ", assets_version())
cli_alert_info("Keeping version {assets_version()}")
versions <- setdiff(versions, assets_version())
}

Expand Down Expand Up @@ -286,10 +289,10 @@ assets_remove <- function(
lapply(versions, function(version) {
target_dir <- assets_dir_impl(dir = dir, version = version)
if (fs::dir_exists(target_dir)) {
message("Removing ", target_dir)
cli_progress_step("Removing {.path {target_dir}}")
unlink_path(target_dir)
} else {
message(target_dir, " folder does not exist")
cli_alert_warning("{.path {target_dir}} folder does not exist")
}
})

Expand Down Expand Up @@ -333,33 +336,36 @@ assets_dirs <- function(



#' @describeIn assets Prints information about the local shinylive
#' assets that have been installed.
#' @describeIn assets Prints information about the local shinylive assets that
#' have been installed. Invisibly returns a table of installed asset versions
#' and their associated paths.
#' @param quiet In `assets_info()`, if `quiet = TRUE`, the function will not
#' print the assets information to the console.
#' @export
assets_info <- function() {
assets_info <- function(quiet = FALSE) {
installed_versions <- assets_dirs()
if (length(installed_versions) == 0) {
installed_versions <- "(None)"
}

cat(
collapse(c(
paste0("shinylive R package version: ", SHINYLIVE_R_VERSION),
paste0("shinylive web assets version: ", assets_version()),
"",
"Local cached shinylive asset dir:",
collapse(" ", assets_cache_dir()),
"",
"Installed assets:",
if (assets_cache_dir_exists()) {
collapse(" ", installed_versions)
} else {
" (Cache dir does not exist)"
},
""
)),
sep = ""
)
local_quiet(quiet)

cli_text("shinylive R package version: {.field {SHINYLIVE_R_VERSION}}")
cli_text("shinylive web assets version: {.field {assets_version()}}")
cli_text("")
cli_text("Local cached shinylive asset dir:")
cli_bullets(c(">" = "{.path {assets_cache_dir()}}"))
cli_text("")
cli_text("Installed assets:")
if (assets_cache_dir_exists()) {
cli_installed <- c()
for (i in seq_along(installed_versions)) {
cli_installed <- c(cli_installed, c("*" = sprintf("{.path {installed_versions[%s]}}", i)))
}
cli_bullets(cli_installed)
} else {
cli_bullets("(Cache dir does not exist)")
}

versions <- vapply(
strsplit(installed_versions, "shinylive-", fixed = TRUE),
Expand All @@ -375,7 +381,7 @@ assets_info <- function() {

class(data) <- c("tbl_df", "tbl", "data.frame")

invisible(data)
if (is_quiet()) data else invisible(data)
}


Expand Down
47 changes: 47 additions & 0 deletions R/cli.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local_quiet <- function(quiet = FALSE, .envir = parent.frame()) {
withr::local_options(list(shinylive.quiet = quiet), .local_envir = .envir)
}

is_quiet <- function() {
isTRUE(getOption("shinylive.quiet", FALSE))
}

cli_alert <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_alert(..., .envir = .envir)
}

cli_alert_info <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_alert_info(..., .envir = .envir)
}

cli_alert_warning <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_alert_warning(..., .envir = .envir)
}

cli_alert_success <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_alert_success(..., .envir = .envir)
}

cli_progress_step <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_progress_step(..., .envir = .envir)
}

cli_progress_done <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_progress_done(..., .envir = .envir)
}

cli_text <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_text(..., .envir = .envir)
}

cli_bullets <- function(..., .envir = parent.frame()) {
if (is_quiet()) return(invisible())
cli::cli_bullets(..., .envir = .envir)
}
6 changes: 3 additions & 3 deletions R/deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ shinylive_common_dep_htmldep <- function(

# Put load-shinylive-sw.js in the scripts first
if (is.null(load_shinylive_dep)) {
stop("load-shinylive-sw.js not found in assets")
cli::cli_abort("{.path load-shinylive-sw.js} not found in assets")
}
scripts <- c(list(load_shinylive_dep), scripts)

Expand All @@ -216,7 +216,7 @@ shinylive_common_dep_htmldep <- function(
}
},
{
stop("unknown dep_type: ", dep_type)
cli::cli_abort("Unknown {.var dep_type}: {.val dep_type}")
}
)

Expand Down Expand Up @@ -297,7 +297,7 @@ shinylive_common_files <- function(
)
},
{
stop("unknown dep_type: ", dep_type)
cli::cli_abort("Unknown {.var dep_type}: {.val dep_type}")
}
)

Expand Down
Loading
Loading