R/gepetto-helpers.R

Defines functions install_gepetto start_gepetto stop_gepetto

Documented in install_gepetto start_gepetto stop_gepetto

#' Install gepetto
#'
#' This function verifies you have what you need to install `gepetto` then
#' does so (globally). Ideally, you'll have `Node.js` and `npm` already on your
#' system (since this function will not install them).
#'
#' While you _can_ try to install gepetto this way, it is highly recommended that
#' you follow the instructions (<https://gitlab.com/hrbrmstr/gepetto>) manually
#' since many Node.js/npm installations have permissions issues due to errant
#' historical use of `sudo`.
#'
#' @md
#' @export
install_gepetto <- function() {

  node <- Sys.which("node")
  npm <- Sys.which("npm")

  if (node == "") {
    message(
      "Could not locate Node.js. Please visit <https://nodejs.org/en/download/package-manager/> ",
      "and obtain it via one of the methods described there then try installing gepetto from ",
      "a fresh R session.", sep=""
    )
    return()
  }

  if (npm == "") {
    message(
      "Could not local npm. Please visit <https://www.npmjs.com/get-npm> to ensure ",
      "you have installed both Node.js and npm correctly, then try installing gepetto ",
      "from a fresh R session.", sep=""
    )
    return()
  }

  res <- processx::run(node, "--version", error_on_status = FALSE)
  if (res$status != 0) {
    message(
      "An error occurred while trying to determine the version of Node.js that is ",
      "installed on the system. Please ensure you have it installed correctly and ",
      "try installing gepetto again from a fresh R session.", sep=""
    )
    return()
  }

  node_vers <- unlist(strsplit(res$stdout, "\\."))[1]
  if (!(node_vers %in% c("v10", "v9"))) {
    message(
      "You have a very old installation of Node.js. Please update it and try installing ",
      "gepetto again from a fresh R session.", sep=""
    )
    return()
  }

  res <- processx::run(npm, "--version", error_on_status = FALSE)
  if (res$status != 0) {
    message(
      "An error occurred while trying to determine the version of npm that is ",
      "installed on the system. Please ensure you have it installed correctly and ",
      "try installing gepetto again from a fresh R session.", sep=""
    )
    return()
  }

  npm_vers <- unlist(strsplit(res$stdout, "\\."))[1]
  if (!(npm_vers %in% c("6"))) {
    message(
      "You have an old installation of npm. Please update it and try installing ",
      "gepetto again from a fresh R session.", sep=""
    )
    return()
  }

  message("Attemping to install gepetto")

  processx::run(
    command = "npm",
    args =  c("install", "https://gitlab.com/hrbrmstr/gepetto.git", "--global"),
    error_on_status = FALSE,
    echo_cmd = TRUE,
    echo = TRUE
  ) -> res

  if (res$status == 0) {
    message("gepetto has been installed.")
  } else {
    message(
      "There was an error installing gepetto. Check the error log and make sure ",
      "permissions are set correctly or for other errors that occurred which ",
      "prevented the installation frombeing successful and try again or follow the ",
      "manual instructions at <https://gitlab.com/hrbrmstr/gepetto>.", sep=""
    )
  }

}

#' Start/stop gepetto
#'
#' These functions can help start/stop gepetto instances. You must remember to
#' save the object returned by [start_gepetto()].
#'
#' Note that [stop_gepetto()] kills the process. You can/should consider using
#' [gep_stop()] instead.
#'
#' @md
#' @param host IP/host to use instead of `localhost`
#' @param port port to use besides the default (`8080`)
#' @param pxobj the `processx` object of a running gepetto instance generated by [start_gepetto()]
#' @return [start_gepetto()] returns a `processx` object
#' @export
#' @examples \dontrun{
#' pid <- start_gepetto()
#' # ... do stuff
#' stop_gepetto(pid)
#' }
start_gepetto <- function(host = "localhost", port = 8080) {

  gep <- Sys.which("gepetto")
  if (gep == "") stop("gepetto not found.", call.=FALSE)

  HOST <- Sys.getenv("HOST")
  on.exit(Sys.setenv(HOST=HOST), add=TRUE)
  PORT <- Sys.getenv("PORT")
  on.exit(Sys.setenv(PORT=PORT), add=TRUE)

  Sys.setenv(HOST=host)
  Sys.setenv(PORT=port)
  processx::process$new(
    command = "gepetto", stdout = "|", stderr = "|"
  ) -> res

  res

}

#' @rdname start_gepetto
#' @export
stop_gepetto <- function(pxobj) {
  pxobj$kill()
}
hrbrmstr/decapitated documentation built on Aug. 2, 2019, 8:42 p.m.