From d9c29d97c870fe8f4fda445536eef41858644b4d Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 3 Sep 2024 18:35:40 +0200 Subject: [PATCH 1/8] fix: use_internal_file doesnt strip the extension anymore --- R/use_files_internal_tools.R | 13 +++++++++---- R/use_files_shared_tools.R | 10 ++++++++-- tests/testthat/test-use_files.R | 4 ++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/R/use_files_internal_tools.R b/R/use_files_internal_tools.R index adfa4fe8..ccb01c91 100644 --- a/R/use_files_internal_tools.R +++ b/R/use_files_internal_tools.R @@ -36,16 +36,21 @@ perform_checks_and_copy_if_everything_is_ok <- function( ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) - name <- build_name( - name, - path_to_copy_from - ) if (is.null(file_type)) { + name <- build_name( + name, + path_to_copy_from, + with_ext = TRUE + ) where_to_copy_to <- fs_path( directory_to_copy_to, name ) } else { + name <- build_name( + name, + path_to_copy_from + ) check_file_has_the_correct_extension( path = path_to_copy_from, file_type diff --git a/R/use_files_shared_tools.R b/R/use_files_shared_tools.R index 2b86b6f5..59e6250e 100644 --- a/R/use_files_shared_tools.R +++ b/R/use_files_shared_tools.R @@ -1,12 +1,18 @@ build_name <- function( name = NULL, - url + url, + with_ext = FALSE ) { if (is.null(name)) { name <- basename(url) } check_name_length_is_one(name) - file_path_sans_ext(name) + if (with_ext) { + return(name) + } + return( + file_path_sans_ext(name) + ) } check_directory_exists <- function(dir){ diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R index 63ffc145..696a0dd2 100644 --- a/tests/testthat/test-use_files.R +++ b/tests/testthat/test-use_files.R @@ -66,6 +66,10 @@ test_that( expect_exists( path_to_file ) + expect_equal( + file_ext(path_to_file), + ext + ) }, funs_and_ext, names(funs_and_ext) From 70a2fe2b4bd3cbc00e77af649157260b967c6fca Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 4 Sep 2024 21:22:20 +0200 Subject: [PATCH 2/8] refactor: add_file now uses the use_internal_ logic --- R/add_files.R | 844 ++++++++++++-------------------- R/utils.R | 6 + tests/testthat/test-add_files.R | 54 +- 3 files changed, 333 insertions(+), 571 deletions(-) diff --git a/R/add_files.R b/R/add_files.R index b9bbf1a6..7b9ad8b7 100644 --- a/R/add_files.R +++ b/R/add_files.R @@ -3,6 +3,7 @@ #' These functions create files inside the `inst/app` folder. #' #' @inheritParams add_module +#' @param dir_create Deprecated. Will be removed in future versions and throws an error for now. #' @param dir Path to the dir where the file while be created. #' @param with_doc_ready For JS file - Should the default file include `$( document ).ready()`? #' @param template Function writing in the created file. @@ -34,7 +35,7 @@ add_js_file <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, with_doc_ready = TRUE, template = golem::js_template, ... @@ -44,56 +45,30 @@ add_js_file <- function( msg = "`name` is required" ) - check_name_length_is_one(name) - - name <- file_path_sans_ext(name) - - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) } - dir <- fs_path_abs(dir) - - where <- fs_path( - dir, - sprintf("%s.js", name) - ) + temp_js <- tempfile(fileext = ".js") + write_there <- write_there_builder(temp_js) - if (!fs_file_exists(where)) { - fs_file_create(where) - if (with_doc_ready) { - write_there <- function(...) { - write(..., file = where, append = TRUE) - } - write_there("$( document ).ready(function() {") - template(path = where, ...) - write_there("});") - } else { - template(path = where, ...) - } - file_created_dance( - where, - after_creation_message_js, - pkg, - dir, - name, - open - ) + if (with_doc_ready) { + write_there("$( document ).ready(function() {") + template(path = temp_js, ...) + write_there("});") } else { - file_already_there_dance( - where = where, - open_file = open - ) + template(path = temp_js, ...) } + + use_internal_js_file( + path = temp_js, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -103,58 +78,32 @@ add_js_handler <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, template = golem::js_handler_template, ... ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) - check_name_length_is_one(name) - - name <- file_path_sans_ext(name) - - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) } - dir <- fs_path_abs(dir) + temp_js <- tempfile(fileext = ".js") - where <- fs_path( - dir, - sprintf("%s.js", name) - ) + template(path = temp_js, ...) - if (!fs_file_exists(where)) { - fs_file_create(where) - - template(path = where, ...) - - file_created_dance( - where, - after_creation_message_js, - pkg, - dir, - name, - open_file = open - ) - } else { - file_already_there_dance( - where, - open_file = open - ) - } + use_internal_js_file( + path = temp_js, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -164,7 +113,7 @@ add_js_input_binding <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, initialize = FALSE, dev = FALSE, events = list( @@ -172,12 +121,16 @@ add_js_input_binding <- function( rate_policy = FALSE ) ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) - check_name_length_is_one(name) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) + } attempt::stop_if( length(events$name) == 0, @@ -189,120 +142,91 @@ add_js_input_binding <- function( msg = "Incomplete events list" ) + temp_js <- tempfile(fileext = ".js") + raw_name <- name name <- file_path_sans_ext( sprintf("input-%s", name) ) - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - - dir <- fs_path_abs(dir) - - where <- fs_path( - dir, - sprintf("%s.js", name) - ) - - if (!fs_file_exists(where)) { - fs_file_create(where) + temp_js <- tempfile(fileext = ".js") - write_there <- function(...) { - write(..., file = where, append = TRUE) - } + write_there <- write_there_builder(temp_js) - # If we find at least 1 event with a rate policy, we allow - # the getRatePolicy method - global_rate_policy <- sum(sapply(events$rate_policy, `[[`, 1)) > 0 + # If we find at least 1 event with a rate policy, we allow + # the getRatePolicy method + global_rate_policy <- sum(sapply(events$rate_policy, `[[`, 1)) > 0 - write_there(sprintf("var %s = new Shiny.InputBinding();", raw_name)) - write_there(sprintf("$.extend(%s, {", raw_name)) - # find - write_there(" find: function(scope) {") - write_there(" // JS logic $(scope).find('whatever')") + write_there(sprintf("var %s = new Shiny.InputBinding();", raw_name)) + write_there(sprintf("$.extend(%s, {", raw_name)) + # find + write_there(" find: function(scope) {") + write_there(" // JS logic $(scope).find('whatever')") + write_there(" },") + # initialize + if (initialize) { + write_there(" initialize: function(el) {") + write_there(" // optional part. Only if the input relies on a JS API with specific initialization.") write_there(" },") - # initialize - if (initialize) { - write_there(" initialize: function(el) {") - write_there(" // optional part. Only if the input relies on a JS API with specific initialization.") - write_there(" },") + } + # get value + write_there(" getValue: function(el) {") + if (dev) write_there(" console.log($(el));") + write_there(" // JS code to get value") + write_there(" },") + # set value + write_there(" setValue: function(el, value) {") + if (dev) write_there(" console.log('New value is: ' + value);") + write_there(" // JS code to set value") + write_there(" },") + # receive + write_there(" receiveMessage: function(el, data) {") + write_there(" // this.setValue(el, data);") + if (dev) write_there(" console.log('Updated ...');") + write_there(" },") + # subscribe + write_there(" subscribe: function(el, callback) {") + # list of event listeners + lapply(seq_along(events$name), function(i) { + write_there(sprintf(" $(el).on('%s.%s', function(e) {", events$name[i], raw_name)) + if (events$rate_policy[i]) { + write_there(" callback(true);") + } else { + write_there(" callback();") } - # get value - write_there(" getValue: function(el) {") - if (dev) write_there(" console.log($(el));") - write_there(" // JS code to get value") - write_there(" },") - # set value - write_there(" setValue: function(el, value) {") - if (dev) write_there(" console.log('New value is: ' + value);") - write_there(" // JS code to set value") - write_there(" },") - # receive - write_there(" receiveMessage: function(el, data) {") - write_there(" // this.setValue(el, data);") - if (dev) write_there(" console.log('Updated ...');") - write_there(" },") - # subscribe - write_there(" subscribe: function(el, callback) {") - # list of event listeners - lapply(seq_along(events$name), function(i) { - write_there(sprintf(" $(el).on('%s.%s', function(e) {", events$name[i], raw_name)) - if (events$rate_policy[i]) { - write_there(" callback(true);") - } else { - write_there(" callback();") - } - if (dev) write_there(" console.log('Subscribe ...');") - write_there(" });") - write_there("") - }) + if (dev) write_there(" console.log('Subscribe ...');") + write_there(" });") + write_there("") + }) + write_there(" },") + + # rate policy if any + if (global_rate_policy) { + write_there(" getRatePolicy: function() {") + write_there(" return {") + write_there(" policy: 'debounce',") + write_there(" delay: 250") + write_there(" };") write_there(" },") - - # rate policy if any - if (global_rate_policy) { - write_there(" getRatePolicy: function() {") - write_there(" return {") - write_there(" policy: 'debounce',") - write_there(" delay: 250") - write_there(" };") - write_there(" },") - } - - # unsubscribe - write_there(" unsubscribe: function(el) {") - write_there(sprintf(" $(el).off('.%s');", raw_name)) - write_there(" }") - - # end - write_there("});") - write_there(sprintf("Shiny.inputBindings.register(%s, 'shiny.whatever');", raw_name)) - - - file_created_dance( - where, - after_creation_message_js, - pkg, - dir, - name, - open_file = open - ) - } else { - file_already_there_dance( - where, - open_file = open - ) } + + # unsubscribe + write_there(" unsubscribe: function(el) {") + write_there(sprintf(" $(el).off('.%s');", raw_name)) + write_there(" }") + + # end + write_there("});") + write_there(sprintf("Shiny.inputBindings.register(%s, 'shiny.whatever');", raw_name)) + + use_internal_js_file( + path = temp_js, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -312,14 +236,18 @@ add_js_output_binding <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE + dir_create ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) - check_name_length_is_one(name) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) + } raw_name <- name @@ -327,64 +255,31 @@ add_js_output_binding <- function( sprintf("output-%s", name) ) - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" + temp_js <- tempfile(fileext = ".js") + + write_there <- write_there_builder(temp_js) + + write_there(sprintf("var %s = new Shiny.OutputBinding();", raw_name)) + write_there(sprintf("$.extend(%s, {", raw_name)) + # find + write_there(" find: function(scope) {") + write_there(" // JS logic $(scope).find('whatever')") + write_there(" },") + # renderValue + write_there(" renderValue: function(el, data) {") + write_there(" // JS logic") + write_there(" }") + # end + write_there("});") + write_there(sprintf("Shiny.outputBindings.register(%s, 'shiny.whatever');", raw_name)) + + use_internal_js_file( + path = temp_js, + name = name, + pkg = pkg, + dir = dir, + open = open ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - - dir <- fs_path_abs(dir) - - where <- fs_path( - dir, - sprintf("%s.js", name) - ) - - if (!fs_file_exists(where)) { - fs_file_create(where) - - write_there <- function(...) { - write(..., file = where, append = TRUE) - } - - # write in the file! - - write_there(sprintf("var %s = new Shiny.OutputBinding();", raw_name)) - write_there(sprintf("$.extend(%s, {", raw_name)) - # find - write_there(" find: function(scope) {") - write_there(" // JS logic $(scope).find('whatever')") - write_there(" },") - # renderValue - write_there(" renderValue: function(el, data) {") - write_there(" // JS logic") - write_there(" }") - # end - write_there("});") - write_there(sprintf("Shiny.outputBindings.register(%s, 'shiny.whatever');", raw_name)) - - - file_created_dance( - where, - after_creation_message_js, - pkg, - dir, - name, - open_file = open - ) - } else { - file_already_there_dance( - where, - open_file = open - ) - } } #' @export @@ -394,59 +289,31 @@ add_css_file <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, template = golem::css_template, ... ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) - check_name_length_is_one(name) - - name <- file_path_sans_ext(name) - - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) } - dir <- fs_path_abs(dir) + temp_css <- tempfile(fileext = ".css") + template(path = temp_css, ...) - where <- fs_path( - dir, - sprintf( - "%s.css", - name - ) + use_internal_css_file( + path = temp_css, + name = name, + pkg = pkg, + dir = dir, + open = open ) - - if (!fs_file_exists(where)) { - fs_file_create(where) - template(path = where, ...) - file_created_dance( - where, - after_creation_message_css, - pkg, - dir, - name, - open - ) - } else { - file_already_there_dance( - where = where, - open_file = open - ) - } } #' @export @@ -456,99 +323,73 @@ add_sass_file <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, template = golem::sass_template, ... ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) - check_name_length_is_one(name) - - name <- file_path_sans_ext(name) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) + } - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) + temp_js <- tempfile(fileext = ".sass") + template(path = temp_js, ...) - dir_created <- create_if_needed( - dir, - type = "directory" + add_sass_code_to_dev_script( + dir = dir, + name = name ) - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - - dir_abs <- fs_path_abs(dir) - - where <- fs_path( - dir_abs, - sprintf( + use_internal_file( + path = temp_js, + name = sprintf( "%s.sass", name - ) + ), + pkg = pkg, + dir = dir, + open = open ) - if (!fs_file_exists(where)) { - fs_file_create(where) - template(path = where, ...) - file_created_dance( - where, - after_creation_message_sass, - pkg, - dir_abs, - name, - open - ) - - add_sass_code( - where = where, - dir = dir, - name = name - ) - + on.exit({ cat_green_tick( "After running the compilation, your CSS file will be automatically link in `golem_add_external_resources()`." ) - } else { - file_already_there_dance( - where = where, - open_file = open - ) - } + }) } -add_sass_code <- function(where, dir, name) { - if (fs_file_exists(where)) { - if (fs_file_exists("dev/run_dev.R")) { - lines <- readLines("dev/run_dev.R") - new_lines <- append( - x = lines, - values = c( - "# Sass code compilation", - sprintf( - 'sass::sass(input = sass::sass_file("%s/%s.sass"), output = "%s/%s.css", cache = NULL)', - dir, - name, - dir, - name - ), - "" +add_sass_code_to_dev_script <- function(dir, name) { + if (fs_file_exists("dev/run_dev.R")) { + lines <- readLines("dev/run_dev.R") + new_lines <- append( + x = lines, + values = c( + "# Sass code compilation", + sprintf( + 'sass::sass(input = sass::sass_file("%s/%s.sass"), output = "%s/%s.css", cache = NULL)', + dir, + name, + dir, + name ), - after = 0 - ) - writeLines( - text = new_lines, - con = "dev/run_dev.R" - ) + "" + ), + after = 0 + ) + writeLines( + text = new_lines, + con = "dev/run_dev.R" + ) - cat_green_tick( - "Code added in run_dev.R to compile your Sass file to CSS file." - ) - } + cat_green_tick( + "Code added in run_dev.R to compile your Sass file to CSS file." + ) } } @@ -560,85 +401,86 @@ add_empty_file <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE, + dir_create, template = golem::empty_template, ... ) { - attempt::stop_if( + stop_if( missing(name), msg = "`name` is required" ) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) + } + + 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.") + return( + add_js_file( + name = name, + pkg = pkg, + dir = dir, + open = open, + template = template, + ... + ) + ) } 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.") + return( + add_css_file( + name = name, + pkg = pkg, + dir = dir, + open = open, + template = template, + ... + ) + ) } 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.") + return( + add_sass_file( + name = name, + pkg = pkg, + dir = dir, + open = open, + template = template, + ... + ) + ) } 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)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - - dir <- fs_path_abs(dir) - - if (extension != "") { - where <- fs_path( - dir, - sprintf( - "%s.%s", - name, - extension - ) - ) - } else { - where <- fs_path( - dir, - sprintf( - "%s", - name + return( + add_html_template( + name = name, + pkg = pkg, + dir = dir, + open = open ) ) } - if (!fs_file_exists(where)) { - fs_file_create(where) - template(path = where, ...) - file_created_dance( - where, - after_creation_message_generic, - pkg, - dir, - name, - open - ) - } else { - file_already_there_dance( - where = where, - open_file = open - ) - } + temp_file <- tempfile() + template(path = temp_file, ...) + use_internal_file( + path = temp_file, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -648,67 +490,40 @@ add_html_template <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE + dir_create ) { - name <- file_path_sans_ext(name) - - check_name_length_is_one(name) - - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) } - dir <- fs_path_abs(dir) + temp_html <- tempfile(fileext = ".html") + write_there <- write_there_builder(temp_html) - where <- fs_path( - dir, + write_there("") + write_there("") + write_there(" ") + write_there( sprintf( - "%s.html", - name + " %s", + get_golem_name() ) ) - - if (!fs_file_exists(where)) { - fs_file_create(where) - write_there <- function(...) write(..., file = where, append = TRUE) - write_there("") - write_there("") - write_there(" ") - write_there( - sprintf( - " %s", - get_golem_name() - ) - ) - write_there(" ") - write_there(" ") - write_there(" {{ body }}") - write_there(" ") - write_there("") - write_there("") - file_created_dance( - where, - after_creation_message_html_template, - pkg, - dir, - name, - open - ) - } else { - file_already_there_dance( - where = where, - open_file = open - ) - } + write_there(" ") + write_there(" ") + write_there(" {{ body }}") + write_there(" ") + write_there("") + write_there("") + + use_internal_html_template( + path = temp_html, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -718,55 +533,28 @@ add_partial_html_template <- function( pkg = get_golem_wd(), dir = "inst/app/www", open = TRUE, - dir_create = TRUE + dir_create ) { - name <- file_path_sans_ext(name) - check_name_length_is_one(name) - - old <- setwd(fs_path_abs(pkg)) - on.exit(setwd(old)) - - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - - dir <- fs_path_abs(dir) - - where <- fs_path( - dir, - sprintf( - "%s.html", - name - ) - ) - - if (!fs_file_exists(where)) { - fs_file_create(where) - write_there <- function(...) write(..., file = where, append = TRUE) - write_there("
") - write_there(" {{ content }}") - write_there("
") - write_there("") - file_created_dance( - where, - after_creation_message_html_template, - pkg, - dir, - name, - open - ) - } else { - file_already_there_dance( - where = where, - open_file = open + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." ) } + temp_html <- tempfile(fileext = ".html") + write_there <- write_there_builder(temp_html) + + write_there("
") + write_there(" {{ content }}") + write_there("
") + write_there("") + + use_internal_html_template( + path = temp_html, + name = name, + pkg = pkg, + dir = dir, + open = open + ) } #' @export @@ -774,24 +562,20 @@ add_partial_html_template <- function( add_ui_server_files <- function( pkg = get_golem_wd(), dir = "inst/app", - dir_create = TRUE + dir_create ) { + if (!missing(dir_create)) { + cli_cli_abort( + "The dir_create argument is deprecated." + ) + } .Deprecated(msg = "This function will be deprecated in a future version of {golem}.\nPlease comment on https://github.com/ThinkR-open/golem/issues/445 if you want it to stay.") old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) - dir_created <- create_if_needed( - dir, - type = "directory" - ) - - if (!dir_created) { - cat_dir_necessary() - return(invisible(FALSE)) - } - dir <- fs_path_abs(dir) + check_directory_exists(dir) # UI where <- fs_path(dir, "ui.R") @@ -799,7 +583,7 @@ add_ui_server_files <- function( if (!fs_file_exists(where)) { fs_file_create(where) - write_there <- function(...) write(..., file = where, append = TRUE) + write_there <- write_there_builder(where) pkg <- get_golem_name() @@ -821,7 +605,7 @@ add_ui_server_files <- function( if (!fs_file_exists(where)) { fs_file_create(where) - write_there <- function(...) write(..., file = where, append = TRUE) + write_there <- write_there_builder(where) write_there( sprintf( diff --git a/R/utils.R b/R/utils.R index b6cbe18a..7ee94b9b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -239,3 +239,9 @@ check_name_syntax <- function(name) { ) } } + +write_there_builder <- function(file_to_write_to) { + function(...) { + write(..., file = file_to_write_to, append = TRUE) + } +} \ No newline at end of file diff --git a/tests/testthat/test-add_files.R b/tests/testthat/test-add_files.R index c84ba2fe..453029eb 100644 --- a/tests/testthat/test-add_files.R +++ b/tests/testthat/test-add_files.R @@ -1,23 +1,3 @@ -test_already_there_dance <- function( - fun, - filename, - template -) { - testthat::with_mocked_bindings( - file_already_there_dance = function(...) { - return(TRUE) - }, - { - expect_true( - fun( - filename, - open = FALSE - ) - ) - } - ) -} - test_add_file <- function( fun, file_with_extension, @@ -30,25 +10,24 @@ test_add_file <- function( file_sans_extension <- tools::file_path_sans_ext( file_with_extension ) + output <- file.path( + "inst/app/www", + sprintf( + "%s%s", + output_suffix, + file_with_extension + ) + ) + unlink(output) expect_error(fun()) fun( file_sans_extension, open = FALSE ) expect_exists( - file.path( - "inst/app/www", - sprintf( - "%s%s", - output_suffix, - file_with_extension - ) - ) - ) - test_already_there_dance( - fun, - file_sans_extension + output ) + unlink(output) if (with_template) { fun( sprintf( @@ -85,16 +64,17 @@ test_add_file <- function( all_lines ) ) + unlink(output) } } test_that("add_file works", { run_quietly_in_a_dummy_golem({ + test_add_file( add_js_file, "add_js_file.js" ) - test_add_file( add_js_handler, "add_js_handler.js" @@ -139,10 +119,6 @@ test_that("add_file works", { "template.html" ) ) - test_already_there_dance( - add_html_template, - "template.html" - ) add_partial_html_template( open = FALSE @@ -154,10 +130,6 @@ test_that("add_file works", { "partial_template.html" ) ) - test_already_there_dance( - add_partial_html_template, - "partial_template.html" - ) for ( file in c( From 2413210e1d1c7a1227cf23afb08b8a2cb1498a0a Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 4 Sep 2024 21:22:39 +0200 Subject: [PATCH 3/8] refactor: refactoring write_there --- R/add_dockerfiles_renv.R | 4 +--- R/add_r_files.R | 4 +--- R/add_rstudio_files.R | 4 +--- R/modules_fn.R | 8 ++------ R/templates.R | 21 +++++---------------- 5 files changed, 10 insertions(+), 31 deletions(-) diff --git a/R/add_dockerfiles_renv.R b/R/add_dockerfiles_renv.R index 75e5f645..44ccc262 100644 --- a/R/add_dockerfiles_renv.R +++ b/R/add_dockerfiles_renv.R @@ -416,9 +416,7 @@ add_dockerfile_with_renv_heroku <- function( "README" ) - write_there <- function(...) { - write(..., file = readme_output, append = TRUE) - } + write_there <- write_there_builder(readme_output) write_there("From your command line, run:\n") diff --git a/R/add_r_files.R b/R/add_r_files.R index f5e8d1b3..1708267b 100644 --- a/R/add_r_files.R +++ b/R/add_r_files.R @@ -177,9 +177,7 @@ append_roxygen_comment <- function( ext, export = FALSE ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) file_type <- " " diff --git a/R/add_rstudio_files.R b/R/add_rstudio_files.R index f2a4eb64..a02e31aa 100644 --- a/R/add_rstudio_files.R +++ b/R/add_rstudio_files.R @@ -23,9 +23,7 @@ add_rstudio_files <- function( if (!fs_file_exists(where)) { fs_file_create(where) - write_there <- function(..., here = where) { - write(..., here, append = TRUE) - } + write_there <- write_there_builder(where) usethis_use_build_ignore(basename(where)) usethis_use_build_ignore("rsconnect") diff --git a/R/modules_fn.R b/R/modules_fn.R index b7c295c9..036e1b92 100644 --- a/R/modules_fn.R +++ b/R/modules_fn.R @@ -194,9 +194,7 @@ module_template <- function( ph_server = " ", ... ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there(sprintf("#' %s UI Function", name)) write_there("#'") @@ -324,9 +322,7 @@ use_module_test <- function( if (!fs_file_exists(path)) { fs_file_create(path) - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there("testServer(") write_there(sprintf(" mod_%s_server,", name)) diff --git a/R/templates.R b/R/templates.R index 4d2a2dbf..9181155b 100644 --- a/R/templates.R +++ b/R/templates.R @@ -39,9 +39,7 @@ js_handler_template <- function( name = "fun", code = " " ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there("$( document ).ready(function() {") write_there( @@ -58,9 +56,7 @@ js_template <- function( path, code = " " ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there(code) } @@ -71,10 +67,7 @@ css_template <- function( path, code = " " ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } - + write_there <- write_there_builder(path) write_there(code) } @@ -84,9 +77,7 @@ sass_template <- function( path, code = " " ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there(code) } @@ -97,9 +88,7 @@ empty_template <- function( path, code = " " ) { - write_there <- function(...) { - write(..., file = path, append = TRUE) - } + write_there <- write_there_builder(path) write_there(code) } From 03a924d1f4eac2dc9cf48377ae9722a29b7247dc Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 4 Sep 2024 21:31:44 +0200 Subject: [PATCH 4/8] style: styler --- R/add_dockerfiles.R | 219 +++++++++--------- R/add_r_files.R | 14 +- R/add_rstudio_files.R | 8 +- R/after_creation_msg.R | 2 +- R/boostrap_base.R | 2 +- R/boostrap_cli.R | 1 - R/bootstrap_rlang.R | 2 +- R/bootstrap_testthat.R | 2 +- R/bootstrap_usethis.R | 1 - R/cats.R | 2 +- R/create_golem.R | 48 ++-- R/golem-yaml-get.R | 1 - R/golem-yaml-set.R | 9 +- R/install_dev_deps.R | 9 +- R/templates.R | 2 +- R/test_helpers.R | 2 - R/use_favicon.R | 4 - R/use_files_external_tools.R | 2 +- R/use_files_internal.R | 12 +- R/use_files_internal_tools.R | 2 +- R/use_files_shared_tools.R | 2 +- R/use_readme.R | 2 - R/use_recommended.R | 3 +- R/utils.R | 2 +- R/with_opt.R | 1 - README.Rmd | 13 +- tests/testthat/setup.R | 1 - tests/testthat/test-add_files.R | 3 +- tests/testthat/test-config.R | 5 +- tests/testthat/test-create_golem.R | 5 +- tests/testthat/test-enable_roxygenize.R | 43 ++-- tests/testthat/test-file_endings.R | 3 +- tests/testthat/test-globals.R | 1 - tests/testthat/test-golem-yaml-get.R | 1 - tests/testthat/test-golem-yaml-utils.R | 5 +- tests/testthat/test-golem_welcome_page.R | 1 - tests/testthat/test-install_dev_deps.R | 7 +- tests/testthat/test-is_golem.R | 10 +- tests/testthat/test-reload.R | 12 +- tests/testthat/test-set_golem_options.R | 1 - tests/testthat/test-use_favicon.R | 57 ++--- tests/testthat/test-use_files.R | 1 - .../testthat/test-use_files_external_tools.R | 1 - .../testthat/test-use_files_internal_tools.R | 2 +- tests/testthat/test-use_recommended.R | 37 ++- tests/testthat/test-utils.R | 4 +- tests/testthat/test-with_opt.R | 8 +- vignettes/a_start.Rmd | 14 +- vignettes/f_extending_golem.Rmd | 22 +- 49 files changed, 301 insertions(+), 310 deletions(-) diff --git a/R/add_dockerfiles.R b/R/add_dockerfiles.R index fe1ec582..6565c7a1 100644 --- a/R/add_dockerfiles.R +++ b/R/add_dockerfiles.R @@ -83,25 +83,26 @@ talk_once <- function(.f, msg = "") { #' } #' @return The `{dockerfiler}` object, invisibly. add_dockerfile <- function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - port = 80, - host = "0.0.0.0", - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + port = 80, + host = "0.0.0.0", + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL +) { add_dockerfile_( path = path, output = output, @@ -122,26 +123,26 @@ add_dockerfile <- function( add_dockerfile_ <- talk_once( function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - port = 80, - host = "0.0.0.0", - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL -) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + port = 80, + host = "0.0.0.0", + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL + ) { where <- fs_path(pkg, output) usethis_use_build_ignore( @@ -190,23 +191,24 @@ add_dockerfile_ <- talk_once( #' @export #' @rdname dockerfiles add_dockerfile_shinyproxy <- function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL +) { add_dockerfile_shinyproxy_( path = path, output = output, @@ -225,23 +227,24 @@ add_dockerfile_shinyproxy <- function( add_dockerfile_shinyproxy_ <- talk_once( function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL + ) { where <- fs_path(pkg, output) usethis_use_build_ignore(output) @@ -282,23 +285,24 @@ add_dockerfile_shinyproxy_ <- talk_once( #' @export #' @rdname dockerfiles add_dockerfile_heroku <- function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL +) { add_dockerfile_heroku_( path = path, output = output, @@ -317,23 +321,24 @@ add_dockerfile_heroku <- function( add_dockerfile_heroku_ <- talk_once( function( - path = "DESCRIPTION", - output = "Dockerfile", - pkg = get_golem_wd(), - from = paste0( - "rocker/verse:", - R.Version()$major, - ".", - R.Version()$minor - ), - as = NULL, - sysreqs = TRUE, - repos = c(CRAN = "https://cran.rstudio.com/"), - expand = FALSE, - open = TRUE, - update_tar_gz = TRUE, - build_golem_from_source = TRUE, - extra_sysreqs = NULL) { + path = "DESCRIPTION", + output = "Dockerfile", + pkg = get_golem_wd(), + from = paste0( + "rocker/verse:", + R.Version()$major, + ".", + R.Version()$minor + ), + as = NULL, + sysreqs = TRUE, + repos = c(CRAN = "https://cran.rstudio.com/"), + expand = FALSE, + open = TRUE, + update_tar_gz = TRUE, + build_golem_from_source = TRUE, + extra_sysreqs = NULL + ) { where <- fs_path(pkg, output) usethis_use_build_ignore(output) diff --git a/R/add_r_files.R b/R/add_r_files.R index 1708267b..bb9d409e 100644 --- a/R/add_r_files.R +++ b/R/add_r_files.R @@ -143,12 +143,12 @@ add_utils <- function( #' @rdname file_creation #' @export add_r6 <- function( - name, - module = NULL, - pkg = get_golem_wd(), - open = TRUE, - dir_create = TRUE, - with_test = FALSE + name, + module = NULL, + pkg = get_golem_wd(), + open = TRUE, + dir_create = TRUE, + with_test = FALSE ) { add_r_files( name, @@ -209,7 +209,7 @@ append_roxygen_comment <- function( } if (file_type == "R6") { write_there(paste0(name, " <- R6::R6Class(")) - write_there(paste0(" classname = '", name, "',")) + write_there(paste0(" classname = '", name, "',")) write_there(" public = list()") write_there(")") } diff --git a/R/add_rstudio_files.R b/R/add_rstudio_files.R index a02e31aa..ac2cbd5e 100644 --- a/R/add_rstudio_files.R +++ b/R/add_rstudio_files.R @@ -7,7 +7,7 @@ add_rstudio_files <- function( "Shiny Server", "ShinyApps.io" ) - ) { +) { service <- match.arg(service) where <- fs_path(pkg, "app.R") @@ -111,7 +111,7 @@ add_rstudio_files <- function( add_positconnect_file <- function( pkg = get_golem_wd(), open = TRUE - ) { +) { add_rstudio_files( pkg = pkg, open = open, @@ -138,7 +138,7 @@ add_rstudioconnect_file <- function( add_shinyappsio_file <- function( pkg = get_golem_wd(), open = TRUE - ) { +) { add_rstudio_files( pkg = pkg, open = open, @@ -151,7 +151,7 @@ add_shinyappsio_file <- function( add_shinyserver_file <- function( pkg = get_golem_wd(), open = TRUE - ) { +) { add_rstudio_files( pkg = pkg, open = open, diff --git a/R/after_creation_msg.R b/R/after_creation_msg.R index 3c866f77..e6a28c5a 100644 --- a/R/after_creation_msg.R +++ b/R/after_creation_msg.R @@ -144,4 +144,4 @@ file_already_there_dance <- function( where = where, open_file = open_file ) -} \ No newline at end of file +} diff --git a/R/boostrap_base.R b/R/boostrap_base.R index 57f13d5f..ca2a8423 100644 --- a/R/boostrap_base.R +++ b/R/boostrap_base.R @@ -6,4 +6,4 @@ curl_get_headers <- function(...) { utils_download_file <- function(...) { utils::download.file(...) -} \ No newline at end of file +} diff --git a/R/boostrap_cli.R b/R/boostrap_cli.R index c605e2b4..c10a80fc 100644 --- a/R/boostrap_cli.R +++ b/R/boostrap_cli.R @@ -51,4 +51,3 @@ cli_cli_abort <- function(message) { stop(message) } } - diff --git a/R/bootstrap_rlang.R b/R/bootstrap_rlang.R index 52c662a3..c678ae62 100644 --- a/R/bootstrap_rlang.R +++ b/R/bootstrap_rlang.R @@ -1,4 +1,4 @@ # For mocking purposes rlang_is_interactive <- rlang::is_interactive -rlang_is_installed <- rlang::is_installed \ No newline at end of file +rlang_is_installed <- rlang::is_installed diff --git a/R/bootstrap_testthat.R b/R/bootstrap_testthat.R index 342f53d5..07cab358 100644 --- a/R/bootstrap_testthat.R +++ b/R/bootstrap_testthat.R @@ -13,4 +13,4 @@ check_testthat_installed <- function( testthat_expect_snapshot <- function(...) { check_testthat_installed() testthat::expect_snapshot(...) -} \ No newline at end of file +} diff --git a/R/bootstrap_usethis.R b/R/bootstrap_usethis.R index a92ad3a1..062ae005 100644 --- a/R/bootstrap_usethis.R +++ b/R/bootstrap_usethis.R @@ -1,4 +1,3 @@ - # All the fns here check that {usethis} is installed # before doing anything. check_usethis_installed <- function(reason = "for project and file manipulation.") { diff --git a/R/cats.R b/R/cats.R index 1d7813e2..fe65b622 100644 --- a/R/cats.R +++ b/R/cats.R @@ -121,4 +121,4 @@ cat_automatically_linked <- function() { "File automatically linked in `golem_add_external_resources()`." ) }) -} \ No newline at end of file +} diff --git a/R/create_golem.R b/R/create_golem.R index 60219bdc..2cbb60b0 100644 --- a/R/create_golem.R +++ b/R/create_golem.R @@ -33,35 +33,35 @@ replace_package_name <- function( copy_golem_skeleton_and_replace_name <- function( path_to_golem, package_name -){ - cli_cat_rule("Copying package skeleton") - from <- golem_sys("shinyexample") - - # Copy over whole directory - fs_dir_copy( - path = from, - new_path = path_to_golem, - overwrite = TRUE - ) +) { + cli_cat_rule("Copying package skeleton") + from <- golem_sys("shinyexample") + + # Copy over whole directory + fs_dir_copy( + path = from, + new_path = path_to_golem, + overwrite = TRUE + ) - # Listing copied files ***from source directory*** - copied_files <- list.files( - path = from, - full.names = FALSE, - all.files = TRUE, - recursive = TRUE - ) + # Listing copied files ***from source directory*** + copied_files <- list.files( + path = from, + full.names = FALSE, + all.files = TRUE, + recursive = TRUE + ) - replace_package_name( - copied_files, - package_name, - path_to_golem - ) - cat_green_tick("Copied app skeleton") + replace_package_name( + copied_files, + package_name, + path_to_golem + ) + cat_green_tick("Copied app skeleton") } # For mocking in tests -here_set_here <- function(...){ +here_set_here <- function(...) { here::set_here(...) } diff --git a/R/golem-yaml-get.R b/R/golem-yaml-get.R index 613e720f..ddd3590e 100644 --- a/R/golem-yaml-get.R +++ b/R/golem-yaml-get.R @@ -1,4 +1,3 @@ - #' @importFrom config get get_golem_things <- function( value, diff --git a/R/golem-yaml-set.R b/R/golem-yaml-set.R index 4430b7f3..9ecd2732 100644 --- a/R/golem-yaml-set.R +++ b/R/golem-yaml-set.R @@ -4,7 +4,7 @@ set_golem_wd <- function( golem_wd = golem::pkg_path(), pkg = golem::pkg_path(), talkative = TRUE - ) { +) { if ( golem_wd == "golem::pkg_path()" | normalizePath(golem_wd) == normalizePath(golem::pkg_path()) @@ -34,7 +34,6 @@ set_golem_name <- function( talkative = TRUE, old_name = golem::pkg_name() ) { - name <- force(name) pkg <- force(pkg) old_name <- force(old_name) @@ -87,7 +86,7 @@ set_golem_name <- function( path = path ) - if (old_name != name){ + if (old_name != name) { cli_cli_alert_info( sprintf("Please note that the old name %s might still be in some places, for example in the ./docs folder.", old_name) ) @@ -158,7 +157,7 @@ set_golem_version <- function( version = golem::pkg_version(), pkg = golem::pkg_path(), talkative = TRUE - ) { +) { path <- fs_path_abs(pkg) # Changing in YAML @@ -184,4 +183,4 @@ set_golem_version <- function( ) invisible(version) -} \ No newline at end of file +} diff --git a/R/install_dev_deps.R b/R/install_dev_deps.R index aced76df..700afc16 100644 --- a/R/install_dev_deps.R +++ b/R/install_dev_deps.R @@ -32,9 +32,10 @@ #' #' @return Used for side-effects install_dev_deps <- function( - dev_deps, - force_install = FALSE, - ...) { + dev_deps, + force_install = FALSE, + ... +) { if (!force_install) { if (!interactive()) { # In non interactive mode with force_install turned to FALSE, @@ -62,7 +63,7 @@ install_dev_deps <- function( } } - if (missing(dev_deps)){ + if (missing(dev_deps)) { dev_deps <- getFromNamespace("dev_deps", "golem") } diff --git a/R/templates.R b/R/templates.R index 9181155b..393a7d5c 100644 --- a/R/templates.R +++ b/R/templates.R @@ -87,7 +87,7 @@ sass_template <- function( empty_template <- function( path, code = " " - ) { +) { write_there <- write_there_builder(path) write_there(code) diff --git a/R/test_helpers.R b/R/test_helpers.R index 0a850269..ba8068b2 100644 --- a/R/test_helpers.R +++ b/R/test_helpers.R @@ -195,5 +195,3 @@ expect_running <- function( testthat::expect_true(shinyproc$is_alive()) shinyproc$kill() } - - diff --git a/R/use_favicon.R b/R/use_favicon.R index c9784726..7bb7e3e0 100644 --- a/R/use_favicon.R +++ b/R/use_favicon.R @@ -1,5 +1,3 @@ - - #' Add a favicon to your shinyapp #' #' @param path Path to your favicon file (.ico or .png) @@ -163,5 +161,3 @@ favicon <- function( ) ) } - - diff --git a/R/use_files_external_tools.R b/R/use_files_external_tools.R index f820310f..c750ee90 100644 --- a/R/use_files_external_tools.R +++ b/R/use_files_external_tools.R @@ -79,4 +79,4 @@ perform_checks_and_download_if_everything_is_ok <- function( open_file = open, catfun = cat_downloaded ) -} \ No newline at end of file +} diff --git a/R/use_files_internal.R b/R/use_files_internal.R index c3ae061b..8758dec9 100644 --- a/R/use_files_internal.R +++ b/R/use_files_internal.R @@ -8,8 +8,7 @@ use_internal_js_file <- function( open = FALSE, dir_create ) { - - if (!missing(dir_create)){ + if (!missing(dir_create)) { cli_cli_abort( "The dir_create argument is deprecated." ) @@ -36,8 +35,7 @@ use_internal_css_file <- function( open = FALSE, dir_create ) { - - if (!missing(dir_create)){ + if (!missing(dir_create)) { cli_cli_abort( "The dir_create argument is deprecated." ) @@ -64,8 +62,7 @@ use_internal_html_template <- function( open = FALSE, dir_create ) { - - if (!missing(dir_create)){ + if (!missing(dir_create)) { cli_cli_abort( "The dir_create argument is deprecated." ) @@ -92,8 +89,7 @@ use_internal_file <- function( open = FALSE, dir_create ) { - - if (!missing(dir_create)){ + if (!missing(dir_create)) { cli_cli_abort( "The dir_create argument is deprecated." ) diff --git a/R/use_files_internal_tools.R b/R/use_files_internal_tools.R index ccb01c91..9553f634 100644 --- a/R/use_files_internal_tools.R +++ b/R/use_files_internal_tools.R @@ -82,4 +82,4 @@ perform_checks_and_copy_if_everything_is_ok <- function( open_file = open, catfun = cat_copied ) -} \ No newline at end of file +} diff --git a/R/use_files_shared_tools.R b/R/use_files_shared_tools.R index 59e6250e..08ba86ed 100644 --- a/R/use_files_shared_tools.R +++ b/R/use_files_shared_tools.R @@ -15,7 +15,7 @@ build_name <- function( ) } -check_directory_exists <- function(dir){ +check_directory_exists <- function(dir) { if (!fs_dir_exists(dir)) { cli_cli_abort( sprintf( diff --git a/R/use_readme.R b/R/use_readme.R index 05d4491c..6b99e502 100644 --- a/R/use_readme.R +++ b/R/use_readme.R @@ -57,11 +57,9 @@ check_overwrite <- function(overwrite, tmp_pth) { stop("README.Rmd already exists. Set `overwrite = TRUE` to overwrite.") } } - } generate_readme_tmpl <- function(pkg_name) { - tmp_file <- readLines( golem_sys("utils/empty_readme.Rmd") ) diff --git a/R/use_recommended.R b/R/use_recommended.R index e82770ec..73259d41 100644 --- a/R/use_recommended.R +++ b/R/use_recommended.R @@ -70,8 +70,7 @@ use_recommended_tests <- function( golem_sys( "utils", "test-golem-recommended.R" - ) - , + ), fs_path(pkg, "tests", "testthat"), overwrite = TRUE ) diff --git a/R/utils.R b/R/utils.R index 7ee94b9b..7ca9956a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -244,4 +244,4 @@ write_there_builder <- function(file_to_write_to) { function(...) { write(..., file = file_to_write_to, append = TRUE) } -} \ No newline at end of file +} diff --git a/R/with_opt.R b/R/with_opt.R index 2810e65f..39f43932 100644 --- a/R/with_opt.R +++ b/R/with_opt.R @@ -126,4 +126,3 @@ get_golem_options <- function(which = NULL) { getShinyOption("golem_options")[[which]] } } - diff --git a/README.Rmd b/README.Rmd index ba3ed742..4bb67e54 100644 --- a/README.Rmd +++ b/README.Rmd @@ -23,18 +23,18 @@ link_to_branch_svg <- paste0( "https://codecov.io/gh/ThinkR-open/golem/branch/", name_branch, "/graph/badge.svg" - ) +) link_to_branch_html <- paste0( "https://app.codecov.io/github/ThinkR-open/golem/tree/", name_branch - ) +) ``` ```{r, echo = FALSE, results = "asis"} -cat('[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)') -cat('[![R-CMD-check](https://github.com/ThinkR-open/golem/workflows/R-CMD-check/badge.svg)](https://github.com/ThinkR-open/golem/actions)') +cat("[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)") +cat("[![R-CMD-check](https://github.com/ThinkR-open/golem/workflows/R-CMD-check/badge.svg)](https://github.com/ThinkR-open/golem/actions)") cat(paste0("\n[![Coverage status](", link_to_branch_svg, ")](", link_to_branch_html, ")")) -cat('[![CRAN status](https://www.r-pkg.org/badges/version/golem)](https://cran.r-project.org/package=golem)') +cat("[![CRAN status](https://www.r-pkg.org/badges/version/golem)](https://cran.r-project.org/package=golem)") ``` @@ -114,7 +114,8 @@ unloadNamespace("golem") ``` ```{r error = TRUE} -Sys.setenv("NOT_CRAN" = TRUE);covr::package_coverage() +Sys.setenv("NOT_CRAN" = TRUE) +covr::package_coverage() ``` ## CoC diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 0802dc22..2c72198c 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -305,7 +305,6 @@ test_check(\"shinyexample\")", } run_quietly_in_a_dummy_golem <- function(expr) { - on.exit( { unlink( diff --git a/tests/testthat/test-add_files.R b/tests/testthat/test-add_files.R index 453029eb..e55ee390 100644 --- a/tests/testthat/test-add_files.R +++ b/tests/testthat/test-add_files.R @@ -70,7 +70,6 @@ test_add_file <- function( test_that("add_file works", { run_quietly_in_a_dummy_golem({ - test_add_file( add_js_file, "add_js_file.js" @@ -184,4 +183,4 @@ test_that( } }) } -) \ No newline at end of file +) diff --git a/tests/testthat/test-config.R b/tests/testthat/test-config.R index e5974d34..60e07a7f 100644 --- a/tests/testthat/test-config.R +++ b/tests/testthat/test-config.R @@ -94,9 +94,10 @@ test_that("config finding works", { ) }) -test_that("ask_golem_creation_upon_config works",{ +test_that("ask_golem_creation_upon_config works", { testthat::with_mocked_bindings( - yesno = paste,{ + yesno = paste, + { expect_snapshot( ask_golem_creation_upon_config( "/home/golem" diff --git a/tests/testthat/test-create_golem.R b/tests/testthat/test-create_golem.R index 0a2e5977..4c6eb719 100644 --- a/tests/testthat/test-create_golem.R +++ b/tests/testthat/test-create_golem.R @@ -60,9 +60,10 @@ test_that("create_golem works", { test_that("create_golem_gui works", { testthat::with_mocked_bindings( - create_golem = function(...){ + create_golem = function(...) { return(TRUE) - }, { + }, + { expect_error( create_golem_gui() ) diff --git a/tests/testthat/test-enable_roxygenize.R b/tests/testthat/test-enable_roxygenize.R index eeb4985d..30fb4e66 100644 --- a/tests/testthat/test-enable_roxygenize.R +++ b/tests/testthat/test-enable_roxygenize.R @@ -6,27 +6,28 @@ create_temp_rproj <- function() { test_that( - "enable_roxygenize function updates the .Rproj file correctly" -, { - # Create a temporary .Rproj file - temp_rproj <- create_temp_rproj() - - withr::with_options( - c("usethis.quiet" = TRUE), - { - enable_roxygenize(path = temp_rproj) - } - ) - - updated_content <- yaml::read_yaml(temp_rproj) - - expect_equal( - updated_content[["PackageRoxygenize"]], - "rd,collate,namespace" - ) - - unlink(temp_rproj) -}) + "enable_roxygenize function updates the .Rproj file correctly", + { + # Create a temporary .Rproj file + temp_rproj <- create_temp_rproj() + + withr::with_options( + c("usethis.quiet" = TRUE), + { + enable_roxygenize(path = temp_rproj) + } + ) + + updated_content <- yaml::read_yaml(temp_rproj) + + expect_equal( + updated_content[["PackageRoxygenize"]], + "rd,collate,namespace" + ) + + unlink(temp_rproj) + } +) test_that("enable_roxygenize function prints correct messages", { temp_rproj <- create_temp_rproj() diff --git a/tests/testthat/test-file_endings.R b/tests/testthat/test-file_endings.R index f37edf62..c93648b9 100644 --- a/tests/testthat/test-file_endings.R +++ b/tests/testthat/test-file_endings.R @@ -3,7 +3,8 @@ # Throws a warning, we'll expect_silent() the readLines() test_that( - "All files have a proper EOF",{ + "All files have a proper EOF", + { new_golem <- perform_inside_a_new_golem(function() { return(getwd()) }) diff --git a/tests/testthat/test-globals.R b/tests/testthat/test-globals.R index 59a35e2f..c1940d13 100644 --- a/tests/testthat/test-globals.R +++ b/tests/testthat/test-globals.R @@ -6,5 +6,4 @@ test_that("set_golem_global function sets global variables correctly", { set_golem_global("another_var", 123) expect_equal(.golem_globals$another_var, 123) rm("another_var", envir = .golem_globals) - }) diff --git a/tests/testthat/test-golem-yaml-get.R b/tests/testthat/test-golem-yaml-get.R index 9a9cb33b..66f2c9e1 100644 --- a/tests/testthat/test-golem-yaml-get.R +++ b/tests/testthat/test-golem-yaml-get.R @@ -26,5 +26,4 @@ test_that("get_golem_things works", { "0.0.0.9000" ) }) - }) diff --git a/tests/testthat/test-golem-yaml-utils.R b/tests/testthat/test-golem-yaml-utils.R index b929f37e..24fc359a 100644 --- a/tests/testthat/test-golem-yaml-utils.R +++ b/tests/testthat/test-golem-yaml-utils.R @@ -49,7 +49,8 @@ test_that( ) test_that( - "amend_golem_config works", { + "amend_golem_config works", + { run_quietly_in_a_dummy_golem({ amend_golem_config( "this", @@ -88,4 +89,4 @@ test_that( ) }) } -) \ No newline at end of file +) diff --git a/tests/testthat/test-golem_welcome_page.R b/tests/testthat/test-golem_welcome_page.R index 6dd6fd56..bf3bbafd 100644 --- a/tests/testthat/test-golem_welcome_page.R +++ b/tests/testthat/test-golem_welcome_page.R @@ -1,5 +1,4 @@ test_that("golem_welcome_page works", { - html <- golem_welcome_page() expect_true(inherits(html, "shiny.tag.list")) diff --git a/tests/testthat/test-install_dev_deps.R b/tests/testthat/test-install_dev_deps.R index 51b23453..81d30b0e 100644 --- a/tests/testthat/test-install_dev_deps.R +++ b/tests/testthat/test-install_dev_deps.R @@ -37,13 +37,14 @@ test_that("install_dev_deps works", { test_that("check_dev_deps_are_installed works", { withr::with_temp_libpaths({ testthat::with_mocked_bindings( - rlang_is_installed = function(...){ + rlang_is_installed = function(...) { return(FALSE) - },{ + }, + { expect_message( check_dev_deps_are_installed() ) } ) }) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-is_golem.R b/tests/testthat/test-is_golem.R index 21fc025b..63681c93 100644 --- a/tests/testthat/test-is_golem.R +++ b/tests/testthat/test-is_golem.R @@ -9,11 +9,11 @@ test_that("is_golem works", { value = TRUE ) run_quietly_in_a_dummy_golem({ - for (file in to_create) { - file.create( - file - ) - } + for (file in to_create) { + file.create( + file + ) + } expect_true( is_golem(".") ) diff --git a/tests/testthat/test-reload.R b/tests/testthat/test-reload.R index bb9d1fac..af4333b0 100644 --- a/tests/testthat/test-reload.R +++ b/tests/testthat/test-reload.R @@ -1,9 +1,10 @@ test_that("detach_all_attached works", { testthat::with_mocked_bindings( .package = "base", - detach = function(...){ + detach = function(...) { return(TRUE) - },{ + }, + { res <- detach_all_attached() } ) @@ -42,12 +43,13 @@ test_that("test document_and_reload", { 3 ) testthat::with_mocked_bindings( - roxygen2_roxygenise = function(...){ + roxygen2_roxygenise = function(...) { return(TRUE) }, - pkgload_load_all = function(...){ + pkgload_load_all = function(...) { return(TRUE) - },{ + }, + { run_quietly_in_a_dummy_golem({ expect_null( document_and_reload( diff --git a/tests/testthat/test-set_golem_options.R b/tests/testthat/test-set_golem_options.R index 43b043a7..62a33505 100644 --- a/tests/testthat/test-set_golem_options.R +++ b/tests/testthat/test-set_golem_options.R @@ -1,5 +1,4 @@ test_that("set_golem_options works", { - run_quietly_in_a_dummy_golem({ testthat::with_mocked_bindings( usethis_proj_set = function(...) { diff --git a/tests/testthat/test-use_favicon.R b/tests/testthat/test-use_favicon.R index 1a79f3ba..97beae49 100644 --- a/tests/testthat/test-use_favicon.R +++ b/tests/testthat/test-use_favicon.R @@ -13,35 +13,36 @@ test_that("use_favicon works", { ), destfile ) - },{ - use_favicon() - expect_true( - file.exists("inst/app/www/favicon.ico") - ) + }, + { + use_favicon() + expect_true( + file.exists("inst/app/www/favicon.ico") + ) - lapply( - c( - "test.jpeg", - "test.bmp", - "test.gif", - "test.tiff" - ), - function(.x) { - expect_error( - use_favicon(path = .x) - ) - } - ) - remove_favicon() - expect_false( - file.exists("inst/app/www/favicon.ico") - ) - use_favicon( - path = "https://fr.wikipedia.org//static/favicon/wikipedia.ico" - ) - expect_true( - file.exists("inst/app/www/favicon.ico") - ) + lapply( + c( + "test.jpeg", + "test.bmp", + "test.gif", + "test.tiff" + ), + function(.x) { + expect_error( + use_favicon(path = .x) + ) + } + ) + remove_favicon() + expect_false( + file.exists("inst/app/www/favicon.ico") + ) + use_favicon( + path = "https://fr.wikipedia.org//static/favicon/wikipedia.ico" + ) + expect_true( + file.exists("inst/app/www/favicon.ico") + ) } ) }) diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R index 696a0dd2..693408a9 100644 --- a/tests/testthat/test-use_files.R +++ b/tests/testthat/test-use_files.R @@ -50,7 +50,6 @@ test_that( ) mapply( function(fun, ext) { - if (ext != "txt") { expect_error( fun( diff --git a/tests/testthat/test-use_files_external_tools.R b/tests/testthat/test-use_files_external_tools.R index 1498dcf9..207d35ec 100644 --- a/tests/testthat/test-use_files_external_tools.R +++ b/tests/testthat/test-use_files_external_tools.R @@ -1,4 +1,3 @@ - test_that("check_url_has_the_correct_extension(url, where) works", { testthat::expect_snapshot(error = TRUE, { check_url_has_the_correct_extension( diff --git a/tests/testthat/test-use_files_internal_tools.R b/tests/testthat/test-use_files_internal_tools.R index 12a3eef8..1f6acc2b 100644 --- a/tests/testthat/test-use_files_internal_tools.R +++ b/tests/testthat/test-use_files_internal_tools.R @@ -34,4 +34,4 @@ test_that("check_url_has_the_correct_extension(url, where) works", { "js" ) ) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-use_recommended.R b/tests/testthat/test-use_recommended.R index 522f1994..94cccd30 100644 --- a/tests/testthat/test-use_recommended.R +++ b/tests/testthat/test-use_recommended.R @@ -73,25 +73,24 @@ test_that( } ) }) - # Testing adding testthat if processx - # is not available - run_quietly_in_a_dummy_golem({ - testthat::with_mocked_bindings( - usethis_use_testthat = function() { - dir.create("tests") - dir.create("tests/testthat") - file.create( - "tests/testthat.R" - ) - }, - { - - use_recommended_tests( - pkg = ".", - spellcheck = FALSE - ) - } + # Testing adding testthat if processx + # is not available + run_quietly_in_a_dummy_golem({ + testthat::with_mocked_bindings( + usethis_use_testthat = function() { + dir.create("tests") + dir.create("tests/testthat") + file.create( + "tests/testthat.R" ) - }) + }, + { + use_recommended_tests( + pkg = ".", + spellcheck = FALSE + ) + } + ) + }) } ) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 36c0daad..96f9b0c2 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -55,7 +55,7 @@ test_that( rlang_is_interactive = function() { return(TRUE) }, - ask_golem_creation_file = function(path,type) { + ask_golem_creation_file = function(path, type) { return(TRUE) }, code = { @@ -70,7 +70,7 @@ test_that( rlang_is_interactive = function() { return(TRUE) }, - ask_golem_creation_file = function(path,type) { + ask_golem_creation_file = function(path, type) { return(TRUE) }, code = { diff --git a/tests/testthat/test-with_opt.R b/tests/testthat/test-with_opt.R index dc86cd2e..d47746dc 100644 --- a/tests/testthat/test-with_opt.R +++ b/tests/testthat/test-with_opt.R @@ -1,4 +1,3 @@ - test_that( "with_golem_options() returns the maintenance page", { @@ -32,10 +31,11 @@ test_that( ) test_that( - "We can explicitely print the golem app", { + "We can explicitely print the golem app", + { res <- testthat::with_mocked_bindings( .package = "base", - print = function(...){ + print = function(...) { return("Kilian Jornet") }, code = { @@ -85,7 +85,7 @@ test_that( expect_true( inherits( this_shouldnt_be_printed_but_be_a_shiny_app_obj, - "shiny.appobj" + "shiny.appobj" ) ) } diff --git a/vignettes/a_start.Rmd b/vignettes/a_start.Rmd index 8da26f29..b5daa0cc 100644 --- a/vignettes/a_start.Rmd +++ b/vignettes/a_start.Rmd @@ -118,7 +118,7 @@ golem::fill_desc( email = "AUTHOR@MAIL.COM", # Your email role = c("aut", "cre"), # Your role (here author/creator) set_options = TRUE # Set the global golem options - ), + ), repo_url = NULL, # The URL of the GitHub repo (optional), pkg_version = "0.0.0.9000" # The version of the package containing the app ) @@ -167,10 +167,10 @@ golem::use_recommended_deps() + If you want to change the default favicon: ```{r} - # Remove current favicon - golem::remove_favicon() - # Add a new one - golem::use_favicon(path = "path/to/favicon") +# Remove current favicon +golem::remove_favicon() +# Add a new one +golem::use_favicon(path = "path/to/favicon") ``` Note that you can add an URL, and the favicon will be downloaded to the `inst/app/www` folder. @@ -179,8 +179,8 @@ Note that you can add an URL, and the favicon will be downloaded to the `inst/ap + Utils: these two functions add two files with additional helper functions for your `{golem}` project. They can be used along the process of building your app: ```{r} - golem::use_utils_ui(with_test = TRUE) - golem::use_utils_server(with_test = TRUE) +golem::use_utils_ui(with_test = TRUE) +golem::use_utils_server(with_test = TRUE) ``` For a detailed description of the generated functions see the respective files `R/golem_utils_server.R` and `R/golem_utils_ui.R` for which also default tests diff --git a/vignettes/f_extending_golem.Rmd b/vignettes/f_extending_golem.Rmd index 834ddf4a..adf8f5a8 100644 --- a/vignettes/f_extending_golem.Rmd +++ b/vignettes/f_extending_golem.Rmd @@ -250,11 +250,12 @@ In order to use your own page, you need to pass either an `html_document` or a ` ```{r} run_app <- function( - onStart = NULL, - options = list(), - enableBookmarking = NULL, - uiPattern = "/", - ...) { + onStart = NULL, + options = list(), + enableBookmarking = NULL, + uiPattern = "/", + ... +) { with_golem_options( app = shinyApp( ui = app_ui, @@ -279,11 +280,12 @@ or: ```{r} run_app <- function( - onStart = NULL, - options = list(), - enableBookmarking = NULL, - uiPattern = "/", - ...) { + onStart = NULL, + options = list(), + enableBookmarking = NULL, + uiPattern = "/", + ... +) { with_golem_options( app = shinyApp( ui = app_ui, From f84ce27beadd6bf77a59e710b04e57f78460fea8 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 6 Sep 2024 12:31:31 +0200 Subject: [PATCH 5/8] chore: this check is not necessary anymore --- R/add_files.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/add_files.R b/R/add_files.R index 7b9ad8b7..ad6715c1 100644 --- a/R/add_files.R +++ b/R/add_files.R @@ -416,9 +416,6 @@ add_empty_file <- function( ) } - - check_name_length_is_one(name) - extension <- file_ext(name) if (extension == "js") { From ae2acad9ffa52295150c4cdbc7e825f8397629bd Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 6 Sep 2024 13:07:05 +0200 Subject: [PATCH 6/8] refactor: one message for deprecation --- R/add_files.R | 40 +++++++-------------------- R/{after_creation_msg.R => cli_msg.R} | 6 ++++ R/use_files_external.R | 16 +++-------- R/use_files_internal.R | 16 +++-------- 4 files changed, 24 insertions(+), 54 deletions(-) rename R/{after_creation_msg.R => cli_msg.R} (96%) diff --git a/R/add_files.R b/R/add_files.R index ad6715c1..62d0db31 100644 --- a/R/add_files.R +++ b/R/add_files.R @@ -46,9 +46,7 @@ add_js_file <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_js <- tempfile(fileext = ".js") @@ -88,9 +86,7 @@ add_js_handler <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_js <- tempfile(fileext = ".js") @@ -127,9 +123,7 @@ add_js_input_binding <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } attempt::stop_if( @@ -244,9 +238,7 @@ add_js_output_binding <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } raw_name <- name @@ -299,9 +291,7 @@ add_css_file <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_css <- tempfile(fileext = ".css") @@ -333,9 +323,7 @@ add_sass_file <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_js <- tempfile(fileext = ".sass") @@ -411,9 +399,7 @@ add_empty_file <- function( ) if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } extension <- file_ext(name) @@ -490,9 +476,7 @@ add_html_template <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_html <- tempfile(fileext = ".html") @@ -533,9 +517,7 @@ add_partial_html_template <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } temp_html <- tempfile(fileext = ".html") write_there <- write_there_builder(temp_html) @@ -562,9 +544,7 @@ add_ui_server_files <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } .Deprecated(msg = "This function will be deprecated in a future version of {golem}.\nPlease comment on https://github.com/ThinkR-open/golem/issues/445 if you want it to stay.") diff --git a/R/after_creation_msg.R b/R/cli_msg.R similarity index 96% rename from R/after_creation_msg.R rename to R/cli_msg.R index e6a28c5a..367e7201 100644 --- a/R/after_creation_msg.R +++ b/R/cli_msg.R @@ -145,3 +145,9 @@ file_already_there_dance <- function( open_file = open_file ) } + +cli_abort_dir_create <- function(){ + cli_cli_abort( + "The dir_create argument is deprecated." + ) +} \ No newline at end of file diff --git a/R/use_files_external.R b/R/use_files_external.R index 3d77a55a..f4553131 100644 --- a/R/use_files_external.R +++ b/R/use_files_external.R @@ -25,9 +25,7 @@ use_external_js_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_download_if_everything_is_ok( @@ -52,9 +50,7 @@ use_external_css_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_download_if_everything_is_ok( @@ -79,9 +75,7 @@ use_external_html_template <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_download_if_everything_is_ok( @@ -106,9 +100,7 @@ use_external_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_download_if_everything_is_ok( diff --git a/R/use_files_internal.R b/R/use_files_internal.R index 8758dec9..df70f924 100644 --- a/R/use_files_internal.R +++ b/R/use_files_internal.R @@ -9,9 +9,7 @@ use_internal_js_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_copy_if_everything_is_ok( @@ -36,9 +34,7 @@ use_internal_css_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_copy_if_everything_is_ok( @@ -63,9 +59,7 @@ use_internal_html_template <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_copy_if_everything_is_ok( @@ -90,9 +84,7 @@ use_internal_file <- function( dir_create ) { if (!missing(dir_create)) { - cli_cli_abort( - "The dir_create argument is deprecated." - ) + cli_abort_dir_create() } perform_checks_and_copy_if_everything_is_ok( From 78b062ec2e48dddfac5085914d630db6dbb901c3 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 6 Sep 2024 13:07:20 +0200 Subject: [PATCH 7/8] test: adding tests that it fails if the dir_create arg is provided --- tests/testthat/test-add_files.R | 7 +++++++ tests/testthat/test-use_files.R | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/tests/testthat/test-add_files.R b/tests/testthat/test-add_files.R index e55ee390..11466c40 100644 --- a/tests/testthat/test-add_files.R +++ b/tests/testthat/test-add_files.R @@ -20,6 +20,13 @@ test_add_file <- function( ) unlink(output) expect_error(fun()) + expect_error( + fun( + file_sans_extension, + open = FALSE, + dir_create = TRUE + ) + ) fun( file_sans_extension, open = FALSE diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R index 693408a9..bc86f0da 100644 --- a/tests/testthat/test-use_files.R +++ b/tests/testthat/test-use_files.R @@ -16,6 +16,13 @@ test_that( mapply( function(fun, ext) { unlink(paste0("this.", ext)) + expect_error({ + fun( + url = paste0("this.", ext), + pkg = ".", + dir_create = TRUE + ) + }) path_to_file <- fun( url = paste0("this.", ext), pkg = "." From c94029d400fa34ac6ce80c9de11e9305baf4c3c3 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 6 Sep 2024 13:13:41 +0200 Subject: [PATCH 8/8] doc: news update & version --- DESCRIPTION | 2 +- NEWS.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 70bd028c..6373f3d7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: golem Title: A Framework for Robust Shiny Applications -Version: 0.5.1.9001 +Version: 0.5.1.9002 Authors@R: c( person("Colin", "Fay", , "contact@colinfay.me", role = c("cre", "aut"), comment = c(ORCID = "0000-0001-7343-1846")), diff --git a/NEWS.md b/NEWS.md index 08cc227f..18a2d0e5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,13 +4,13 @@ ## Breaking change -- The use_*_files now fail when: +- The `add_*_files` and `use_*_files` now fail when: - The directory where the user tries to add the file doesn't exist. `{golem}` used to try to create the directory but that's not the function job — use_*_file functions should only be there to add file (Singe responsabily ) - The file that the user tries to create already exists ## Internal changes -- Full refactoring of the use_*_files functions that now all share the same behavior +- Full refactoring of the `add_*_files` and `use_*_files` functions that now all share the same behavior # golem 0.5.1