R/build.R

Defines functions build_binder binder_builder

Documented in binder_builder build_binder

#' Builds binder
#'
#' This function kicks off the image build on Mybinder.org. Although it is not
#' entirely necessary to run this step, doing so will ensure that there is a
#' built image on binder ready to launch. Otherwise the first time (or if your
#' binder is infrequently used), the build can take a long time.
#' @param path path to local git controlled folder
#' @template hub
#' @template urlpath
#'
#' @importFrom httr GET content
#' @importFrom cliapp cli_alert_warning
binder_builder <-
  function(path = ".",
             hub = "mybinder.org",
             urlpath = "rstudio") {
    path <- sanitize_path(path)
    if (!has_a_git_remote()) {
      stop(
        "Cannot build without the project having a Git remote. Please connec this to a public repository on GitHub"
      )
    }
    user <- gh_tree_remote(path)$username
    repo <- gh_tree_remote(path)$repo
    # nocov start
    binder_runtime <-
      glue::glue("https://{hub}/build/gh/{user}/{repo}/master")
    res <- httr::GET(binder_runtime)
    url <-
      glue("https://{hub}/v2/gh/{user}/{repo}/master?urlpath={urlpath}")
    return(url)
    # nocov end
  }


#' Builds binder in the background (recommended over calling `binder_builder` directly)
#'
#' This function builds binder in the background and once an image is ready,
#' will open the Binder URL. You are feel to kill this process anytime and the build
#' still continue on the server.
#' @param path path to local Git controlled folder
#' @template hub
#' @template urlpath
#' @export
build_binder <-
  function(path = ".",
             hub = "mybinder.org",
             urlpath = "rstudio") {
    proceed <- TRUE

    if (!is_clean(path)) {
      if (interactive()) {
        proceed <- usethis::ui_yeah(
          "There are uncommitted files in your repo. Until committed and pushed to GitHub, Binder cannot build from these files. Do you still wish to continue?"
        )
      } else { # end if interactive
        warning("There are uncommitted files in your repo. Until committed and pushed to GitHub, Binder cannot build from these files.")
      }
    } # end not is clean

    if (proceed) {
      cliapp::cli_alert_info(
        glue::glue(
          "Your Binder is being built in the background. Once built, your browser will automatically launch. You can also click the binder badge on your README at any time."
        )
      )
      # nocov start
      `%...>%` <- promises::`%...>%`
      # TODO: Fix unbroken promises
      # binder_plan <- future::plan("list")
      # on.exit(future::plan(binder_plan))
      multisession <- "future" %:::% "multisession"
      future::plan(multisession, workers = 2)

      future::future({
        binder_builder(path, hub, urlpath)
      }) %...>% utils::browseURL
      # nocov end
    } # end proceed
  }
karthik/holepunch documentation built on Feb. 20, 2023, 5:21 a.m.