From f66c8376d8f25c653b5b42cd7c6dd7971f3edf0f Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 10 Oct 2023 21:38:00 +0200 Subject: [PATCH] feat: adding add_empty_file add_empty_file creates an empty file in the www directory in the spirit of the add_* functions issue #837 --- NAMESPACE | 4 +- R/add_files.R | 85 +++++++-------------------------- R/templates.R | 13 +++++ R/utils.R | 15 ++++++ man/add_files.Rd | 17 ++----- man/template.Rd | 3 ++ tests/testthat/helper-config.R | 6 ++- tests/testthat/test-add_files.R | 43 +++++++---------- 8 files changed, 76 insertions(+), 110 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a22aea95..cffd70e0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,6 @@ # Generated by roxygen2: do not edit by hand export(activate_js) -export(add_any_file) export(add_css_file) export(add_dockerfile) export(add_dockerfile_heroku) @@ -9,6 +8,7 @@ export(add_dockerfile_shinyproxy) export(add_dockerfile_with_renv) export(add_dockerfile_with_renv_heroku) export(add_dockerfile_with_renv_shinyproxy) +export(add_empty_file) export(add_fct) export(add_html_template) export(add_js_file) @@ -37,6 +37,7 @@ export(css_template) export(detach_all_attached) export(disable_autoload) export(document_and_reload) +export(empty_template) export(expect_html_equal) export(expect_running) export(expect_shinytag) @@ -103,6 +104,7 @@ importFrom(shiny,getShinyOption) importFrom(shiny,htmlTemplate) importFrom(shiny,includeScript) importFrom(shiny,tags) +importFrom(tools,file_ext) importFrom(utils,capture.output) importFrom(utils,file.edit) importFrom(utils,getFromNamespace) diff --git a/R/add_files.R b/R/add_files.R index 64384177..66e6098d 100644 --- a/R/add_files.R +++ b/R/add_files.R @@ -524,13 +524,13 @@ add_sass_file <- function( #' @export #' @rdname add_files #' @importFrom tools file_ext -add_any_file <- function( +add_empty_file <- function( name, pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, dir_create = TRUE, - template = golem::css_template, + template = golem::empty_template, ... ) { attempt::stop_if( @@ -538,10 +538,23 @@ add_any_file <- function( msg = "`name` is required" ) - check_name_length(name) + check_name_length_is_one(name) extension <- file_ext(name) + if (extension == "js"){ + warning("We've noticed you are trying to create a .js file. \nYou may want to use `add_js_file()` in future calls.") + } + if (extension == "css"){ + warning("We've noticed you are trying to create a .css file. \nYou may want to use `add_css_file()` in future calls.") + } + if (extension == "sass"){ + warning("We've noticed you are trying to create a .sass file. \nYou may want to use `add_sass_file()` in future calls.") + } + if (extension == "html"){ + warning("We've noticed you are trying to create a .html file. \nYou may want to use `add_html_template()` in future calls.") + } + name <- file_path_sans_ext(name) old <- setwd(fs_path_abs(pkg)) @@ -583,7 +596,7 @@ add_any_file <- function( template(path = where, ...) file_created_dance( where, - after_creation_message, + after_creation_message_generic, pkg, dir, name, @@ -597,70 +610,6 @@ add_any_file <- function( } } -#' @export -#' @rdname add_files -#' @importFrom tools file_ext -add_file <- function( - name, - pkg = get_golem_wd(), - dir = "inst/app/www", - open = TRUE, - dir_create = TRUE, - template = golem::css_template, - ... -) { - attempt::stop_if( - missing(name), - msg = "`name` is required" - ) - - check_name_length(name) - - extension <- file_ext(name) - - if (extension == "css") { - add_css_file( - name = name, - pkg = pkg, - dir = dir, - open = open, - dir_create = dir_create, - template = template, - ... - ) - } else if (extension == "js") { - add_js_file( - name = name, - pkg = pkg, - dir = dir, - open = open, - dir_create = dir_create, - template = template, - ... - ) - } else if (extension == "sass") { - add_sass_file( - name = name, - pkg = pkg, - dir = dir, - open = open, - dir_create = dir_create, - template = template, - ... - ) - } else { - add_any_file( - name = name, - pkg = pkg, - dir = dir, - open = open, - dir_create = dir_create, - template = template, - ... - ) - } -} - #' @export #' @rdname add_files add_html_template <- function( diff --git a/R/templates.R b/R/templates.R index 4ebdab80..4d2a2dbf 100644 --- a/R/templates.R +++ b/R/templates.R @@ -90,3 +90,16 @@ sass_template <- function( write_there(code) } + +#' @export +#' @rdname template +empty_template <- function( + path, + code = " " + ) { + write_there <- function(...) { + write(..., file = path, append = TRUE) + } + + write_there(code) +} diff --git a/R/utils.R b/R/utils.R index 71586c8e..48ad64e4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -233,6 +233,21 @@ desc_exist <- function(pkg) { ) } +after_creation_message_generic <- function( + pkg, + dir, + name +){ + do_if_unquiet({ + cli_cat_bullet( + sprintf( + "File %s created", + name + ) + ) + }) +} + after_creation_message_js <- function( pkg, dir, diff --git a/man/add_files.Rd b/man/add_files.Rd index 0b2ef64a..4d545967 100644 --- a/man/add_files.Rd +++ b/man/add_files.Rd @@ -7,8 +7,7 @@ \alias{add_js_output_binding} \alias{add_css_file} \alias{add_sass_file} -\alias{add_any_file} -\alias{add_file} +\alias{add_empty_file} \alias{add_html_template} \alias{add_partial_html_template} \alias{add_ui_server_files} @@ -74,23 +73,13 @@ add_sass_file( ... ) -add_any_file( +add_empty_file( name, pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, dir_create = TRUE, - template = golem::css_template, - ... -) - -add_file( - name, - pkg = get_golem_wd(), - dir = "inst/app/www", - open = TRUE, - dir_create = TRUE, - template = golem::css_template, + template = golem::empty_template, ... ) diff --git a/man/template.Rd b/man/template.Rd index 230101b2..f60113c9 100644 --- a/man/template.Rd +++ b/man/template.Rd @@ -5,6 +5,7 @@ \alias{js_template} \alias{css_template} \alias{sass_template} +\alias{empty_template} \title{Golem's default custom templates} \usage{ js_handler_template(path, name = "fun", code = " ") @@ -14,6 +15,8 @@ js_template(path, code = " ") css_template(path, code = " ") sass_template(path, code = " ") + +empty_template(path, code = " ") } \arguments{ \item{path}{The path to the JS script where this template will be written.} diff --git a/tests/testthat/helper-config.R b/tests/testthat/helper-config.R index 55c0ec26..409c11c9 100644 --- a/tests/testthat/helper-config.R +++ b/tests/testthat/helper-config.R @@ -22,8 +22,10 @@ remove_files <- function(path, pattern = NULL) { recursive = TRUE ) if (length(fls) > 0) { - res <- lapply(fls, function(x) { - if (file.exists(x)) unlink(x, force = TRUE) + try({ + res <- lapply(fls, function(x) { + if (file.exists(x)) unlink(x, force = TRUE) + }) }) } } diff --git a/tests/testthat/test-add_files.R b/tests/testthat/test-add_files.R index 672f15a1..5b349a04 100644 --- a/tests/testthat/test-add_files.R +++ b/tests/testthat/test-add_files.R @@ -2,11 +2,17 @@ expect_add_file <- function( fun, ext, pak, - fp + fp, + name ) { fun_nms <- deparse(substitute(fun)) + if (missing(name)){ + name <- rand_name() + } + if (fun_nms == "add_empty_file") { + name <- paste0(name, ".", ext) + } - name <- rand_name() # Be sure to remove all files in case there are remove_files("inst/app/www", ext) @@ -23,11 +29,12 @@ expect_add_file <- function( if (fun_nms == "add_js_output_binding") { name <- sprintf("output-%s", name) } + # Test that the file exists expect_exists( file.path( "inst/app/www", - paste0(name, ".", ext) + paste0(file_path_sans_ext(name), ".", ext) ) ) # Check that the file exsts @@ -40,7 +47,10 @@ expect_add_file <- function( expect_equal(l_ff, length(readLines(ff))) # Try another file in another dir - bis <- paste0(name, rand_name()) + bis <- paste0(file_path_sans_ext(name), rand_name()) + if (fun_nms == "add_empty_file") { + bis <- paste0(bis, ".", ext) + } fun(bis, pkg = pak, open = FALSE, dir = normalizePath(fp)) expect_exists(normalizePath(fp)) ff <- list.files( @@ -126,30 +136,13 @@ test_that("add_files", { pak = pkg, fp = fp ) - expect_add_file_without_ext( - add_file, - name = "random.R", - pak = pkg, - fp = fp - ) - expect_add_file_without_ext( - add_file, - name = "random", - pak = pkg, - fp = fp - ) - expect_add_file_without_ext( - add_any_file, - name = "random", - pak = pkg, - fp = fp - ) - expect_add_file_without_ext( - add_any_file, - name = "random.json", + expect_add_file( + add_empty_file, + ext = "txt", pak = pkg, fp = fp ) + }) })