R/devutils.R

.get_env <- function(x){
  UNSET <- "__UNSET__";
  v <- Sys.getenv(x, unset = UNSET)
  if (v == UNSET){
    return(NULL)
  }
  return(v)
}

.is_server <- function(){
  is_server <- Sys.getenv("IS_SERVER", unset = "NO")
  if(is_server == "NO"){
    return(FALSE)
  } else {
    return(TRUE)
  }
}

#' @export
lclz_load_deps <- function (){
  github_auth_token <- .get_env("GITHUB_AUTH_TOKEN")
  if(is.null(github_auth_token)){
    stop("GITHUB_AUTH_TOKEN should be define in system enviroment")
  }

  if (dir.exists("../localizebase")){
    # if you clone localizebase, this will allow locally add to localizebase while working on current project
    devtools::install_deps("../localizebase", upgrade = "never")
    devtools::load_all("../localizebase")
  } else {
    if (.Platform$OS.type == "windows"){
      oldrepos = getOption("repos")
      options(repos = oldrepos["CRAN"])
      tryCatch(
        {devtools::install_github("madlandev/localizebase", auth_token = github_auth_token, upgrade = "never")},
        finally = {    options(repos = oldrepos)})
    } else {
      devtools::install_github("madlandev/localizebase", auth_token = github_auth_token, upgrade = "never")
    }
    requireNamespace("localizebase")
  }

  if (dir.exists("../modules")){
    # if you clone localizebase, this will allow locally add to localizebase while working on current project
    devtools::install_deps("../modules", upgrade = "never")
    devtools::load_all("../modules")
  } else {
    # token should be created with https://github.com/settings/tokens
    if (.Platform$OS.type == "windows"){
      oldrepos = getOption("repos")
      options(repos = oldrepos["CRAN"])
      tryCatch(
        {devtools::install_github("madlandev/modules", auth_token = github_auth_token, upgrade = "never")},
        finally = {options(repos = oldrepos)})
    } else {
      devtools::install_github("madlandev/modules", auth_token = github_auth_token, upgrade = "never")
    }
    requireNamespace("modules")
  }
  # done packages -----------------------------

  options('import.path' = "./R")

}


#' @export
lclz_project_skeleton <- function(){
  if (!dir.exists("R")) {
    message("creating ./R for source files")
    dir.create("R")
  }
  if (!file.exists(file.path("R","dependencies.R"))) {
    message("add dependencies.R that helps with dependencies managment")
    file.copy(system.file("devutils/dependencies.R", package = "localizebasesetup"),
              "R/dependencies.R")}
  if (!file.exists(file.path("R","notebook_template.R"))) {
    message("add notebook_template.R")
    file.copy(system.file("devutils/notebook_template.Rmd", package = "localizebasesetup"),
              "R/notebook_template.Rmd")}

}
madlandev/localizebasesetup documentation built on June 1, 2019, 5:01 a.m.