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

use_file: Copies local file to app/www #520

Merged
merged 1 commit into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -57,6 +57,7 @@ export(use_external_file)
export(use_external_html_template)
export(use_external_js_file)
export(use_favicon)
export(use_file)
export(use_recommended_deps)
export(use_recommended_tests)
export(use_utils_server)
Expand Down
47 changes: 47 additions & 0 deletions R/use_files.R
Expand Up @@ -4,6 +4,7 @@
#'
#' @inheritParams add_module
#' @param url String representation of URL for the file to be downloaded
#' @param path String representation of the local path for the file to be implemented (use_file only)
#' @param dir Path to the dir where the file while be created.
#' @note See `?htmltools::htmlTemplate` and `https://shiny.rstudio.com/articles/templates.html` for more information about `htmlTemplate`.
#' @export
Expand Down Expand Up @@ -240,3 +241,49 @@ use_external_file <- function(
cat_downloaded(where)

}

#' @export
#' @rdname use_file
#' @importFrom fs path_abs file_exists
use_file <- function(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

Thanks 💪

Could you name this use_internal_file() please?

Also, would you fancy making the use_internal_css_file(), use_internal_js_file() and use_internal_html_template()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing!

Looking through the external functions, I saw that you use @importFrom fs path_abs, Since you also use fs::file_exists I took a chance and added that to the Roxygen skeleton (and cleaned up the fs:: bits). If you prefer to have it reverted, let me know.

devtools::check() still results in the same two aforementioned failures.

I have only made minimal checks on the js, html, and css file handlers. I can confirm that they provide their respective messages, as well as it copies their target files to the www. js and css also successfully checks for file extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #529. (Should I close this request then?)

path,
name,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
){
if (missing(name)){
name <- basename(path)
}

old <- setwd(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 <- path_abs(dir)

where <- path(
dir, name
)

if (file_exists(where)){
cat_exists(where)
return(invisible(FALSE))
}

cat_start_copy()

file.copy(path, where)

cat_copied(where)

}
18 changes: 18 additions & 0 deletions R/utils.R
Expand Up @@ -167,6 +167,24 @@ cat_downloaded <- function(
)
}

cat_start_copy <- function(){
cat_line("")
cat_rule("Copying file")
}

cat_copied <- function(
where,
file = "File"
){
cat_green_tick(
sprintf(
"%s copied to %s",
file,
where
)
)
}

cat_created <- function(
where,
file = "File"
Expand Down
2 changes: 1 addition & 1 deletion man/document_and_reload.Rd

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

2 changes: 2 additions & 0 deletions man/use_files.Rd

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