modules/grab/install.r

#' @export
install <- function(repo = NULL, global_install = FALSE) {
    box::use(
        here[here],
        glue[glue],
        jsonlite[read_json],
        . / util[get_module_list]
    )

    # grab modules of current working directory
    # else grab specified modules
    if (is.null(repo)) {
        package <- get_module_list()
        package <- glue("{package$modules$author}/{package$modules$moduleName}")
        invisible(lapply(package, install_helper))
    } else {
        invisible(lapply(repo, install_helper))
    }
}


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

    if (global_install == TRUE) {
        lib <- get_global_dir()
    } else {
        lib <- glue("{here()}/modules")
    }

     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.