R/dev-use_utils.R

Defines functions dev_copy_template dev_use_utils

Documented in dev_copy_template dev_use_utils

#' @title
#' Development Utilities
#'
#' @description
#' Adds development utilities to the package
#'
#' * `dev_use_utils("package")` or `dev_use_utils("golem")` adds utilities to
#'   `dev/dev-utils.R` and loads it in your .Rprofile.
#'   - `la()` is a shortcut for `devtools::load_all()`
#'   - `dnr()` is a shortcut for documenting the package and then reloading
#'     Additional arguments are available to export all functions or to reset
#'     the global environment to a clean slate, suitable for testing that
#'     you're not relying on global environment objects for your package.
#'   - `lar()` is a shortcut for `la()` and running the golem app
#'   - `dnrr()` is a shortcut for `dnr()` and running the golem app
#'
#' @name dev_use_utils
NULL

#' @describeIn dev_use_utils Package development utilities
#' @param type either "package" or "golem" depending on the package being built
#' @param file location of the dev-utils.R file
#' @param overwrite whether or not to overwrite an existing file
#' @importFrom here here
#' @importFrom stringr str_trim
#' @export
dev_use_utils <- function(type,
                          file = here("dev", "dev-utils.R"),
                          overwrite = FALSE,
                          envir = parent.frame()) {

    # Assertions
    assert_that(type %in% c("package", "golem", "plumber"))

    # Create development directory
    if (!dir.exists(here("dev"))) {
        log_info("Creating dev directory")
        dir.create(here("dev"), recursive = TRUE)
    }

    # Copy development utils
    log_info("Writing utilities file")
    dev_copy_template(
        template    = glue("dev-utils-{type}.R"),
        destination = file,
        overwrite   = overwrite,
        envir = envir
    )

    # Add to .Rprofile
    load_line <- glue(
        'if (file.exists("~/.Rprofile")) source("~/.Rprofile"); ',
        'if (interactive()) source("dev/dev-utils.R")'
    )
    if (!file.exists(here(".Rprofile"))) {
        log_info("Creating and adding to .Rprofile")
        writeLines(load_line, here(".Rprofile"))
    } else {
        rprofile_lines <- readLines(here(".Rprofile"))
        if (!any(str_trim(rprofile_lines) == load_line)) {
            log_info("Adding to .Rprofile")
            rprofile_lines <- c(load_line, "", rprofile_lines)
            writeLines(rprofile_lines, here(".Rprofile"))
        } else {
            log_info("Already in Rprofile")
        }
    }

}

#' @title
#' Copy template with string substitution
#'
#' @description
#' Copy a template onto a destination while replacing expressions inside
#' triple curly braces `{{{` and `}}}`.
#'
#' @param template    location inside the inst/template folder
#' @param destination where rendered file will be located
#' @param overwrite   whether to overwrite the file if needed
#' @param skip        if exists and not overwrite, skip instead of error
#' @export
dev_copy_template <- function(template, destination,
                              overwrite = FALSE, skip = TRUE,
                              envir = parent.frame()) {

    # Assertions
    assert_that(is.string(template))
    assert_that(is.string(destination))

    # Resolve arguments
    if (file.exists(destination)) {
        if (overwrite) {
            file.remove(destination)
        } else {
            log_warn("File already exists")
            if (!skip) {
                stop("File already exists")
            } else {
                log_info("Skipping")
            }
        }
    }

    # Copy template file
    system.file("template", template, package = "tjutils") %>%
        readLines() %>%
        purrr::map_chr(glue,
                       .open = "{{{", .close = "}}}",
                       .envir = list(envir = envir)) %>%
        writeLines(destination)

}
tjpalanca/tjutils documentation built on Jan. 20, 2021, 2:01 p.m.