Skip to content

Commit

Permalink
feat: adding add_empty_file
Browse files Browse the repository at this point in the history
add_empty_file creates an empty
file in the www directory in the
spirit of the add_* functions

issue #837
  • Loading branch information
ColinFay committed Oct 11, 2023
1 parent b5be561 commit f66c837
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 110 deletions.
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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)
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
85 changes: 17 additions & 68 deletions R/add_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -524,24 +524,37 @@ 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(
missing(name),
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))
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions R/templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 3 additions & 14 deletions man/add_files.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/template.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/helper-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
}
Expand Down
43 changes: 18 additions & 25 deletions tests/testthat/test-add_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
)

})
})

Expand Down

0 comments on commit f66c837

Please sign in to comment.