R/locate_exec.R

Defines functions locate_exec.pip locate_exec.python locate_exec.npm locate_exec.node locate_exec.libreoffice locate_exec.chrome locate_exec

Documented in locate_exec

#' @export
#' @title Find an executable
#'
#' @description Searches for an executable in a some places and use the
#' highest version found (unless a specific version is requested).
#' @inheritParams exec_version
#' @param cache if `FALSE`, search for the executable again even if the
#' executable has been found previously.
#' @param dir A character vector of directory paths under which
#' the executable may be found.
#' @param version The version of the executable to look for (e.g., \code{"14.15.4"}).
#' If NULL (the default), it searches for the highest version.
#' @return A list containing the directory and version of the executable if found. If not found,
#' the version will be `0` and the dir will be `NULL`.
#' @export
#' @examples
#' locate_exec("chrome")
#' locate_exec("chrome", version = "88.0.4324.150")
#' locate_exec("libreoffice")
#' locate_exec("node")
#' locate_exec("npm")
#' locate_exec("python")
#' locate_exec("pip")
locate_exec <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  exec <- match.arg(exec, choices = supported_exec, several.ok = FALSE)
  class(exec) <- exec
  UseMethod("locate_exec", exec)
}

#' @export
locate_exec.chrome <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$chrome))

  if(!is.null(dir)){
    sources <- absolute_path(dir)
  } else if(is_osx()){
    sources <- "/Applications/Google Chrome.app/Contents/MacOS"
  } else if(is_windows()){
    regpath <- "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App paths"
    sources <- utils::readRegistry(paste0(regpath, "\\chrome.exe"))$Path
  } else {
    sources <- c(
      dirname(Sys.which(exec)),
      "/usr/local/bin",
      "/usr/bin"
    )
  }

  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src))
      get_chrome_version(src)
    else
      numeric_version("0")
  })

  match_v <- match_version(sources, versions, match_version = version)
  set_exec_info(match_v$dir, match_v$ver, exec = exec)
  as.list(.exec$chrome)
}

#' @export
locate_exec.libreoffice <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$libreoffice))

  if(is.null(dir)){
    sources <- c(
      dirname(Sys.which("soffice")),
      "/Applications/LibreOffice.app/Contents/MacOS",
      "/usr/local/bin",
      "/usr/bin",
      "C:/Program Files/LibreOffice/program"
    )
  } else {
    sources <- dir
  }
  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src)){
      extract_numeric_version(src, "soffice")
    } else
      numeric_version("0")
  })


  match_v <- match_version(sources, versions, match_version = version)

  set_exec_info(exec = exec, match_v$dir, match_v$ver)

  as.list(.exec$libreoffice)
}

#' @export
locate_exec.node <- function(exec, cache = TRUE, dir = NULL, version = NULL){

  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$node))

  if(is.null(dir)){
    sources <- c(dirname(Sys.which(exec)),
                 "C:/Program Files/nodejs",
                 "/usr/local/bin", "/usr/bin")
  } else {
    sources <- dir
  }
  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src))
      extract_numeric_version(src, exec)
    else
      numeric_version("0")
  })

  match_v <- match_version(sources, versions, match_version = version)

  set_exec_info(match_v$dir, match_v$ver, exec = exec)

  as.list(.exec$node)
}

#' @export
locate_exec.npm <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$npm))

  if(is.null(dir)){
    sources <- c(
      dirname(Sys.which(exec)),
      "C:/Program Files/nodejs",
      "/usr/local/bin",
      "/usr/bin")
  } else {
    sources <- dir
  }
  sources <- sources[!is.na(sources) & !"" %in% sources]
  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src))
      extract_numeric_version(src, exec)
    else
      numeric_version("0")
  })
  match_v <- match_version(sources, versions, match_version = version)
  set_exec_info(match_v$dir, match_v$ver, exec = exec)
  as.list(.exec$npm)
}

#' @export
locate_exec.python <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$python))

  # look up python in potential sources unless user has supplied `dir`
  if(is.null(dir)){
    userdir <- path.expand("~")
    sources <- c(
      dirname(Sys.which(exec)),
      file.path(userdir, "opt", "miniconda3", "bin"),
      file.path(userdir, "opt", "anaconda3", "bin"),
      file.path(userdir, "opt", "miniconda2", "bin"),
      file.path(userdir, "opt", "anaconda2", "bin"),
      file.path(userdir, "miniconda3", "bin"),
      file.path(userdir, "anaconda3", "bin"),
      file.path(userdir, "miniconda2", "bin"),
      file.path(userdir, "anaconda2", "bin"),
      "/usr/local/bin",
      "/usr/bin")
  } else {
    sources <- dir
  }

  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src))
      extract_numeric_version(src, exec)
    else
      numeric_version("0")
  })

  match_v <- match_version(sources, versions, match_version = version)
  set_exec_info(match_v$dir, match_v$ver, exec = exec)
  as.list(.exec$python)
}

#' @export
locate_exec.pip <- function(exec, cache = TRUE, dir = NULL, version = NULL){
  if (!cache) set_exec_info(NULL, exec = exec)
  if(exe_located(exec = exec, version = version)) return(as.list(.exec$pip))

  # look up pip in potential sources unless user has supplied `dir`
  if(is.null(dir)){
    userdir <- path.expand("~")
    sources <- c(
      dirname(Sys.which("python")),
      file.path(userdir, "opt", "miniconda3", "bin"),
      file.path(userdir, "opt", "anaconda3", "bin"),
      file.path(userdir, "opt", "miniconda2", "bin"),
      file.path(userdir, "opt", "anaconda2", "bin"),
      file.path(userdir, "miniconda3", "bin"),
      file.path(userdir, "anaconda3", "bin"),
      file.path(userdir, "miniconda2", "bin"),
      file.path(userdir, "anaconda2", "bin"),
      "/usr/local/bin",
      "/usr/bin")
  } else {
    sources <- dir
  }

  sources <- path.expand(sources)

  versions <- lapply(sources, function(src) {
    if(valid_exec_dir(src))
      extract_numeric_version(src, "python")
    else
      numeric_version("0")
  })

  match_v <- match_version(sources, versions, match_version = version)
  if(!is_windows())
    match_v$ver <- extract_numeric_version(match_v$dir, "pip")
  else match_v$ver <- extract_numeric_version(match_v$dir, "Scripts/pip")
  set_exec_info(match_v$dir, match_v$ver, exec = exec)
  as.list(.exec$pip)
}
davidgohel/locatexec documentation built on Feb. 17, 2021, 9:46 a.m.