inst/templates/modules.r

.on_load <- function(ns) {
    box::use(cli[cli_h1, cli_div])
    module_name <- box::name()
    cli_div(theme = list(span.emph = list(color = "orange")))
    cli_h1(
        'Loading module "{.emph {module_name}}"'
    )
}

#' @export
grab_modules <- function(repo = NULL, lib = NULL) {
    box::use(
        here[here],
        desc[desc_get]
    )

    # grab modules of current working directory
    # else grab specified modules
    if (is.null(repo)) {
        if (is.null(lib)) {
            lib <- normalizePath(paste0(here(), "/modules"))
        }

        modules <- desc_get("Modules")
        module_list <- gsub("[[:space:]]", "", modules)
        module_list <- as.list(unlist(strsplit(module_list, ",")))

        invisible(lapply(module_list, grab_from_github))
    } else {
        invisible(lapply(repo, grab_from_github))
    }
}


#' @description very rudementary function to download
#' a github repo and save it into a library
grab_from_github <- function(repo, lib = NULL) {
    box::use(
        utils[download.file, URLencode, untar],
        glue[glue],
        .. / dir / module_lib[get_global_dir]
    )

    if (is.null(lib)) {
        lib <- get_global_dir()
    }

     git_template <- "https://api.github.com/repos/%s/%s/tarball/%s"
     input <- as.list(unlist(strsplit(repo, "[/@]")))

     if (is.null(unlist(input[3]))) {
         git_request <- URLencode(
             sprintf(
                 git_template,
                 input[1],
                 input[2],
                 ""
             )
         )
     } else {
         git_request <- URLencode(
             sprintf(
                 git_template,
                 input[1],
                 input[2],
                 input[3]
             )
         )
     }

     tempZipFile <- tempfile(".tar.gz")
     loc <- glue("{lib}/{input[2]}")
     download.file(git_request, tempZipFile, "auto")
     untar(
         tempZipFile,
         files = NULL,
         list = FALSE,
         exdir = loc,
         extra = "--strip-components 1"
     )

}
ElianHugh/boxingtape documentation built on Feb. 13, 2021, 12:48 a.m.